From 01abcc2f4dce2435bab6946a662cbbd83a89753c Mon Sep 17 00:00:00 2001 From: Hans-Christoph Steiner Date: Fri, 13 Jul 2018 17:42:21 +0200 Subject: [PATCH] basic onStopJob() method for UpdateJobService If the scheduler decides it should cancel a job, this will attempt to shut down UpdateService. --- .../org/fdroid/fdroid/UpdateJobService.java | 2 +- .../java/org/fdroid/fdroid/UpdateService.java | 19 +++++++++++++------ 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/org/fdroid/fdroid/UpdateJobService.java b/app/src/main/java/org/fdroid/fdroid/UpdateJobService.java index 6911584b4..88ed0b674 100644 --- a/app/src/main/java/org/fdroid/fdroid/UpdateJobService.java +++ b/app/src/main/java/org/fdroid/fdroid/UpdateJobService.java @@ -20,7 +20,7 @@ public class UpdateJobService extends JobService { @Override public boolean onStopJob(JobParameters params) { - // TODO this should gracefully stop UpdateService + UpdateService.stopNow(this); return true; } } diff --git a/app/src/main/java/org/fdroid/fdroid/UpdateService.java b/app/src/main/java/org/fdroid/fdroid/UpdateService.java index 8f2bd52da..209de56b3 100644 --- a/app/src/main/java/org/fdroid/fdroid/UpdateService.java +++ b/app/src/main/java/org/fdroid/fdroid/UpdateService.java @@ -93,8 +93,6 @@ public class UpdateService extends JobIntentService { private NotificationCompat.Builder notificationBuilder; private AppUpdateStatusManager appUpdateStatusManager; - private static boolean updating; - public static void updateNow(Context context) { updateRepoNow(context, null); } @@ -184,7 +182,7 @@ public class UpdateService extends JobIntentService { * the app to users, so they know something is happening. */ public static boolean isUpdating() { - return updating; + return updateService != null; } private static volatile boolean isScheduleIfStillOnWifiRunning; @@ -230,11 +228,22 @@ public class UpdateService extends JobIntentService { isScheduleIfStillOnWifiRunning = false; return null; } + + } + + private static UpdateService updateService; + + public static void stopNow(Context context) { + if (updateService != null) { + updateService.stopSelf(JOB_ID); + updateService = null; + } } @Override public void onCreate() { super.onCreate(); + updateService = this; notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); @@ -263,6 +272,7 @@ public class UpdateService extends JobIntentService { super.onDestroy(); notificationManager.cancel(NOTIFY_ID_UPDATING); LocalBroadcastManager.getInstance(this).unregisterReceiver(updateStatusReceiver); + updateService = null; } public static void sendStatus(Context context, int statusCode) { @@ -409,7 +419,6 @@ public class UpdateService extends JobIntentService { return; } - updating = true; setNotification(); LocalBroadcastManager.getInstance(this).registerReceiver(updateStatusReceiver, new IntentFilter(LOCAL_ACTION_STATUS)); @@ -498,8 +507,6 @@ public class UpdateService extends JobIntentService { } catch (Exception e) { Log.e(TAG, "Exception during update processing", e); sendStatus(this, STATUS_ERROR_GLOBAL, e.getMessage()); - } finally { - updating = false; } long time = System.currentTimeMillis() - startTime;