From 3441abdd57eebbdfefdf793f3e189841720e6058 Mon Sep 17 00:00:00 2001 From: cretin45 Date: Mon, 2 Feb 2015 16:44:52 -0800 Subject: [PATCH] SetupWizard: Add backup restore Change-Id: Ide7149eee903cce11dfeb7fb919b707c614c8735 --- AndroidManifest.xml | 1 + res/layout/location_settings.xml | 47 +++++++++++ res/layout/mobile_data_settings.xml | 4 +- res/values/strings.xml | 8 +- .../setupwizard/SetupWizardApp.java | 5 +- .../setupwizard/setup/CMSetupWizardData.java | 2 +- .../setupwizard/setup/GmsAccountPage.java | 79 ++++++++++++++++--- ...ttingsPage.java => OtherSettingsPage.java} | 51 ++++++++++-- .../setupwizard/util/SetupWizardUtils.java | 2 +- 9 files changed, 177 insertions(+), 22 deletions(-) rename src/com/cyanogenmod/setupwizard/setup/{LocationSettingsPage.java => OtherSettingsPage.java} (79%) diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 03c7982..443b7e9 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -44,6 +44,7 @@ + + + + + + + + + + + + + + android:textOff="@string/no" + android:textOn="@string/yes"/> diff --git a/res/values/strings.xml b/res/values/strings.xml index 4c012a0..634a9ca 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -42,6 +42,7 @@ GMS account Choose a SIM for Data Location Services + Other Services Date & time Current date Current time @@ -51,6 +52,9 @@ Set your time zone and adjust current date and time if needed + Back up my data + Back up app data, Wi-Fi passwords, and other settings to Google servers + Location services allows Maps and other apps to gather and use data such as your approximate location. For example, Maps may use your approximate location to locate nearby coffee shops. Allow apps that have asked your permission to use your location information. This may include your current location and past locations. Improve location accuracy by allowing apps to use the GPS on your phone. @@ -60,8 +64,8 @@ No service Emergency calls only Do you want to use mobile data during setup? Turning on mobile data may be subject to data charges. - No - Yes + No + Yes SIM %d - %s diff --git a/src/com/cyanogenmod/setupwizard/SetupWizardApp.java b/src/com/cyanogenmod/setupwizard/SetupWizardApp.java index 8743424..22a376c 100644 --- a/src/com/cyanogenmod/setupwizard/SetupWizardApp.java +++ b/src/com/cyanogenmod/setupwizard/SetupWizardApp.java @@ -38,10 +38,13 @@ public class SetupWizardApp extends Application { public static final String EXTRA_AUTO_FINISH = "wifi_auto_finish_on_connect"; public static final String EXTRA_SHOW_BUTTON_BAR = "extra_prefs_show_button_bar"; public static final String EXTRA_USE_IMMERSIVE = "useImmersiveMode"; + public static final String EXTRA_THEME = "theme"; + public static final String EXTRA_MATERIAL_LIGHT = "material_light"; public static final int REQUEST_CODE_SETUP_WIFI = 0; public static final int REQUEST_CODE_SETUP_GMS= 1; - public static final int REQUEST_CODE_SETUP_CYANOGEN= 2; + public static final int REQUEST_CODE_RESTORE_GMS= 2; + public static final int REQUEST_CODE_SETUP_CYANOGEN= 3; private StatusBarManager mStatusBarManager; diff --git a/src/com/cyanogenmod/setupwizard/setup/CMSetupWizardData.java b/src/com/cyanogenmod/setupwizard/setup/CMSetupWizardData.java index c8eb4eb..a75e0c1 100644 --- a/src/com/cyanogenmod/setupwizard/setup/CMSetupWizardData.java +++ b/src/com/cyanogenmod/setupwizard/setup/CMSetupWizardData.java @@ -68,7 +68,7 @@ public class CMSetupWizardData extends AbstractSetupData { } pages.add(new CyanogenServicesPage(mContext, this)); pages.add(new CyanogenSettingsPage(mContext, this)); - pages.add(new LocationSettingsPage(mContext, this)); + pages.add(new OtherSettingsPage(mContext, this)); pages.add(new DateTimePage(mContext, this)); pages.add(new FinishPage(mContext, this)); return new PageList(pages.toArray(new SetupPage[pages.size()])); diff --git a/src/com/cyanogenmod/setupwizard/setup/GmsAccountPage.java b/src/com/cyanogenmod/setupwizard/setup/GmsAccountPage.java index 2c5ed6b..7c899d1 100644 --- a/src/com/cyanogenmod/setupwizard/setup/GmsAccountPage.java +++ b/src/com/cyanogenmod/setupwizard/setup/GmsAccountPage.java @@ -25,9 +25,13 @@ import android.app.Activity; import android.app.ActivityOptions; import android.app.Fragment; import android.app.FragmentManager; +import android.content.ContentQueryMap; +import android.content.ContentResolver; import android.content.Context; import android.content.Intent; +import android.database.Cursor; import android.os.Bundle; +import android.provider.Settings; import com.cyanogenmod.setupwizard.R; import com.cyanogenmod.setupwizard.SetupWizardApp; @@ -35,13 +39,37 @@ import com.cyanogenmod.setupwizard.ui.LoadingFragment; import com.cyanogenmod.setupwizard.util.SetupWizardUtils; import java.io.IOException; +import java.util.Observable; +import java.util.Observer; public class GmsAccountPage extends SetupPage { public static final String TAG = "GmsAccountPage"; - public GmsAccountPage(Context context, SetupDataCallbacks callbacks) { + public static final String ACTION_RESTORE = "com.google.android.setupwizard.RESTORE"; + + private ContentQueryMap mContentQueryMap; + private Observer mSettingsObserver; + + private boolean mBackupEnabled = false; + + public GmsAccountPage(final Context context, SetupDataCallbacks callbacks) { super(context, callbacks); + final ContentResolver res = context.getContentResolver(); + mSettingsObserver = new Observer() { + public void update(Observable o, Object arg) { + mBackupEnabled = (Settings.Secure.getInt(res, + Settings.Secure.BACKUP_AUTO_RESTORE, 0) == 1) || + (Settings.Secure.getInt(res, + Settings.Secure.BACKUP_ENABLED, 0) == 1); + } + }; + Cursor settingsCursor = res.query(Settings.Secure.CONTENT_URI, null, + "(" + Settings.System.NAME + "=? OR " + Settings.System.NAME + "=?)", + new String[]{Settings.Secure.BACKUP_AUTO_RESTORE, Settings.Secure.BACKUP_ENABLED}, + null); + mContentQueryMap = new ContentQueryMap(settingsCursor, Settings.System.NAME, true, null); + mContentQueryMap.addObserver(mSettingsObserver); } @Override @@ -85,19 +113,52 @@ public class GmsAccountPage extends SetupPage { @Override public boolean onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == SetupWizardApp.REQUEST_CODE_SETUP_GMS) { - if (resultCode == Activity.RESULT_OK || resultCode == Activity.RESULT_FIRST_USER) { - if (SetupWizardUtils.accountExists(mContext, SetupWizardApp.ACCOUNT_TYPE_GMS)) { - setHidden(true); - } - getCallbacks().onNextPage(); - } else if (resultCode == Activity.RESULT_CANCELED) { - getCallbacks().onPreviousPage(); + if (!mBackupEnabled) { + launchGmsRestorePage((Activity) mContext); + } else { + handleResult(resultCode); } + } else if (requestCode == SetupWizardApp.REQUEST_CODE_RESTORE_GMS) { + handleResult(resultCode); } return true; } - public void launchGmsAccountSetup(final Activity activity) { + @Override + public void onFinishSetup() { + if (mContentQueryMap != null) { + mContentQueryMap.close(); + } + + } + + private void handleResult(int resultCode) { + if (resultCode == Activity.RESULT_CANCELED) { + getCallbacks().onPreviousPage(); + } else { + if (SetupWizardUtils.accountExists(mContext, SetupWizardApp.ACCOUNT_TYPE_GMS)) { + setHidden(true); + } + getCallbacks().onNextPage(); + } + } + + private static void launchGmsRestorePage(final Activity activity) { + Intent intent = new Intent(ACTION_RESTORE); + intent.putExtra(SetupWizardApp.EXTRA_ALLOW_SKIP, true); + intent.putExtra(SetupWizardApp.EXTRA_USE_IMMERSIVE, true); + intent.putExtra(SetupWizardApp.EXTRA_FIRST_RUN, true); + intent.putExtra(SetupWizardApp.EXTRA_THEME, SetupWizardApp.EXTRA_MATERIAL_LIGHT); + ActivityOptions options = + ActivityOptions.makeCustomAnimation(activity, + android.R.anim.fade_in, + android.R.anim.fade_out); + activity.startActivityForResult( + intent, + SetupWizardApp.REQUEST_CODE_RESTORE_GMS, options.toBundle()); + } + + private void launchGmsAccountSetup(final Activity activity) { Bundle bundle = new Bundle(); bundle.putBoolean(SetupWizardApp.EXTRA_FIRST_RUN, true); bundle.putBoolean(SetupWizardApp.EXTRA_ALLOW_SKIP, true); diff --git a/src/com/cyanogenmod/setupwizard/setup/LocationSettingsPage.java b/src/com/cyanogenmod/setupwizard/setup/OtherSettingsPage.java similarity index 79% rename from src/com/cyanogenmod/setupwizard/setup/LocationSettingsPage.java rename to src/com/cyanogenmod/setupwizard/setup/OtherSettingsPage.java index 0264599..7d72fdc 100644 --- a/src/com/cyanogenmod/setupwizard/setup/LocationSettingsPage.java +++ b/src/com/cyanogenmod/setupwizard/setup/OtherSettingsPage.java @@ -18,12 +18,15 @@ package com.cyanogenmod.setupwizard.setup; import android.app.Fragment; import android.app.FragmentManager; +import android.app.backup.IBackupManager; import android.content.ContentQueryMap; import android.content.ContentResolver; import android.content.Context; import android.database.Cursor; import android.location.LocationManager; import android.os.Bundle; +import android.os.RemoteException; +import android.os.ServiceManager; import android.provider.Settings; import android.view.View; import android.widget.CheckBox; @@ -34,11 +37,11 @@ import com.cyanogenmod.setupwizard.ui.SetupPageFragment; import java.util.Observable; import java.util.Observer; -public class LocationSettingsPage extends SetupPage { +public class OtherSettingsPage extends SetupPage { - private static final String TAG = "LocationSettingsPage"; + private static final String TAG = "OtherSettingsPage"; - public LocationSettingsPage(Context context, SetupDataCallbacks callbacks) { + public OtherSettingsPage(Context context, SetupDataCallbacks callbacks) { super(context, callbacks); } @@ -49,7 +52,7 @@ public class LocationSettingsPage extends SetupPage { Bundle args = new Bundle(); args.putString(Page.KEY_PAGE_ARGUMENT, getKey()); args.putInt(Page.KEY_PAGE_ACTION, action); - fragment = new LocationSettingsFragment(); + fragment = new OtherSettingsFragment(); fragment.setArguments(args); } return fragment; @@ -62,20 +65,24 @@ public class LocationSettingsPage extends SetupPage { @Override public int getTitleResId() { - return R.string.setup_location; + return R.string.setup_other; } - public static class LocationSettingsFragment extends SetupPageFragment { + public static class OtherSettingsFragment extends SetupPageFragment { + private View mBackupRow; private View mLocationRow; private View mGpsRow; private View mNetworkRow; + private CheckBox mBackup; private CheckBox mNetwork; private CheckBox mGps; private CheckBox mLocationAccess; private ContentResolver mContentResolver; + private IBackupManager mBackupManager; + // These provide support for receiving notification when Location Manager settings change. // This is necessary because the Network Location Provider can change settings // if the user does not confirm enabling the provider. @@ -83,6 +90,13 @@ public class LocationSettingsPage extends SetupPage { private Observer mSettingsObserver; + private View.OnClickListener mBackupClickListener = new View.OnClickListener() { + @Override + public void onClick(View view) { + onToggleBackup(!mBackup.isChecked()); + } + }; + private View.OnClickListener mLocationClickListener = new View.OnClickListener() { @Override public void onClick(View view) { @@ -110,11 +124,16 @@ public class LocationSettingsPage extends SetupPage { public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); mContentResolver = getActivity().getContentResolver(); + mBackupManager = IBackupManager.Stub.asInterface( + ServiceManager.getService(Context.BACKUP_SERVICE)); getActivity().getWindow().setStatusBarColor(getResources().getColor(R.color.primary_dark)); } @Override protected void initializePage() { + mBackupRow = mRootView.findViewById(R.id.backup); + mBackupRow.setOnClickListener(mBackupClickListener); + mBackup = (CheckBox) mRootView.findViewById(R.id.backup_checkbox); mLocationRow = mRootView.findViewById(R.id.location); mLocationRow.setOnClickListener(mLocationClickListener); mLocationAccess = (CheckBox) mRootView.findViewById(R.id.location_checkbox); @@ -135,10 +154,12 @@ public class LocationSettingsPage extends SetupPage { public void onResume() { super.onResume(); updateLocationToggles(); + updateBackupToggle(); if (mSettingsObserver == null) { mSettingsObserver = new Observer() { public void update(Observable o, Object arg) { updateLocationToggles(); + updateBackupToggle(); } }; } @@ -166,6 +187,24 @@ public class LocationSettingsPage extends SetupPage { mContentQueryMap.close(); } + private boolean isBackupRestoreEnabled() { + try { + return mBackupManager.isBackupEnabled(); + } catch (Exception e) { + return false; + } + } + + private void updateBackupToggle() { + mBackup.setChecked(isBackupRestoreEnabled()); + } + + private void onToggleBackup(boolean checked) { + try { + mBackupManager.setBackupEnabled(checked); + } catch (RemoteException e) {} + updateBackupToggle(); + } private void updateLocationToggles() { boolean gpsEnabled = Settings.Secure.isLocationProviderEnabled( diff --git a/src/com/cyanogenmod/setupwizard/util/SetupWizardUtils.java b/src/com/cyanogenmod/setupwizard/util/SetupWizardUtils.java index d2be99f..e55cc3e 100644 --- a/src/com/cyanogenmod/setupwizard/util/SetupWizardUtils.java +++ b/src/com/cyanogenmod/setupwizard/util/SetupWizardUtils.java @@ -58,7 +58,7 @@ public class SetupWizardUtils { intent.putExtra(SetupWizardApp.EXTRA_FIRST_RUN, true); intent.putExtra(SetupWizardApp.EXTRA_ALLOW_SKIP, true); intent.putExtra(SetupWizardApp.EXTRA_USE_IMMERSIVE, true); - intent.putExtra("theme", "material_light"); + intent.putExtra(SetupWizardApp.EXTRA_THEME, SetupWizardApp.EXTRA_MATERIAL_LIGHT); intent.putExtra(SetupWizardApp.EXTRA_AUTO_FINISH, false); ActivityOptions options = ActivityOptions.makeCustomAnimation(context,