Guest User

Untitled

a guest
Sep 27th, 2012
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.86 KB | None | 0 0
  1. public class StopwatchActivity extends ListActivity {
  2.     private static String TAG = "StopwatchActivity";
  3.  
  4.     // View elements in stopwatch.xml
  5.     private TextView m_elapsedTime, tvRow;
  6.     private Button m_start;
  7.     private Button m_pause;
  8.     private Button m_reset;
  9.     private Button m_lap;
  10.     private Context mContext;
  11.     private int id;
  12.  
  13.     private ArrayAdapter<String> m_lapList;
  14.  
  15.     // Timer to update the elapsedTime display
  16.     private final long mFrequency = 100; // milliseconds
  17.     private final int TICK_WHAT = 2;
  18.     private Handler mHandler = new Handler() {
  19.         public void handleMessage(Message m) {
  20.             updateElapsedTime();
  21.             sendMessageDelayed(Message.obtain(this, TICK_WHAT), mFrequency);
  22.         }
  23.     };
  24.  
  25.     // Connection to the backgorund StopwatchService
  26.     private StopwatchService m_stopwatchService;
  27.     private ServiceConnection m_stopwatchServiceConn = new ServiceConnection() {
  28.         public void onServiceConnected(ComponentName name, IBinder service) {
  29.             m_stopwatchService = ((StopwatchService.LocalBinder) service)
  30.                     .getService();
  31.             showCorrectButtons();
  32.         }
  33.  
  34.         public void onServiceDisconnected(ComponentName name) {
  35.             m_stopwatchService = null;
  36.         }
  37.     };
  38.  
  39.     private void bindStopwatchService() {
  40.         bindService(new Intent(this, StopwatchService.class),
  41.                 m_stopwatchServiceConn, Context.BIND_AUTO_CREATE);
  42.     }
  43.  
  44.     private void unbindStopwatchService() {
  45.         if (m_stopwatchService != null) {
  46.             unbindService(m_stopwatchServiceConn);
  47.         }
  48.     }
  49.  
  50.     @SuppressWarnings("unchecked")
  51.     @Override
  52.     public void onCreate(Bundle savedInstanceState) {
  53.         super.onCreate(savedInstanceState);
  54.  
  55.         setContentView(R.layout.stopwatch);
  56.  
  57.         startService(new Intent(this, StopwatchService.class));
  58.         bindStopwatchService();
  59.  
  60.         m_elapsedTime = (TextView) findViewById(R.id.ElapsedTime);
  61.         Typeface typeface = Typeface.createFromAsset(getAssets(),
  62.                 "Fonts/DS-DIGIT.TTF");
  63.         m_elapsedTime.setTypeface(typeface);
  64.         m_start = (Button) findViewById(R.id.StartButton);
  65.         m_pause = (Button) findViewById(R.id.PauseButton);
  66.         m_reset = (Button) findViewById(R.id.ResetButton);
  67.         m_lap = (Button) findViewById(R.id.LapButton);
  68.  
  69.         setListAdapter(new ArrayAdapter<String>(this, R.layout.laps_row));
  70.         m_lapList = (ArrayAdapter<String>) getListAdapter();
  71.  
  72.         mHandler.sendMessageDelayed(Message.obtain(mHandler, TICK_WHAT),
  73.                 mFrequency);
  74.     }
  75.  
  76.     @Override
  77.     protected void onDestroy() {
  78.         super.onDestroy();
  79.         unbindStopwatchService();
  80.     }
  81.  
  82.     private void showCorrectButtons() {
  83.         Log.d(TAG, "showCorrectButtons");
  84.  
  85.         if (m_stopwatchService != null) {
  86.             if (m_stopwatchService.isStopwatchRunning()) {
  87.                 showPauseLapButtons();
  88.             } else {
  89.                 showStartResetButtons();
  90.             }
  91.         }
  92.     }
  93.  
  94.     private void showPauseLapButtons() {
  95.         Log.d(TAG, "showPauseLapButtons");
  96.  
  97.         m_start.setVisibility(View.GONE);
  98.         m_reset.setVisibility(View.GONE);
  99.         m_pause.setVisibility(View.VISIBLE);
  100.         m_lap.setVisibility(View.VISIBLE);
  101.     }
  102.  
  103.     private void showStartResetButtons() {
  104.         Log.d(TAG, "showStartResetButtons");
  105.  
  106.         m_start.setVisibility(View.VISIBLE);
  107.         m_reset.setVisibility(View.VISIBLE);
  108.         m_pause.setVisibility(View.GONE);
  109.         m_lap.setVisibility(View.GONE);
  110.     }
  111.  
  112.     public void onStartClicked(View v) {
  113.         Log.d(TAG, "start button clicked");
  114.         m_stopwatchService.start();
  115.  
  116.         showPauseLapButtons();
  117.     }
  118.  
  119.     public void onPauseClicked(View v) {
  120.         Log.d(TAG, "pause button clicked");
  121.         m_stopwatchService.pause();
  122.  
  123.         showStartResetButtons();
  124.     }
  125.  
  126.     public void onResetClicked(View v) {
  127.         Log.d(TAG, "reset button clicked");
  128.         m_stopwatchService.reset();
  129.  
  130.         m_lapList.clear();
  131.     }
  132.  
  133.     public void onLapClicked(View v) {
  134.         Log.d(TAG, "lap button clicked");
  135.         m_stopwatchService.lap();
  136.  
  137.         m_lapList.insert(m_stopwatchService.getFormattedElapsedTime(), 0);
  138.     }
  139.  
  140.     public void updateElapsedTime() {
  141.         if (m_stopwatchService != null)
  142.             m_elapsedTime.setText(m_stopwatchService.getFormattedElapsedTime());
  143.     }
Advertisement
Add Comment
Please, Sign In to add comment