Rework activity launching and result handling
* Rename or eliminate methods that could be easily confused for AOSP
methods due to the reuse of AOSP names.
* Separate out the handling of results for subactivites and for the
next action (intent), each using its own activity launcher and
callback.
* Add a StartDecoratedActivityForResult contract to ease the launching
of activities without the caller needing to add SUW-related extras.
Change-Id: Iffaba4c51b2c90c42b8b243874a62cdea9e0c793
diff --git a/src/org/lineageos/setupwizard/BaseSetupWizardActivity.java b/src/org/lineageos/setupwizard/BaseSetupWizardActivity.java
index 5dc3afe..04e07d2 100644
--- a/src/org/lineageos/setupwizard/BaseSetupWizardActivity.java
+++ b/src/org/lineageos/setupwizard/BaseSetupWizardActivity.java
@@ -8,12 +8,15 @@
import static android.view.View.INVISIBLE;
+import static androidx.activity.result.contract.ActivityResultContracts.StartActivityForResult;
+
import static com.google.android.setupcompat.util.ResultCodes.RESULT_SKIP;
import static org.lineageos.setupwizard.SetupWizardApp.LOGV;
import android.annotation.NonNull;
import android.annotation.Nullable;
+import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.wifi.WifiManager;
@@ -26,7 +29,7 @@
import androidx.activity.OnBackPressedCallback;
import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultLauncher;
-import androidx.activity.result.contract.ActivityResultContracts;
+import androidx.activity.result.contract.ActivityResultContract;
import androidx.appcompat.app.AppCompatActivity;
import com.android.settingslib.Utils;
@@ -47,9 +50,10 @@
private NavigationLayout mNavigationBar;
- private final ActivityResultLauncher<Intent> activityResultLauncher = registerForActivityResult(
- new ActivityResultContracts.StartActivityForResult(),
- BaseSetupWizardActivity.this::onActivityResult);
+ private final ActivityResultLauncher<Intent> mNextIntentResultLauncher =
+ registerForActivityResult(
+ new StartDecoratedActivityForResult(),
+ BaseSetupWizardActivity.this::onNextIntentResult);
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
@@ -252,7 +256,7 @@
}
setResult(resultCode, data);
Intent intent = WizardManagerHelper.getNextIntent(getIntent(), resultCode, data);
- startActivityForResult(intent);
+ mNextIntentResultLauncher.launch(intent);
}
/** Adorn the Intent with Setup Wizard-related extras. */
@@ -268,15 +272,11 @@
super.startActivity(decorateIntent(intent));
}
- protected final void startActivityForResult(@NonNull Intent intent) {
- activityResultLauncher.launch(decorateIntent(intent));
- }
-
- protected void onActivityResult(ActivityResult activityResult) {
+ protected void onNextIntentResult(@NonNull ActivityResult activityResult) {
int resultCode = activityResult.getResultCode();
Intent data = activityResult.getData();
if (LOGV) {
- StringBuilder append = new StringBuilder().append("onActivityResult(")
+ StringBuilder append = new StringBuilder().append("onNextIntentResult(")
.append(resultCode).append(", ");
Bundle extras = null;
if (data != null) {
@@ -340,4 +340,21 @@
TransitionHelper.applyBackwardTransition(BaseSetupWizardActivity.this,
DEFAULT_TRANSITION, true);
}
+
+ protected final class StartDecoratedActivityForResult
+ extends ActivityResultContract<Intent, ActivityResult> {
+
+ private final StartActivityForResult mWrappedContract = new StartActivityForResult();
+
+ @NonNull
+ @Override
+ public Intent createIntent(@NonNull Context context, @NonNull Intent intent) {
+ return decorateIntent(mWrappedContract.createIntent(context, intent));
+ }
+
+ @Override
+ public ActivityResult parseResult(int resultCode, @Nullable Intent result) {
+ return mWrappedContract.parseResult(resultCode, result);
+ }
+ }
}
diff --git a/src/org/lineageos/setupwizard/BluetoothSetupActivity.java b/src/org/lineageos/setupwizard/BluetoothSetupActivity.java
index 91ba556..26127ee 100644
--- a/src/org/lineageos/setupwizard/BluetoothSetupActivity.java
+++ b/src/org/lineageos/setupwizard/BluetoothSetupActivity.java
@@ -43,7 +43,7 @@
}
@Override
- protected void onActivityResult(ActivityResult activityResult) {
+ protected void onSubactivityResult(ActivityResult activityResult) {
Intent data = activityResult.getData();
if (mIsSubactivityNotFound) {
finishAction(RESULT_ACTIVITY_NOT_FOUND);
diff --git a/src/org/lineageos/setupwizard/SimMissingActivity.java b/src/org/lineageos/setupwizard/SimMissingActivity.java
index d521497..8cac4a4 100644
--- a/src/org/lineageos/setupwizard/SimMissingActivity.java
+++ b/src/org/lineageos/setupwizard/SimMissingActivity.java
@@ -27,7 +27,7 @@
}
@Override
- protected void onActivityResult(ActivityResult activityResult) {
+ protected void onSubactivityResult(ActivityResult activityResult) {
int resultCode = activityResult.getResultCode();
Intent data = activityResult.getData();
if (resultCode != RESULT_CANCELED) {
diff --git a/src/org/lineageos/setupwizard/SubBaseActivity.java b/src/org/lineageos/setupwizard/SubBaseActivity.java
index 05caf36..4751bd4 100644
--- a/src/org/lineageos/setupwizard/SubBaseActivity.java
+++ b/src/org/lineageos/setupwizard/SubBaseActivity.java
@@ -18,6 +18,7 @@
import android.util.Log;
import androidx.activity.result.ActivityResult;
+import androidx.activity.result.ActivityResultLauncher;
public abstract class SubBaseActivity extends BaseSetupWizardActivity {
@@ -27,6 +28,11 @@
protected abstract void onStartSubactivity();
+ private final ActivityResultLauncher<Intent> mSubactivityResultLauncher =
+ registerForActivityResult(
+ new StartDecoratedActivityForResult(),
+ SubBaseActivity.this::onSubactivityResult);
+
@Override
protected void onCreate(Bundle savedInstanceState) {
if (LOGV) {
@@ -61,7 +67,7 @@
subactivityIntent.putExtra(EXTRA_WIZARD_BUNDLE, wizardBundle);
}
try {
- startActivityForResult(subactivityIntent);
+ mSubactivityResultLauncher.launch(subactivityIntent);
} catch (ActivityNotFoundException e) {
Log.w(TAG, "activity not found; start next screen and finish; intent=" + intent);
mIsSubactivityNotFound = true;
@@ -70,8 +76,17 @@
}
@Override
- protected void onActivityResult(ActivityResult activityResult) {
- super.onActivityResult(activityResult);
+ protected void onNextIntentResult(@NonNull ActivityResult activityResult) {
+ super.onNextIntentResult(activityResult);
+ int resultCode = activityResult.getResultCode();
+ Intent data = activityResult.getData();
+ if (resultCode == RESULT_CANCELED && data != null
+ && data.getBooleanExtra("onBackPressed", false)) {
+ onStartSubactivity();
+ }
+ }
+
+ protected void onSubactivityResult(@NonNull ActivityResult activityResult) {
int resultCode = activityResult.getResultCode();
Intent data = activityResult.getData();
if (resultCode != RESULT_CANCELED) {
diff --git a/src/org/lineageos/setupwizard/backup/RestoreIntroActivity.java b/src/org/lineageos/setupwizard/backup/RestoreIntroActivity.java
index b43798b..b81d552 100644
--- a/src/org/lineageos/setupwizard/backup/RestoreIntroActivity.java
+++ b/src/org/lineageos/setupwizard/backup/RestoreIntroActivity.java
@@ -28,7 +28,7 @@
}
@Override
- protected void onActivityResult(ActivityResult activityResult) {
+ protected void onSubactivityResult(ActivityResult activityResult) {
int resultCode = activityResult.getResultCode();
Intent data = activityResult.getData();
if (resultCode != RESULT_CANCELED) {