From 709e835ad83ece86e8a86641922b54543d9764b4 Mon Sep 17 00:00:00 2001 From: Anselm Binninger Date: Sun, 12 Nov 2017 23:00:50 +0100 Subject: [PATCH] Fix ISE in StartSyncReceiver (#580) --- app/src/main/AndroidManifest.xml | 6 +++++ .../de/tum/in/tumcampusapp/auxiliary/Const.kt | 7 ++++++ .../services/BackgroundService.java | 24 +++++++++++-------- .../services/DownloadService.java | 23 +++++++++--------- .../services/FillCacheService.java | 20 +++++++++------- .../services/SendMessageService.java | 18 +++++++------- .../services/SendWifiMeasurementService.java | 16 ++++++++----- .../tumcampusapp/services/SilenceService.java | 20 ++++++++-------- .../services/StartSyncReceiver.java | 12 +++++----- 9 files changed, 84 insertions(+), 62 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index c511669a82..803243a9d8 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -619,21 +619,27 @@ download(intent, DownloadService.this)).start(); } diff --git a/app/src/main/java/de/tum/in/tumcampusapp/services/FillCacheService.java b/app/src/main/java/de/tum/in/tumcampusapp/services/FillCacheService.java index eebf5b1459..9e37198c4f 100644 --- a/app/src/main/java/de/tum/in/tumcampusapp/services/FillCacheService.java +++ b/app/src/main/java/de/tum/in/tumcampusapp/services/FillCacheService.java @@ -1,21 +1,19 @@ package de.tum.in.tumcampusapp.services; -import android.app.IntentService; +import android.content.Context; import android.content.Intent; +import android.support.annotation.NonNull; +import android.support.v4.app.JobIntentService; import de.tum.in.tumcampusapp.auxiliary.Utils; import de.tum.in.tumcampusapp.managers.CacheManager; +import static de.tum.in.tumcampusapp.auxiliary.Const.FILL_CACHE_SERVICE_JOB_ID; + /** * Service used to fill caches in background, for faster/offline access **/ -public class FillCacheService extends IntentService { - - private static final String CACHE_SERVICE = "FillCacheService"; - - public FillCacheService() { - super(CACHE_SERVICE); - } +public class FillCacheService extends JobIntentService { @Override public void onCreate() { @@ -29,8 +27,12 @@ public void onDestroy() { Utils.logv("FillCacheService has stopped"); } + static void enqueueWork(Context context, Intent work) { + enqueueWork(context, FillCacheService.class, FILL_CACHE_SERVICE_JOB_ID, work); + } + @Override - protected void onHandleIntent(Intent intent) { + protected void onHandleWork(@NonNull Intent intent) { new Thread(() -> { // Fill cache service CacheManager cache = new CacheManager(FillCacheService.this); diff --git a/app/src/main/java/de/tum/in/tumcampusapp/services/SendMessageService.java b/app/src/main/java/de/tum/in/tumcampusapp/services/SendMessageService.java index e817e596ce..5a859d75ec 100644 --- a/app/src/main/java/de/tum/in/tumcampusapp/services/SendMessageService.java +++ b/app/src/main/java/de/tum/in/tumcampusapp/services/SendMessageService.java @@ -1,8 +1,10 @@ package de.tum.in.tumcampusapp.services; -import android.app.IntentService; +import android.content.Context; import android.content.Intent; import android.os.Bundle; +import android.support.annotation.NonNull; +import android.support.v4.app.JobIntentService; import android.support.v4.content.LocalBroadcastManager; import java.io.IOException; @@ -16,26 +18,24 @@ import de.tum.in.tumcampusapp.models.gcm.GCMChat; import de.tum.in.tumcampusapp.models.tumcabe.ChatMessage; +import static de.tum.in.tumcampusapp.auxiliary.Const.SEND_MESSAGE_SERVICE_JOB_ID; + /** * Service used to silence the mobile during lectures */ -public class SendMessageService extends IntentService { +public class SendMessageService extends JobIntentService { public static final int MAX_SEND_TRIES = 5; /** * Interval in milliseconds to check for current lectures */ - private static final String SEND_MESSAGE_SERVICE = "SendMessageService"; - /** - * default init (run intent in new thread) - */ - public SendMessageService() { - super(SEND_MESSAGE_SERVICE); + static void enqueueWork(Context context, Intent work) { + enqueueWork(context, SendMessageService.class, SEND_MESSAGE_SERVICE_JOB_ID, work); } @Override - protected void onHandleIntent(Intent intent) { + protected void onHandleWork(@NonNull Intent intent) { // Get all unsent messages from database List unsentMsg = ChatMessageManager.getAllUnsentUpdated(this); if (unsentMsg.isEmpty()) { diff --git a/app/src/main/java/de/tum/in/tumcampusapp/services/SendWifiMeasurementService.java b/app/src/main/java/de/tum/in/tumcampusapp/services/SendWifiMeasurementService.java index f2d731b2f7..a500c3f50c 100644 --- a/app/src/main/java/de/tum/in/tumcampusapp/services/SendWifiMeasurementService.java +++ b/app/src/main/java/de/tum/in/tumcampusapp/services/SendWifiMeasurementService.java @@ -1,28 +1,32 @@ package de.tum.in.tumcampusapp.services; -import android.app.IntentService; +import android.content.Context; import android.content.Intent; +import android.support.annotation.NonNull; +import android.support.v4.app.JobIntentService; import de.tum.in.tumcampusapp.managers.WifiMeasurementManager; +import static de.tum.in.tumcampusapp.auxiliary.Const.SEND_WIFI_SERVICE_JOB_ID; + /** * This service is getting used by StartSyncReceiver to send the accumulated WifiMeasurements * every X hours */ -public class SendWifiMeasurementService extends IntentService { +public class SendWifiMeasurementService extends JobIntentService { //Maximum retries for sending the wifi-measurement list to the server public static final int MAX_SEND_TRIES = 3; //Time between retries for trying again public static final int TIME_BETWEEN_MILIS = 300; - private static final String SEND_WIFI_MEASUREMENT_SERVICE = "SendWifiMeasurementService"; - public SendWifiMeasurementService() { - super(SEND_WIFI_MEASUREMENT_SERVICE); + + static void enqueueWork(Context context, Intent work) { + enqueueWork(context, SendWifiMeasurementService.class, SEND_WIFI_SERVICE_JOB_ID, work); } @Override - protected void onHandleIntent(Intent intent) { + protected void onHandleWork(@NonNull Intent intent) { WifiMeasurementManager wm = new WifiMeasurementManager(this); wm.uploadMeasurementsToRemote(MAX_SEND_TRIES, TIME_BETWEEN_MILIS); } diff --git a/app/src/main/java/de/tum/in/tumcampusapp/services/SilenceService.java b/app/src/main/java/de/tum/in/tumcampusapp/services/SilenceService.java index b9977455bc..7479e4e936 100644 --- a/app/src/main/java/de/tum/in/tumcampusapp/services/SilenceService.java +++ b/app/src/main/java/de/tum/in/tumcampusapp/services/SilenceService.java @@ -1,7 +1,6 @@ package de.tum.in.tumcampusapp.services; import android.app.AlarmManager; -import android.app.IntentService; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; @@ -10,6 +9,8 @@ import android.media.AudioManager; import android.os.Build; import android.provider.Settings; +import android.support.annotation.NonNull; +import android.support.v4.app.JobIntentService; import java.text.ParseException; import java.text.SimpleDateFormat; @@ -19,24 +20,19 @@ import de.tum.in.tumcampusapp.auxiliary.Utils; import de.tum.in.tumcampusapp.managers.CalendarManager; +import static de.tum.in.tumcampusapp.auxiliary.Const.SILENCE_SERVICE_JOB_ID; + /** * Service used to silence the mobile during lectures */ -public class SilenceService extends IntentService { +public class SilenceService extends JobIntentService { /** * Interval in milliseconds to check for current lectures */ private static final int CHECK_INTERVAL = 60000 * 15; // 15 Minutes private static final int CHECK_DELAY = 10000; // 10 Seconds after Calendar changed - private static final String SILENCE_SERVICE = "SilenceService"; - /** - * default init (run intent in new thread) - */ - public SilenceService() { - super(SILENCE_SERVICE); - } private static long getWaitDuration(String timeToEventString) { long timeToEvent = Long.MAX_VALUE; @@ -61,8 +57,12 @@ public void onDestroy() { Utils.log("SilenceService has stopped"); } + static void enqueueWork(Context context, Intent work) { + enqueueWork(context, SilenceService.class, SILENCE_SERVICE_JOB_ID, work); + } + @Override - protected void onHandleIntent(Intent intent) { + protected void onHandleWork(@NonNull Intent intent) { //Abort, if the settings changed if (!Utils.getSettingBool(this, Const.SILENCE_SERVICE, false)) { // Don't schedule a new run, since the service is disabled diff --git a/app/src/main/java/de/tum/in/tumcampusapp/services/StartSyncReceiver.java b/app/src/main/java/de/tum/in/tumcampusapp/services/StartSyncReceiver.java index e3843fa6f1..32c147fdda 100644 --- a/app/src/main/java/de/tum/in/tumcampusapp/services/StartSyncReceiver.java +++ b/app/src/main/java/de/tum/in/tumcampusapp/services/StartSyncReceiver.java @@ -7,6 +7,7 @@ import android.content.Context; import android.content.Intent; import android.os.Build; +import android.support.v4.app.JobIntentService; import de.tum.in.tumcampusapp.auxiliary.Const; import de.tum.in.tumcampusapp.auxiliary.Utils; @@ -51,17 +52,16 @@ public void onReceive(Context context, Intent intent) { // Start BackgroundService if (launch || backgroundServicePermitted) { Utils.logv("Start background service..."); - Intent i = new Intent(context, BackgroundService.class); + Intent i = new Intent(); i.putExtra(Const.APP_LAUNCHES, launch); - context.startService(i); + BackgroundService.enqueueWork(context, i); } - - context.startService(new Intent(context, SendMessageService.class)); + SendMessageService.enqueueWork(context,new Intent()); // Also start the SilenceService. It checks if it is enabled, so we don't need to - context.startService(new Intent(context, SilenceService.class)); + SilenceService.enqueueWork(context,new Intent()); if (intent.getAction() != "android.net.wifi.WIFI_STATE_CHANGED" && Utils.getInternalSettingBool(context, WifiMeasurementManager.WIFI_SCANS_ALLOWED, false)) { - context.startService(new Intent(context, SendWifiMeasurementService.class)); + SendWifiMeasurementService.enqueueWork(context,new Intent()); } } } \ No newline at end of file