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.ArrayAdapter;
12 import android.widget.ImageButton;
13 import android.widget.ListAdapter;
14 import android.widget.ListView;
15 import android.widget.RelativeLayout;
16 import android.widget.TextView;
18 import net.osdn.gokigen.joggingtimer.R;
19 import net.osdn.gokigen.joggingtimer.recordlist.ListActivity;
20 import net.osdn.gokigen.joggingtimer.stopwatch.graphview.LapTimeGraphView;
21 import net.osdn.gokigen.joggingtimer.stopwatch.listview.LapTimeItems;
22 import net.osdn.gokigen.joggingtimer.utilities.TimeStringConvert;
24 import java.text.SimpleDateFormat;
25 import java.util.Date;
26 import java.util.List;
27 import java.util.Locale;
33 public class MainActivity extends WearableActivity implements IClickCallback, MyTimerTrigger.ITimeoutReceiver, MyTimerCounter.ICounterStatusNotify
35 private final String TAG = toString();
36 private final IWearableActivityControl controller = new WearableActivityController();
37 private MyTimerCounter counter = new MyTimerCounter();
38 private boolean isCounterLapTime = true;
39 private boolean isLaptimeView = true;
40 private boolean pendingStart = false;
41 private int currentLapCount = 0;
42 private ITimerStopTrigger stopTrigger = null;
48 protected void onCreate(Bundle savedInstanceState)
50 super.onCreate(savedInstanceState);
51 Log.v(TAG, "onCreate()");
53 setContentView(R.layout.activity_main);
55 controller.setup(this, this, counter);
65 protected void onResume()
70 Intent intent = getIntent();
71 String action = intent.getAction();
72 Log.v(TAG, "onResume() : " + action);
74 boolean isStartTimer = false;
77 if (action.equals("com.google.android.wearable.action.STOPWATCH"))
81 else if (action.equals("vnd.google.fitness.TRACK"))
83 String activity = intent.getStringExtra("actionStatus");
84 if ((activity != null)&&(activity.equals("ActiveActionStatus")))
90 isLaptimeView = controller.getDisplayMode();
91 //Log.v(TAG, "isLaptimeView " + isLaptimeView);
93 controller.setupReferenceData();
107 protected void onPause()
110 Log.v(TAG, "onPause()");
118 public void onStart()
121 Log.v(TAG, "onStart()");
124 counter.setCallback(this);
125 controller.setupDatabase(this, false);
136 Log.v(TAG, "onStop()");
137 if (stopTrigger != null)
139 stopTrigger.forceStop();
141 controller.exitApplication(this);
149 public void onEnterAmbient(Bundle ambientDetails)
151 super.onEnterAmbient(ambientDetails);
152 Log.v(TAG, "onEnterAmbient()");
160 public void onExitAmbient()
162 super.onExitAmbient();
163 Log.v(TAG, "onExitAmbient()");
164 //updateTimerLabel();
172 public void onUpdateAmbient()
174 super.onUpdateAmbient();
175 Log.v(TAG, "onUpdateAmbient()");
181 private void updateTimerLabel()
183 ITimerCounter timerCounter = counter;
184 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 else if (timerCounter.isReset())
225 bgColor = Color.BLACK;
226 insetLayout.setBackgroundColor(bgColor);
227 insetLayout.invalidate();
229 layout.setBackgroundColor(bgColor);
232 btn1.setImageResource(R.drawable.ic_play_arrow_black_24dp);
233 btn1.setBackgroundColor(bgColor);
234 btn1.setVisibility(View.VISIBLE);
237 btn2.setImageResource(R.drawable.ic_format_list_bulleted_black_24dp);
238 btn2.setBackgroundColor(bgColor);
239 btn2.setVisibility(View.VISIBLE);
242 btn3.setImageResource(R.drawable.ic_refresh_black_24dp);
243 btn3.setBackgroundColor(bgColor);
244 btn3.setVisibility(View.INVISIBLE);
249 bgColor = Color.BLACK;
250 insetLayout.setBackgroundColor(bgColor);
251 insetLayout.invalidate();
253 layout.setBackgroundColor(bgColor);
256 btn1.setImageResource(R.drawable.ic_play_arrow_black_24dp);
257 btn1.setVisibility(View.VISIBLE);
258 btn1.setBackgroundColor(bgColor);
261 btn2.setImageResource(R.drawable.ic_format_list_bulleted_black_24dp);
262 btn2.setVisibility(View.VISIBLE);
263 btn2.setBackgroundColor(bgColor);
266 btn3.setImageResource(R.drawable.ic_refresh_black_24dp);
267 btn3.setVisibility(View.VISIBLE);
268 btn3.setBackgroundColor(bgColor);
271 updateElapsedTimes();
275 public void clickedCounter()
278 isCounterLapTime = !isCounterLapTime;
285 public void clickedBtn1()
294 private void startTimer()
298 ITimerCounter timerCounter = counter;
299 if (timerCounter != null)
301 LapTimeGraphView graphView = findViewById(R.id.graph_area);
302 if (timerCounter.isStarted())
304 Log.v(TAG, "startTimer() LAP TIME");
305 // チャタリング防止(ラップタイムとして、3秒以内は記録しないようにする)
306 long currentElapsedTime = timerCounter.getCurrentElapsedTime();
307 if (currentElapsedTime > 3000)
310 long lapTime = timerCounter.timeStamp();
311 long refLapTime = timerCounter.getReferenceLapTime(currentLapCount);
312 long diffTime = (refLapTime == 0) ? 0 : (currentElapsedTime - refLapTime);
313 controller.vibrate(50);
314 controller.getDataEntry().appendTimeData(lapTime);
315 controller.addTimeStamp(currentLapCount, currentElapsedTime, diffTime);
316 //Log.v(TAG, " [[[ " + currentLapCount + " lap: " + currentElapsedTime + " diff:" + diffTime + " (" + refLapTime + ") ]]]");
318 if (graphView != null)
320 graphView.notifyLapTime();
326 Log.v(TAG, "startTimer() START");
327 controller.clearTimeStamp();
328 timerCounter.start();
329 MyTimerTrigger trigger = new MyTimerTrigger(this, 100, timerCounter);
330 trigger.startTimer();
332 stopTrigger = trigger;
333 controller.timerStarted(true);
334 controller.vibrate(120);
336 Date date = new Date();
337 SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
338 String title = sdf1.format(date);
339 long startTime = timerCounter.getStartTime();
340 controller.getDataEntry().createIndex(title, startTime);
342 if (graphView != null)
344 graphView.notifyStarted(startTime);
361 private boolean stopTimer()
366 ITimerCounter timerCounter = counter;
367 if (timerCounter != null)
369 if (timerCounter.isStarted())
372 controller.timerStarted(false);
373 controller.vibrate(120);
374 controller.getDataEntry().finishTimeData(timerCounter.getStartTime(), timerCounter.getStopTime());
376 int lapCount = currentLapCount + 1;
377 long currentElapsedTime = timerCounter.getLastElapsedTime() - timerCounter.getElapsedTime(currentLapCount);
378 long refLapTime = timerCounter.getReferenceLapTime(lapCount);
379 long diffTime = (refLapTime == 0) ? 0 : (currentElapsedTime - refLapTime);
380 controller.addTimeStamp(lapCount, currentElapsedTime, diffTime);
383 LapTimeGraphView graphView = findViewById(R.id.graph_area);
384 if (graphView != null)
386 graphView.notifyStopped();
403 public void clickedBtn2()
405 ITimerCounter timerCounter = counter;
406 if (timerCounter != null)
408 if (!timerCounter.isStarted())
411 launchListActivity();
414 controller.vibrate(35);
424 public void clickedBtn3()
426 ITimerCounter timerCounter = counter;
427 if (timerCounter != null)
429 if (!timerCounter.isStarted())
431 timerCounter.reset();
432 controller.vibrate(50);
433 controller.clearTimeStamp();
435 LapTimeGraphView graphView = findViewById(R.id.graph_area);
436 if (graphView != null)
438 graphView.notifyReset();
446 public void clickedArea()
448 Log.v(TAG, "clickedArea()");
453 public boolean pushedBtn1()
459 public boolean pushedBtn2()
461 return (stopTimer());
465 public boolean pushedBtn3()
471 public boolean pushedArea()
473 isLaptimeView = !isLaptimeView;
474 controller.setDisplayMode(isLaptimeView);
475 Log.v(TAG, "pushedArea() : " + isLaptimeView);
476 changeGraphicView(isLaptimeView);
486 public void timeout()
490 runOnUiThread(new Runnable() {
507 private void updateMainSubCounter()
509 TextView main = findViewById(R.id.main_counter);
510 TextView sub = findViewById(R.id.sub_counter1);
512 ITimerCounter timerCounter = counter;
513 if (timerCounter != null)
515 long time1 = timerCounter.getPastTime();
516 CharSequence str1 = TimeStringConvert.getTimeString(time1);
517 CharSequence str2 = "";
518 if (timerCounter.isStarted())
520 long time2 = timerCounter.getCurrentElapsedTime();
521 int lapCount = timerCounter.getElapsedCount();
522 if ((time2 >= 100) && (lapCount > 1))
524 str2 = "[" + lapCount + "] " + TimeStringConvert.getTimeString(time2);
528 if ((str2.length() > 0)&&(isCounterLapTime))
548 private void updateElapsedTimes()
552 updateElapsedTimesGraph();
556 updateElapsedTimesText();
564 private void updateElapsedTimesGraph()
566 Log.v(TAG, "updateElapsedTimesGraph()");
567 LapTimeGraphView view = findViewById(R.id.graph_area);
575 private void updateElapsedTimesText()
577 // Log.v(TAG, "updateElapsedTimesText()");
581 * Launch ListActivity
584 private void launchListActivity()
586 Log.v(TAG, "launchListActivity()");
589 Intent intent = new Intent(this, ListActivity.class);
590 startActivity(intent);
603 protected void onUserLeaveHint()
605 Log.v(TAG, "onUserLeaveHint() " );
606 // ハードキー(ホームボタン)が押されたとき、これがひろえるが...
614 public boolean dispatchKeyEvent(KeyEvent event)
616 Log.v(TAG, "dispatchKeyEvent() : " + event.getAction() + " (" + event.getKeyCode() + ")");
618 return (super.dispatchKeyEvent(event));
626 public boolean onKeyDown(int keyCode, KeyEvent event)
628 Log.v(TAG, "onKeyDown() : " + event.getAction() + " (" + event.getKeyCode() + ")" + keyCode);
629 if (event.getRepeatCount() == 0)
631 if (keyCode == KeyEvent.KEYCODE_STEM_1)
636 else if (keyCode == KeyEvent.KEYCODE_STEM_2)
641 else if (keyCode == KeyEvent.KEYCODE_STEM_3)
647 return (super.onKeyDown(keyCode, event));
655 public void counterStatusChanged(final boolean forceStartTimer)
657 ITimerCounter timerCounter = counter;
662 LapTimeGraphView graphView = findViewById(R.id.graph_area);
663 if (timerCounter != null)
665 MyTimerTrigger trigger = new MyTimerTrigger(this, 100, timerCounter);
666 trigger.startTimer();
667 stopTrigger = trigger;
669 if (graphView != null)
671 graphView.notifyLapTime();
680 runOnUiThread(new Runnable()
690 pendingStart = false;
694 reloadLapTimeList(forceStartTimer);
697 changeGraphicView(isLaptimeView);
709 private void reloadLapTimeList(final boolean forceStartTimer)
711 ITimerCounter timerCounter = counter;
712 if ((!forceStartTimer)||(timerCounter == null))
717 // Adapter と TimerCounterの整合性を確認
720 List<Long> lapTimeList = timerCounter.getLapTimeList();
721 int lapCount = lapTimeList.size();
722 int listCount = controller.getLapTimeCount();
723 if (lapCount != listCount)
725 Log.v(TAG, "LAP COUNT IS MISMATCH!!! lap:" + lapCount + " vs list:" + listCount);
727 controller.clearTimeStamp();
728 long prevTime = lapTimeList.get(0);
729 for (long lapTime : lapTimeList)
732 if (prevTime != lapTime)
734 long refLapTime = counter.getReferenceLapTime(index - 1);
735 long curLapTime = lapTime - prevTime;
736 long calcRefLapTime = (refLapTime == 0) ? 0 : (curLapTime - refLapTime);
737 controller.addTimeStamp((index - 1), curLapTime, calcRefLapTime);
741 currentLapCount = lapCount - 1;
754 private void changeGraphicView(boolean isGraphics)
758 LapTimeGraphView graphView = findViewById(R.id.graph_area);
759 ListView listView = findViewById(R.id.laptime_list_area);
762 graphView.setITimerCounter(counter);
763 graphView.setVisibility(View.VISIBLE);
764 listView.setVisibility(View.GONE);
768 graphView.setVisibility(View.GONE);
769 listView.setVisibility(View.VISIBLE);
771 //controller.vibrate(30);