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.storage.ITimeEntryDatabase;
19 import net.osdn.gokigen.joggingtimer.storage.ITimeEntryDatabaseCallback;
20 import net.osdn.gokigen.joggingtimer.storage.TimeEntryDatabaseFactory;
21 import net.osdn.gokigen.joggingtimer.storage.contract.TimeEntryData;
23 import java.util.ArrayList;
25 import static android.content.Context.VIBRATOR_SERVICE;
32 class WearableActivityController implements IWearableActivityControl, ITimeEntryDatabaseCallback, IDataEntry
34 private final String TAG = toString();
35 private final String PREF_KEY_TIMER_STARTED = "TMR_START";
36 private final String PREF_KEY_TIMER_INDEXID = "TMR_INDEX";
38 private SharedPreferences preferences = null;
39 private final ButtonClickListener clickListener = new ButtonClickListener();
40 private ITimeEntryDatabase database = null;
41 private IDatabaseReloadCallback dbCallback = null;
42 private boolean isReadyDatabase = false;
43 private boolean pendingLoadReference = false;
44 private long recordingIndexId = -1;
45 private ILapTimeHolder lapTimeHolder = null;
47 private Vibrator vibrator = null;
48 //private PowerManager powerManager = null;
51 WearableActivityController() {
52 Log.v(TAG, "WearableActivityController()");
56 public void setup(WearableActivity activity, IClickCallback callback, IDatabaseReloadCallback dbCallback) {
57 this.preferences = PreferenceManager.getDefaultSharedPreferences(activity);
58 this.dbCallback = dbCallback;
59 setupPermissions(activity);
60 setupHardwares(activity);
61 setupScreen(activity);
62 //setupDatabase(activity, false); // chhange true if when databaese file should be cleared.
63 setupListeners(activity, callback);
70 private void setupPermissions(WearableActivity activity) {
71 if ((ContextCompat.checkSelfPermission(activity, Manifest.permission.VIBRATE) != PackageManager.PERMISSION_GRANTED) ||
72 (ContextCompat.checkSelfPermission(activity, Manifest.permission.WAKE_LOCK) != PackageManager.PERMISSION_GRANTED)) {
73 ActivityCompat.requestPermissions(activity,
75 Manifest.permission.VIBRATE,
76 Manifest.permission.WAKE_LOCK,
86 private void setupHardwares(WearableActivity activity) {
88 vibrator = (Vibrator) activity.getSystemService(VIBRATOR_SERVICE);
91 //powerManager = (PowerManager) activity.getSystemService(POWER_SERVICE);
98 private void setupScreen(WearableActivity activity)
100 TextView mTextView = activity.findViewById(R.id.text);
101 if (mTextView != null) {
102 mTextView.setText(R.string.app_name);
105 LapTimeArrayAdapter adapter = new LapTimeArrayAdapter(activity.getApplicationContext(), R.layout.column_laptime);
106 adapter.clearLapTime();
107 lapTimeHolder = adapter;
108 ListView lapTimeArea = activity.findViewById(R.id.laptime_list_area);
109 lapTimeArea.setAdapter(adapter);
116 public void setupDatabase(final WearableActivity activity, final boolean isInitialize) {
117 database = new TimeEntryDatabaseFactory(activity, this).getEntryDatabase();
118 Thread thread = new Thread(new Runnable() {
123 // 既存のデータベースを消去する場合、、、
124 TimeEntryDatabaseFactory.deleteDatabase(activity);
127 } catch (Exception e) {
138 private void setupListeners(WearableActivity activity, IClickCallback callback)
142 clickListener.setCallback(callback);
143 ImageButton btn1 = activity.findViewById(R.id.btn1);
144 btn1.setOnClickListener(clickListener);
145 btn1.setOnLongClickListener(clickListener);
147 ImageButton btn2 = activity.findViewById(R.id.btn2);
148 btn2.setOnClickListener(clickListener);
149 btn2.setOnLongClickListener(clickListener);
151 ImageButton btn3 = activity.findViewById(R.id.btn3);
152 btn3.setOnClickListener(clickListener);
153 btn3.setOnLongClickListener(clickListener);
155 TextView main = activity.findViewById(R.id.main_counter);
156 main.setOnClickListener(clickListener);
157 main.setOnLongClickListener(clickListener);
159 TextView sub1 = activity.findViewById(R.id.sub_counter1);
160 sub1.setOnClickListener(clickListener);
161 sub1.setOnLongClickListener(clickListener);
163 ListView lap = activity.findViewById(R.id.laptime_list_area);
164 lap.setOnClickListener(clickListener);
165 lap.setOnLongClickListener(clickListener);
168 TextView sub2 = activity.findViewById(R.id.sub_counter2);
169 sub2.setOnClickListener(clickListener);
170 sub2.setOnLongClickListener(clickListener);
172 TextView sub3 = activity.findViewById(R.id.sub_counter3);
173 sub3.setOnClickListener(clickListener);
174 sub3.setOnLongClickListener(clickListener);
176 TextView sub4 = activity.findViewById(R.id.sub_counter4);
177 sub4.setOnClickListener(clickListener);
178 sub4.setOnLongClickListener(clickListener);
180 TextView sub5 = activity.findViewById(R.id.sub_counter5);
181 sub5.setOnClickListener(clickListener);
182 sub5.setOnLongClickListener(clickListener);
184 TextView sub6 = activity.findViewById(R.id.sub_counter6);
185 sub6.setOnClickListener(clickListener);
186 sub6.setOnLongClickListener(clickListener);
188 LapTimeGraphView graphView = activity.findViewById(R.id.graph_area);
189 graphView.setOnClickListener(clickListener);
190 graphView.setOnLongClickListener(clickListener);
191 graphView.setOnTouchListener(clickListener);
204 private void closeDatabase() {
205 Log.v(TAG, "closeDatabase()");
206 Thread thread = new Thread(new Runnable() {
210 if (isReadyDatabase) {
211 isReadyDatabase = false;
213 Log.v(TAG, "closeDatabase() EXECUTE...");
215 } catch (Exception e) {
229 public void exitApplication(WearableActivity activity) {
230 Log.v(TAG, "exitApplication()");
235 public void vibrate(final int duration) {
237 if ((vibrator == null) || (!vibrator.hasVibrator())) {
241 Thread thread = new Thread(new Runnable() {
244 vibrator.vibrate(duration);
248 } catch (Exception e) {
254 public IDataEntry getDataEntry() {
259 public void timerStarted(boolean isStarted) {
261 SharedPreferences.Editor editor = preferences.edit();
262 editor.putBoolean(PREF_KEY_TIMER_STARTED, isStarted);
264 } catch (Exception e) {
270 public void setupReferenceData()
277 pendingLoadReference = false;
281 pendingLoadReference = true;
291 public void prepareFinished(boolean isReady)
293 Log.v(TAG, "database prepareFinished() : " + isReady);
294 isReadyDatabase = isReady;
297 boolean isStarted = preferences.getBoolean(PREF_KEY_TIMER_STARTED, false);
298 recordingIndexId = preferences.getLong(PREF_KEY_TIMER_INDEXID, -1);
300 Log.v(TAG, "isStarted : " + isStarted + " indexId : " + recordingIndexId);
302 // load reference data
303 if (pendingLoadReference)
306 pendingLoadReference = false;
309 // load current lap time list
310 if ((isStarted) && (recordingIndexId >= 0) && (isReadyDatabase))
312 ArrayList<Long> list = new ArrayList<>();
313 Cursor cursor = database.getAllDetailData(recordingIndexId);
314 while (cursor.moveToNext())
316 list.add(cursor.getLong(cursor.getColumnIndex(TimeEntryData.EntryData.COLUMN_NAME_TIME_ENTRY)));
318 dbCallback.dataIsReloaded(list);
327 private void loadReferenceData()
329 // load reference data
332 ArrayList<Long> refList = null;
333 Cursor cursor = database.getAllReferenceDetailData();
334 if (cursor != null) {
335 refList = new ArrayList<>();
336 while (cursor.moveToNext())
338 refList.add(cursor.getLong(cursor.getColumnIndex(TimeEntryData.EntryData.COLUMN_NAME_TIME_ENTRY)));
341 dbCallback.referenceDataIsReloaded(refList);
350 public void timeEntryFinished(OperationType operationType, boolean result, long indexId, long dataId)
352 Log.v(TAG, "database timeEntryFinished() : " + result + " [" + indexId + "] " + dataId);
356 public void dataEntryFinished(OperationType operationType, boolean result, long id, String title)
358 Log.v(TAG, "database dataEntryFinished() : " + result + " [" + id + "] " + title);
359 if ((result)&&(operationType == OperationType.CREATED))
365 private void setIndexId(long id)
369 recordingIndexId = id;
370 SharedPreferences.Editor editor = preferences.edit();
371 editor.putLong(PREF_KEY_TIMER_INDEXID, recordingIndexId);
382 public void createIndex(final String title, final long startTime)
384 final String memo = "";
386 Log.v(TAG, "createIndex() " + title + " " + startTime);
387 Thread thread = new Thread(new Runnable() {
395 database.createIndexData(title, memo, icon, startTime);
408 public void appendTimeData(final long elapsedTime)
410 Log.v(TAG, "appendTimeData() " + " " + elapsedTime);
411 Thread thread = new Thread(new Runnable() {
419 database.appendTimeData(recordingIndexId, elapsedTime);
432 public void finishTimeData(final long startTime, final long endTime)
434 Log.v(TAG, "finishTimeData() " + startTime + " " + endTime);
435 Thread thread = new Thread(new Runnable() {
443 database.finishTimeData(recordingIndexId, startTime, endTime);