Skip to content

Commit b006ed2

Browse files
committed
Revert "switch to jobscheduler"
This reverts commit 387fd92.
1 parent 387fd92 commit b006ed2

File tree

7 files changed

+117
-65
lines changed

7 files changed

+117
-65
lines changed

src/main/AndroidManifest.xml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
package="de.j4velin.pedometer"
44
xmlns:android="http://schemas.android.com/apk/res/android"
55
xmlns:tools="http://schemas.android.com/tools"
6-
android:versionCode="157"
7-
android:versionName="1.5.7">
6+
android:versionCode="156"
7+
android:versionName="1.5.6">
88

99
<uses-sdk
10-
android:minSdkVersion="21"
10+
android:maxSdkVersion="27"
11+
android:minSdkVersion="19"
1112
android:targetSdkVersion="26"/>
1213

1314
<uses-feature
@@ -47,8 +48,7 @@
4748
</receiver>
4849

4950
<service
50-
android:name=".SensorListener"
51-
android:permission="android.permission.BIND_JOB_SERVICE"/>
51+
android:name=".SensorListener"/>
5252

5353
<service
5454
android:name=".widget.DashClock"
Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/*
22
* Copyright 2013 Thomas Hoffmann
3-
*
3+
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
7-
*
7+
*
88
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
9+
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
1212
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -16,22 +16,18 @@
1616

1717
package de.j4velin.pedometer;
1818

19-
import android.app.job.JobInfo;
20-
import android.app.job.JobScheduler;
19+
import de.j4velin.pedometer.util.Logger;
2120
import android.content.BroadcastReceiver;
22-
import android.content.ComponentName;
2321
import android.content.Context;
2422
import android.content.Intent;
25-
import android.hardware.Sensor;
26-
27-
import de.j4velin.pedometer.util.Logger;
2823

2924
public class AppUpdatedReceiver extends BroadcastReceiver {
3025

31-
@Override
32-
public void onReceive(final Context context, final Intent intent) {
33-
if (BuildConfig.DEBUG) Logger.log("app updated");
34-
SensorListener.schedulePeriodicJob(context);
35-
}
26+
@Override
27+
public void onReceive(final Context context, final Intent intent) {
28+
if (BuildConfig.DEBUG)
29+
Logger.log("app updated");
30+
context.startService(new Intent(context, SensorListener.class));
31+
}
3632

3733
}

src/main/java/de/j4velin/pedometer/BootReceiver.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,6 @@ public void onReceive(final Context context, final Intent intent) {
4747
db.close();
4848
prefs.edit().remove("correctShutdown").apply();
4949

50-
SensorListener.schedulePeriodicJob(context);
50+
context.startService(new Intent(context, SensorListener.class));
5151
}
5252
}

src/main/java/de/j4velin/pedometer/SensorListener.java

Lines changed: 93 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,25 @@
2020
import android.app.Notification;
2121
import android.app.NotificationManager;
2222
import android.app.PendingIntent;
23-
import android.app.job.JobInfo;
24-
import android.app.job.JobParameters;
25-
import android.app.job.JobScheduler;
26-
import android.app.job.JobService;
27-
import android.content.ComponentName;
23+
import android.app.Service;
24+
import android.content.BroadcastReceiver;
2825
import android.content.Context;
2926
import android.content.Intent;
27+
import android.content.IntentFilter;
3028
import android.content.SharedPreferences;
3129
import android.hardware.Sensor;
3230
import android.hardware.SensorEvent;
3331
import android.hardware.SensorEventListener;
3432
import android.hardware.SensorManager;
33+
import android.os.Build;
34+
import android.os.IBinder;
3535

3636
import java.text.NumberFormat;
3737
import java.util.Date;
3838
import java.util.Locale;
3939

4040
import de.j4velin.pedometer.ui.Activity_Main;
41+
import de.j4velin.pedometer.util.API23Wrapper;
4142
import de.j4velin.pedometer.util.Logger;
4243
import de.j4velin.pedometer.util.Util;
4344
import de.j4velin.pedometer.widget.WidgetUpdateService;
@@ -49,17 +50,18 @@
4950
* This service won't be needed any more if there is a way to read the
5051
* step-value without waiting for a sensor event
5152
*/
52-
public class SensorListener extends JobService implements SensorEventListener {
53+
public class SensorListener extends Service implements SensorEventListener {
5354

5455
private final static int NOTIFICATION_ID = 1;
55-
private final static int JOB_ID = 1;
56+
private final static long MICROSECONDS_IN_ONE_MINUTE = 60000000;
5657
private final static long SAVE_OFFSET_TIME = AlarmManager.INTERVAL_HOUR;
5758
private final static int SAVE_OFFSET_STEPS = 500;
5859

5960
private static int steps;
6061
private static int lastSaveSteps;
6162
private static long lastSaveTime;
62-
private JobParameters jobParameters;
63+
64+
private final BroadcastReceiver shutdownReceiver = new ShutdownRecevier();
6365

6466
public final static String ACTION_UPDATE_NOTIFICATION = "updateNotificationState";
6567

@@ -72,11 +74,12 @@ public void onAccuracyChanged(final Sensor sensor, int accuracy) {
7274

7375
@Override
7476
public void onSensorChanged(final SensorEvent event) {
75-
if (BuildConfig.DEBUG) Logger.log("step count received: "+event.values[0]);
76-
if (event.values[0] < Integer.MAX_VALUE) {
77+
if (event.values[0] > Integer.MAX_VALUE) {
78+
if (BuildConfig.DEBUG) Logger.log("probably not a real value: " + event.values[0]);
79+
return;
80+
} else {
7781
steps = (int) event.values[0];
7882
updateIfNecessary();
79-
jobFinished(jobParameters, false);
8083
}
8184
}
8285

@@ -107,30 +110,68 @@ private void updateIfNecessary() {
107110
}
108111
}
109112

110-
public static void schedulePeriodicJob(final Context context) {
111-
if (BuildConfig.DEBUG) Logger.log("SensorListener schedulePeriodicJob");
112-
ComponentName serviceComponent = new ComponentName(context, SensorListener.class);
113-
JobInfo.Builder builder = new JobInfo.Builder(JOB_ID, serviceComponent);
114-
builder.setPeriodic(AlarmManager.INTERVAL_HOUR);
115-
JobScheduler jobScheduler =
116-
(JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE);
117-
jobScheduler.schedule(builder.build());
113+
@Override
114+
public IBinder onBind(final Intent intent) {
115+
return null;
116+
}
117+
118+
@Override
119+
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();
126+
}
127+
128+
// restart service every hour to save the current step count
129+
long nextUpdate = Math.min(Util.getTomorrow(),
130+
System.currentTimeMillis() + AlarmManager.INTERVAL_HOUR);
131+
if (BuildConfig.DEBUG) Logger.log("next update: " + new Date(nextUpdate).toLocaleString());
132+
AlarmManager am =
133+
(AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
134+
PendingIntent pi = PendingIntent
135+
.getService(getApplicationContext(), 2, new Intent(this, SensorListener.class),
136+
PendingIntent.FLAG_UPDATE_CURRENT);
137+
if (Build.VERSION.SDK_INT >= 23) {
138+
API23Wrapper.setAlarmWhileIdle(am, AlarmManager.RTC, nextUpdate, pi);
139+
} else {
140+
am.set(AlarmManager.RTC, nextUpdate, pi);
141+
}
142+
143+
return START_STICKY;
144+
}
118145

146+
@Override
147+
public void onCreate() {
148+
super.onCreate();
149+
if (BuildConfig.DEBUG) Logger.log("SensorListener onCreate");
150+
reRegisterSensor();
151+
updateNotificationState();
119152
}
120153

121154
@Override
122-
public boolean onStartJob(JobParameters jobParameters) {
123-
if (BuildConfig.DEBUG) Logger.log("SensorListener onStartJob");
124-
this.jobParameters = jobParameters;
125-
registerSensor();
126-
return true; // keep running until receiving a step value
155+
public void onTaskRemoved(final Intent rootIntent) {
156+
super.onTaskRemoved(rootIntent);
157+
if (BuildConfig.DEBUG) Logger.log("sensor service task removed");
158+
// Restart service in 500 ms
159+
((AlarmManager) getSystemService(Context.ALARM_SERVICE))
160+
.set(AlarmManager.RTC, System.currentTimeMillis() + 500, PendingIntent
161+
.getService(this, 3, new Intent(this, SensorListener.class), 0));
127162
}
128163

129164
@Override
130-
public boolean onStopJob(JobParameters jobParameters) {
131-
if (BuildConfig.DEBUG) Logger.log("SensorListener onStopJob");
132-
unregisterSensor();
133-
return false;
165+
public void onDestroy() {
166+
super.onDestroy();
167+
if (BuildConfig.DEBUG) Logger.log("SensorListener onDestroy");
168+
try {
169+
SensorManager sm = (SensorManager) getSystemService(SENSOR_SERVICE);
170+
sm.unregisterListener(this);
171+
} catch (Exception e) {
172+
if (BuildConfig.DEBUG) Logger.log(e);
173+
e.printStackTrace();
174+
}
134175
}
135176

136177
private void updateNotificationState() {
@@ -170,26 +211,38 @@ private void updateNotificationState() {
170211
}
171212
}
172213

173-
private void registerSensor() {
174-
if (BuildConfig.DEBUG) Logger.log("re-register sensor listener");
175-
SensorManager sm = (SensorManager) getSystemService(SENSOR_SERVICE);
176-
if (BuildConfig.DEBUG) {
177-
Logger.log("step sensors: " + sm.getSensorList(Sensor.TYPE_STEP_COUNTER).size());
178-
if (sm.getSensorList(Sensor.TYPE_STEP_COUNTER).size() < 1) return; // emulator
179-
Logger.log("default: " + sm.getDefaultSensor(Sensor.TYPE_STEP_COUNTER).getName());
180-
}
181-
182-
sm.registerListener(this, sm.getDefaultSensor(Sensor.TYPE_STEP_COUNTER),
183-
SensorManager.SENSOR_DELAY_FASTEST);
214+
private void registerBroadcastReceiver() {
215+
if (BuildConfig.DEBUG) Logger.log("register broadcastreceiver");
216+
IntentFilter filter = new IntentFilter();
217+
filter.addAction(Intent.ACTION_SHUTDOWN);
218+
registerReceiver(shutdownReceiver, filter);
184219
}
185220

186-
private void unregisterSensor() {
221+
private void reRegisterSensor() {
222+
if (BuildConfig.DEBUG) Logger.log("re-register sensor listener");
187223
SensorManager sm = (SensorManager) getSystemService(SENSOR_SERVICE);
188224
try {
189225
sm.unregisterListener(this);
190226
} catch (Exception e) {
191227
if (BuildConfig.DEBUG) Logger.log(e);
192228
e.printStackTrace();
193229
}
230+
231+
if (BuildConfig.DEBUG) {
232+
Logger.log("step sensors: " + sm.getSensorList(Sensor.TYPE_STEP_COUNTER).size());
233+
if (sm.getSensorList(Sensor.TYPE_STEP_COUNTER).size() < 1) return; // emulator
234+
Logger.log("default: " + sm.getDefaultSensor(Sensor.TYPE_STEP_COUNTER).getName());
235+
}
236+
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+
}
194247
}
195248
}

src/main/java/de/j4velin/pedometer/ShutdownRecevier.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ public class ShutdownRecevier extends BroadcastReceiver {
2929
public void onReceive(final Context context, final Intent intent) {
3030
if (BuildConfig.DEBUG) Logger.log("shutting down");
3131

32+
context.startService(new Intent(context, SensorListener.class));
33+
3234
// if the user used a root script for shutdown, the DEVICE_SHUTDOWN
3335
// broadcast might not be send. Therefore, the app will check this
3436
// setting on the next boot and displays an error message if it's not

src/main/java/de/j4velin/pedometer/ui/Fragment_Overview.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@
5050
import de.j4velin.pedometer.BuildConfig;
5151
import de.j4velin.pedometer.Database;
5252
import de.j4velin.pedometer.R;
53-
import de.j4velin.pedometer.SensorListener;
5453
import de.j4velin.pedometer.util.Logger;
5554
import de.j4velin.pedometer.util.Util;
5655

@@ -68,7 +67,6 @@ public class Fragment_Overview extends Fragment implements SensorEventListener {
6867
public void onCreate(final Bundle savedInstanceState) {
6968
super.onCreate(savedInstanceState);
7069
setHasOptionsMenu(true);
71-
SensorListener.schedulePeriodicJob(getActivity());
7270
}
7371

7472
@Override

src/main/java/de/j4velin/pedometer/util/API23Wrapper.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@
2020
import android.app.Activity;
2121
import android.app.AlarmManager;
2222
import android.app.PendingIntent;
23-
import android.app.job.JobInfo;
24-
import android.app.job.JobScheduler;
25-
import android.content.Context;
2623
import android.os.Build;
2724

2825
@TargetApi(Build.VERSION_CODES.M)
@@ -31,4 +28,10 @@ public class API23Wrapper {
3128
public static void requestPermission(final Activity a, final String[] permissions) {
3229
a.requestPermissions(permissions, 42);
3330
}
31+
32+
public static void setAlarmWhileIdle(AlarmManager am, int type, long time,
33+
PendingIntent intent) {
34+
am.setAndAllowWhileIdle(type, time, intent);
35+
}
36+
3437
}

0 commit comments

Comments
 (0)