SetupWizardActivity: implement setup [1/n]

This commit is contained in:
Peter Cai 2021-03-17 15:33:54 +08:00
parent 924ad14008
commit 13599506c5
5 changed files with 106 additions and 1 deletions

View file

@ -15,6 +15,7 @@ import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;
import androidx.activity.result.ActivityResult;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
@ -112,7 +113,12 @@ public class MainActivity extends AppCompatActivity {
.setNegativeButton(R.string.first_run_alert_cancel,
(dialog, which) -> finish())
.show();*/
startActivity(new Intent(this, SetupWizardActivity.class));
registerForActivityResult(new SetupWizardActivity.SetupWizardContract(), (Boolean result) -> {
if (result)
init();
else
finish();
}).launch(null);
} else {
// Initialize the settings
SettingsManager.getInstance().applyAll();

View file

@ -1,12 +1,15 @@
package net.typeblog.shelter.ui;
import androidx.activity.result.contract.ActivityResultContract;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import android.app.admin.DevicePolicyManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
@ -19,10 +22,13 @@ import com.android.setupwizardlib.view.NavigationBar;
import net.typeblog.shelter.R;
public class SetupWizardActivity extends AppCompatActivity {
private DevicePolicyManager mPolicyManager = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setup_wizard);
mPolicyManager = getSystemService(DevicePolicyManager.class);
// Don't use switchToFragment for the first time
// because we don't want animation for the first fragment
// (it would have nothing to animate upon, resulting in a black background)
@ -43,6 +49,30 @@ public class SetupWizardActivity extends AppCompatActivity {
.commit();
}
private void finishWithResult(boolean succeeded) {
setResult(succeeded ? RESULT_OK : RESULT_CANCELED);
finish();
}
private void setupProfile() {
// Placeholder
switchToFragment(new FailedFragment(), false);
}
public static class SetupWizardContract extends ActivityResultContract<Void, Boolean> {
@NonNull
@Override
public Intent createIntent(@NonNull Context context, Void input) {
return new Intent(context, SetupWizardActivity.class);
}
@Override
public Boolean parseResult(int resultCode, @Nullable Intent intent) {
return resultCode == RESULT_OK;
}
}
// ==== SetupWizard steps ====
private static abstract class BaseWizardFragment extends Fragment implements NavigationBar.NavigationBarListener {
protected SetupWizardActivity mActivity = null;
protected SetupWizardLayout mWizard = null;
@ -195,10 +225,69 @@ public class SetupWizardActivity extends AppCompatActivity {
mActivity.switchToFragment(new CompatibilityFragment(), true);
}
@Override
public void onNavigateNext() {
super.onNavigateNext();
mActivity.switchToFragment(new PleaseWaitFragment(), false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mWizard.setHeaderText(R.string.setup_wizard_ready);
}
}
public static class PleaseWaitFragment extends TextWizardFragment {
@Override
protected int getLayoutResource() {
return R.layout.fragment_setup_wizard_generic_text;
}
@Override
protected int getTextRes() {
return R.string.setup_wizard_please_wait_text;
}
@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
mActivity.setupProfile();
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mWizard.setHeaderText(R.string.setup_wizard_please_wait);
mWizard.setProgressBarColor(view.getContext().getColorStateList(R.color.setup_wizard_progress_bar));
mWizard.setProgressBarShown(true);
mWizard.getNavigationBar().getBackButton().setVisibility(View.GONE);
mWizard.getNavigationBar().getNextButton().setVisibility(View.GONE);
}
}
public static class FailedFragment extends TextWizardFragment {
@Override
protected int getLayoutResource() {
return R.layout.fragment_setup_wizard_generic_text;
}
@Override
protected int getTextRes() {
return R.string.setup_wizard_failed_text;
}
@Override
public void onNavigateNext() {
super.onNavigateNext();
mActivity.finishWithResult(false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mWizard.setHeaderText(R.string.setup_wizard_failed);
mWizard.getNavigationBar().getBackButton().setVisibility(View.GONE);
}
}
}

View file

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true"
android:color="@color/colorAccentSetupWizard" />
</selector>

View file

@ -3,6 +3,7 @@
<color name="colorPrimary">#FAFAFA</color>
<color name="colorPrimaryDark">#C2C2C2</color>
<color name="colorAccent">#009688</color>
<color name="colorAccentSetupWizard">#FFC107</color>
<color name="black">#333333</color>
<color name="grey">#999999</color>
<color name="disabledAppBackground">#E0F2F1</color>

View file

@ -17,6 +17,10 @@
<string name="setup_wizard_compatibility_text">Shelter is developed and tested on AOSP-like Android derivatives. This includes AOSP (Android Open Source Project), Google Android (on Pixels), and <b>most AOSP-based open-source custom ROMs</b> such as LineageOS. If your phone is running one of the Android derivatives listed above, then congratulations! Shelter is probably going to work correctly on your device.\n\nSome device vendors introduce very invasive customizations into the Android code base, resulting in conflicts, incompatibility and unexpected behavior. Some custom ROMs can also introduce compatibility-breaking changes, but generally these are rarer occurrences compared to phone vendor-introduced incompatibilities.\n\nSheler is merely an interface into the Work Profile feature provided by the system. If the feature provided by the system is broken or non-standard, <b>Shelter could not magically resolve the issue on its own</b>. If you are currently using a vendor-modified Android version that is known to break Work Profiles, <b>you have been warned</b>. You may proceed anyway, but there is no guarantee that Shelter would behave correctly under these circumstances.</string>
<string name="setup_wizard_ready">Ready?</string>
<string name="setup_wizard_ready_text">We are now ready to set up Shelter for you. Please first ensure that your device is <b>not</b> in Do Not Disturb mode, because you will need to <b>click on a notification</b> later to finalize the setup process.\n\nWhen you are ready, click on \"Next\" to begin the setup process.</string>
<string name="setup_wizard_please_wait">Please wait…</string>
<string name="setup_wizard_please_wait_text">We are trying to initialize Work Profile and set up Shelter on your device.</string>
<string name="setup_wizard_failed">Setup failed</string>
<string name="setup_wizard_failed_text">We regret to inform you that we were not able to set up Shelter for you.\n\nIf you did not cancel the setup manually, then the reason for the failure is most commonly due to a heavily modified system, or a conflict between Shelter and other Work Profile managers. Unfortunately, there is not much that we could do about this.\n\nClick "Next" to exit.</string>
<!-- Notifications -->
<string name="notifications_important">Shelter Important</string>