[RESTRICT AUTOMERGE] Protect Context#startService from BackgroundServiceStartNotAllowedException
We wrap the Context#startService calls in a try-catch block to avoid
the possibility of an exception due to background start. This can
happen if the activity is started when the screen is locked.
No need to add a try-catch block to Context#stopService, because
according to the documentation, if the service was not started,
nothing will happen (it won't throw anything).
Test: manually provisioned to work and device owner during and after
setup wizard with and without a background process limit
Fixes: 197412962
Change-Id: I5335b9f0e774fd950a091e98d144f546a7610dd8
(cherry picked from commit 233976ff2177a96f9e149a0d82ebc2e4d1bd5a92)
diff --git a/src/com/android/managedprovisioning/finalization/FinalizationActivityBase.java b/src/com/android/managedprovisioning/finalization/FinalizationActivityBase.java
index fd884ad..57009dc 100644
--- a/src/com/android/managedprovisioning/finalization/FinalizationActivityBase.java
+++ b/src/com/android/managedprovisioning/finalization/FinalizationActivityBase.java
@@ -23,6 +23,7 @@
import static com.android.managedprovisioning.provisioning.Constants.PROVISIONING_SERVICE_INTENT;
import android.app.Activity;
+import android.app.BackgroundServiceStartNotAllowedException;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
@@ -34,6 +35,7 @@
import android.os.UserManager;
import com.android.managedprovisioning.common.Globals;
+import com.android.managedprovisioning.common.ProvisionLogger;
import com.android.managedprovisioning.common.TransitionHelper;
import com.android.managedprovisioning.provisioning.ProvisioningService;
@@ -85,9 +87,7 @@
super.onCreate(savedInstanceState);
mFinalizationController = createFinalizationController();
- if (savedInstanceState == null) {
- getApplicationContext().startService(PROVISIONING_SERVICE_INTENT);
- } else {
+ if (savedInstanceState != null) {
final Bundle controllerState = savedInstanceState.getBundle(CONTROLLER_STATE_KEY);
if (controllerState != null) {
mFinalizationController.restoreInstanceState(controllerState);
@@ -102,6 +102,16 @@
tryFinalizeProvisioning();
}
+ @Override
+ protected void onStart() {
+ super.onStart();
+ try {
+ getApplicationContext().startService(PROVISIONING_SERVICE_INTENT);
+ } catch (BackgroundServiceStartNotAllowedException e) {
+ ProvisionLogger.loge(e);
+ }
+ }
+
protected TransitionHelper getTransitionHelper() {
return mTransitionHelper;
}
diff --git a/src/com/android/managedprovisioning/preprovisioning/PreProvisioningActivity.java b/src/com/android/managedprovisioning/preprovisioning/PreProvisioningActivity.java
index 0114437..baf1fce 100644
--- a/src/com/android/managedprovisioning/preprovisioning/PreProvisioningActivity.java
+++ b/src/com/android/managedprovisioning/preprovisioning/PreProvisioningActivity.java
@@ -28,6 +28,7 @@
import static com.google.android.setupcompat.util.WizardManagerHelper.EXTRA_IS_SETUP_FLOW;
import android.app.Activity;
+import android.app.BackgroundServiceStartNotAllowedException;
import android.app.DialogFragment;
import android.content.ComponentName;
import android.content.Intent;
@@ -120,9 +121,6 @@
@Override
protected void onCreate(Bundle savedInstanceState) {
- if (savedInstanceState == null) {
- getApplicationContext().startService(PROVISIONING_SERVICE_INTENT);
- }
// TODO(b/192074477): Remove deferred setup-specific logic after the managed account flow
// starts ManagedProvisioning with the isSetupFlow extra
// TODO(b/178822333): Remove NFC-specific logic after adding support for the
@@ -139,6 +137,16 @@
logMetrics();
}
+ @Override
+ protected void onStart() {
+ super.onStart();
+ try {
+ getApplicationContext().startService(PROVISIONING_SERVICE_INTENT);
+ } catch (BackgroundServiceStartNotAllowedException e) {
+ ProvisionLogger.loge(e);
+ }
+ }
+
private boolean isNfcSetup() {
return ACTION_NDEF_DISCOVERED.equals(getIntent().getAction());
}