SetupWizard: Restore last page on setup wizard death.

Since the restore process (or any external setup wizard
  process) can take a long time to finish execution, save
  the current page and restore it on recreation when necessary.

Change-Id: I575cf58047eb307c95408a0955dd7e5b50a62107
TICKET: CYNGNOS-2233
This commit is contained in:
Adnan Begovic 2016-03-17 13:58:26 -07:00
parent 744827989c
commit 57080c360c
4 changed files with 65 additions and 0 deletions

View File

@ -135,6 +135,21 @@ public abstract class AbstractSetupData extends BroadcastReceiver implements Set
doPreviousNext(runnable); doPreviousNext(runnable);
} }
@Override
public void setCurrentPage(String key) {
if (mPageList.getPage(key) != null) {
mCurrentPageIndex = mPageList.getPageIndex(key);
}
}
public Page getNextPage(String key) {
if (mPageList.getPage(key) != null) {
int currentPageIndex = mPageList.getPageIndex(key);
return mPageList.getPage(currentPageIndex + 1);
}
return null;
}
private boolean advanceToNextUnhidden() { private boolean advanceToNextUnhidden() {
while (mCurrentPageIndex < mPageList.size()) { while (mCurrentPageIndex < mPageList.size()) {
mCurrentPageIndex++; mCurrentPageIndex++;

View File

@ -16,6 +16,8 @@
package com.cyanogenmod.setupwizard.setup; package com.cyanogenmod.setupwizard.setup;
import android.text.TextUtils;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
public class PageList extends LinkedHashMap<String, Page> { public class PageList extends LinkedHashMap<String, Page> {
@ -30,6 +32,17 @@ public class PageList extends LinkedHashMap<String, Page> {
return get(key); return get(key);
} }
public int getPageIndex(String key) {
int i=0;
for (Page page : values()) {
if (TextUtils.equals(page.getKey(), key)) {
return i;
}
i++;
}
return i;
}
public Page getPage(int index) { public Page getPage(int index) {
int i=0; int i=0;
for (Page page : values()) { for (Page page : values()) {

View File

@ -26,5 +26,6 @@ public interface SetupDataCallbacks {
boolean isCurrentPage(Page page); boolean isCurrentPage(Page page);
Page getPage(String key); Page getPage(String key);
Page getPage(int key); Page getPage(int key);
void setCurrentPage(String key);
void addFinishRunnable(Runnable runnable); void addFinishRunnable(Runnable runnable);
} }

View File

@ -20,6 +20,7 @@ import android.animation.Animator;
import android.app.Activity; import android.app.Activity;
import android.app.WallpaperManager; import android.app.WallpaperManager;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo; import android.content.pm.ActivityInfo;
import android.content.res.Resources; import android.content.res.Resources;
import android.graphics.Bitmap; import android.graphics.Bitmap;
@ -28,7 +29,9 @@ import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.os.UserHandle; import android.os.UserHandle;
import android.preference.PreferenceManager;
import android.provider.Settings; import android.provider.Settings;
import android.text.TextUtils;
import android.view.MotionEvent; import android.view.MotionEvent;
import android.view.View; import android.view.View;
import android.view.ViewAnimationUtils; import android.view.ViewAnimationUtils;
@ -42,6 +45,7 @@ import com.cyanogenmod.setupwizard.R;
import com.cyanogenmod.setupwizard.SetupWizardApp; import com.cyanogenmod.setupwizard.SetupWizardApp;
import com.cyanogenmod.setupwizard.cmstats.SetupStats; import com.cyanogenmod.setupwizard.cmstats.SetupStats;
import com.cyanogenmod.setupwizard.setup.CMSetupWizardData; import com.cyanogenmod.setupwizard.setup.CMSetupWizardData;
import com.cyanogenmod.setupwizard.setup.GmsAccountPage;
import com.cyanogenmod.setupwizard.setup.Page; import com.cyanogenmod.setupwizard.setup.Page;
import com.cyanogenmod.setupwizard.setup.SetupDataCallbacks; import com.cyanogenmod.setupwizard.setup.SetupDataCallbacks;
import com.cyanogenmod.setupwizard.util.EnableAccessibilityController; import com.cyanogenmod.setupwizard.util.EnableAccessibilityController;
@ -56,6 +60,7 @@ public class SetupWizardActivity extends Activity implements SetupDataCallbacks,
ThemeManager.ThemeChangeListener { ThemeManager.ThemeChangeListener {
private static final String TAG = SetupWizardActivity.class.getSimpleName(); private static final String TAG = SetupWizardActivity.class.getSimpleName();
private static final String KEY_LAST_PAGE_TAG = "last_page_tag";
private static final int UI_FLAGS = View.SYSTEM_UI_FLAG_LAYOUT_STABLE private static final int UI_FLAGS = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
@ -143,6 +148,27 @@ public class SetupWizardActivity extends Activity implements SetupDataCallbacks,
if (savedInstanceState != null && savedInstanceState.containsKey("data")) { if (savedInstanceState != null && savedInstanceState.containsKey("data")) {
mSetupData.load(savedInstanceState.getBundle("data")); mSetupData.load(savedInstanceState.getBundle("data"));
} }
final SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
if (sharedPreferences.contains(KEY_LAST_PAGE_TAG)) {
final String lastPage = sharedPreferences.getString(KEY_LAST_PAGE_TAG,
mSetupData.getCurrentPage().getKey());
final boolean backupEnabled = (Settings.Secure.getInt(getContentResolver(),
Settings.Secure.BACKUP_AUTO_RESTORE, 0) == 1) ||
(Settings.Secure.getInt(getContentResolver(),
Settings.Secure.BACKUP_ENABLED, 0) == 1);
if (TextUtils.equals(lastPage, GmsAccountPage.TAG) && backupEnabled) {
// We probably already restored, skip ahead!
mSetupData.setCurrentPage(mSetupData.getNextPage(lastPage).getKey());
} else {
// else just restore
mSetupData.setCurrentPage(sharedPreferences.getString(KEY_LAST_PAGE_TAG,
mSetupData.getCurrentPage().getKey()));
}
Page page = mSetupData.getCurrentPage();
page.doLoadAction(getFragmentManager(), Page.ACTION_NEXT);
}
mEnableAccessibilityController = mEnableAccessibilityController =
EnableAccessibilityController.getInstance(getApplicationContext()); EnableAccessibilityController.getInstance(getApplicationContext());
mRootView.setOnTouchListener(new View.OnTouchListener() { mRootView.setOnTouchListener(new View.OnTouchListener() {
@ -190,6 +216,8 @@ public class SetupWizardActivity extends Activity implements SetupDataCallbacks,
protected void onDestroy() { protected void onDestroy() {
super.onDestroy(); super.onDestroy();
if (mSetupData != null) { if (mSetupData != null) {
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
prefs.edit().putString(KEY_LAST_PAGE_TAG, mSetupData.getCurrentPage().getKey()).apply();
mSetupData.onDestroy(); mSetupData.onDestroy();
mSetupData.unregisterListener(this); mSetupData.unregisterListener(this);
unregisterReceiver(mSetupData); unregisterReceiver(mSetupData);
@ -230,6 +258,14 @@ public class SetupWizardActivity extends Activity implements SetupDataCallbacks,
} }
} }
@Override
public void setCurrentPage(String key) {
Page page = mSetupData.getCurrentPage();
if (!isFinishing()) {
page.doLoadAction(getFragmentManager(), Page.ACTION_NEXT);
}
}
@Override @Override
public void onPageLoaded(Page page) { public void onPageLoaded(Page page) {
updateButtonBar(); updateButtonBar();