OSDN Git Service

538fd19a36ced3011318b99fd7cdff79f47d6f4e
[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.RelativeLayout;
13 import android.widget.TextView;
14
15 import net.osdn.gokigen.joggingtimer.R;
16 import net.osdn.gokigen.joggingtimer.recordlist.ListActivity;
17 import net.osdn.gokigen.joggingtimer.utilities.TimeStringConvert;
18
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;
24
25 /**
26  *
27  *
28  */
29 public class MainActivity extends WearableActivity implements IClickCallback, MyTimerTrigger.ITimeoutReceiver
30 {
31     private final String TAG = toString();
32     private final IWearableActivityControl controller = new WearableActivityController();
33     private MyTimerCounter counter = new MyTimerCounter();
34     private boolean isCounterLapTime = false;
35
36     /**
37      *
38      */
39     @Override
40     protected void onCreate(Bundle savedInstanceState)
41     {
42         super.onCreate(savedInstanceState);
43         Log.v(TAG, "onCreate()");
44
45         setContentView(R.layout.activity_main);
46
47         controller.setup(this, this);
48
49         // Enables Always-on
50         setAmbientEnabled();
51     }
52
53     /**
54      *
55      */
56     @Override
57     protected void onSaveInstanceState(Bundle outState)
58     {
59         super.onSaveInstanceState(outState);
60
61         /* ここで状態を保存 */
62         outState.putParcelable("timerCounter", counter);
63     }
64
65     /**
66      *
67      */
68     @Override
69     protected void onRestoreInstanceState(Bundle savedInstanceState)
70     {
71         super.onRestoreInstanceState(savedInstanceState);
72
73         /* ここで保存した状態を読み出して設定 */
74         counter = savedInstanceState.getParcelable("timerCounter");
75     }
76
77     /**
78      *
79      */
80     @Override
81     protected void onResume()
82     {
83         super.onResume();
84
85         // インテントを取得する
86         Intent intent = getIntent();
87         String action = intent.getAction();
88         Log.v(TAG, "onResume() : " + action);
89
90         boolean isStartTimer = false;
91         if (action != null)
92         {
93             if (action.equals("com.google.android.wearable.action.STOPWATCH"))
94             {
95                 isStartTimer = true;
96             }
97             else if (action.equals("vnd.google.fitness.TRACK"))
98             {
99                 String activity = intent.getStringExtra("actionStatus");
100                 if ((activity != null)&&(activity.equals("ActiveActionStatus")))
101                 {
102                     isStartTimer = true;
103                 }
104             }
105         }
106         if (isStartTimer)
107         {
108             // start a timer!
109             startTimer();
110         }
111     }
112
113     /**
114      *
115      */
116     @Override
117     protected void onPause()
118     {
119         super.onPause();
120         Log.v(TAG, "onPause()");
121     }
122
123     /**
124      *
125      *
126      */
127     @Override
128     public void onStart()
129     {
130         super.onStart();
131         Log.v(TAG, "onStart()");
132         controller.setupDatabase(this, false);
133     }
134
135     /**
136      *
137      *
138      */
139     @Override
140     public void onStop()
141     {
142         super.onStop();
143         Log.v(TAG, "onStop()");
144         controller.exitApplication(this);
145     }
146
147     /**
148      *
149      *
150      */
151     @Override
152     public void onEnterAmbient(Bundle ambientDetails)
153     {
154         super.onEnterAmbient(ambientDetails);
155         Log.v(TAG, "onEnterAmbient()");
156     }
157
158     /**
159      *
160      *
161      */
162     @Override
163     public void onExitAmbient()
164     {
165         super.onExitAmbient();
166         Log.v(TAG, "onExitAmbient()");
167         //updateTimerLabel();
168     }
169
170     /**
171      *
172      *
173      */
174     @Override
175     public void onUpdateAmbient()
176     {
177         super.onUpdateAmbient();
178         Log.v(TAG, "onUpdateAmbient()");
179     }
180
181     /**
182      *
183      */
184     public void updateTimerLabel()
185     {
186         ITimerCounter timerCounter = counter;
187         if (timerCounter != null)
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                 updateElapsedTimes();
224             }
225             else if (timerCounter.isReset())
226             {
227                 bgColor = Color.BLACK;
228                 insetLayout.setBackgroundColor(bgColor);
229                 insetLayout.invalidate();
230
231                 layout.setBackgroundColor(bgColor);
232                 layout.invalidate();
233
234                 btn1.setImageResource(R.drawable.ic_play_arrow_black_24dp);
235                 btn1.setBackgroundColor(bgColor);
236                 btn1.setVisibility(View.VISIBLE);
237                 btn1.invalidate();
238
239                 btn2.setImageResource(R.drawable.ic_format_list_bulleted_black_24dp);
240                 btn2.setBackgroundColor(bgColor);
241                 btn2.setVisibility(View.VISIBLE);
242                 btn2.invalidate();
243
244                 btn3.setImageResource(R.drawable.ic_refresh_black_24dp);
245                 btn3.setBackgroundColor(bgColor);
246                 btn3.setVisibility(View.INVISIBLE);
247                 btn3.invalidate();
248
249                 updateElapsedTimes();
250             }
251             else
252             {
253                 bgColor = Color.BLACK;
254                 insetLayout.setBackgroundColor(bgColor);
255                 insetLayout.invalidate();
256
257                 layout.setBackgroundColor(bgColor);
258                 layout.invalidate();
259
260                 btn1.setImageResource(R.drawable.ic_play_arrow_black_24dp);
261                 btn1.setVisibility(View.VISIBLE);
262                 btn1.setBackgroundColor(bgColor);
263                 btn1.invalidate();
264
265                 btn2.setImageResource(R.drawable.ic_format_list_bulleted_black_24dp);
266                 btn2.setVisibility(View.VISIBLE);
267                 btn2.setBackgroundColor(bgColor);
268                 btn2.invalidate();
269
270                 btn3.setImageResource(R.drawable.ic_refresh_black_24dp);
271                 btn3.setVisibility(View.VISIBLE);
272                 btn3.setBackgroundColor(bgColor);
273                 btn3.invalidate();
274
275                 updateElapsedTimes();
276             }
277         }
278     }
279
280     @Override
281     public void clickedCounter()
282     {
283         // 表示順番を変える
284         isCounterLapTime = !isCounterLapTime;
285     }
286
287     /**
288      *
289      */
290     @Override
291     public void clickedBtn1()
292     {
293         startTimer();
294     }
295
296     /**
297      *
298      *
299      */
300     private void startTimer()
301     {
302         try
303         {
304             ITimerCounter timerCounter = counter;
305             if (timerCounter != null)
306             {
307                 if (timerCounter.isStarted())
308                 {
309                     Log.v(TAG, "startTimer() LAP TIME");
310                     // チャタリング防止(ラップタイムとして、3秒以内は記録しないようにする)
311                     if (timerCounter.getCurrentElapsedTime() > 3000)
312                     {
313                         long lapTime = timerCounter.timeStamp();
314                         controller.vibrate(50);
315                         controller.getDataEntry().appendTimeData(lapTime);
316                     }
317                 }
318                 else
319                 {
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);
326
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());
331                 }
332                 updateTimerLabel();
333             }
334         }
335         catch (Exception e)
336         {
337             e.printStackTrace();
338         }
339     }
340
341     /**
342      *
343      *
344      */
345     private boolean stopTimer()
346     {
347         boolean ret = false;
348         try
349         {
350             ITimerCounter timerCounter = counter;
351             if (timerCounter != null)
352             {
353                 if (timerCounter.isStarted())
354                 {
355                     timerCounter.stop();
356                     controller.timerStarted(false);
357                     controller.vibrate(120);
358                     controller.getDataEntry().finishTimeData(timerCounter.getStartTime(), timerCounter.getStopTime());
359                     ret = true;
360                 }
361                 updateTimerLabel();
362             }
363         }
364         catch (Exception e)
365         {
366             e.printStackTrace();
367         }
368         return (ret);
369     }
370
371     /**
372      *
373      */
374     @Override
375     public void clickedBtn2()
376     {
377         ITimerCounter timerCounter = counter;
378         if (timerCounter != null)
379         {
380             if (!timerCounter.isStarted())
381             {
382                 // 停止中は、記録一覧を呼び出す
383                 launchListActivity();
384
385                 // ぶるぶる
386                 controller.vibrate(35);
387             }
388         }
389         updateTimerLabel();
390     }
391
392     /**
393      *
394      */
395     @Override
396     public void clickedBtn3()
397     {
398         ITimerCounter timerCounter = counter;
399         if (timerCounter != null)
400         {
401             if (!timerCounter.isStarted())
402             {
403                 timerCounter.reset();
404                 controller.vibrate(50);
405             }
406             updateTimerLabel();
407         }
408     }
409
410     @Override
411     public boolean pushedBtn1()
412     {
413         return (false);
414     }
415
416     @Override
417     public boolean pushedBtn2()
418     {
419         return (stopTimer());
420     }
421
422     @Override
423     public boolean pushedBtn3()
424     {
425         return (false);
426     }
427
428     @Override
429     public void dataIsReloaded(ArrayList<Long> list)
430     {
431         ITimerCounter timerCounter = counter;
432         if ((timerCounter != null)&&(list != null))
433         {
434             try
435             {
436                 timerCounter.reloadTimerCounter(list.get(0), list);
437
438                 MyTimerTrigger trigger = new MyTimerTrigger(this, 100, timerCounter);
439                 trigger.startTimer();
440             }
441             catch (Exception e)
442             {
443                 e.printStackTrace();
444             }
445         }
446
447         runOnUiThread(new Runnable()
448         {
449             @Override
450             public void run()
451             {
452                 updateTimerLabel();
453             }
454         });
455     }
456
457     /**
458      *
459      *
460      */
461     @Override
462     public void timeout()
463     {
464         try
465         {
466             runOnUiThread(new Runnable() {
467                 @Override
468                 public void run() {
469                     updateTimerLabel();
470                 }
471             });
472         }
473         catch (Exception e)
474         {
475             e.printStackTrace();
476         }
477     }
478
479     /**
480      *
481      *
482      */
483     private void updateMainSubCounter()
484     {
485         TextView main = findViewById(R.id.main_counter);
486         TextView sub = findViewById(R.id.sub_counter1);
487
488         ITimerCounter timerCounter = counter;
489         if (timerCounter != null)
490         {
491             long time1 = timerCounter.getPastTime();
492             CharSequence str1 = TimeStringConvert.getTimeString(time1);
493
494             CharSequence str2 = "";
495             if (timerCounter.isStarted())
496             {
497                 long time2 = timerCounter.getCurrentElapsedTime();
498                 int lapCount = timerCounter.getElapsedCount();
499                 if ((time2 >= 100) && (lapCount > 1))
500                 {
501                     str2 =  "[" + lapCount + "] " + TimeStringConvert.getTimeString(time2);
502                 }
503             }
504
505             if ((str2.length() > 0)&&(isCounterLapTime))
506             {
507                 // ラップタイムの方を大きく表示する
508                 main.setText(str2);
509                 sub.setText(str1);
510             }
511             else
512             {
513                 main.setText(str1);
514                 sub.setText(str2);
515             }
516             main.invalidate();
517             sub.invalidate();
518         }
519     }
520
521     /**
522      *
523      *
524      */
525     private void updateElapsedTimes()
526     {
527         String dummy = "";
528         TextView area1 = findViewById(R.id.sub_counter2);
529         TextView area2 = findViewById(R.id.sub_counter3);
530         TextView area3 = findViewById(R.id.sub_counter4);
531
532         ITimerCounter timerCounter = counter;
533         if (timerCounter != null)
534         {
535             List<Long> elapsedTimes = timerCounter.getTimerList();
536             int indexSize = elapsedTimes.size();
537             if (indexSize <= 1)
538             {
539                 // ラップの記録がないので表示しません
540                 area1.setText(dummy);
541                 area1.invalidate();
542                 area2.setText(dummy);
543                 area2.invalidate();
544                 area3.setText(dummy);
545                 area3.invalidate();
546                 return;
547             }
548             if (indexSize <= 2)
549             {
550                 // ラップが1つとれた場合
551                 long time = (elapsedTimes.get(indexSize - 1) - elapsedTimes.get(indexSize - 2));
552                 String elapsedTime = "[" + (timerCounter.getElapsedCount() - 1) + "] " + TimeStringConvert.getTimeString(time);
553                 area1.setText(elapsedTime);
554                 area1.invalidate();
555                 area2.setText(dummy);
556                 area2.invalidate();
557                 area3.setText(dummy);
558                 area3.invalidate();
559                 return;
560             }
561             if (indexSize <= 3)
562             {
563                 // ラップが3つとれた場合
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);
569                 area1.invalidate();
570                 area2.setText(elapsedTime2);
571                 area2.invalidate();
572                 area3.setText(dummy);
573                 area3.invalidate();
574                 return;
575             }
576
577             // ラップが4つ以上ある場合
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);
585             area1.invalidate();
586             area2.setText(elapsedTime2);
587             area2.invalidate();
588             area3.setText(elapsedTime3);
589             area3.invalidate();
590         }
591     }
592
593     /**
594      *  Launch ListActivity
595      *
596      */
597     private void launchListActivity()
598     {
599         Log.v(TAG, "launchListActivity()");
600         try
601         {
602             Intent intent = new Intent(this, ListActivity.class);
603             startActivity(intent);
604         }
605         catch (Exception e)
606         {
607             e.printStackTrace();
608         }
609     }
610
611     /*
612      *
613      *
614      */
615     @Override
616     protected void onUserLeaveHint ()
617     {
618         Log.v(TAG, "onUserLeaveHint() " );
619         // ハードキー(ホームボタン)が押されたとき、これがひろえるが...
620     }
621
622     /*
623      *
624      *
625      */
626     @Override
627     public boolean dispatchKeyEvent(KeyEvent event)
628     {
629         Log.v(TAG, "dispatchKeyEvent() : " + event.getAction() + " (" + event.getKeyCode() + ")");
630
631         return (super.dispatchKeyEvent(event));
632     }
633
634     /*
635      *
636      *
637      */
638     @Override
639     public boolean onKeyDown(int keyCode, KeyEvent event)
640     {
641         Log.v(TAG, "onKeyDown() : " + event.getAction() + " (" + event.getKeyCode() + ")" + keyCode);
642         if (event.getRepeatCount() == 0)
643         {
644             if (keyCode == KeyEvent.KEYCODE_STEM_1)
645             {
646                 startTimer();
647                 return (true);
648             }
649             else if (keyCode == KeyEvent.KEYCODE_STEM_2)
650             {
651                 startTimer();
652                 return (true);
653             }
654             else if (keyCode == KeyEvent.KEYCODE_STEM_3)
655             {
656                 startTimer();
657                 return (true);
658             }
659         }
660         return (super.onKeyDown(keyCode, event));
661     }
662
663     /*
664      *
665      *
666      */
667 /*
668     private String getElapsedTime()
669     {
670         String elapsedTime = "";
671         ITimerCounter timerCounter = counter;
672         if (timerCounter != null)
673         {
674             int count = timerCounter.getElapsedCount();
675             if (count > 0)
676             {
677                 elapsedTime = "[" + timerCounter.getElapsedCount() + "] " + MyTimerCounter.getTimeString(timerCounter.getCurrentElapsedTime());
678             }
679         }
680         return (elapsedTime);
681     }
682 */
683 }