OSDN Git Service

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