1 package net.osdn.gokigen.joggingtimer.stopwatch;
3 import android.Manifest;
4 import android.content.SharedPreferences;
5 import android.content.pm.PackageManager;
6 import android.database.Cursor;
7 import android.os.Vibrator;
8 import android.preference.PreferenceManager;
9 import android.support.v4.app.ActivityCompat;
10 import android.support.v4.content.ContextCompat;
11 import android.support.wearable.activity.WearableActivity;
12 import android.util.Log;
13 import android.widget.ImageButton;
14 import android.widget.ListView;
15 import android.widget.TextView;
17 import net.osdn.gokigen.joggingtimer.R;
18 import net.osdn.gokigen.joggingtimer.stopwatch.graphview.LapTimeGraphView;
19 import net.osdn.gokigen.joggingtimer.stopwatch.listview.ILapTimeHolder;
20 import net.osdn.gokigen.joggingtimer.stopwatch.listview.LapTimeArrayAdapter;
21 import net.osdn.gokigen.joggingtimer.stopwatch.listview.LapTimeItems;
22 import net.osdn.gokigen.joggingtimer.storage.ITimeEntryDatabase;
23 import net.osdn.gokigen.joggingtimer.storage.ITimeEntryDatabaseCallback;
24 import net.osdn.gokigen.joggingtimer.storage.TimeEntryDatabaseFactory;
25 import net.osdn.gokigen.joggingtimer.storage.contract.TimeEntryData;
27 import java.util.ArrayList;
29 import static android.content.Context.VIBRATOR_SERVICE;
36 class WearableActivityController implements IWearableActivityControl, ITimeEntryDatabaseCallback, IDataEntry
38 private final String TAG = toString();
39 private final String PREF_KEY_TIMER_STARTED = "TMR_START";
40 private final String PREF_KEY_TIMER_INDEXID = "TMR_INDEX";
42 private SharedPreferences preferences = null;
43 private final ButtonClickListener clickListener = new ButtonClickListener();
44 private ITimeEntryDatabase database = null;
45 private IDatabaseReloadCallback dbCallback = null;
46 private boolean isReadyDatabase = false;
47 private boolean pendingLoadReference = false;
48 private long recordingIndexId = -1;
49 private ILapTimeHolder lapTimeHolder = null;
51 private Vibrator vibrator = null;
52 //private PowerManager powerManager = null;
55 WearableActivityController() {
56 Log.v(TAG, "WearableActivityController()");
60 public void setup(WearableActivity activity, IClickCallback callback, IDatabaseReloadCallback dbCallback) {
61 this.preferences = PreferenceManager.getDefaultSharedPreferences(activity);
62 this.dbCallback = dbCallback;
63 setupPermissions(activity);
64 setupHardwares(activity);
65 setupScreen(activity);
66 //setupDatabase(activity, false); // chhange true if when databaese file should be cleared.
67 setupListeners(activity, callback);
74 private void setupPermissions(WearableActivity activity) {
75 if ((ContextCompat.checkSelfPermission(activity, Manifest.permission.VIBRATE) != PackageManager.PERMISSION_GRANTED) ||
76 (ContextCompat.checkSelfPermission(activity, Manifest.permission.WAKE_LOCK) != PackageManager.PERMISSION_GRANTED)) {
77 ActivityCompat.requestPermissions(activity,
79 Manifest.permission.VIBRATE,
80 Manifest.permission.WAKE_LOCK,
90 private void setupHardwares(WearableActivity activity) {
92 vibrator = (Vibrator) activity.getSystemService(VIBRATOR_SERVICE);
95 //powerManager = (PowerManager) activity.getSystemService(POWER_SERVICE);
102 private void setupScreen(WearableActivity activity)
104 TextView mTextView = activity.findViewById(R.id.text);
105 if (mTextView != null) {
106 mTextView.setText(R.string.app_name);
109 LapTimeArrayAdapter adapter = new LapTimeArrayAdapter(activity.getApplicationContext(), R.layout.column_laptime);
110 adapter.clearLapTime();
111 lapTimeHolder = adapter;
112 ListView lapTimeArea = activity.findViewById(R.id.laptime_list_area);
113 lapTimeArea.setAdapter(adapter);
120 public void setupDatabase(final WearableActivity activity, final boolean isInitialize) {
121 database = new TimeEntryDatabaseFactory(activity, this).getEntryDatabase();
122 Thread thread = new Thread(new Runnable() {
127 // 既存のデータベースを消去する場合、、、
128 TimeEntryDatabaseFactory.deleteDatabase(activity);
131 } catch (Exception e) {
142 private void setupListeners(WearableActivity activity, IClickCallback callback)
146 clickListener.setCallback(callback);
147 ImageButton btn1 = activity.findViewById(R.id.btn1);
148 btn1.setOnClickListener(clickListener);
149 btn1.setOnLongClickListener(clickListener);
151 ImageButton btn2 = activity.findViewById(R.id.btn2);
152 btn2.setOnClickListener(clickListener);
153 btn2.setOnLongClickListener(clickListener);
155 ImageButton btn3 = activity.findViewById(R.id.btn3);
156 btn3.setOnClickListener(clickListener);
157 btn3.setOnLongClickListener(clickListener);
159 TextView main = activity.findViewById(R.id.main_counter);
160 main.setOnClickListener(clickListener);
161 main.setOnLongClickListener(clickListener);
163 TextView sub1 = activity.findViewById(R.id.sub_counter1);
164 sub1.setOnClickListener(clickListener);
165 sub1.setOnLongClickListener(clickListener);
167 ListView lap = activity.findViewById(R.id.laptime_list_area);
168 lap.setOnClickListener(clickListener);
169 lap.setOnLongClickListener(clickListener);
171 LapTimeGraphView graphView = activity.findViewById(R.id.graph_area);
172 graphView.setOnClickListener(clickListener);
173 graphView.setOnLongClickListener(clickListener);
174 graphView.setOnTouchListener(clickListener);
187 private void closeDatabase()
189 Log.v(TAG, "closeDatabase()");
190 Thread thread = new Thread(new Runnable() {
194 if (isReadyDatabase) {
195 isReadyDatabase = false;
197 Log.v(TAG, "closeDatabase() EXECUTE...");
199 } catch (Exception e) {
213 public void exitApplication(WearableActivity activity)
215 Log.v(TAG, "exitApplication()");
220 public void vibrate(final int duration)
224 if ((vibrator == null) || (!vibrator.hasVibrator()))
229 Thread thread = new Thread(new Runnable() {
232 vibrator.vibrate(duration);
244 public IDataEntry getDataEntry() {
249 public void timerStarted(boolean isStarted)
253 SharedPreferences.Editor editor = preferences.edit();
254 editor.putBoolean(PREF_KEY_TIMER_STARTED, isStarted);
264 public void addTimeStamp(long count, long lapTime, long diffTime)
266 if (lapTimeHolder != null)
268 lapTimeHolder.addLapTime(new LapTimeItems(count, lapTime, diffTime));
273 public void clearTimeStamp()
275 if (lapTimeHolder != null)
277 lapTimeHolder.clearLapTime();
282 public void setupReferenceData()
289 pendingLoadReference = false;
293 pendingLoadReference = true;
303 public void prepareFinished(boolean isReady)
305 Log.v(TAG, "database prepareFinished() : " + isReady);
306 isReadyDatabase = isReady;
309 boolean isStarted = preferences.getBoolean(PREF_KEY_TIMER_STARTED, false);
310 recordingIndexId = preferences.getLong(PREF_KEY_TIMER_INDEXID, -1);
312 Log.v(TAG, "isStarted : " + isStarted + " indexId : " + recordingIndexId);
314 // load reference data
315 if (pendingLoadReference)
318 pendingLoadReference = false;
321 // load current lap time list
322 if ((isStarted) && (recordingIndexId >= 0) && (isReadyDatabase))
324 ArrayList<Long> list = new ArrayList<>();
325 Cursor cursor = database.getAllDetailData(recordingIndexId);
326 while (cursor.moveToNext())
328 list.add(cursor.getLong(cursor.getColumnIndex(TimeEntryData.EntryData.COLUMN_NAME_TIME_ENTRY)));
330 dbCallback.dataIsReloaded(list);
339 private void loadReferenceData()
341 // load reference data
344 ArrayList<Long> refList = null;
345 Cursor cursor = database.getAllReferenceDetailData();
346 if (cursor != null) {
347 refList = new ArrayList<>();
348 while (cursor.moveToNext())
350 refList.add(cursor.getLong(cursor.getColumnIndex(TimeEntryData.EntryData.COLUMN_NAME_TIME_ENTRY)));
353 dbCallback.referenceDataIsReloaded(refList);
362 public void timeEntryFinished(OperationType operationType, boolean result, long indexId, long dataId)
364 Log.v(TAG, "database timeEntryFinished() : " + result + " [" + indexId + "] " + dataId);
368 public void dataEntryFinished(OperationType operationType, boolean result, long id, String title)
370 Log.v(TAG, "database dataEntryFinished() : " + result + " [" + id + "] " + title);
371 if ((result)&&(operationType == OperationType.CREATED))
377 private void setIndexId(long id)
381 recordingIndexId = id;
382 SharedPreferences.Editor editor = preferences.edit();
383 editor.putLong(PREF_KEY_TIMER_INDEXID, recordingIndexId);
394 public void createIndex(final String title, final long startTime)
396 final String memo = "";
398 Log.v(TAG, "createIndex() " + title + " " + startTime);
399 Thread thread = new Thread(new Runnable() {
407 database.createIndexData(title, memo, icon, startTime);
420 public void appendTimeData(final long elapsedTime)
422 Log.v(TAG, "appendTimeData() " + " " + elapsedTime);
423 Thread thread = new Thread(new Runnable() {
431 database.appendTimeData(recordingIndexId, elapsedTime);
444 public void finishTimeData(final long startTime, final long endTime)
446 Log.v(TAG, "finishTimeData() " + startTime + " " + endTime);
447 Thread thread = new Thread(new Runnable() {
455 database.finishTimeData(recordingIndexId, startTime, endTime);