* 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
113 lines
3.7 KiB
Java
113 lines
3.7 KiB
Java
/*
|
|
* SPDX-FileCopyrightText: 2017-2024 The LineageOS Project
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*/
|
|
|
|
package org.lineageos.setupwizard;
|
|
|
|
import static com.google.android.setupcompat.util.ResultCodes.RESULT_ACTIVITY_NOT_FOUND;
|
|
|
|
import static org.lineageos.setupwizard.SetupWizardApp.EXTRA_SCRIPT_URI;
|
|
import static org.lineageos.setupwizard.SetupWizardApp.EXTRA_WIZARD_BUNDLE;
|
|
import static org.lineageos.setupwizard.SetupWizardApp.LOGV;
|
|
|
|
import android.annotation.NonNull;
|
|
import android.content.ActivityNotFoundException;
|
|
import android.content.Intent;
|
|
import android.os.Bundle;
|
|
import android.util.Log;
|
|
|
|
import androidx.activity.result.ActivityResult;
|
|
import androidx.activity.result.ActivityResultLauncher;
|
|
|
|
public abstract class SubBaseActivity extends BaseSetupWizardActivity {
|
|
|
|
public static final String TAG = SubBaseActivity.class.getSimpleName();
|
|
|
|
protected boolean mIsSubactivityNotFound = false;
|
|
|
|
protected abstract void onStartSubactivity();
|
|
|
|
private final ActivityResultLauncher<Intent> mSubactivityResultLauncher =
|
|
registerForActivityResult(
|
|
new StartDecoratedActivityForResult(),
|
|
SubBaseActivity.this::onSubactivityResult);
|
|
|
|
@Override
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
if (LOGV) {
|
|
Log.d(TAG, "onCreate savedInstanceState=" + savedInstanceState);
|
|
}
|
|
super.onCreate(savedInstanceState);
|
|
setNextAllowed(false);
|
|
if (savedInstanceState == null) {
|
|
onStartSubactivity();
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onStart() {
|
|
super.onStart();
|
|
}
|
|
|
|
@Override
|
|
protected void onSaveInstanceState(@NonNull Bundle outState) {
|
|
super.onSaveInstanceState(outState);
|
|
}
|
|
|
|
@Override
|
|
protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {
|
|
super.onRestoreInstanceState(savedInstanceState);
|
|
}
|
|
|
|
protected void startSubactivity(Intent subactivityIntent) {
|
|
Intent intent = getIntent();
|
|
Bundle wizardBundle = intent.getBundleExtra(EXTRA_WIZARD_BUNDLE);
|
|
if (wizardBundle.containsKey(EXTRA_SCRIPT_URI)) {
|
|
subactivityIntent.putExtra(EXTRA_WIZARD_BUNDLE, wizardBundle);
|
|
}
|
|
try {
|
|
mSubactivityResultLauncher.launch(subactivityIntent);
|
|
} catch (ActivityNotFoundException e) {
|
|
Log.w(TAG, "activity not found; start next screen and finish; intent=" + intent);
|
|
mIsSubactivityNotFound = true;
|
|
finishAction(RESULT_ACTIVITY_NOT_FOUND);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
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) {
|
|
nextAction(resultCode, data);
|
|
} else if (mIsSubactivityNotFound) {
|
|
finishAction(RESULT_ACTIVITY_NOT_FOUND);
|
|
} else if (data != null && data.getBooleanExtra("onBackPressed", false)) {
|
|
onStartSubactivity();
|
|
} else {
|
|
finishAction(RESULT_CANCELED);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
protected int getLayoutResId() {
|
|
return R.layout.setup_loading_page;
|
|
}
|
|
|
|
@Override
|
|
protected int getTitleResId() {
|
|
return R.string.loading;
|
|
}
|
|
}
|