diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index b40188688c..aeeb53475d 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -153,8 +153,7 @@ + android:exported="false" /> @@ -173,7 +172,7 @@ + android:exported="true" /> @@ -240,11 +239,11 @@ + android:exported="true" /> + android:exported="true" /> @@ -285,7 +284,7 @@ + android:exported="true" /> 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 95fd6bfb09..6c2e7c6920 100644 --- a/app/src/main/java/info/nightscout/androidaps/receivers/DataReceiver.java +++ b/app/src/main/java/info/nightscout/androidaps/receivers/DataReceiver.java @@ -1,9 +1,8 @@ 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; @@ -11,16 +10,15 @@ import org.slf4j.LoggerFactory; import info.nightscout.androidaps.logging.L; import info.nightscout.androidaps.services.DataService; -public class DataReceiver extends BroadcastReceiver { +public class DataReceiver extends WakefulBroadcastReceiver { 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); - // Explicitly specify that GcmIntentService will handle the intent. - ComponentName comp = new ComponentName(context.getPackageName(), - DataService.class.getName()); - DataService.enqueueWork(context, intent.setComponent(comp)); + startWakefulService(context, new Intent(context, DataService.class) + .setAction(intent.getAction()) + .putExtras(intent)); } } 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 24a7698e9a..6b8ac46f11 100644 --- a/app/src/main/java/info/nightscout/androidaps/services/DataService.java +++ b/app/src/main/java/info/nightscout/androidaps/services/DataService.java @@ -1,13 +1,10 @@ package info.nightscout.androidaps.services; -import android.content.Context; +import android.app.IntentService; 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; @@ -19,16 +16,15 @@ 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; @@ -38,30 +34,21 @@ 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 JobIntentService { +public class DataService extends IntentService { private Logger log = LoggerFactory.getLogger(L.DATASERVICE); - // 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); + public DataService() { + super("DataService"); + registerBus(); } @Override - public void onDestroy() { - super.onDestroy(); - if (L.isEnabled(L.DATASERVICE)) - log.debug("All work complete"); - } - - @Override - protected void onHandleWork(@NonNull Intent intent) { + protected void onHandleIntent(final Intent intent) { if (L.isEnabled(L.DATASERVICE)) { log.debug("onHandleIntent " + intent); log.debug("onHandleIntent " + BundleLogger.log(intent.getExtras())); @@ -113,7 +100,7 @@ public class DataService extends JobIntentService { 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); @@ -121,6 +108,22 @@ public class DataService extends JobIntentService { 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) {