1 package net.osdn.gokigen.joggingtimer.stopwatch;
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;
14 import net.osdn.gokigen.joggingtimer.R;
15 import net.osdn.gokigen.joggingtimer.recordlist.ListActivity;
16 import net.osdn.gokigen.joggingtimer.utilities.TimeStringConvert;
19 import java.text.SimpleDateFormat;
20 import java.util.Date;
21 import java.util.List;
22 import java.util.Locale;
28 public class MainActivity extends WearableActivity implements IClickCallback, MyTimerTrigger.ITimeoutReceiver
30 private final String TAG = toString();
31 private final IWearableActivityControl controller = new WearableActivityController();
32 private MyTimerCounter counter = new MyTimerCounter();
33 private boolean isCounterLapTime = false;
39 protected void onCreate(Bundle savedInstanceState)
41 super.onCreate(savedInstanceState);
42 Log.v(TAG, "onCreate()");
44 setContentView(R.layout.activity_main);
46 controller.setup(this, this);
56 protected void onSaveInstanceState(Bundle outState)
58 super.onSaveInstanceState(outState);
61 outState.putParcelable("timerCounter", counter);
68 protected void onRestoreInstanceState(Bundle savedInstanceState)
70 super.onRestoreInstanceState(savedInstanceState);
72 /* ここで保存した状態を読み出して設定 */
73 counter = savedInstanceState.getParcelable("timerCounter");
80 protected void onResume()
85 Intent intent = getIntent();
86 String action = intent.getAction();
87 Log.v(TAG, "onResume() : " + action);
89 boolean isStartTimer = false;
92 if (action.equals("com.google.android.wearable.action.STOPWATCH"))
96 else if (action.equals("vnd.google.fitness.TRACK"))
98 String activity = intent.getStringExtra("actionStatus");
99 if ((activity != null)&&(activity.equals("ActiveActionStatus")))
116 protected void onPause()
119 Log.v(TAG, "onPause()");
127 public void onStart()
130 Log.v(TAG, "onStart()");
141 Log.v(TAG, "onStop()");
142 controller.exitApplication(this);
150 public void onEnterAmbient(Bundle ambientDetails)
152 super.onEnterAmbient(ambientDetails);
153 Log.v(TAG, "onEnterAmbient()");
161 public void onExitAmbient()
163 super.onExitAmbient();
164 Log.v(TAG, "onExitAmbient()");
173 public void onUpdateAmbient()
175 super.onUpdateAmbient();
176 Log.v(TAG, "onUpdateAmbient()");
182 public void updateTimerLabel()
184 ITimerCounter timerCounter = counter;
185 if (timerCounter != null)
188 BoxInsetLayout insetLayout = findViewById(R.id.box_inset_layout);
189 RelativeLayout layout = findViewById(R.id.relative_main_layout);
191 ImageButton btn1 = findViewById(R.id.btn1);
192 ImageButton btn2 = findViewById(R.id.btn2);
193 ImageButton btn3 = findViewById(R.id.btn3);
195 updateMainSubCounter();
197 if (timerCounter.isStarted())
199 bgColor = Color.CYAN;
200 insetLayout.setBackgroundColor(bgColor);
201 insetLayout.invalidate();
203 layout.setBackgroundColor(bgColor);
206 btn1.setImageResource(R.drawable.ic_flag_black_24dp);
207 btn1.setBackgroundColor(bgColor);
208 btn1.setVisibility(View.VISIBLE);
211 btn2.setImageResource(R.drawable.ic_stop_black_24dp);
212 btn2.setBackgroundColor(bgColor);
213 btn2.setVisibility(View.VISIBLE);
216 btn3.setImageResource(R.drawable.ic_block_black_24dp);
217 btn3.setBackgroundColor(bgColor);
218 btn3.setVisibility(View.INVISIBLE);
221 updateElapsedTimes();
223 else if (timerCounter.isReset())
225 bgColor = Color.WHITE;
226 insetLayout.setBackgroundColor(bgColor);
227 insetLayout.invalidate();
229 layout.setBackgroundColor(bgColor);
232 btn1.setImageResource(R.drawable.ic_play_arrow_black_24dp);
233 btn1.setBackgroundColor(bgColor);
234 btn1.setVisibility(View.VISIBLE);
237 btn2.setImageResource(R.drawable.ic_format_list_bulleted_black_24dp);
238 btn2.setBackgroundColor(bgColor);
239 btn2.setVisibility(View.VISIBLE);
242 btn3.setImageResource(R.drawable.ic_refresh_black_24dp);
243 btn3.setBackgroundColor(bgColor);
244 btn3.setVisibility(View.INVISIBLE);
247 updateElapsedTimes();
251 bgColor = Color.rgb(250, 80, 80);
252 insetLayout.setBackgroundColor(bgColor);
253 insetLayout.invalidate();
255 layout.setBackgroundColor(bgColor);
258 btn1.setImageResource(R.drawable.ic_play_arrow_black_24dp);
259 btn1.setVisibility(View.VISIBLE);
260 btn1.setBackgroundColor(bgColor);
263 btn2.setImageResource(R.drawable.ic_format_list_bulleted_black_24dp);
264 btn2.setVisibility(View.VISIBLE);
265 btn2.setBackgroundColor(bgColor);
268 btn3.setImageResource(R.drawable.ic_refresh_black_24dp);
269 btn3.setVisibility(View.VISIBLE);
270 btn3.setBackgroundColor(bgColor);
273 updateElapsedTimes();
279 public void clickedCounter()
282 isCounterLapTime = !isCounterLapTime;
289 public void clickedBtn1()
298 private void startTimer()
302 ITimerCounter timerCounter = counter;
303 if (timerCounter != null)
305 if (timerCounter.isStarted())
307 Log.v(TAG, "startTimer() LAP TIME");
308 // チャタリング防止(ラップタイムとして、3秒以内は記録しないようにする)
309 if (timerCounter.getCurrentElapsedTime() > 3000)
311 long lapTime = timerCounter.timeStamp();
312 controller.vibrate(50);
313 controller.getDataEntry().appendTimeData(lapTime);
318 Log.v(TAG, "startTimer() START");
319 timerCounter.start();
320 MyTimerTrigger trigger = new MyTimerTrigger(this, 100, timerCounter);
321 trigger.startTimer();
322 controller.vibrate(120);
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());
342 private boolean stopTimer()
347 ITimerCounter timerCounter = counter;
348 if (timerCounter != null)
350 if (timerCounter.isStarted())
353 controller.vibrate(120);
354 controller.getDataEntry().finishTimeData(timerCounter.getStartTime(), timerCounter.getStopTime());
371 public void clickedBtn2()
373 ITimerCounter timerCounter = counter;
374 if (timerCounter != null)
376 if (!timerCounter.isStarted())
379 launchListActivity();
389 public void clickedBtn3()
391 ITimerCounter timerCounter = counter;
392 if (timerCounter != null)
394 if (!timerCounter.isStarted())
396 timerCounter.reset();
397 controller.vibrate(50);
404 public boolean pushedBtn1()
410 public boolean pushedBtn2()
412 return (stopTimer());
416 public boolean pushedBtn3()
426 public void timeout()
430 runOnUiThread(new Runnable() {
443 private void updateMainSubCounter()
445 TextView main = findViewById(R.id.main_counter);
446 TextView sub = findViewById(R.id.sub_counter1);
448 ITimerCounter timerCounter = counter;
449 if (timerCounter != null)
451 long time1 = timerCounter.getPastTime();
452 CharSequence str1 = TimeStringConvert.getTimeString(time1);
454 CharSequence str2 = "";
455 if (timerCounter.isStarted())
457 long time2 = timerCounter.getCurrentElapsedTime();
458 int lapCount = timerCounter.getElapsedCount();
459 if ((time2 >= 100) && (lapCount > 1))
461 str2 = "[" + lapCount + "] " + TimeStringConvert.getTimeString(time2);
465 if ((str2.length() > 0)&&(isCounterLapTime))
482 private void updateElapsedTimes()
485 TextView area1 = findViewById(R.id.sub_counter2);
486 TextView area2 = findViewById(R.id.sub_counter3);
487 TextView area3 = findViewById(R.id.sub_counter4);
489 ITimerCounter timerCounter = counter;
490 if (timerCounter != null)
492 List<Long> elapsedTimes = timerCounter.getTimerList();
493 int indexSize = elapsedTimes.size();
497 area1.setText(dummy);
499 area2.setText(dummy);
501 area3.setText(dummy);
508 long time = (elapsedTimes.get(indexSize - 1) - elapsedTimes.get(indexSize - 2));
509 String elapsedTime = "[" + (timerCounter.getElapsedCount() - 1) + "] " + TimeStringConvert.getTimeString(time);
510 area1.setText(elapsedTime);
512 area2.setText(dummy);
514 area3.setText(dummy);
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);
527 area2.setText(elapsedTime2);
529 area3.setText(dummy);
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);
543 area2.setText(elapsedTime2);
545 area3.setText(elapsedTime3);
551 * Launch ListActivity
554 private void launchListActivity()
556 Log.v(TAG, "launchListActivity()");
559 Intent intent = new Intent(this, ListActivity.class);
560 startActivity(intent);
573 private String getElapsedTime()
575 String elapsedTime = "";
576 ITimerCounter timerCounter = counter;
577 if (timerCounter != null)
579 int count = timerCounter.getElapsedCount();
582 elapsedTime = "[" + timerCounter.getElapsedCount() + "] " + MyTimerCounter.getTimeString(timerCounter.getCurrentElapsedTime());
585 return (elapsedTime);