1818
1919import  android .app .AlarmManager ;
2020import  android .app .Notification ;
21+ import  android .app .NotificationManager ;
2122import  android .app .PendingIntent ;
2223import  android .app .Service ;
2324import  android .content .BroadcastReceiver ;
5253 */ 
5354public  class  SensorListener  extends  Service  implements  SensorEventListener  {
5455
55-     private  final  static  int  NOTIFICATION_ID  = 1 ;
56+     public  final  static  int  NOTIFICATION_ID  = 1 ;
5657    private  final  static  long  MICROSECONDS_IN_ONE_MINUTE  = 60000000 ;
5758    private  final  static  long  SAVE_OFFSET_TIME  = AlarmManager .INTERVAL_HOUR ;
5859    private  final  static  int  SAVE_OFFSET_STEPS  = 500 ;
@@ -63,8 +64,6 @@ public class SensorListener extends Service implements SensorEventListener {
6364
6465    private  final  BroadcastReceiver  shutdownReceiver  = new  ShutdownRecevier ();
6566
66-     public  final  static  String  ACTION_UPDATE_NOTIFICATION  = "updateNotificationState" ;
67- 
6867    @ Override 
6968    public  void  onAccuracyChanged (final  Sensor  sensor , int  accuracy ) {
7069        // nobody knows what happens here: step value might magically decrease 
@@ -108,14 +107,26 @@ private boolean updateIfNecessary() {
108107            db .close ();
109108            lastSaveSteps  = steps ;
110109            lastSaveTime  = System .currentTimeMillis ();
111-             startForeground ( NOTIFICATION_ID ,  getNotification () ); // update notification 
110+             showNotification ( ); // update notification 
112111            startService (new  Intent (this , WidgetUpdateService .class ));
113112            return  true ;
114113        } else  {
115114            return  false ;
116115        }
117116    }
118117
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+ 
119130    @ Override 
120131    public  IBinder  onBind (final  Intent  intent ) {
121132        return  null ;
@@ -126,7 +137,7 @@ public int onStartCommand(final Intent intent, int flags, int startId) {
126137        reRegisterSensor ();
127138        registerBroadcastReceiver ();
128139        if  (!updateIfNecessary ()) {
129-             startForeground ( NOTIFICATION_ID ,  getNotification () );
140+             showNotification ( );
130141        }
131142
132143        // restart service every hour to save the current step count 
@@ -176,34 +187,35 @@ public void onDestroy() {
176187        }
177188    }
178189
179-     private   Notification  getNotification () {
190+     public   static   Notification  getNotification (final   Context   context ) {
180191        if  (BuildConfig .DEBUG ) Logger .log ("getNotification" );
181-         SharedPreferences  prefs  = getSharedPreferences ("pedometer" , Context .MODE_PRIVATE );
192+         SharedPreferences  prefs  = context . getSharedPreferences ("pedometer" , Context .MODE_PRIVATE );
182193        int  goal  = prefs .getInt ("goal" , 10000 );
183-         Database  db  = Database .getInstance (this );
194+         Database  db  = Database .getInstance (context );
184195        int  today_offset  = db .getSteps (Util .getToday ());
185196        if  (steps  == 0 )
186197            steps  = db .getCurrentSteps (); // use saved value if we haven't anything better 
187198        db .close ();
188199        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 );
191202        if  (steps  > 0 ) {
192203            if  (today_offset  == Integer .MIN_VALUE ) today_offset  = -steps ;
193204            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 ,
198210                                    NumberFormat .getInstance (Locale .getDefault ())
199211                                            .format ((goal  - today_offset  - steps ))));
200212        } 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 ));
203215        }
204216        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 ),
207219                        PendingIntent .FLAG_UPDATE_CURRENT )).setSmallIcon (R .drawable .ic_notification )
208220                .setOngoing (true );
209221        return  notificationBuilder .build ();
@@ -232,15 +244,8 @@ private void reRegisterSensor() {
232244            Logger .log ("default: "  + sm .getDefaultSensor (Sensor .TYPE_STEP_COUNTER ).getName ());
233245        }
234246
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 ));
245250    }
246251}
0 commit comments