18
18
19
19
import android .app .AlarmManager ;
20
20
import android .app .Notification ;
21
+ import android .app .NotificationManager ;
21
22
import android .app .PendingIntent ;
22
23
import android .app .Service ;
23
24
import android .content .BroadcastReceiver ;
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
@@ -108,14 +107,26 @@ private boolean updateIfNecessary() {
108
107
db .close ();
109
108
lastSaveSteps = steps ;
110
109
lastSaveTime = System .currentTimeMillis ();
111
- startForeground ( NOTIFICATION_ID , getNotification () ); // update notification
110
+ showNotification ( ); // update notification
112
111
startService (new Intent (this , WidgetUpdateService .class ));
113
112
return true ;
114
113
} else {
115
114
return false ;
116
115
}
117
116
}
118
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
+ }
127
+ }
128
+ }
129
+
119
130
@ Override
120
131
public IBinder onBind (final Intent intent ) {
121
132
return null ;
@@ -126,7 +137,7 @@ public int onStartCommand(final Intent intent, int flags, int startId) {
126
137
reRegisterSensor ();
127
138
registerBroadcastReceiver ();
128
139
if (!updateIfNecessary ()) {
129
- startForeground ( NOTIFICATION_ID , getNotification () );
140
+ showNotification ( );
130
141
}
131
142
132
143
// restart service every hour to save the current step count
@@ -176,34 +187,35 @@ public void onDestroy() {
176
187
}
177
188
}
178
189
179
- private Notification getNotification () {
190
+ public static Notification getNotification (final Context context ) {
180
191
if (BuildConfig .DEBUG ) Logger .log ("getNotification" );
181
- SharedPreferences prefs = getSharedPreferences ("pedometer" , Context .MODE_PRIVATE );
192
+ SharedPreferences prefs = context . getSharedPreferences ("pedometer" , Context .MODE_PRIVATE );
182
193
int goal = prefs .getInt ("goal" , 10000 );
183
- Database db = Database .getInstance (this );
194
+ Database db = Database .getInstance (context );
184
195
int today_offset = db .getSteps (Util .getToday ());
185
196
if (steps == 0 )
186
197
steps = db .getCurrentSteps (); // use saved value if we haven't anything better
187
198
db .close ();
188
199
Notification .Builder notificationBuilder =
189
- Build .VERSION .SDK_INT >= 26 ? API26Wrapper .getNotificationBuilder (this ) :
190
- new Notification .Builder (this );
200
+ Build .VERSION .SDK_INT >= 26 ? API26Wrapper .getNotificationBuilder (context ) :
201
+ new Notification .Builder (context );
191
202
if (steps > 0 ) {
192
203
if (today_offset == Integer .MIN_VALUE ) today_offset = -steps ;
193
204
notificationBuilder .setProgress (goal , today_offset + steps , false ).setContentText (
194
- today_offset + steps >= goal ? getString (R .string .goal_reached_notification ,
195
- NumberFormat .getInstance (Locale .getDefault ())
196
- .format ((today_offset + steps ))) :
197
- getString (R .string .notification_text ,
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 ,
198
210
NumberFormat .getInstance (Locale .getDefault ())
199
211
.format ((goal - today_offset - steps ))));
200
212
} else { // still no step value?
201
- notificationBuilder
202
- . setContentText ( getString (R .string .your_progress_will_be_shown_here_soon ));
213
+ notificationBuilder . setContentText (
214
+ context . getString (R .string .your_progress_will_be_shown_here_soon ));
203
215
}
204
216
notificationBuilder .setPriority (Notification .PRIORITY_MIN ).setShowWhen (false )
205
- .setContentTitle (getString (R .string .notification_title )).setContentIntent (
206
- PendingIntent .getActivity (this , 0 , new Intent (this , Activity_Main .class ),
217
+ .setContentTitle (context . getString (R .string .notification_title )).setContentIntent (
218
+ PendingIntent .getActivity (context , 0 , new Intent (context , Activity_Main .class ),
207
219
PendingIntent .FLAG_UPDATE_CURRENT )).setSmallIcon (R .drawable .ic_notification )
208
220
.setOngoing (true );
209
221
return notificationBuilder .build ();
@@ -232,15 +244,8 @@ private void reRegisterSensor() {
232
244
Logger .log ("default: " + sm .getDefaultSensor (Sensor .TYPE_STEP_COUNTER ).getName ());
233
245
}
234
246
235
- if (Build .VERSION .SDK_INT >= 27 ) {
236
- // do not use batching on Android P and newer as we dont live long enough to recieve
237
- // those value due to aggressive power saving
238
- sm .registerListener (this , sm .getDefaultSensor (Sensor .TYPE_STEP_COUNTER ),
239
- SensorManager .SENSOR_DELAY_FASTEST );
240
- } else {
241
- // enable batching with delay of max 5 min
242
- sm .registerListener (this , sm .getDefaultSensor (Sensor .TYPE_STEP_COUNTER ),
243
- SensorManager .SENSOR_DELAY_NORMAL , (int ) (5 * MICROSECONDS_IN_ONE_MINUTE ));
244
- }
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 ));
245
250
}
246
251
}
0 commit comments