• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.test.tilebenchmark;
18 
19 import android.app.Activity;
20 import android.os.AsyncTask;
21 import android.os.Bundle;
22 import android.view.GestureDetector.SimpleOnGestureListener;
23 import android.view.MotionEvent;
24 import android.view.View;
25 import android.view.View.OnClickListener;
26 import android.widget.Button;
27 import android.widget.SeekBar;
28 import android.widget.SeekBar.OnSeekBarChangeListener;
29 import android.widget.TextView;
30 import android.widget.Toast;
31 
32 import java.io.FileInputStream;
33 import java.io.IOException;
34 import java.io.ObjectInputStream;
35 
36 /**
37  * Interface for playing back WebView tile rendering status. Draws viewport and
38  * states of tiles and statistics for off-line analysis.
39  */
40 public class PlaybackActivity extends Activity {
41     private static final float SCROLL_SCALER = 0.125f;
42 
43     PlaybackView mPlaybackView;
44     SeekBar mSeekBar;
45     Button mForward;
46     Button mBackward;
47     TextView mFrameDisplay;
48 
49     private int mFrame = -1;
50     private int mFrameMax;
51 
52     private class TouchFrameChangeListener extends SimpleOnGestureListener {
53         float mDist = 0;
54 
55         @Override
onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)56         public boolean onScroll(MotionEvent e1, MotionEvent e2,
57                 float distanceX, float distanceY) {
58             // aggregate scrolls so that small ones can add up
59             mDist += distanceY * SCROLL_SCALER;
60             int intComponent = (int) Math.floor(Math.abs(mDist));
61             if (intComponent >= 1) {
62                 int scrollDist = (mDist > 0) ? intComponent : -intComponent;
63                 setFrame(null, mFrame + scrollDist);
64                 mDist -= scrollDist;
65             }
66             return super.onScroll(e1, e2, distanceX, distanceY);
67         }
68     };
69 
70     private class SeekFrameChangeListener implements OnSeekBarChangeListener {
71         @Override
onStopTrackingTouch(SeekBar seekBar)72         public void onStopTrackingTouch(SeekBar seekBar) {
73         }
74 
75         @Override
onStartTrackingTouch(SeekBar seekBar)76         public void onStartTrackingTouch(SeekBar seekBar) {
77         }
78 
79         @Override
onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)80         public void onProgressChanged(SeekBar seekBar, int progress,
81                 boolean fromUser) {
82             setFrame(seekBar, progress);
83         }
84     };
85 
86     private class LoadFileTask extends AsyncTask<String, Void, RunData> {
87         @Override
doInBackground(String... params)88         protected RunData doInBackground(String... params) {
89             RunData data = null;
90             try {
91                 FileInputStream fis = openFileInput(params[0]);
92                 ObjectInputStream in = new ObjectInputStream(fis);
93                 data = (RunData) in.readObject();
94                 in.close();
95             } catch (IOException ex) {
96                 ex.printStackTrace();
97             } catch (ClassNotFoundException ex) {
98                 ex.printStackTrace();
99             }
100             return data;
101         }
102 
103         @Override
onPostExecute(RunData data)104         protected void onPostExecute(RunData data) {
105             if (data == null) {
106                 Toast.makeText(getApplicationContext(),
107                         getResources().getString(R.string.error_no_data),
108                         Toast.LENGTH_LONG).show();
109                 return;
110             }
111             mPlaybackView.setData(data);
112 
113             mFrameMax = data.frames.length - 1;
114             mSeekBar.setMax(mFrameMax);
115 
116             setFrame(null, 0);
117         }
118     }
119 
setFrame(View changer, int f)120     private void setFrame(View changer, int f) {
121         if (f < 0) {
122             f = 0;
123         } else if (f > mFrameMax) {
124             f = mFrameMax;
125         }
126 
127         if (mFrame == f) {
128             return;
129         }
130 
131         mFrame = f;
132         mForward.setEnabled(mFrame != mFrameMax);
133         mBackward.setEnabled(mFrame != 0);
134         if (changer != mSeekBar) {
135             mSeekBar.setProgress(mFrame);
136         }
137         mFrameDisplay.setText(Integer.toString(mFrame));
138         mPlaybackView.setFrame(mFrame);
139     };
140 
141     /** Called when the activity is first created. */
142     @Override
onCreate(Bundle savedInstanceState)143     public void onCreate(Bundle savedInstanceState) {
144         super.onCreate(savedInstanceState);
145         setContentView(R.layout.playback);
146 
147         mPlaybackView = (PlaybackView) findViewById(R.id.playback);
148         mSeekBar = (SeekBar) findViewById(R.id.seek_bar);
149         mForward = (Button) findViewById(R.id.forward);
150         mBackward = (Button) findViewById(R.id.backward);
151         mFrameDisplay = (TextView) findViewById(R.id.frame_display);
152 
153         mForward.setOnClickListener(new OnClickListener() {
154             @Override
155             public void onClick(View v) {
156                 setFrame(v, mFrame + 1);
157             }
158         });
159 
160         mBackward.setOnClickListener(new OnClickListener() {
161             @Override
162             public void onClick(View v) {
163                 setFrame(v, mFrame - 1);
164             }
165         });
166 
167         mSeekBar.setOnSeekBarChangeListener(new SeekFrameChangeListener());
168 
169         mPlaybackView.setOnGestureListener(new TouchFrameChangeListener());
170 
171         new LoadFileTask().execute(ProfileActivity.TEMP_FILENAME);
172     }
173 }
174