1 package net.osdn.gokigen.joggingtimer.stopwatch;
3 import android.Manifest;
4 import android.annotation.SuppressLint;
5 import android.content.SharedPreferences;
6 import android.content.pm.PackageManager;
7 import android.database.Cursor;
8 import android.os.Vibrator;
9 import android.preference.PreferenceManager;
10 import android.support.wearable.activity.WearableActivity;
11 import android.util.Log;
12 import android.widget.ImageButton;
13 import android.widget.ListView;
14 import android.widget.TextView;
16 import net.osdn.gokigen.joggingtimer.R;
17 import net.osdn.gokigen.joggingtimer.stopwatch.graphview.LapTimeGraphView;
18 import net.osdn.gokigen.joggingtimer.stopwatch.listview.ILapTimeHolder;
19 import net.osdn.gokigen.joggingtimer.stopwatch.listview.LapTimeArrayAdapter;
20 import net.osdn.gokigen.joggingtimer.stopwatch.listview.LapTimeItems;
21 import net.osdn.gokigen.joggingtimer.storage.ITimeEntryDatabase;
22 import net.osdn.gokigen.joggingtimer.storage.ITimeEntryDatabaseCallback;
23 import net.osdn.gokigen.joggingtimer.storage.TimeEntryDatabaseFactory;
24 import net.osdn.gokigen.joggingtimer.storage.contract.TimeEntryData;
26 import java.util.ArrayList;
28 import static android.content.Context.VIBRATOR_SERVICE;
30 import androidx.core.app.ActivityCompat;
31 import androidx.core.content.ContextCompat;
38 class WearableActivityController implements IWearableActivityControl, ITimeEntryDatabaseCallback, IDataEntry
40 private final String TAG = toString();
41 private final String PREF_KEY_TIMER_STARTED = "TMR_START";
42 private final String PREF_KEY_TIMER_INDEXID = "TMR_INDEX";
43 private final String PREF_KEY_DISPLAY_LAPGRAPHIC = "DISP_LAPGRPH";
45 private SharedPreferences preferences = null;
46 private final ButtonClickListener clickListener = new ButtonClickListener();
47 private ITimeEntryDatabase database = null;
48 private IDatabaseReloadCallback dbCallback = null;
49 private boolean isReadyDatabase = false;
50 private boolean pendingLoadReference = false;
51 private long recordingIndexId = -1;
52 private ILapTimeHolder lapTimeHolder = null;
54 private Vibrator vibrator = null;
55 //private PowerManager powerManager = null;
58 WearableActivityController() {
59 Log.v(TAG, "WearableActivityController()");
63 public void setup(WearableActivity activity, IClickCallback callback, IDatabaseReloadCallback dbCallback)
65 this.preferences = PreferenceManager.getDefaultSharedPreferences(activity);
66 this.dbCallback = dbCallback;
67 setupPermissions(activity);
68 setupHardwares(activity);
69 setupScreen(activity);
70 //setupDatabase(activity, false); // chhange true if when databaese file should be cleared.
71 setupListeners(activity, callback);
78 private void setupPermissions(WearableActivity activity)
80 if ((ContextCompat.checkSelfPermission(activity, Manifest.permission.VIBRATE) != PackageManager.PERMISSION_GRANTED) ||
81 (ContextCompat.checkSelfPermission(activity, Manifest.permission.WAKE_LOCK) != PackageManager.PERMISSION_GRANTED)) {
82 ActivityCompat.requestPermissions(activity,
84 Manifest.permission.VIBRATE,
85 Manifest.permission.WAKE_LOCK,
95 private void setupHardwares(WearableActivity activity)
98 vibrator = (Vibrator) activity.getSystemService(VIBRATOR_SERVICE);
101 //powerManager = (PowerManager) activity.getSystemService(POWER_SERVICE);
108 private void setupScreen(WearableActivity activity)
110 TextView mTextView = activity.findViewById(R.id.text);
111 if (mTextView != null)
113 mTextView.setText(R.string.app_name);
116 LapTimeArrayAdapter adapter = new LapTimeArrayAdapter(activity.getApplicationContext(), R.layout.column_laptime);
117 adapter.clearLapTime();
118 lapTimeHolder = adapter;
119 ListView lapTimeArea = activity.findViewById(R.id.laptime_list_area);
120 lapTimeArea.setAdapter(adapter);
127 public void setupDatabase(final WearableActivity activity, final boolean isInitialize)
129 database = new TimeEntryDatabaseFactory(activity, this).getEntryDatabase();
130 Thread thread = new Thread(() -> {
135 // 既存のデータベースを消去する場合、、、
136 TimeEntryDatabaseFactory.deleteDatabase(activity);
151 private void setupListeners(WearableActivity activity, IClickCallback callback)
155 clickListener.setCallback(callback);
156 ImageButton btn1 = activity.findViewById(R.id.btn1);
157 btn1.setOnClickListener(clickListener);
158 btn1.setOnLongClickListener(clickListener);
160 ImageButton btn2 = activity.findViewById(R.id.btn2);
161 btn2.setOnClickListener(clickListener);
162 btn2.setOnLongClickListener(clickListener);
164 ImageButton btn3 = activity.findViewById(R.id.btn3);
165 btn3.setOnClickListener(clickListener);
166 btn3.setOnLongClickListener(clickListener);
168 TextView main = activity.findViewById(R.id.main_counter);
169 main.setOnClickListener(clickListener);
170 main.setOnLongClickListener(clickListener);
172 TextView sub1 = activity.findViewById(R.id.sub_counter1);
173 sub1.setOnClickListener(clickListener);
174 sub1.setOnLongClickListener(clickListener);
176 ListView lap = activity.findViewById(R.id.laptime_list_area);
177 //lap.setOnClickListener(clickListener);
178 lap.setOnLongClickListener(clickListener);
180 LapTimeGraphView graphView = activity.findViewById(R.id.graph_area);
181 graphView.setOnTouchListener(clickListener);
182 graphView.setOnClickListener(clickListener);
183 graphView.setOnLongClickListener(clickListener);
195 private void closeDatabase()
197 Log.v(TAG, "closeDatabase()");
198 Thread thread = new Thread(() -> {
202 isReadyDatabase = false;
205 Log.v(TAG, "closeDatabase() EXECUTE...");
222 public void exitApplication(WearableActivity activity)
224 Log.v(TAG, "exitApplication()");
229 public void vibrate(final int duration)
233 if ((vibrator == null) || (!vibrator.hasVibrator()))
238 Thread thread = new Thread(() -> vibrator.vibrate(duration));
248 public IDataEntry getDataEntry() {
253 public boolean getDisplayMode()
257 return (preferences.getBoolean(PREF_KEY_DISPLAY_LAPGRAPHIC, false));
267 public void timerStarted(boolean isStarted)
271 SharedPreferences.Editor editor = preferences.edit();
272 editor.putBoolean(PREF_KEY_TIMER_STARTED, isStarted);
282 public void setDisplayMode(boolean displayLapTime)
286 SharedPreferences.Editor editor = preferences.edit();
287 editor.putBoolean(PREF_KEY_DISPLAY_LAPGRAPHIC, displayLapTime);
298 public void addTimeStamp(long count, long lapTime, long diffTime)
300 if (lapTimeHolder != null)
302 lapTimeHolder.addLapTime(new LapTimeItems(count, lapTime, diffTime));
307 public void clearTimeStamp()
309 if (lapTimeHolder != null)
311 lapTimeHolder.clearLapTime();
316 public int getLapTimeCount()
319 if (lapTimeHolder != null)
321 count = lapTimeHolder.getLapTimeCount();
327 public void setupReferenceData()
334 pendingLoadReference = false;
338 pendingLoadReference = true;
347 @SuppressLint("Range")
349 public void prepareFinished(boolean isReady)
351 Log.v(TAG, "database prepareFinished() : " + isReady);
352 isReadyDatabase = isReady;
356 boolean isStarted = preferences.getBoolean(PREF_KEY_TIMER_STARTED, false);
357 recordingIndexId = preferences.getLong(PREF_KEY_TIMER_INDEXID, -1);
359 Log.v(TAG, "isStarted : " + isStarted + " indexId : " + recordingIndexId);
361 // load reference data
362 if (pendingLoadReference)
365 pendingLoadReference = false;
368 // load current lap time list
369 if ((isStarted) && (recordingIndexId >= 0) && (isReadyDatabase))
371 ArrayList<Long> list = new ArrayList<>();
372 Cursor cursor = database.getAllDetailData(recordingIndexId);
373 while (cursor.moveToNext())
375 list.add(cursor.getLong(cursor.getColumnIndex(TimeEntryData.EntryData.COLUMN_NAME_TIME_ENTRY)));
377 dbCallback.dataIsReloaded(list);
386 @SuppressLint("Range")
387 private void loadReferenceData()
389 // load reference data
392 ArrayList<Long> refList = null;
393 Cursor cursor = database.getAllReferenceDetailData();
396 refList = new ArrayList<>();
397 while (cursor.moveToNext())
399 refList.add(cursor.getLong(cursor.getColumnIndex(TimeEntryData.EntryData.COLUMN_NAME_TIME_ENTRY)));
402 dbCallback.referenceDataIsReloaded(refList);
411 public void timeEntryFinished(OperationType operationType, boolean result, long indexId, long dataId)
413 Log.v(TAG, "database timeEntryFinished() : " + result + " [" + indexId + "] " + dataId);
417 public void modelDataEntryFinished(OperationType operationType, boolean result, long indexId, String title)
423 public void dataEntryFinished(OperationType operationType, boolean result, long id, String title)
425 Log.v(TAG, "database dataEntryFinished() : " + result + " [" + id + "] " + title);
426 if ((result)&&(operationType == OperationType.CREATED))
432 private void setIndexId(long id)
436 recordingIndexId = id;
437 SharedPreferences.Editor editor = preferences.edit();
438 editor.putLong(PREF_KEY_TIMER_INDEXID, recordingIndexId);
449 public void createIndex(final String title, final long startTime)
451 final String memo = "";
453 Log.v(TAG, "createIndex() " + title + " " + startTime);
454 Thread thread = new Thread(() -> {
459 database.createIndexData(title, memo, icon, startTime);
471 public void appendTimeData(final long elapsedTime)
473 Log.v(TAG, "appendTimeData() " + " " + elapsedTime);
474 Thread thread = new Thread(() -> {
479 database.appendTimeData(recordingIndexId, elapsedTime);
491 public void finishTimeData(final long startTime, final long endTime)
493 Log.v(TAG, "finishTimeData() " + startTime + " " + endTime);
494 Thread thread = new Thread(() -> {
499 database.finishTimeData(recordingIndexId, startTime, endTime);