basic onStopJob() method for UpdateJobService

If the scheduler decides it should cancel a job, this will attempt to shut
down UpdateService.
This commit is contained in:
Hans-Christoph Steiner 2018-07-13 17:42:21 +02:00
parent a912eebe5b
commit 01abcc2f4d
2 changed files with 14 additions and 7 deletions

View File

@ -20,7 +20,7 @@ public class UpdateJobService extends JobService {
@Override @Override
public boolean onStopJob(JobParameters params) { public boolean onStopJob(JobParameters params) {
// TODO this should gracefully stop UpdateService UpdateService.stopNow(this);
return true; return true;
} }
} }

View File

@ -93,8 +93,6 @@ public class UpdateService extends JobIntentService {
private NotificationCompat.Builder notificationBuilder; private NotificationCompat.Builder notificationBuilder;
private AppUpdateStatusManager appUpdateStatusManager; private AppUpdateStatusManager appUpdateStatusManager;
private static boolean updating;
public static void updateNow(Context context) { public static void updateNow(Context context) {
updateRepoNow(context, null); updateRepoNow(context, null);
} }
@ -184,7 +182,7 @@ public class UpdateService extends JobIntentService {
* the app to users, so they know something is happening. * the app to users, so they know something is happening.
*/ */
public static boolean isUpdating() { public static boolean isUpdating() {
return updating; return updateService != null;
} }
private static volatile boolean isScheduleIfStillOnWifiRunning; private static volatile boolean isScheduleIfStillOnWifiRunning;
@ -230,11 +228,22 @@ public class UpdateService extends JobIntentService {
isScheduleIfStillOnWifiRunning = false; isScheduleIfStillOnWifiRunning = false;
return null; return null;
} }
}
private static UpdateService updateService;
public static void stopNow(Context context) {
if (updateService != null) {
updateService.stopSelf(JOB_ID);
updateService = null;
}
} }
@Override @Override
public void onCreate() { public void onCreate() {
super.onCreate(); super.onCreate();
updateService = this;
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
@ -263,6 +272,7 @@ public class UpdateService extends JobIntentService {
super.onDestroy(); super.onDestroy();
notificationManager.cancel(NOTIFY_ID_UPDATING); notificationManager.cancel(NOTIFY_ID_UPDATING);
LocalBroadcastManager.getInstance(this).unregisterReceiver(updateStatusReceiver); LocalBroadcastManager.getInstance(this).unregisterReceiver(updateStatusReceiver);
updateService = null;
} }
public static void sendStatus(Context context, int statusCode) { public static void sendStatus(Context context, int statusCode) {
@ -409,7 +419,6 @@ public class UpdateService extends JobIntentService {
return; return;
} }
updating = true;
setNotification(); setNotification();
LocalBroadcastManager.getInstance(this).registerReceiver(updateStatusReceiver, LocalBroadcastManager.getInstance(this).registerReceiver(updateStatusReceiver,
new IntentFilter(LOCAL_ACTION_STATUS)); new IntentFilter(LOCAL_ACTION_STATUS));
@ -498,8 +507,6 @@ public class UpdateService extends JobIntentService {
} catch (Exception e) { } catch (Exception e) {
Log.e(TAG, "Exception during update processing", e); Log.e(TAG, "Exception during update processing", e);
sendStatus(this, STATUS_ERROR_GLOBAL, e.getMessage()); sendStatus(this, STATUS_ERROR_GLOBAL, e.getMessage());
} finally {
updating = false;
} }
long time = System.currentTimeMillis() - startTime; long time = System.currentTimeMillis() - startTime;