39
39
40
40
import de .j4velin .pedometer .ui .Activity_Main ;
41
41
import de .j4velin .pedometer .util .API23Wrapper ;
42
+ import de .j4velin .pedometer .util .API26Wrapper ;
42
43
import de .j4velin .pedometer .util .Logger ;
43
44
import de .j4velin .pedometer .util .Util ;
44
45
import de .j4velin .pedometer .widget .WidgetUpdateService ;
52
53
*/
53
54
public class SensorListener extends Service implements SensorEventListener {
54
55
55
- private final static int NOTIFICATION_ID = 1 ;
56
+ public final static int NOTIFICATION_ID = 1 ;
56
57
private final static long MICROSECONDS_IN_ONE_MINUTE = 60000000 ;
57
58
private final static long SAVE_OFFSET_TIME = AlarmManager .INTERVAL_HOUR ;
58
59
private final static int SAVE_OFFSET_STEPS = 500 ;
@@ -63,8 +64,6 @@ public class SensorListener extends Service implements SensorEventListener {
63
64
64
65
private final BroadcastReceiver shutdownReceiver = new ShutdownRecevier ();
65
66
66
- public final static String ACTION_UPDATE_NOTIFICATION = "updateNotificationState" ;
67
-
68
67
@ Override
69
68
public void onAccuracyChanged (final Sensor sensor , int accuracy ) {
70
69
// nobody knows what happens here: step value might magically decrease
@@ -83,7 +82,10 @@ public void onSensorChanged(final SensorEvent event) {
83
82
}
84
83
}
85
84
86
- private void updateIfNecessary () {
85
+ /**
86
+ * @return true, if notification was updated
87
+ */
88
+ private boolean updateIfNecessary () {
87
89
if (steps > lastSaveSteps + SAVE_OFFSET_STEPS ||
88
90
(steps > 0 && System .currentTimeMillis () > lastSaveTime + SAVE_OFFSET_TIME )) {
89
91
if (BuildConfig .DEBUG ) Logger .log (
@@ -105,8 +107,23 @@ private void updateIfNecessary() {
105
107
db .close ();
106
108
lastSaveSteps = steps ;
107
109
lastSaveTime = System .currentTimeMillis ();
108
- updateNotificationState ();
110
+ showNotification (); // update notification
109
111
startService (new Intent (this , WidgetUpdateService .class ));
112
+ return true ;
113
+ } else {
114
+ return false ;
115
+ }
116
+ }
117
+
118
+ private void showNotification () {
119
+ if (Build .VERSION .SDK_INT >= 26 ) {
120
+ startForeground (NOTIFICATION_ID , getNotification (this ));
121
+ } else if (getSharedPreferences ("pedometer" , Context .MODE_PRIVATE )
122
+ .getBoolean ("notification" , true )) {
123
+ {
124
+ ((NotificationManager ) getSystemService (Context .NOTIFICATION_SERVICE ))
125
+ .notify (NOTIFICATION_ID , getNotification (this ));
126
+ }
110
127
}
111
128
}
112
129
@@ -117,12 +134,10 @@ public IBinder onBind(final Intent intent) {
117
134
118
135
@ Override
119
136
public int onStartCommand (final Intent intent , int flags , int startId ) {
120
- if (intent != null && intent .getBooleanExtra (ACTION_UPDATE_NOTIFICATION , false )) {
121
- updateNotificationState ();
122
- } else {
123
- reRegisterSensor ();
124
- registerBroadcastReceiver ();
125
- updateIfNecessary ();
137
+ reRegisterSensor ();
138
+ registerBroadcastReceiver ();
139
+ if (!updateIfNecessary ()) {
140
+ showNotification ();
126
141
}
127
142
128
143
// restart service every hour to save the current step count
@@ -147,8 +162,6 @@ public int onStartCommand(final Intent intent, int flags, int startId) {
147
162
public void onCreate () {
148
163
super .onCreate ();
149
164
if (BuildConfig .DEBUG ) Logger .log ("SensorListener onCreate" );
150
- reRegisterSensor ();
151
- updateNotificationState ();
152
165
}
153
166
154
167
@ Override
@@ -174,41 +187,38 @@ public void onDestroy() {
174
187
}
175
188
}
176
189
177
- private void updateNotificationState () {
178
- if (BuildConfig .DEBUG ) Logger .log ("SensorListener updateNotificationState" );
179
- SharedPreferences prefs = getSharedPreferences ("pedometer" , Context .MODE_PRIVATE );
180
- NotificationManager nm =
181
- (NotificationManager ) getSystemService (Context .NOTIFICATION_SERVICE );
182
- if (prefs .getBoolean ("notification" , true )) {
183
- int goal = prefs .getInt ("goal" , 10000 );
184
- Database db = Database .getInstance (this );
185
- int today_offset = db .getSteps (Util .getToday ());
186
- if (steps == 0 )
187
- steps = db .getCurrentSteps (); // use saved value if we haven't anything better
188
- db .close ();
189
- Notification .Builder notificationBuilder = new Notification .Builder (this );
190
- if (steps > 0 ) {
191
- if (today_offset == Integer .MIN_VALUE ) today_offset = -steps ;
192
- notificationBuilder .setProgress (goal , today_offset + steps , false ).setContentText (
193
- today_offset + steps >= goal ? getString (R .string .goal_reached_notification ,
194
- NumberFormat .getInstance (Locale .getDefault ())
195
- .format ((today_offset + steps ))) :
196
- getString (R .string .notification_text ,
197
- NumberFormat .getInstance (Locale .getDefault ())
198
- .format ((goal - today_offset - steps ))));
199
- } else { // still no step value?
200
- notificationBuilder
201
- .setContentText (getString (R .string .your_progress_will_be_shown_here_soon ));
202
- }
203
- notificationBuilder .setPriority (Notification .PRIORITY_MIN ).setShowWhen (false )
204
- .setContentTitle (getString (R .string .notification_title )).setContentIntent (
205
- PendingIntent .getActivity (this , 0 , new Intent (this , Activity_Main .class ),
206
- PendingIntent .FLAG_UPDATE_CURRENT ))
207
- .setSmallIcon (R .drawable .ic_notification ).setOngoing (true );
208
- nm .notify (NOTIFICATION_ID , notificationBuilder .build ());
209
- } else {
210
- nm .cancel (NOTIFICATION_ID );
190
+ public static Notification getNotification (final Context context ) {
191
+ if (BuildConfig .DEBUG ) Logger .log ("getNotification" );
192
+ SharedPreferences prefs = context .getSharedPreferences ("pedometer" , Context .MODE_PRIVATE );
193
+ int goal = prefs .getInt ("goal" , 10000 );
194
+ Database db = Database .getInstance (context );
195
+ int today_offset = db .getSteps (Util .getToday ());
196
+ if (steps == 0 )
197
+ steps = db .getCurrentSteps (); // use saved value if we haven't anything better
198
+ db .close ();
199
+ Notification .Builder notificationBuilder =
200
+ Build .VERSION .SDK_INT >= 26 ? API26Wrapper .getNotificationBuilder (context ) :
201
+ new Notification .Builder (context );
202
+ if (steps > 0 ) {
203
+ if (today_offset == Integer .MIN_VALUE ) today_offset = -steps ;
204
+ notificationBuilder .setProgress (goal , today_offset + steps , false ).setContentText (
205
+ today_offset + steps >= goal ?
206
+ context .getString (R .string .goal_reached_notification ,
207
+ NumberFormat .getInstance (Locale .getDefault ())
208
+ .format ((today_offset + steps ))) :
209
+ context .getString (R .string .notification_text ,
210
+ NumberFormat .getInstance (Locale .getDefault ())
211
+ .format ((goal - today_offset - steps ))));
212
+ } else { // still no step value?
213
+ notificationBuilder .setContentText (
214
+ context .getString (R .string .your_progress_will_be_shown_here_soon ));
211
215
}
216
+ notificationBuilder .setPriority (Notification .PRIORITY_MIN ).setShowWhen (false )
217
+ .setContentTitle (context .getString (R .string .notification_title )).setContentIntent (
218
+ PendingIntent .getActivity (context , 0 , new Intent (context , Activity_Main .class ),
219
+ PendingIntent .FLAG_UPDATE_CURRENT )).setSmallIcon (R .drawable .ic_notification )
220
+ .setOngoing (true );
221
+ return notificationBuilder .build ();
212
222
}
213
223
214
224
private void registerBroadcastReceiver () {
@@ -234,15 +244,8 @@ private void reRegisterSensor() {
234
244
Logger .log ("default: " + sm .getDefaultSensor (Sensor .TYPE_STEP_COUNTER ).getName ());
235
245
}
236
246
237
- if (Build .VERSION .SDK_INT >= 27 ) {
238
- // do not use batching on Android P and newer as we dont live long enough to recieve
239
- // those value due to aggressive power saving
240
- sm .registerListener (this , sm .getDefaultSensor (Sensor .TYPE_STEP_COUNTER ),
241
- SensorManager .SENSOR_DELAY_FASTEST );
242
- } else {
243
- // enable batching with delay of max 5 min
244
- sm .registerListener (this , sm .getDefaultSensor (Sensor .TYPE_STEP_COUNTER ),
245
- SensorManager .SENSOR_DELAY_NORMAL , (int ) (5 * MICROSECONDS_IN_ONE_MINUTE ));
246
- }
247
+ // enable batching with delay of max 5 min
248
+ sm .registerListener (this , sm .getDefaultSensor (Sensor .TYPE_STEP_COUNTER ),
249
+ SensorManager .SENSOR_DELAY_NORMAL , (int ) (5 * MICROSECONDS_IN_ONE_MINUTE ));
247
250
}
248
251
}
0 commit comments