1 package net.osdn.gokigen.joggingtimer.stopwatch;
3 import android.content.Intent;
4 import android.graphics.Color;
5 import android.os.Bundle;
6 import android.support.wear.widget.BoxInsetLayout;
7 import android.support.wearable.activity.WearableActivity;
8 import android.util.Log;
9 import android.view.KeyEvent;
10 import android.view.View;
11 import android.widget.ImageButton;
12 import android.widget.RelativeLayout;
13 import android.widget.TextView;
15 import net.osdn.gokigen.joggingtimer.R;
16 import net.osdn.gokigen.joggingtimer.recordlist.ListActivity;
17 import net.osdn.gokigen.joggingtimer.utilities.TimeStringConvert;
19 import java.text.SimpleDateFormat;
20 import java.util.ArrayList;
21 import java.util.Date;
22 import java.util.List;
23 import java.util.Locale;
29 public class MainActivity extends WearableActivity implements IClickCallback, MyTimerTrigger.ITimeoutReceiver, MyTimerCounter.ICounterStatusNotify
31 private final String TAG = toString();
32 private final IWearableActivityControl controller = new WearableActivityController();
33 private MyTimerCounter counter = new MyTimerCounter();
34 private boolean isCounterLapTime = false;
40 protected void onCreate(Bundle savedInstanceState)
42 super.onCreate(savedInstanceState);
43 Log.v(TAG, "onCreate()");
45 setContentView(R.layout.activity_main);
47 controller.setup(this, this, counter);
57 protected void onResume()
62 Intent intent = getIntent();
63 String action = intent.getAction();
64 Log.v(TAG, "onResume() : " + action);
66 boolean isStartTimer = false;
69 if (action.equals("com.google.android.wearable.action.STOPWATCH"))
73 else if (action.equals("vnd.google.fitness.TRACK"))
75 String activity = intent.getStringExtra("actionStatus");
76 if ((activity != null)&&(activity.equals("ActiveActionStatus")))
82 controller.setupReferenceData();
95 protected void onPause()
98 Log.v(TAG, "onPause()");
106 public void onStart()
109 Log.v(TAG, "onStart()");
112 counter.setCallback(this);
113 controller.setupDatabase(this, false);
124 Log.v(TAG, "onStop()");
125 controller.exitApplication(this);
133 public void onEnterAmbient(Bundle ambientDetails)
135 super.onEnterAmbient(ambientDetails);
136 Log.v(TAG, "onEnterAmbient()");
144 public void onExitAmbient()
146 super.onExitAmbient();
147 Log.v(TAG, "onExitAmbient()");
148 //updateTimerLabel();
156 public void onUpdateAmbient()
158 super.onUpdateAmbient();
159 Log.v(TAG, "onUpdateAmbient()");
165 public void updateTimerLabel()
167 ITimerCounter timerCounter = counter;
168 if (timerCounter != null)
171 BoxInsetLayout insetLayout = findViewById(R.id.box_inset_layout);
172 RelativeLayout layout = findViewById(R.id.relative_main_layout);
174 ImageButton btn1 = findViewById(R.id.btn1);
175 ImageButton btn2 = findViewById(R.id.btn2);
176 ImageButton btn3 = findViewById(R.id.btn3);
178 updateMainSubCounter();
180 if (timerCounter.isStarted())
182 bgColor = Color.BLACK;
183 insetLayout.setBackgroundColor(bgColor);
184 insetLayout.invalidate();
186 layout.setBackgroundColor(bgColor);
189 btn1.setImageResource(R.drawable.ic_flag_black_24dp);
190 btn1.setBackgroundColor(bgColor);
191 btn1.setVisibility(View.VISIBLE);
194 btn2.setImageResource(R.drawable.ic_stop_black_24dp);
195 btn2.setBackgroundColor(bgColor);
196 btn2.setVisibility(View.VISIBLE);
199 btn3.setImageResource(R.drawable.ic_block_black_24dp);
200 btn3.setBackgroundColor(bgColor);
201 btn3.setVisibility(View.INVISIBLE);
204 updateElapsedTimes();
206 else if (timerCounter.isReset())
208 bgColor = Color.BLACK;
209 insetLayout.setBackgroundColor(bgColor);
210 insetLayout.invalidate();
212 layout.setBackgroundColor(bgColor);
215 btn1.setImageResource(R.drawable.ic_play_arrow_black_24dp);
216 btn1.setBackgroundColor(bgColor);
217 btn1.setVisibility(View.VISIBLE);
220 btn2.setImageResource(R.drawable.ic_format_list_bulleted_black_24dp);
221 btn2.setBackgroundColor(bgColor);
222 btn2.setVisibility(View.VISIBLE);
225 btn3.setImageResource(R.drawable.ic_refresh_black_24dp);
226 btn3.setBackgroundColor(bgColor);
227 btn3.setVisibility(View.INVISIBLE);
230 updateElapsedTimes();
234 bgColor = Color.BLACK;
235 insetLayout.setBackgroundColor(bgColor);
236 insetLayout.invalidate();
238 layout.setBackgroundColor(bgColor);
241 btn1.setImageResource(R.drawable.ic_play_arrow_black_24dp);
242 btn1.setVisibility(View.VISIBLE);
243 btn1.setBackgroundColor(bgColor);
246 btn2.setImageResource(R.drawable.ic_format_list_bulleted_black_24dp);
247 btn2.setVisibility(View.VISIBLE);
248 btn2.setBackgroundColor(bgColor);
251 btn3.setImageResource(R.drawable.ic_refresh_black_24dp);
252 btn3.setVisibility(View.VISIBLE);
253 btn3.setBackgroundColor(bgColor);
256 updateElapsedTimes();
262 public void clickedCounter()
265 isCounterLapTime = !isCounterLapTime;
272 public void clickedBtn1()
281 private void startTimer()
285 ITimerCounter timerCounter = counter;
286 if (timerCounter != null)
288 if (timerCounter.isStarted())
290 Log.v(TAG, "startTimer() LAP TIME");
291 // チャタリング防止(ラップタイムとして、3秒以内は記録しないようにする)
292 if (timerCounter.getCurrentElapsedTime() > 3000)
294 long lapTime = timerCounter.timeStamp();
295 controller.vibrate(50);
296 controller.getDataEntry().appendTimeData(lapTime);
301 Log.v(TAG, "startTimer() START");
302 timerCounter.start();
303 MyTimerTrigger trigger = new MyTimerTrigger(this, 100, timerCounter);
304 trigger.startTimer();
305 controller.timerStarted(true);
306 controller.vibrate(120);
308 Date date = new Date();
309 SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
310 String title = sdf1.format(date);
311 controller.getDataEntry().createIndex(title, timerCounter.getStartTime());
326 private boolean stopTimer()
331 ITimerCounter timerCounter = counter;
332 if (timerCounter != null)
334 if (timerCounter.isStarted())
337 controller.timerStarted(false);
338 controller.vibrate(120);
339 controller.getDataEntry().finishTimeData(timerCounter.getStartTime(), timerCounter.getStopTime());
356 public void clickedBtn2()
358 ITimerCounter timerCounter = counter;
359 if (timerCounter != null)
361 if (!timerCounter.isStarted())
364 launchListActivity();
367 controller.vibrate(35);
377 public void clickedBtn3()
379 ITimerCounter timerCounter = counter;
380 if (timerCounter != null)
382 if (!timerCounter.isStarted())
384 timerCounter.reset();
385 controller.vibrate(50);
392 public boolean pushedBtn1()
398 public boolean pushedBtn2()
400 return (stopTimer());
404 public boolean pushedBtn3()
414 public void timeout()
418 runOnUiThread(new Runnable() {
435 private void updateMainSubCounter()
437 TextView main = findViewById(R.id.main_counter);
438 TextView sub = findViewById(R.id.sub_counter1);
440 ITimerCounter timerCounter = counter;
441 if (timerCounter != null)
443 long time1 = timerCounter.getPastTime();
444 CharSequence str1 = TimeStringConvert.getTimeString(time1);
446 CharSequence str2 = "";
447 if (timerCounter.isStarted())
449 long time2 = timerCounter.getCurrentElapsedTime();
450 int lapCount = timerCounter.getElapsedCount();
451 if ((time2 >= 100) && (lapCount > 1))
453 str2 = "[" + lapCount + "] " + TimeStringConvert.getTimeString(time2);
457 if ((str2.length() > 0)&&(isCounterLapTime))
477 private void updateElapsedTimes()
480 TextView area1 = findViewById(R.id.sub_counter2);
481 TextView area2 = findViewById(R.id.sub_counter3);
482 TextView area3 = findViewById(R.id.sub_counter4);
484 ITimerCounter timerCounter = counter;
485 if (timerCounter != null)
487 List<Long> elapsedTimes = timerCounter.getTimerList();
488 int indexSize = elapsedTimes.size();
492 area1.setText(dummy);
494 area2.setText(dummy);
496 area3.setText(dummy);
503 long time = (elapsedTimes.get(indexSize - 1) - elapsedTimes.get(indexSize - 2));
504 String elapsedTime = "[" + (timerCounter.getElapsedCount() - 1) + "] " + TimeStringConvert.getTimeString(time);
505 area1.setText(elapsedTime);
507 area2.setText(dummy);
509 area3.setText(dummy);
516 long time1 = (elapsedTimes.get(indexSize - 2) - elapsedTimes.get(indexSize - 3));
517 long time2 = (elapsedTimes.get(indexSize - 1) - elapsedTimes.get(indexSize - 2));
518 String elapsedTime1 = "[" + (timerCounter.getElapsedCount() - 2) + "] " + TimeStringConvert.getTimeString(time1);
519 String elapsedTime2 = "[" + (timerCounter.getElapsedCount() - 1) + "] " + TimeStringConvert.getTimeString(time2);
520 area1.setText(elapsedTime1);
522 area2.setText(elapsedTime2);
524 area3.setText(dummy);
530 long time1 = (elapsedTimes.get(indexSize - 3) - elapsedTimes.get(indexSize - 4));
531 long time2 = (elapsedTimes.get(indexSize - 2) - elapsedTimes.get(indexSize - 3));
532 long time3 = (elapsedTimes.get(indexSize - 1) - elapsedTimes.get(indexSize - 2));
533 String elapsedTime1 = "[" + (timerCounter.getElapsedCount() - 3) + "] " + TimeStringConvert.getTimeString(time1);
534 String elapsedTime2 = "[" + (timerCounter.getElapsedCount() - 2) + "] " + TimeStringConvert.getTimeString(time2);
535 String elapsedTime3 = "[" + (timerCounter.getElapsedCount() - 1) + "] " + TimeStringConvert.getTimeString(time3);
536 area1.setText(elapsedTime1);
538 area2.setText(elapsedTime2);
540 area3.setText(elapsedTime3);
546 * Launch ListActivity
549 private void launchListActivity()
551 Log.v(TAG, "launchListActivity()");
554 Intent intent = new Intent(this, ListActivity.class);
555 startActivity(intent);
568 protected void onUserLeaveHint ()
570 Log.v(TAG, "onUserLeaveHint() " );
571 // ハードキー(ホームボタン)が押されたとき、これがひろえるが...
579 public boolean dispatchKeyEvent(KeyEvent event)
581 Log.v(TAG, "dispatchKeyEvent() : " + event.getAction() + " (" + event.getKeyCode() + ")");
583 return (super.dispatchKeyEvent(event));
591 public boolean onKeyDown(int keyCode, KeyEvent event)
593 Log.v(TAG, "onKeyDown() : " + event.getAction() + " (" + event.getKeyCode() + ")" + keyCode);
594 if (event.getRepeatCount() == 0)
596 if (keyCode == KeyEvent.KEYCODE_STEM_1)
601 else if (keyCode == KeyEvent.KEYCODE_STEM_2)
606 else if (keyCode == KeyEvent.KEYCODE_STEM_3)
612 return (super.onKeyDown(keyCode, event));
616 public void counterStatusChanged(boolean forceStartTimer)
622 ITimerCounter timerCounter = counter;
623 if (timerCounter != null)
625 MyTimerTrigger trigger = new MyTimerTrigger(this, 100, timerCounter);
626 trigger.startTimer();
635 runOnUiThread(new Runnable()
650 private String getElapsedTime()
652 String elapsedTime = "";
653 ITimerCounter timerCounter = counter;
654 if (timerCounter != null)
656 int count = timerCounter.getElapsedCount();
659 elapsedTime = "[" + timerCounter.getElapsedCount() + "] " + MyTimerCounter.getTimeString(timerCounter.getCurrentElapsedTime());
662 return (elapsedTime);