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.ListView;
13 import android.widget.RelativeLayout;
14 import android.widget.TextView;
16 import net.osdn.gokigen.joggingtimer.R;
17 import net.osdn.gokigen.joggingtimer.recordlist.ListActivity;
18 import net.osdn.gokigen.joggingtimer.stopwatch.graphview.LapTimeGraphView;
19 import net.osdn.gokigen.joggingtimer.utilities.TimeStringConvert;
21 import java.text.SimpleDateFormat;
22 import java.util.Date;
23 import java.util.List;
24 import java.util.Locale;
30 public class MainActivity extends WearableActivity implements IClickCallback, MyTimerTrigger.ITimeoutReceiver, MyTimerCounter.ICounterStatusNotify
32 private final String TAG = toString();
33 private final IWearableActivityControl controller = new WearableActivityController();
34 private MyTimerCounter counter = new MyTimerCounter();
35 private boolean isCounterLapTime = true;
36 private boolean isLaptimeView = true;
37 private boolean pendingStart = false;
38 private int currentLapCount = 0;
39 private ITimerStopTrigger stopTrigger = null;
45 protected void onCreate(Bundle savedInstanceState)
47 super.onCreate(savedInstanceState);
48 Log.v(TAG, "onCreate()");
50 setContentView(R.layout.activity_main);
52 controller.setup(this, this, counter);
62 protected void onResume()
67 Intent intent = getIntent();
68 String action = intent.getAction();
69 Log.v(TAG, "onResume() : " + action);
71 boolean isStartTimer = false;
74 if (action.equals("com.google.android.wearable.action.STOPWATCH"))
78 else if (action.equals("vnd.google.fitness.TRACK"))
80 String activity = intent.getStringExtra("actionStatus");
81 if ((activity != null)&&(activity.equals("ActiveActionStatus")))
87 isLaptimeView = controller.getDisplayMode();
88 //Log.v(TAG, "isLaptimeView " + isLaptimeView);
90 controller.setupReferenceData();
104 protected void onPause()
107 Log.v(TAG, "onPause()");
115 public void onStart()
118 Log.v(TAG, "onStart()");
121 counter.setCallback(this);
122 controller.setupDatabase(this, false);
133 Log.v(TAG, "onStop()");
134 if (stopTrigger != null)
136 stopTrigger.forceStop();
138 controller.exitApplication(this);
146 public void onEnterAmbient(Bundle ambientDetails)
148 super.onEnterAmbient(ambientDetails);
149 Log.v(TAG, "onEnterAmbient()");
157 public void onExitAmbient()
159 super.onExitAmbient();
160 Log.v(TAG, "onExitAmbient()");
161 //updateTimerLabel();
169 public void onUpdateAmbient()
171 super.onUpdateAmbient();
172 Log.v(TAG, "onUpdateAmbient()");
178 private void updateTimerLabel()
180 ITimerCounter timerCounter = counter;
181 if (timerCounter == null)
187 BoxInsetLayout insetLayout = findViewById(R.id.box_inset_layout);
188 RelativeLayout layout = findViewById(R.id.relative_main_layout);
190 ImageButton btn1 = findViewById(R.id.btn1);
191 ImageButton btn2 = findViewById(R.id.btn2);
192 ImageButton btn3 = findViewById(R.id.btn3);
194 updateMainSubCounter();
196 if (timerCounter.isStarted())
198 bgColor = Color.BLACK;
199 insetLayout.setBackgroundColor(bgColor);
200 insetLayout.invalidate();
202 layout.setBackgroundColor(bgColor);
205 btn1.setImageResource(R.drawable.ic_flag_black_24dp);
206 btn1.setBackgroundColor(bgColor);
207 btn1.setVisibility(View.VISIBLE);
210 btn2.setImageResource(R.drawable.ic_stop_black_24dp);
211 btn2.setBackgroundColor(bgColor);
212 btn2.setVisibility(View.VISIBLE);
215 btn3.setImageResource(R.drawable.ic_block_black_24dp);
216 btn3.setBackgroundColor(bgColor);
217 btn3.setVisibility(View.INVISIBLE);
220 else if (timerCounter.isReset())
222 bgColor = Color.BLACK;
223 insetLayout.setBackgroundColor(bgColor);
224 insetLayout.invalidate();
226 layout.setBackgroundColor(bgColor);
229 btn1.setImageResource(R.drawable.ic_play_arrow_black_24dp);
230 btn1.setBackgroundColor(bgColor);
231 btn1.setVisibility(View.VISIBLE);
234 btn2.setImageResource(R.drawable.ic_format_list_bulleted_black_24dp);
235 btn2.setBackgroundColor(bgColor);
236 btn2.setVisibility(View.VISIBLE);
239 btn3.setImageResource(R.drawable.ic_refresh_black_24dp);
240 btn3.setBackgroundColor(bgColor);
241 btn3.setVisibility(View.INVISIBLE);
246 bgColor = Color.BLACK;
247 insetLayout.setBackgroundColor(bgColor);
248 insetLayout.invalidate();
250 layout.setBackgroundColor(bgColor);
253 btn1.setImageResource(R.drawable.ic_play_arrow_black_24dp);
254 btn1.setVisibility(View.VISIBLE);
255 btn1.setBackgroundColor(bgColor);
258 btn2.setImageResource(R.drawable.ic_format_list_bulleted_black_24dp);
259 btn2.setVisibility(View.VISIBLE);
260 btn2.setBackgroundColor(bgColor);
263 btn3.setImageResource(R.drawable.ic_refresh_black_24dp);
264 btn3.setVisibility(View.VISIBLE);
265 btn3.setBackgroundColor(bgColor);
268 updateElapsedTimes();
272 public void clickedCounter()
275 isCounterLapTime = !isCounterLapTime;
282 public void clickedBtn1()
291 private void startTimer()
295 ITimerCounter timerCounter = counter;
296 if (timerCounter != null)
298 LapTimeGraphView graphView = findViewById(R.id.graph_area);
299 if (timerCounter.isStarted())
301 Log.v(TAG, "startTimer() LAP TIME");
302 // チャタリング防止(ラップタイムとして、3秒以内は記録しないようにする)
303 long currentElapsedTime = timerCounter.getCurrentElapsedTime();
304 if (currentElapsedTime > 3000)
307 long lapTime = timerCounter.timeStamp();
308 long refLapTime = timerCounter.getReferenceLapTime(currentLapCount);
309 long diffTime = (refLapTime == 0) ? 0 : (currentElapsedTime - refLapTime);
310 controller.vibrate(50);
311 controller.getDataEntry().appendTimeData(lapTime);
312 controller.addTimeStamp(currentLapCount, currentElapsedTime, diffTime);
313 //Log.v(TAG, " [[[ " + currentLapCount + " lap: " + currentElapsedTime + " diff:" + diffTime + " (" + refLapTime + ") ]]]");
315 if (graphView != null)
317 graphView.notifyLapTime();
323 Log.v(TAG, "startTimer() START");
324 controller.clearTimeStamp();
325 timerCounter.start();
326 MyTimerTrigger trigger = new MyTimerTrigger(this, 100, timerCounter);
327 trigger.startTimer();
329 stopTrigger = trigger;
330 controller.timerStarted(true);
331 controller.vibrate(120);
333 Date date = new Date();
334 SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
335 String title = sdf1.format(date);
336 long startTime = timerCounter.getStartTime();
337 controller.getDataEntry().createIndex(title, startTime);
339 if (graphView != null)
341 graphView.notifyStarted(startTime);
358 private boolean stopTimer()
363 ITimerCounter timerCounter = counter;
364 if (timerCounter != null)
366 if (timerCounter.isStarted())
369 controller.timerStarted(false);
370 controller.vibrate(120);
371 controller.getDataEntry().finishTimeData(timerCounter.getStartTime(), timerCounter.getStopTime());
373 int lapCount = currentLapCount + 1;
374 long currentElapsedTime = timerCounter.getLastElapsedTime() - timerCounter.getElapsedTime(currentLapCount);
375 long refLapTime = timerCounter.getReferenceLapTime(lapCount);
376 long diffTime = (refLapTime == 0) ? 0 : (currentElapsedTime - refLapTime);
377 controller.addTimeStamp(lapCount, currentElapsedTime, diffTime);
380 LapTimeGraphView graphView = findViewById(R.id.graph_area);
381 if (graphView != null)
383 graphView.notifyStopped();
400 public void clickedBtn2()
402 ITimerCounter timerCounter = counter;
403 if (timerCounter != null)
405 if (!timerCounter.isStarted())
408 launchListActivity();
411 controller.vibrate(35);
421 public void clickedBtn3()
423 ITimerCounter timerCounter = counter;
424 if (timerCounter != null)
426 if (!timerCounter.isStarted())
428 timerCounter.reset();
429 controller.vibrate(50);
430 controller.clearTimeStamp();
432 LapTimeGraphView graphView = findViewById(R.id.graph_area);
433 if (graphView != null)
435 graphView.notifyReset();
443 public void clickedArea()
445 Log.v(TAG, "clickedArea()");
450 public boolean pushedBtn1()
456 public boolean pushedBtn2()
458 return (stopTimer());
462 public boolean pushedBtn3()
468 public boolean pushedArea()
470 isLaptimeView = !isLaptimeView;
471 controller.setDisplayMode(isLaptimeView);
472 Log.v(TAG, "pushedArea() : " + isLaptimeView);
473 changeGraphicView(isLaptimeView);
483 public void timeout()
487 runOnUiThread(new Runnable() {
504 private void updateMainSubCounter()
506 TextView main = findViewById(R.id.main_counter);
507 TextView sub = findViewById(R.id.sub_counter1);
509 ITimerCounter timerCounter = counter;
510 if (timerCounter != null)
512 long time1 = timerCounter.getPastTime();
513 CharSequence str1 = TimeStringConvert.getTimeString(time1);
514 CharSequence str2 = "";
515 if (timerCounter.isStarted())
517 long time2 = timerCounter.getCurrentElapsedTime();
518 int lapCount = timerCounter.getElapsedCount();
519 if ((time2 >= 100) && (lapCount > 1))
521 str2 = "[" + lapCount + "] " + TimeStringConvert.getTimeString(time2);
525 if ((str2.length() > 0)&&(isCounterLapTime))
545 private void updateElapsedTimes()
549 updateElapsedTimesGraph();
553 updateElapsedTimesText();
561 private void updateElapsedTimesGraph()
563 Log.v(TAG, "updateElapsedTimesGraph()");
564 LapTimeGraphView view = findViewById(R.id.graph_area);
572 private void updateElapsedTimesText()
574 // Log.v(TAG, "updateElapsedTimesText()");
578 * Launch ListActivity
581 private void launchListActivity()
583 Log.v(TAG, "launchListActivity()");
586 Intent intent = new Intent(this, ListActivity.class);
587 startActivity(intent);
600 protected void onUserLeaveHint()
602 Log.v(TAG, "onUserLeaveHint() " );
603 // ハードキー(ホームボタン)が押されたとき、これがひろえるが...
611 public boolean dispatchKeyEvent(KeyEvent event)
613 Log.v(TAG, "dispatchKeyEvent() : " + event.getAction() + " (" + event.getKeyCode() + ")");
615 return (super.dispatchKeyEvent(event));
623 public boolean onKeyDown(int keyCode, KeyEvent event)
625 Log.v(TAG, "onKeyDown() : " + event.getAction() + " (" + event.getKeyCode() + ")" + keyCode);
626 if (event.getRepeatCount() == 0)
628 if (keyCode == KeyEvent.KEYCODE_STEM_1)
633 else if (keyCode == KeyEvent.KEYCODE_STEM_2)
638 else if (keyCode == KeyEvent.KEYCODE_STEM_3)
644 return (super.onKeyDown(keyCode, event));
652 public void counterStatusChanged(final boolean forceStartTimer)
654 ITimerCounter timerCounter = counter;
659 LapTimeGraphView graphView = findViewById(R.id.graph_area);
660 if (timerCounter != null)
662 MyTimerTrigger trigger = new MyTimerTrigger(this, 100, timerCounter);
663 trigger.startTimer();
664 stopTrigger = trigger;
666 if (graphView != null)
668 graphView.notifyLapTime();
677 runOnUiThread(new Runnable()
687 pendingStart = false;
691 reloadLapTimeList(forceStartTimer);
694 changeGraphicView(isLaptimeView);
706 private void reloadLapTimeList(final boolean forceStartTimer)
708 ITimerCounter timerCounter = counter;
709 if ((!forceStartTimer)||(timerCounter == null))
714 // Adapter と TimerCounterの整合性を確認
717 List<Long> lapTimeList = timerCounter.getLapTimeList();
718 int lapCount = lapTimeList.size();
719 int listCount = controller.getLapTimeCount();
720 if (lapCount != listCount)
722 Log.v(TAG, "LAP COUNT IS MISMATCH!!! lap:" + lapCount + " vs list:" + listCount);
724 controller.clearTimeStamp();
725 long prevTime = lapTimeList.get(0);
726 for (long lapTime : lapTimeList)
729 if (prevTime != lapTime)
731 long refLapTime = counter.getReferenceLapTime(index - 1);
732 long curLapTime = lapTime - prevTime;
733 long calcRefLapTime = (refLapTime == 0) ? 0 : (curLapTime - refLapTime);
734 controller.addTimeStamp((index - 1), curLapTime, calcRefLapTime);
738 currentLapCount = lapCount - 1;
751 private void changeGraphicView(boolean isGraphics)
755 LapTimeGraphView graphView = findViewById(R.id.graph_area);
756 ListView listView = findViewById(R.id.laptime_list_area);
759 graphView.setITimerCounter(counter);
760 graphView.setVisibility(View.VISIBLE);
761 listView.setVisibility(View.GONE);
765 graphView.setVisibility(View.GONE);
766 listView.setVisibility(View.VISIBLE);
768 //controller.vibrate(30);