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
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);
57 protected void onSaveInstanceState(Bundle outState)
59 super.onSaveInstanceState(outState);
62 outState.putParcelable("timerCounter", counter);
69 protected void onRestoreInstanceState(Bundle savedInstanceState)
71 super.onRestoreInstanceState(savedInstanceState);
73 /* ここで保存した状態を読み出して設定 */
74 counter = savedInstanceState.getParcelable("timerCounter");
81 protected void onResume()
86 Intent intent = getIntent();
87 String action = intent.getAction();
88 Log.v(TAG, "onResume() : " + action);
90 boolean isStartTimer = false;
93 if (action.equals("com.google.android.wearable.action.STOPWATCH"))
97 else if (action.equals("vnd.google.fitness.TRACK"))
99 String activity = intent.getStringExtra("actionStatus");
100 if ((activity != null)&&(activity.equals("ActiveActionStatus")))
117 protected void onPause()
120 Log.v(TAG, "onPause()");
128 public void onStart()
131 Log.v(TAG, "onStart()");
132 controller.setupDatabase(this, false);
143 Log.v(TAG, "onStop()");
144 controller.exitApplication(this);
152 public void onEnterAmbient(Bundle ambientDetails)
154 super.onEnterAmbient(ambientDetails);
155 Log.v(TAG, "onEnterAmbient()");
163 public void onExitAmbient()
165 super.onExitAmbient();
166 Log.v(TAG, "onExitAmbient()");
167 //updateTimerLabel();
175 public void onUpdateAmbient()
177 super.onUpdateAmbient();
178 Log.v(TAG, "onUpdateAmbient()");
184 public void updateTimerLabel()
186 ITimerCounter timerCounter = counter;
187 if (timerCounter != null)
190 BoxInsetLayout insetLayout = findViewById(R.id.box_inset_layout);
191 RelativeLayout layout = findViewById(R.id.relative_main_layout);
193 ImageButton btn1 = findViewById(R.id.btn1);
194 ImageButton btn2 = findViewById(R.id.btn2);
195 ImageButton btn3 = findViewById(R.id.btn3);
197 updateMainSubCounter();
199 if (timerCounter.isStarted())
201 bgColor = Color.BLACK;
202 insetLayout.setBackgroundColor(bgColor);
203 insetLayout.invalidate();
205 layout.setBackgroundColor(bgColor);
208 btn1.setImageResource(R.drawable.ic_flag_black_24dp);
209 btn1.setBackgroundColor(bgColor);
210 btn1.setVisibility(View.VISIBLE);
213 btn2.setImageResource(R.drawable.ic_stop_black_24dp);
214 btn2.setBackgroundColor(bgColor);
215 btn2.setVisibility(View.VISIBLE);
218 btn3.setImageResource(R.drawable.ic_block_black_24dp);
219 btn3.setBackgroundColor(bgColor);
220 btn3.setVisibility(View.INVISIBLE);
223 updateElapsedTimes();
225 else if (timerCounter.isReset())
227 bgColor = Color.BLACK;
228 insetLayout.setBackgroundColor(bgColor);
229 insetLayout.invalidate();
231 layout.setBackgroundColor(bgColor);
234 btn1.setImageResource(R.drawable.ic_play_arrow_black_24dp);
235 btn1.setBackgroundColor(bgColor);
236 btn1.setVisibility(View.VISIBLE);
239 btn2.setImageResource(R.drawable.ic_format_list_bulleted_black_24dp);
240 btn2.setBackgroundColor(bgColor);
241 btn2.setVisibility(View.VISIBLE);
244 btn3.setImageResource(R.drawable.ic_refresh_black_24dp);
245 btn3.setBackgroundColor(bgColor);
246 btn3.setVisibility(View.INVISIBLE);
249 updateElapsedTimes();
253 bgColor = Color.BLACK;
254 insetLayout.setBackgroundColor(bgColor);
255 insetLayout.invalidate();
257 layout.setBackgroundColor(bgColor);
260 btn1.setImageResource(R.drawable.ic_play_arrow_black_24dp);
261 btn1.setVisibility(View.VISIBLE);
262 btn1.setBackgroundColor(bgColor);
265 btn2.setImageResource(R.drawable.ic_format_list_bulleted_black_24dp);
266 btn2.setVisibility(View.VISIBLE);
267 btn2.setBackgroundColor(bgColor);
270 btn3.setImageResource(R.drawable.ic_refresh_black_24dp);
271 btn3.setVisibility(View.VISIBLE);
272 btn3.setBackgroundColor(bgColor);
275 updateElapsedTimes();
281 public void clickedCounter()
284 isCounterLapTime = !isCounterLapTime;
291 public void clickedBtn1()
300 private void startTimer()
304 ITimerCounter timerCounter = counter;
305 if (timerCounter != null)
307 if (timerCounter.isStarted())
309 Log.v(TAG, "startTimer() LAP TIME");
310 // チャタリング防止(ラップタイムとして、3秒以内は記録しないようにする)
311 if (timerCounter.getCurrentElapsedTime() > 3000)
313 long lapTime = timerCounter.timeStamp();
314 controller.vibrate(50);
315 controller.getDataEntry().appendTimeData(lapTime);
320 Log.v(TAG, "startTimer() START");
321 timerCounter.start();
322 MyTimerTrigger trigger = new MyTimerTrigger(this, 100, timerCounter);
323 trigger.startTimer();
324 controller.timerStarted(true);
325 controller.vibrate(120);
327 Date date = new Date();
328 SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
329 String title = sdf1.format(date);
330 controller.getDataEntry().createIndex(title, "", 0, timerCounter.getStartTime());
345 private boolean stopTimer()
350 ITimerCounter timerCounter = counter;
351 if (timerCounter != null)
353 if (timerCounter.isStarted())
356 controller.timerStarted(false);
357 controller.vibrate(120);
358 controller.getDataEntry().finishTimeData(timerCounter.getStartTime(), timerCounter.getStopTime());
375 public void clickedBtn2()
377 ITimerCounter timerCounter = counter;
378 if (timerCounter != null)
380 if (!timerCounter.isStarted())
383 launchListActivity();
386 controller.vibrate(35);
396 public void clickedBtn3()
398 ITimerCounter timerCounter = counter;
399 if (timerCounter != null)
401 if (!timerCounter.isStarted())
403 timerCounter.reset();
404 controller.vibrate(50);
411 public boolean pushedBtn1()
417 public boolean pushedBtn2()
419 return (stopTimer());
423 public boolean pushedBtn3()
429 public void dataIsReloaded(ArrayList<Long> list)
431 ITimerCounter timerCounter = counter;
432 if ((timerCounter != null)&&(list != null))
436 timerCounter.reloadTimerCounter(list.get(0), list);
438 MyTimerTrigger trigger = new MyTimerTrigger(this, 100, timerCounter);
439 trigger.startTimer();
447 runOnUiThread(new Runnable()
462 public void timeout()
466 runOnUiThread(new Runnable() {
483 private void updateMainSubCounter()
485 TextView main = findViewById(R.id.main_counter);
486 TextView sub = findViewById(R.id.sub_counter1);
488 ITimerCounter timerCounter = counter;
489 if (timerCounter != null)
491 long time1 = timerCounter.getPastTime();
492 CharSequence str1 = TimeStringConvert.getTimeString(time1);
494 CharSequence str2 = "";
495 if (timerCounter.isStarted())
497 long time2 = timerCounter.getCurrentElapsedTime();
498 int lapCount = timerCounter.getElapsedCount();
499 if ((time2 >= 100) && (lapCount > 1))
501 str2 = "[" + lapCount + "] " + TimeStringConvert.getTimeString(time2);
505 if ((str2.length() > 0)&&(isCounterLapTime))
525 private void updateElapsedTimes()
528 TextView area1 = findViewById(R.id.sub_counter2);
529 TextView area2 = findViewById(R.id.sub_counter3);
530 TextView area3 = findViewById(R.id.sub_counter4);
532 ITimerCounter timerCounter = counter;
533 if (timerCounter != null)
535 List<Long> elapsedTimes = timerCounter.getTimerList();
536 int indexSize = elapsedTimes.size();
540 area1.setText(dummy);
542 area2.setText(dummy);
544 area3.setText(dummy);
551 long time = (elapsedTimes.get(indexSize - 1) - elapsedTimes.get(indexSize - 2));
552 String elapsedTime = "[" + (timerCounter.getElapsedCount() - 1) + "] " + TimeStringConvert.getTimeString(time);
553 area1.setText(elapsedTime);
555 area2.setText(dummy);
557 area3.setText(dummy);
564 long time1 = (elapsedTimes.get(indexSize - 2) - elapsedTimes.get(indexSize - 3));
565 long time2 = (elapsedTimes.get(indexSize - 1) - elapsedTimes.get(indexSize - 2));
566 String elapsedTime1 = "[" + (timerCounter.getElapsedCount() - 2) + "] " + TimeStringConvert.getTimeString(time1);
567 String elapsedTime2 = "[" + (timerCounter.getElapsedCount() - 1) + "] " + TimeStringConvert.getTimeString(time2);
568 area1.setText(elapsedTime1);
570 area2.setText(elapsedTime2);
572 area3.setText(dummy);
578 long time1 = (elapsedTimes.get(indexSize - 3) - elapsedTimes.get(indexSize - 4));
579 long time2 = (elapsedTimes.get(indexSize - 2) - elapsedTimes.get(indexSize - 3));
580 long time3 = (elapsedTimes.get(indexSize - 1) - elapsedTimes.get(indexSize - 2));
581 String elapsedTime1 = "[" + (timerCounter.getElapsedCount() - 3) + "] " + TimeStringConvert.getTimeString(time1);
582 String elapsedTime2 = "[" + (timerCounter.getElapsedCount() - 2) + "] " + TimeStringConvert.getTimeString(time2);
583 String elapsedTime3 = "[" + (timerCounter.getElapsedCount() - 1) + "] " + TimeStringConvert.getTimeString(time3);
584 area1.setText(elapsedTime1);
586 area2.setText(elapsedTime2);
588 area3.setText(elapsedTime3);
594 * Launch ListActivity
597 private void launchListActivity()
599 Log.v(TAG, "launchListActivity()");
602 Intent intent = new Intent(this, ListActivity.class);
603 startActivity(intent);
616 protected void onUserLeaveHint ()
618 Log.v(TAG, "onUserLeaveHint() " );
619 // ハードキー(ホームボタン)が押されたとき、これがひろえるが...
627 public boolean dispatchKeyEvent(KeyEvent event)
629 Log.v(TAG, "dispatchKeyEvent() : " + event.getAction() + " (" + event.getKeyCode() + ")");
631 return (super.dispatchKeyEvent(event));
639 public boolean onKeyDown(int keyCode, KeyEvent event)
641 Log.v(TAG, "onKeyDown() : " + event.getAction() + " (" + event.getKeyCode() + ")" + keyCode);
642 if (event.getRepeatCount() == 0)
644 if (keyCode == KeyEvent.KEYCODE_STEM_1)
649 else if (keyCode == KeyEvent.KEYCODE_STEM_2)
654 else if (keyCode == KeyEvent.KEYCODE_STEM_3)
660 return (super.onKeyDown(keyCode, event));
668 private String getElapsedTime()
670 String elapsedTime = "";
671 ITimerCounter timerCounter = counter;
672 if (timerCounter != null)
674 int count = timerCounter.getElapsedCount();
677 elapsedTime = "[" + timerCounter.getElapsedCount() + "] " + MyTimerCounter.getTimeString(timerCounter.getCurrentElapsedTime());
680 return (elapsedTime);