OSDN Git Service

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