Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 package edu.nimbus.glass;
00032
00033
00034 import edu.nimbus.glass.screens.AllTopics;
00035 import edu.nimbus.glass.screens.FieldFocus;
00036 import edu.nimbus.glass.screens.Graph;
00037
00038 import android.content.Context;
00039 import android.graphics.Canvas;
00040
00041 import android.os.SystemClock;
00042 import android.util.Log;
00043 import android.view.LayoutInflater;
00044 import android.view.SurfaceHolder;
00045 import android.view.View;
00046 import android.widget.FrameLayout;
00047
00048 import java.util.concurrent.TimeUnit;
00049
00053 public class TopicRenderer implements SurfaceHolder.Callback {
00054
00055 private static final String TAG = TopicRenderer.class.getSimpleName();
00056
00057
00058
00060 private static final int REFRESH_RATE_FPS = 30;
00061
00063 private static final long FRAME_TIME_MILLIS = TimeUnit.SECONDS.toMillis(1) / REFRESH_RATE_FPS;
00064
00065 private SurfaceHolder mHolder;
00066 private RenderThread mRenderThread;
00067 private int mSurfaceWidth;
00068 private int mSurfaceHeight;
00069
00070 private final FrameLayout mLayout;
00071 private final TopicView mTopicView;
00072 private final TopicManager mTopicManager;
00073
00077 public TopicRenderer(Context context, TopicManager topicManager) {
00078
00079 Log.d("Starting renderer", "render");
00080
00081 LayoutInflater inflater = LayoutInflater.from(context);
00082 mLayout = (FrameLayout) inflater.inflate(R.layout.topic, null);
00083 mLayout.setWillNotDraw(false);
00084
00085 mTopicView = (TopicView) mLayout.findViewById(R.id.topic);
00086
00087
00088 mTopicManager = topicManager;
00089 mTopicView.setTopicManager(mTopicManager);
00090 }
00091
00092 public void setRendererAllFieldsState(){
00093 mTopicView.switchScreen(new AllTopics());
00094 }
00095
00096 public void setRendererGraphState(String field){
00097 mTopicView.switchScreen(new Graph(field));
00098 }
00099
00100 public void setRendererFieldFocusState(String field){
00101 mTopicView.switchScreen(new FieldFocus(field));
00102 }
00103
00104 @Override
00105 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
00106 mSurfaceWidth = width;
00107 mSurfaceHeight = height;
00108 doLayout();
00109 }
00110
00111 @Override
00112 public void surfaceCreated(SurfaceHolder holder) {
00113 mHolder = holder;
00114
00115 mTopicManager.start();
00116 mRenderThread = new RenderThread();
00117 mRenderThread.start();
00118 }
00119
00120 @Override
00121 public void surfaceDestroyed(SurfaceHolder holder) {
00122 mRenderThread.quit();
00123 mTopicManager.stop();
00124 }
00125
00131 private void doLayout() {
00132
00133
00134 int measuredWidth = View.MeasureSpec.makeMeasureSpec(mSurfaceWidth,
00135 View.MeasureSpec.EXACTLY);
00136 int measuredHeight = View.MeasureSpec.makeMeasureSpec(mSurfaceHeight,
00137 View.MeasureSpec.EXACTLY);
00138
00139 mLayout.measure(measuredWidth, measuredHeight);
00140 mLayout.layout(0, 0, mLayout.getMeasuredWidth(), mLayout.getMeasuredHeight());
00141 }
00142
00146 private synchronized void repaint() {
00147 Canvas canvas = null;
00148
00149 try {
00150 canvas = mHolder.lockCanvas();
00151 } catch (RuntimeException e) {
00152 Log.d(TAG, "lockCanvas failed", e);
00153 }
00154
00155 if (canvas != null) {
00156 mLayout.draw(canvas);
00157
00158 try {
00159 mHolder.unlockCanvasAndPost(canvas);
00160 } catch (RuntimeException e) {
00161 Log.d(TAG, "unlockCanvasAndPost failed", e);
00162 }
00163 }
00164 }
00165
00166
00170 private class RenderThread extends Thread {
00171 private boolean mShouldRun;
00172
00176 public RenderThread() {
00177 mShouldRun = true;
00178 }
00179
00185 private synchronized boolean shouldRun() {
00186 return mShouldRun;
00187 }
00188
00192 public synchronized void quit() {
00193 mShouldRun = false;
00194 }
00195
00196 @Override
00197 public void run() {
00198 while (shouldRun()) {
00199 long frameStart = SystemClock.elapsedRealtime();
00200 repaint();
00201 long frameLength = SystemClock.elapsedRealtime() - frameStart;
00202
00203 long sleepTime = FRAME_TIME_MILLIS - frameLength;
00204 if (sleepTime > 0) {
00205 SystemClock.sleep(sleepTime);
00206 }
00207 }
00208 }
00209 }
00210 }