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.View;
10 import android.widget.ImageButton;
11 import android.widget.RelativeLayout;
12 import android.widget.TextView;
13
14 import net.osdn.gokigen.joggingtimer.R;
15 import net.osdn.gokigen.joggingtimer.recordlist.ListActivity;
16 import net.osdn.gokigen.joggingtimer.utilities.TimeStringConvert;
17
18
19 import java.text.SimpleDateFormat;
20 import java.util.Date;
21 import java.util.List;
22 import java.util.Locale;
23
24 /**
25  *
26  *
27  */
28 public class MainActivity extends WearableActivity implements IClickCallback, MyTimerTrigger.ITimeoutReceiver
29 {
30     private final String TAG = toString();
31     private final IWearableActivityControl controller = new WearableActivityController();
32     private MyTimerCounter counter = new MyTimerCounter();
33     private boolean isCounterLapTime = false;
34
35     /**
36      *
37      */
38     @Override
39     protected void onCreate(Bundle savedInstanceState)
40     {
41         super.onCreate(savedInstanceState);
42         Log.v(TAG, "onCreate()");
43
44         setContentView(R.layout.activity_main);
45
46         controller.setup(this, this);
47
48         // Enables Always-on
49         setAmbientEnabled();
50     }
51
52     /**
53      *
54      */
55     @Override
56     protected void onSaveInstanceState(Bundle outState)
57     {
58         super.onSaveInstanceState(outState);
59
60         /* ここで状態を保存 */
61         outState.putParcelable("timerCounter", counter);
62     }
63
64     /**
65      *
66      */
67     @Override
68     protected void onRestoreInstanceState(Bundle savedInstanceState)
69     {
70         super.onRestoreInstanceState(savedInstanceState);
71
72         /* ここで保存した状態を読み出して設定 */
73         counter = savedInstanceState.getParcelable("timerCounter");
74     }
75
76     /**
77      *
78      */
79     @Override
80     protected void onResume()
81     {
82         super.onResume();
83
84         // インテントを取得する
85         Intent intent = getIntent();
86         String action = intent.getAction();
87         Log.v(TAG, "onResume() : " + action);
88
89         boolean isStartTimer = false;
90         if (action != null)
91         {
92             if (action.equals("com.google.android.wearable.action.STOPWATCH"))
93             {
94                 isStartTimer = true;
95             }
96             else if (action.equals("vnd.google.fitness.TRACK"))
97             {
98                 String activity = intent.getStringExtra("actionStatus");
99                 if ((activity != null)&&(activity.equals("ActiveActionStatus")))
100                 {
101                     isStartTimer = true;
102                 }
103             }
104         }
105         if (isStartTimer)
106         {
107             // start a timer!
108             startTimer();
109         }
110     }
111
112     /**
113      *
114      */
115     @Override
116     protected void onPause()
117     {
118         super.onPause();
119         Log.v(TAG, "onPause()");
120     }
121
122     /**
123      *
124      *
125      */
126     @Override
127     public void onStart()
128     {
129         super.onStart();
130         Log.v(TAG, "onStart()");
131     }
132
133     /**
134      *
135      *
136      */
137     @Override
138     public void onStop()
139     {
140         super.onStop();
141         Log.v(TAG, "onStop()");
142         controller.exitApplication(this);
143     }
144
145     /**
146      *
147      *
148      */
149     @Override
150     public void onEnterAmbient(Bundle ambientDetails)
151     {
152         super.onEnterAmbient(ambientDetails);
153         Log.v(TAG, "onEnterAmbient()");
154     }
155
156     /**
157      *
158      *
159      */
160     @Override
161     public void onExitAmbient()
162     {
163         super.onExitAmbient();
164         Log.v(TAG, "onExitAmbient()");
165         updateTimerLabel();
166     }
167
168     /**
169      *
170      *
171      */
172     @Override
173     public void onUpdateAmbient()
174     {
175         super.onUpdateAmbient();
176         Log.v(TAG, "onUpdateAmbient()");
177     }
178
179     /**
180      *
181      */
182     public void updateTimerLabel()
183     {
184         ITimerCounter timerCounter = counter;
185         if (timerCounter != null)
186         {
187             int bgColor;
188             BoxInsetLayout insetLayout = findViewById(R.id.box_inset_layout);
189             RelativeLayout layout = findViewById(R.id.relative_main_layout);
190
191             ImageButton btn1 = findViewById(R.id.btn1);
192             ImageButton btn2 = findViewById(R.id.btn2);
193             ImageButton btn3 = findViewById(R.id.btn3);
194
195             updateMainSubCounter();
196
197             if (timerCounter.isStarted())
198             {
199                 bgColor = Color.CYAN;
200                 insetLayout.setBackgroundColor(bgColor);
201                 insetLayout.invalidate();
202
203                 layout.setBackgroundColor(bgColor);
204                 layout.invalidate();
205
206                 btn1.setImageResource(R.drawable.ic_flag_black_24dp);
207                 btn1.setBackgroundColor(bgColor);
208                 btn1.setVisibility(View.VISIBLE);
209                 btn1.invalidate();
210
211                 btn2.setImageResource(R.drawable.ic_stop_black_24dp);
212                 btn2.setBackgroundColor(bgColor);
213                 btn2.setVisibility(View.VISIBLE);
214                 btn2.invalidate();
215
216                 btn3.setImageResource(R.drawable.ic_block_black_24dp);
217                 btn3.setBackgroundColor(bgColor);
218                 btn3.setVisibility(View.INVISIBLE);
219                 btn3.invalidate();
220
221                 updateElapsedTimes();
222             }
223             else if (timerCounter.isReset())
224             {
225                 bgColor = Color.WHITE;
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                 updateElapsedTimes();
248             }
249             else
250             {
251                 bgColor = Color.rgb(250, 80, 80);
252                 insetLayout.setBackgroundColor(bgColor);
253                 insetLayout.invalidate();
254
255                 layout.setBackgroundColor(bgColor);
256                 layout.invalidate();
257
258                 btn1.setImageResource(R.drawable.ic_play_arrow_black_24dp);
259                 btn1.setVisibility(View.VISIBLE);
260                 btn1.setBackgroundColor(bgColor);
261                 btn1.invalidate();
262
263                 btn2.setImageResource(R.drawable.ic_format_list_bulleted_black_24dp);
264                 btn2.setVisibility(View.VISIBLE);
265                 btn2.setBackgroundColor(bgColor);
266                 btn2.invalidate();
267
268                 btn3.setImageResource(R.drawable.ic_refresh_black_24dp);
269                 btn3.setVisibility(View.VISIBLE);
270                 btn3.setBackgroundColor(bgColor);
271                 btn3.invalidate();
272
273                 updateElapsedTimes();
274             }
275         }
276     }
277
278     @Override
279     public void clickedCounter()
280     {
281         // 表示順番を変える
282         isCounterLapTime = !isCounterLapTime;
283     }
284
285     /**
286      *
287      */
288     @Override
289     public void clickedBtn1()
290     {
291         startTimer();
292     }
293
294     /**
295      *
296      *
297      */
298     private void startTimer()
299     {
300         try
301         {
302             ITimerCounter timerCounter = counter;
303             if (timerCounter != null)
304             {
305                 if (timerCounter.isStarted())
306                 {
307                     Log.v(TAG, "startTimer() LAP TIME");
308                     // チャタリング防止(ラップタイムとして、3秒以内は記録しないようにする)
309                     if (timerCounter.getCurrentElapsedTime() > 3000)
310                     {
311                         long lapTime = timerCounter.timeStamp();
312                         controller.vibrate(50);
313                         controller.getDataEntry().appendTimeData(lapTime);
314                     }
315                 }
316                 else
317                 {
318                     Log.v(TAG, "startTimer() START");
319                     timerCounter.start();
320                     MyTimerTrigger trigger = new MyTimerTrigger(this, 100, timerCounter);
321                     trigger.startTimer();
322                     controller.vibrate(120);
323
324                     Date date = new Date();
325                     SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
326                     String title = sdf1.format(date);
327                     controller.getDataEntry().createIndex(title, "", 0, timerCounter.getStartTime());
328                 }
329                 updateTimerLabel();
330             }
331         }
332         catch (Exception e)
333         {
334             e.printStackTrace();
335         }
336     }
337
338     /**
339      *
340      *
341      */
342     private boolean stopTimer()
343     {
344         boolean ret = false;
345         try
346         {
347             ITimerCounter timerCounter = counter;
348             if (timerCounter != null)
349             {
350                 if (timerCounter.isStarted())
351                 {
352                     timerCounter.stop();
353                     controller.vibrate(120);
354                     controller.getDataEntry().finishTimeData(timerCounter.getStartTime(), timerCounter.getStopTime());
355                     ret = true;
356                 }
357                 updateTimerLabel();
358             }
359         }
360         catch (Exception e)
361         {
362             e.printStackTrace();
363         }
364         return (ret);
365     }
366
367     /**
368      *
369      */
370     @Override
371     public void clickedBtn2()
372     {
373         ITimerCounter timerCounter = counter;
374         if (timerCounter != null)
375         {
376             if (!timerCounter.isStarted())
377             {
378                 // 停止中は、記録一覧を呼び出す
379                 launchListActivity();
380             }
381         }
382         updateTimerLabel();
383     }
384
385     /**
386      *
387      */
388     @Override
389     public void clickedBtn3()
390     {
391         ITimerCounter timerCounter = counter;
392         if (timerCounter != null)
393         {
394             if (!timerCounter.isStarted())
395             {
396                 timerCounter.reset();
397                 controller.vibrate(50);
398             }
399             updateTimerLabel();
400         }
401     }
402
403     @Override
404     public boolean pushedBtn1()
405     {
406         return (false);
407     }
408
409     @Override
410     public boolean pushedBtn2()
411     {
412         return (stopTimer());
413     }
414
415     @Override
416     public boolean pushedBtn3()
417     {
418         return (false);
419     }
420
421     /**
422      *
423      *
424      */
425     @Override
426     public void timeout()
427     {
428         try
429         {
430             runOnUiThread(new Runnable() {
431                 @Override
432                 public void run() {
433                     updateTimerLabel();
434                 }
435             });
436         }
437         catch (Exception e)
438         {
439             e.printStackTrace();
440         }
441     }
442
443     private void updateMainSubCounter()
444     {
445         TextView main = findViewById(R.id.main_counter);
446         TextView sub = findViewById(R.id.sub_counter1);
447
448         ITimerCounter timerCounter = counter;
449         if (timerCounter != null)
450         {
451             long time1 = timerCounter.getPastTime();
452             CharSequence str1 = TimeStringConvert.getTimeString(time1);
453
454             CharSequence str2 = "";
455             if (timerCounter.isStarted())
456             {
457                 long time2 = timerCounter.getCurrentElapsedTime();
458                 int lapCount = timerCounter.getElapsedCount();
459                 if ((time2 >= 100) && (lapCount > 1))
460                 {
461                     str2 =  "[" + lapCount + "] " + TimeStringConvert.getTimeString(time2);
462                 }
463             }
464
465             if ((str2.length() > 0)&&(isCounterLapTime))
466             {
467                 // ラップタイムの方を大きく表示する
468                 main.setText(str2);
469                 sub.setText(str1);
470             }
471             else
472             {
473                 main.setText(str1);
474                 sub.setText(str2);
475             }
476             main.invalidate();
477             sub.invalidate();
478         }
479     }
480
481
482     private void updateElapsedTimes()
483     {
484         String dummy = "";
485         TextView area1 = findViewById(R.id.sub_counter2);
486         TextView area2 = findViewById(R.id.sub_counter3);
487         TextView area3 = findViewById(R.id.sub_counter4);
488
489         ITimerCounter timerCounter = counter;
490         if (timerCounter != null)
491         {
492             List<Long> elapsedTimes = timerCounter.getTimerList();
493             int indexSize = elapsedTimes.size();
494             if (indexSize <= 1)
495             {
496                 // ラップの記録がないので表示しません
497                 area1.setText(dummy);
498                 area1.invalidate();
499                 area2.setText(dummy);
500                 area2.invalidate();
501                 area3.setText(dummy);
502                 area3.invalidate();
503                 return;
504             }
505             if (indexSize <= 2)
506             {
507                 // ラップが1つとれた場合
508                 long time = (elapsedTimes.get(indexSize - 1) - elapsedTimes.get(indexSize - 2));
509                 String elapsedTime = "[" + (timerCounter.getElapsedCount() - 1) + "] " + TimeStringConvert.getTimeString(time);
510                 area1.setText(elapsedTime);
511                 area1.invalidate();
512                 area2.setText(dummy);
513                 area2.invalidate();
514                 area3.setText(dummy);
515                 area3.invalidate();
516                 return;
517             }
518             if (indexSize <= 3)
519             {
520                 // ラップが3つとれた場合
521                 long time1 = (elapsedTimes.get(indexSize - 2) - elapsedTimes.get(indexSize - 3));
522                 long time2 = (elapsedTimes.get(indexSize - 1) - elapsedTimes.get(indexSize - 2));
523                 String elapsedTime1 = "[" +  (timerCounter.getElapsedCount() - 2) + "] " + TimeStringConvert.getTimeString(time1);
524                 String elapsedTime2 = "[" +  (timerCounter.getElapsedCount() - 1) + "] " + TimeStringConvert.getTimeString(time2);
525                 area1.setText(elapsedTime1);
526                 area1.invalidate();
527                 area2.setText(elapsedTime2);
528                 area2.invalidate();
529                 area3.setText(dummy);
530                 area3.invalidate();
531                 return;
532             }
533
534             // ラップが4つ以上ある場合
535             long time1 = (elapsedTimes.get(indexSize - 3) - elapsedTimes.get(indexSize - 4));
536             long time2 = (elapsedTimes.get(indexSize - 2) - elapsedTimes.get(indexSize - 3));
537             long time3 = (elapsedTimes.get(indexSize - 1) - elapsedTimes.get(indexSize - 2));
538             String elapsedTime1 = "[" +  (timerCounter.getElapsedCount() - 3) + "] " + TimeStringConvert.getTimeString(time1);
539             String elapsedTime2 = "[" +  (timerCounter.getElapsedCount() - 2) + "] " + TimeStringConvert.getTimeString(time2);
540             String elapsedTime3 = "[" +  (timerCounter.getElapsedCount() - 1) + "] " + TimeStringConvert.getTimeString(time3);
541             area1.setText(elapsedTime1);
542             area1.invalidate();
543             area2.setText(elapsedTime2);
544             area2.invalidate();
545             area3.setText(elapsedTime3);
546             area3.invalidate();
547         }
548     }
549
550     /**
551      *  Launch ListActivity
552      *
553      */
554     private void launchListActivity()
555     {
556         Log.v(TAG, "launchListActivity()");
557         try
558         {
559             Intent intent = new Intent(this, ListActivity.class);
560             startActivity(intent);
561         }
562         catch (Exception e)
563         {
564             e.printStackTrace();
565         }
566     }
567
568     /*
569      *
570      *
571      */
572 /*
573     private String getElapsedTime()
574     {
575         String elapsedTime = "";
576         ITimerCounter timerCounter = counter;
577         if (timerCounter != null)
578         {
579             int count = timerCounter.getElapsedCount();
580             if (count > 0)
581             {
582                 elapsedTime = "[" + timerCounter.getElapsedCount() + "] " + MyTimerCounter.getTimeString(timerCounter.getCurrentElapsedTime());
583             }
584         }
585         return (elapsedTime);
586     }
587 */
588 }