diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 880752f3a8..ed457c7e69 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -161,7 +161,8 @@ + android:exported="false" + android:permission="android.permission.BIND_JOB_SERVICE"/> @@ -180,7 +181,7 @@ + android:exported="false" /> @@ -247,11 +248,11 @@ + android:exported="false" /> + android:exported="false" /> @@ -292,7 +293,7 @@ + android:exported="false" /> diff --git a/app/src/main/java/info/nightscout/androidaps/receivers/DataReceiver.java b/app/src/main/java/info/nightscout/androidaps/receivers/DataReceiver.java index 6c2e7c6920..95fd6bfb09 100644 --- a/app/src/main/java/info/nightscout/androidaps/receivers/DataReceiver.java +++ b/app/src/main/java/info/nightscout/androidaps/receivers/DataReceiver.java @@ -1,8 +1,9 @@ package info.nightscout.androidaps.receivers; +import android.content.BroadcastReceiver; +import android.content.ComponentName; import android.content.Context; import android.content.Intent; -import androidx.legacy.content.WakefulBroadcastReceiver; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -10,15 +11,16 @@ import org.slf4j.LoggerFactory; import info.nightscout.androidaps.logging.L; import info.nightscout.androidaps.services.DataService; -public class DataReceiver extends WakefulBroadcastReceiver { +public class DataReceiver extends BroadcastReceiver { private static Logger log = LoggerFactory.getLogger(L.DATASERVICE); @Override public void onReceive(Context context, Intent intent) { if (L.isEnabled(L.DATASERVICE)) log.debug("onReceive " + intent); - startWakefulService(context, new Intent(context, DataService.class) - .setAction(intent.getAction()) - .putExtras(intent)); + // Explicitly specify that GcmIntentService will handle the intent. + ComponentName comp = new ComponentName(context.getPackageName(), + DataService.class.getName()); + DataService.enqueueWork(context, intent.setComponent(comp)); } } diff --git a/app/src/main/java/info/nightscout/androidaps/services/DataService.java b/app/src/main/java/info/nightscout/androidaps/services/DataService.java index 6b8ac46f11..24a7698e9a 100644 --- a/app/src/main/java/info/nightscout/androidaps/services/DataService.java +++ b/app/src/main/java/info/nightscout/androidaps/services/DataService.java @@ -1,10 +1,13 @@ package info.nightscout.androidaps.services; -import android.app.IntentService; +import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.provider.Telephony; +import androidx.annotation.NonNull; +import androidx.core.app.JobIntentService; + import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; @@ -16,15 +19,16 @@ import info.nightscout.androidaps.R; import info.nightscout.androidaps.db.CareportalEvent; import info.nightscout.androidaps.events.EventNsFood; import info.nightscout.androidaps.events.EventNsTreatment; +import info.nightscout.androidaps.logging.BundleLogger; import info.nightscout.androidaps.logging.L; import info.nightscout.androidaps.plugins.general.nsclient.data.NSDeviceStatus; import info.nightscout.androidaps.plugins.general.nsclient.data.NSMbg; import info.nightscout.androidaps.plugins.general.nsclient.data.NSSettingsStatus; import info.nightscout.androidaps.plugins.general.overview.events.EventNewNotification; import info.nightscout.androidaps.plugins.general.overview.notifications.Notification; +import info.nightscout.androidaps.plugins.general.smsCommunicator.SmsCommunicatorPlugin; import info.nightscout.androidaps.plugins.profile.ns.NSProfilePlugin; import info.nightscout.androidaps.plugins.pump.danaR.activities.DanaRNSHistorySync; -import info.nightscout.androidaps.plugins.general.smsCommunicator.SmsCommunicatorPlugin; import info.nightscout.androidaps.plugins.source.SourceDexcomPlugin; import info.nightscout.androidaps.plugins.source.SourceEversensePlugin; import info.nightscout.androidaps.plugins.source.SourceGlimpPlugin; @@ -34,21 +38,30 @@ import info.nightscout.androidaps.plugins.source.SourcePoctechPlugin; import info.nightscout.androidaps.plugins.source.SourceTomatoPlugin; import info.nightscout.androidaps.plugins.source.SourceXdripPlugin; import info.nightscout.androidaps.receivers.DataReceiver; -import info.nightscout.androidaps.logging.BundleLogger; import info.nightscout.androidaps.utils.JsonHelper; import info.nightscout.androidaps.utils.SP; -public class DataService extends IntentService { +public class DataService extends JobIntentService { private Logger log = LoggerFactory.getLogger(L.DATASERVICE); - public DataService() { - super("DataService"); - registerBus(); + // Service unique ID + static final int SERVICE_JOB_ID = 4378; + + // Enqueuing work in to this service. + public static void enqueueWork(Context context, Intent work) { + enqueueWork(context, DataService.class, SERVICE_JOB_ID, work); } @Override - protected void onHandleIntent(final Intent intent) { + public void onDestroy() { + super.onDestroy(); + if (L.isEnabled(L.DATASERVICE)) + log.debug("All work complete"); + } + + @Override + protected void onHandleWork(@NonNull Intent intent) { if (L.isEnabled(L.DATASERVICE)) { log.debug("onHandleIntent " + intent); log.debug("onHandleIntent " + BundleLogger.log(intent.getExtras())); @@ -100,7 +113,7 @@ public class DataService extends IntentService { Intents.ACTION_REMOVED_TREATMENT.equals(action) || Intents.ACTION_NEW_CAL.equals(action) || Intents.ACTION_NEW_MBG.equals(action)) - ) { + ) { handleNewDataFromNSClient(intent); } else if (Telephony.Sms.Intents.SMS_RECEIVED_ACTION.equals(action)) { SmsCommunicatorPlugin.getPlugin().handleNewData(intent); @@ -108,22 +121,6 @@ public class DataService extends IntentService { if (L.isEnabled(L.DATASERVICE)) log.debug("onHandleIntent exit " + intent); - DataReceiver.completeWakefulIntent(intent); - } - - @Override - public void onDestroy() { - super.onDestroy(); - MainApp.bus().unregister(this); - } - - private void registerBus() { - try { - MainApp.bus().unregister(this); - } catch (RuntimeException x) { - // Ignore - } - MainApp.bus().register(this); } private void handleNewDataFromNSClient(Intent intent) {