OSDN Git Service

roundタイプの画面表示調整を開始。
[gokigen/JoggingTimer.git] / wear / src / main / java / net / osdn / gokigen / joggingtimer / stopwatch / MainActivity.java
1 package net.osdn.gokigen.joggingtimer.stopwatch;
2
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;
15
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;
20
21 import java.text.SimpleDateFormat;
22 import java.util.Date;
23 import java.util.List;
24 import java.util.Locale;
25
26 /**
27  *
28  *
29  */
30 public class MainActivity extends WearableActivity implements IClickCallback, MyTimerTrigger.ITimeoutReceiver, MyTimerCounter.ICounterStatusNotify
31 {
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;
40
41     /**
42      *
43      */
44     @Override
45     protected void onCreate(Bundle savedInstanceState)
46     {
47         super.onCreate(savedInstanceState);
48         Log.v(TAG, "onCreate()");
49
50         setContentView(R.layout.activity_main);
51
52         controller.setup(this, this, counter);
53
54         // Enables Always-on
55         setAmbientEnabled();
56     }
57
58     /**
59      *
60      */
61     @Override
62     protected void onResume()
63     {
64         super.onResume();
65
66         // インテントを取得する
67         Intent intent = getIntent();
68         String action = intent.getAction();
69         Log.v(TAG, "onResume() : " + action);
70
71         boolean isStartTimer = false;
72         if (action != null)
73         {
74             if (action.equals("com.google.android.wearable.action.STOPWATCH"))
75             {
76                 isStartTimer = true;
77             }
78             else if (action.equals("vnd.google.fitness.TRACK"))
79             {
80                 String activity = intent.getStringExtra("actionStatus");
81                 if ((activity != null)&&(activity.equals("ActiveActionStatus")))
82                 {
83                     isStartTimer = true;
84                 }
85             }
86         }
87         isLaptimeView = controller.getDisplayMode();
88         //Log.v(TAG, "isLaptimeView " + isLaptimeView);
89
90         controller.setupReferenceData();
91
92         if (isStartTimer)
93         {
94             // start a timer!
95             //startTimer();
96             pendingStart = true;
97         }
98     }
99
100     /**
101      *
102      */
103     @Override
104     protected void onPause()
105     {
106         super.onPause();
107         Log.v(TAG, "onPause()");
108     }
109
110     /**
111      *
112      *
113      */
114     @Override
115     public void onStart()
116     {
117         super.onStart();
118         Log.v(TAG, "onStart()");
119
120         // データベースのセットアップ
121         counter.setCallback(this);
122         controller.setupDatabase(this, false);
123     }
124
125     /**
126      *
127      *
128      */
129     @Override
130     public void onStop()
131     {
132         super.onStop();
133         Log.v(TAG, "onStop()");
134         if (stopTrigger != null)
135         {
136             stopTrigger.forceStop();
137         }
138         controller.exitApplication(this);
139     }
140
141     /**
142      *
143      *
144      */
145     @Override
146     public void onEnterAmbient(Bundle ambientDetails)
147     {
148         super.onEnterAmbient(ambientDetails);
149         Log.v(TAG, "onEnterAmbient()");
150     }
151
152     /**
153      *
154      *
155      */
156     @Override
157     public void onExitAmbient()
158     {
159         super.onExitAmbient();
160         Log.v(TAG, "onExitAmbient()");
161         //updateTimerLabel();
162     }
163
164     /**
165      *
166      *
167      */
168     @Override
169     public void onUpdateAmbient()
170     {
171         super.onUpdateAmbient();
172         Log.v(TAG, "onUpdateAmbient()");
173     }
174
175     /**
176      *
177      */
178     private void updateTimerLabel()
179     {
180         ITimerCounter timerCounter = counter;
181         if (timerCounter == null)
182         {
183             return;
184         }
185
186         int bgColor;
187         BoxInsetLayout insetLayout = findViewById(R.id.box_inset_layout);
188         RelativeLayout layout = findViewById(R.id.relative_main_layout);
189
190         ImageButton btn1 = findViewById(R.id.btn1);
191         ImageButton btn2 = findViewById(R.id.btn2);
192         ImageButton btn3 = findViewById(R.id.btn3);
193
194         updateMainSubCounter();
195
196         if (timerCounter.isStarted())
197         {
198             bgColor = Color.BLACK;
199             insetLayout.setBackgroundColor(bgColor);
200             insetLayout.invalidate();
201
202             layout.setBackgroundColor(bgColor);
203             layout.invalidate();
204
205             btn1.setImageResource(R.drawable.ic_flag_black_24dp);
206             btn1.setBackgroundColor(bgColor);
207             btn1.setVisibility(View.VISIBLE);
208             btn1.invalidate();
209
210             btn2.setImageResource(R.drawable.ic_stop_black_24dp);
211             btn2.setBackgroundColor(bgColor);
212             btn2.setVisibility(View.VISIBLE);
213             btn2.invalidate();
214
215             btn3.setImageResource(R.drawable.ic_block_black_24dp);
216             btn3.setBackgroundColor(bgColor);
217             btn3.setVisibility(View.INVISIBLE);
218             btn3.invalidate();
219         }
220         else if (timerCounter.isReset())
221         {
222             bgColor = Color.BLACK;
223             insetLayout.setBackgroundColor(bgColor);
224             insetLayout.invalidate();
225
226             layout.setBackgroundColor(bgColor);
227             layout.invalidate();
228
229             btn1.setImageResource(R.drawable.ic_play_arrow_black_24dp);
230             btn1.setBackgroundColor(bgColor);
231             btn1.setVisibility(View.VISIBLE);
232             btn1.invalidate();
233
234             btn2.setImageResource(R.drawable.ic_format_list_bulleted_black_24dp);
235             btn2.setBackgroundColor(bgColor);
236             btn2.setVisibility(View.VISIBLE);
237             btn2.invalidate();
238
239             btn3.setImageResource(R.drawable.ic_refresh_black_24dp);
240             btn3.setBackgroundColor(bgColor);
241             btn3.setVisibility(View.INVISIBLE);
242             btn3.invalidate();
243         }
244         else
245         {
246             bgColor = Color.BLACK;
247             insetLayout.setBackgroundColor(bgColor);
248             insetLayout.invalidate();
249
250             layout.setBackgroundColor(bgColor);
251             layout.invalidate();
252
253             btn1.setImageResource(R.drawable.ic_play_arrow_black_24dp);
254             btn1.setVisibility(View.VISIBLE);
255             btn1.setBackgroundColor(bgColor);
256             btn1.invalidate();
257
258             btn2.setImageResource(R.drawable.ic_format_list_bulleted_black_24dp);
259             btn2.setVisibility(View.VISIBLE);
260             btn2.setBackgroundColor(bgColor);
261             btn2.invalidate();
262
263             btn3.setImageResource(R.drawable.ic_refresh_black_24dp);
264             btn3.setVisibility(View.VISIBLE);
265             btn3.setBackgroundColor(bgColor);
266             btn3.invalidate();
267         }
268         updateElapsedTimes();
269     }
270
271     @Override
272     public void clickedCounter()
273     {
274         // 表示順番を変える
275         isCounterLapTime = !isCounterLapTime;
276     }
277
278     /**
279      *
280      */
281     @Override
282     public void clickedBtn1()
283     {
284         startTimer();
285     }
286
287     /**
288      *
289      *
290      */
291     private void startTimer()
292     {
293         try
294         {
295             ITimerCounter timerCounter = counter;
296             if (timerCounter != null)
297             {
298                 LapTimeGraphView graphView = findViewById(R.id.graph_area);
299                 if (timerCounter.isStarted())
300                 {
301                     Log.v(TAG, "startTimer() LAP TIME");
302                     // チャタリング防止(ラップタイムとして、3秒以内は記録しないようにする)
303                     long currentElapsedTime = timerCounter.getCurrentElapsedTime();
304                     if (currentElapsedTime > 3000)
305                     {
306                         currentLapCount++;
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 + ") ]]]");
314
315                         if (graphView != null)
316                         {
317                             graphView.notifyLapTime();
318                         }
319                     }
320                 }
321                 else
322                 {
323                     Log.v(TAG, "startTimer() START");
324                     controller.clearTimeStamp();
325                     timerCounter.start();
326                     MyTimerTrigger trigger = new MyTimerTrigger(this, 100, timerCounter);
327                     trigger.startTimer();
328                     currentLapCount = 0;
329                     stopTrigger = trigger;
330                     controller.timerStarted(true);
331                     controller.vibrate(120);
332
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);
338
339                     if (graphView != null)
340                     {
341                         graphView.notifyStarted(startTime);
342                     }
343
344                 }
345                 updateTimerLabel();
346             }
347         }
348         catch (Exception e)
349         {
350             e.printStackTrace();
351         }
352     }
353
354     /**
355      *
356      *
357      */
358     private boolean stopTimer()
359     {
360         boolean ret = false;
361         try
362         {
363             ITimerCounter timerCounter = counter;
364             if (timerCounter != null)
365             {
366                 if (timerCounter.isStarted())
367                 {
368                     timerCounter.stop();
369                     controller.timerStarted(false);
370                     controller.vibrate(120);
371                     controller.getDataEntry().finishTimeData(timerCounter.getStartTime(), timerCounter.getStopTime());
372
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);
378                     ret = true;
379
380                     LapTimeGraphView graphView = findViewById(R.id.graph_area);
381                     if (graphView != null)
382                     {
383                         graphView.notifyStopped();
384                     }
385                 }
386                 updateTimerLabel();
387             }
388         }
389         catch (Exception e)
390         {
391             e.printStackTrace();
392         }
393         return (ret);
394     }
395
396     /**
397      *
398      */
399     @Override
400     public void clickedBtn2()
401     {
402         ITimerCounter timerCounter = counter;
403         if (timerCounter != null)
404         {
405             if (!timerCounter.isStarted())
406             {
407                 // 停止中は、記録一覧を呼び出す
408                 launchListActivity();
409
410                 // ぶるぶる
411                 controller.vibrate(35);
412             }
413         }
414         updateTimerLabel();
415     }
416
417     /**
418      *
419      */
420     @Override
421     public void clickedBtn3()
422     {
423         ITimerCounter timerCounter = counter;
424         if (timerCounter != null)
425         {
426             if (!timerCounter.isStarted())
427             {
428                 timerCounter.reset();
429                 controller.vibrate(50);
430                 controller.clearTimeStamp();
431                 currentLapCount = 0;
432                 LapTimeGraphView graphView = findViewById(R.id.graph_area);
433                 if (graphView != null)
434                 {
435                     graphView.notifyReset();
436                 }
437             }
438             updateTimerLabel();
439         }
440     }
441
442     @Override
443     public void clickedArea()
444     {
445         Log.v(TAG, "clickedArea()");
446
447     }
448
449     @Override
450     public boolean pushedBtn1()
451     {
452         return (false);
453     }
454
455     @Override
456     public boolean pushedBtn2()
457     {
458         return (stopTimer());
459     }
460
461     @Override
462     public boolean pushedBtn3()
463     {
464         return (false);
465     }
466
467     @Override
468     public boolean pushedArea()
469     {
470         isLaptimeView = !isLaptimeView;
471         controller.setDisplayMode(isLaptimeView);
472         Log.v(TAG, "pushedArea() : " + isLaptimeView);
473         changeGraphicView(isLaptimeView);
474         updateTimerLabel();
475         return (true);
476     }
477
478     /**
479      *
480      *
481      */
482     @Override
483     public void timeout()
484     {
485         try
486         {
487             runOnUiThread(new Runnable() {
488                 @Override
489                 public void run() {
490                     updateTimerLabel();
491                 }
492             });
493         }
494         catch (Exception e)
495         {
496             e.printStackTrace();
497         }
498     }
499
500     /**
501      *
502      *
503      */
504     private void updateMainSubCounter()
505     {
506         TextView main = findViewById(R.id.main_counter);
507         TextView sub = findViewById(R.id.sub_counter1);
508
509         ITimerCounter timerCounter = counter;
510         if (timerCounter != null)
511         {
512             long time1 = timerCounter.getPastTime();
513             CharSequence str1 = TimeStringConvert.getTimeString(time1);
514             CharSequence str2 = "";
515             if (timerCounter.isStarted())
516             {
517                 long time2 = timerCounter.getCurrentElapsedTime();
518                 int lapCount = timerCounter.getElapsedCount();
519                 if ((time2 >= 100) && (lapCount > 1))
520                 {
521                     str2 =  "[" + lapCount + "] " + TimeStringConvert.getTimeString(time2);
522                 }
523             }
524
525             if ((str2.length() > 0)&&(isCounterLapTime))
526             {
527                 // ラップタイムの方を大きく表示する
528                 main.setText(str2);
529                 sub.setText(str1);
530             }
531             else
532             {
533                 main.setText(str1);
534                 sub.setText(str2);
535             }
536             main.invalidate();
537             sub.invalidate();
538         }
539     }
540
541     /**
542      *
543      *
544      */
545     private void updateElapsedTimes()
546     {
547         if (isLaptimeView)
548         {
549            updateElapsedTimesGraph();
550         }
551         else
552         {
553             updateElapsedTimesText();
554         }
555     }
556
557     /**
558      *
559      *
560      */
561     private void updateElapsedTimesGraph()
562     {
563         Log.v(TAG, "updateElapsedTimesGraph()");
564         LapTimeGraphView view = findViewById(R.id.graph_area);
565         view.invalidate();
566     }
567
568     /**
569      *
570      *
571      */
572     private void updateElapsedTimesText()
573     {
574         // Log.v(TAG, "updateElapsedTimesText()");
575     }
576
577     /**
578      *  Launch ListActivity
579      *
580      */
581     private void launchListActivity()
582     {
583         Log.v(TAG, "launchListActivity()");
584         try
585         {
586             Intent intent = new Intent(this, ListActivity.class);
587             startActivity(intent);
588         }
589         catch (Exception e)
590         {
591             e.printStackTrace();
592         }
593     }
594
595     /**
596      *
597      *
598      */
599     @Override
600     protected void onUserLeaveHint()
601     {
602         Log.v(TAG, "onUserLeaveHint() " );
603         // ハードキー(ホームボタン)が押されたとき、これがひろえるが...
604     }
605
606     /**
607      *
608      *
609      */
610     @Override
611     public boolean dispatchKeyEvent(KeyEvent event)
612     {
613         Log.v(TAG, "dispatchKeyEvent() : " + event.getAction() + " (" + event.getKeyCode() + ")");
614
615         return (super.dispatchKeyEvent(event));
616     }
617
618     /**
619      *
620      *
621      */
622     @Override
623     public boolean onKeyDown(int keyCode, KeyEvent event)
624     {
625         Log.v(TAG, "onKeyDown() : " + event.getAction() + " (" + event.getKeyCode() + ")" + keyCode);
626         if (event.getRepeatCount() == 0)
627         {
628             if (keyCode == KeyEvent.KEYCODE_STEM_1)
629             {
630                 startTimer();
631                 return (true);
632             }
633             else if (keyCode == KeyEvent.KEYCODE_STEM_2)
634             {
635                 startTimer();
636                 return (true);
637             }
638             else if (keyCode == KeyEvent.KEYCODE_STEM_3)
639             {
640                 startTimer();
641                 return (true);
642             }
643         }
644         return (super.onKeyDown(keyCode, event));
645     }
646
647     /**
648      *
649      *
650      */
651     @Override
652     public void counterStatusChanged(final boolean forceStartTimer)
653     {
654         ITimerCounter timerCounter = counter;
655         if (forceStartTimer)
656         {
657             try
658             {
659                 LapTimeGraphView graphView = findViewById(R.id.graph_area);
660                 if (timerCounter != null)
661                 {
662                     MyTimerTrigger trigger = new MyTimerTrigger(this, 100, timerCounter);
663                     trigger.startTimer();
664                     stopTrigger = trigger;
665                 }
666                 if (graphView != null)
667                 {
668                     graphView.notifyLapTime();
669                 }
670             }
671             catch (Exception e)
672             {
673                 e.printStackTrace();
674             }
675         }
676
677         runOnUiThread(new Runnable()
678         {
679             @Override
680             public void run()
681             {
682
683                 // 自動スタート時の処理。。
684                 if (pendingStart)
685                 {
686                     startTimer();
687                     pendingStart = false;
688                 }
689
690                 // ラップタイム表示状態の更新
691                 reloadLapTimeList(forceStartTimer);
692
693                 // 表示ビューの切り替え
694                 changeGraphicView(isLaptimeView);
695
696                 // 表示のボタン状態を変更
697                 updateTimerLabel();
698             }
699         });
700     }
701
702     /**
703      *
704      *
705      */
706     private void reloadLapTimeList(final boolean forceStartTimer)
707     {
708         ITimerCounter timerCounter = counter;
709         if ((!forceStartTimer)||(timerCounter == null))
710         {
711             return;
712         }
713
714         // Adapter と TimerCounterの整合性を確認
715         try
716         {
717             List<Long> lapTimeList = timerCounter.getLapTimeList();
718             int lapCount = lapTimeList.size();
719             int listCount = controller.getLapTimeCount();
720             if (lapCount != listCount)
721             {
722                 Log.v(TAG, "LAP COUNT IS MISMATCH!!! lap:" + lapCount + " vs list:" + listCount);
723                 int index = 0;
724                 controller.clearTimeStamp();
725                 long prevTime = lapTimeList.get(0);
726                 for (long lapTime : lapTimeList)
727                 {
728                     index++;
729                     if (prevTime != lapTime)
730                     {
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);
735                     }
736                     prevTime = lapTime;
737                 }
738                 currentLapCount = lapCount - 1;
739             }
740         }
741         catch (Exception e)
742         {
743             e.printStackTrace();
744         }
745     }
746
747     /**
748      *
749      *
750      */
751     private void changeGraphicView(boolean isGraphics)
752     {
753         try
754         {
755             LapTimeGraphView graphView = findViewById(R.id.graph_area);
756             ListView listView = findViewById(R.id.laptime_list_area);
757             if (isGraphics)
758             {
759                 graphView.setITimerCounter(counter);
760                 graphView.setVisibility(View.VISIBLE);
761                 listView.setVisibility(View.GONE);
762             }
763             else
764             {
765                 graphView.setVisibility(View.GONE);
766                 listView.setVisibility(View.VISIBLE);
767             }
768             //controller.vibrate(30);
769         }
770         catch (Exception e)
771         {
772             e.printStackTrace();
773         }
774     }
775 }