diff options
author | Alexey 'Cluster' Avdyukhin <clusterrr@clusterrr.com> | 2022-05-11 19:42:01 +0300 |
---|---|---|
committer | Alexey 'Cluster' Avdyukhin <clusterrr@clusterrr.com> | 2022-05-11 19:42:01 +0300 |
commit | 41ad41b76671569fe0a0d206e6a58c72e2d26895 (patch) | |
tree | 7ad426e11565db8c09f05bb1388062f6beae6ab4 | |
parent | f7d6700979a28165d3eb4f5e3c517d1aee71ddf4 (diff) |
Crashes fix, step counter saving after reboot
-rw-r--r-- | app/src/main/java/com/clusterrr/hexeditorwatchface/HexWatchFace.java | 34 | ||||
-rw-r--r-- | app/src/main/res/values/strings.xml | 1 |
2 files changed, 30 insertions, 5 deletions
diff --git a/app/src/main/java/com/clusterrr/hexeditorwatchface/HexWatchFace.java b/app/src/main/java/com/clusterrr/hexeditorwatchface/HexWatchFace.java index cc80120..ec55ff2 100644 --- a/app/src/main/java/com/clusterrr/hexeditorwatchface/HexWatchFace.java +++ b/app/src/main/java/com/clusterrr/hexeditorwatchface/HexWatchFace.java @@ -577,7 +577,13 @@ public class HexWatchFace extends CanvasWatchFaceService { break; case Sensor.TYPE_STEP_COUNTER: SharedPreferences prefs = getApplicationContext().getSharedPreferences(getString(R.string.app_name), MODE_PRIVATE); - int steps = (int)event.values[0]; + int steps; + try { + steps = (int) event.values[0]; + } + catch (Exception ex) { + return; + } // It's a bit tricky because we can get steps since reboot only int todayStepStart = prefs.getInt(getString(R.string.pref_today_step_start), 0); if (steps >= 0 && ( @@ -590,10 +596,29 @@ public class HexWatchFace extends CanvasWatchFaceService { .putInt(getString(R.string.pref_steps_day), mCalendar.get(Calendar.DAY_OF_MONTH)) .putInt(getString(R.string.pref_today_step_start), steps) .apply(); - todayStepStart = steps; + steps = 0; + } else { + // Calculate today steps + steps = Math.max(steps - todayStepStart, 0); + int last = prefs.getInt(getString(R.string.pref_today_step_last), 0); + if (steps < last) { + // Reboot? + Log.d(TAG, "Reboot? Recalculate todayStepStart from " + todayStepStart + " to todayStepStart-"+last); + todayStepStart -= last; + prefs.edit() + .putInt(getString(R.string.pref_steps_day), mCalendar.get(Calendar.DAY_OF_MONTH)) + .putInt(getString(R.string.pref_today_step_start), steps) + .apply(); + steps = Math.max(steps - todayStepStart, 0); + } } - // Calculate today steps - mStepCounter = Math.max(steps - todayStepStart, 0); + if (steps / 10 != mStepCounter / 10) { + // Save last value every 10 steps + prefs.edit() + .putInt(getString(R.string.pref_today_step_last), steps) + .apply(); + } + mStepCounter = steps; Log.d(TAG, "Steps: " + steps + ", today: " + mStepCounter); break; } @@ -601,7 +626,6 @@ public class HexWatchFace extends CanvasWatchFaceService { @Override public void onAccuracyChanged(Sensor sensor, int i) { - } } }
\ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 21a649c..1a64fa2 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -3,6 +3,7 @@ <string name="title_activity_settings">SettingsActivity</string> <string name="pref_today_step_start">today_step_start</string> + <string name="pref_today_step_last">today_step_last</string> <string name="pref_steps_day">steps_day</string> <string name="pref_time_format">time_format</string> <string name="pref_time_system">time_system</string> |