diff options
author | cretin45 <cretin45@gmail.com> | 2015-02-23 13:52:37 -0800 |
---|---|---|
committer | cretin45 <cretin45@gmail.com> | 2015-02-25 14:59:06 -0800 |
commit | 01f21da16bc6c958ee237a6fa9170f7e11fda2c7 (patch) | |
tree | d8d20ece00d73fe751c4170f3bb74bd401c33383 /src/com/cyanogenmod/setupwizard/setup | |
parent | 1d54e5207a6e8e63dc19e5392bf9cfab967310bf (diff) | |
download | packages_apps_SetupWizard-01f21da16bc6c958ee237a6fa9170f7e11fda2c7.zip packages_apps_SetupWizard-01f21da16bc6c958ee237a6fa9170f7e11fda2c7.tar.gz packages_apps_SetupWizard-01f21da16bc6c958ee237a6fa9170f7e11fda2c7.tar.bz2 |
SetupWizard: Add option to apply default theme if one exists
* A bunch of code cleanup/refactoring to make this work
Change-Id: I059fb5fa5bcd7dd8821e993218a320554f10a314
Diffstat (limited to 'src/com/cyanogenmod/setupwizard/setup')
16 files changed, 190 insertions, 94 deletions
diff --git a/src/com/cyanogenmod/setupwizard/setup/AbstractSetupData.java b/src/com/cyanogenmod/setupwizard/setup/AbstractSetupData.java index 9c9d5fc..d78da8e 100644 --- a/src/com/cyanogenmod/setupwizard/setup/AbstractSetupData.java +++ b/src/com/cyanogenmod/setupwizard/setup/AbstractSetupData.java @@ -17,33 +17,30 @@ package com.cyanogenmod.setupwizard.setup; import android.content.BroadcastReceiver; +import android.content.Context; import android.os.Bundle; -import com.cyanogenmod.setupwizard.ui.SetupWizardActivity; - import java.util.ArrayList; public abstract class AbstractSetupData extends BroadcastReceiver implements SetupDataCallbacks { private static final String TAG = AbstractSetupData.class.getSimpleName(); - protected SetupWizardActivity mContext; + protected final Context mContext; private ArrayList<SetupDataCallbacks> mListeners = new ArrayList<SetupDataCallbacks>(); private PageList mPageList; private int mCurrentPageIndex = 0; + private boolean mIsResumed = false; + private OnResumeRunnable mOnResumeRunnable; - public AbstractSetupData(SetupWizardActivity context) { + public AbstractSetupData(Context context) { mContext = context; mPageList = onNewPageList(); } - public void setContext(SetupWizardActivity context) { - mContext = context; - } - protected abstract PageList onNewPageList(); @Override @@ -68,6 +65,13 @@ public abstract class AbstractSetupData extends BroadcastReceiver implements Set } @Override + public void finishSetup() { + for (int i = 0; i < mListeners.size(); i++) { + mListeners.get(i).finishSetup(); + } + } + + @Override public Page getPage(String key) { return mPageList.getPage(key); } @@ -159,7 +163,7 @@ public abstract class AbstractSetupData extends BroadcastReceiver implements Set } private void doPreviousNext(Runnable runnable) { - if (mContext.isResumed()) { + if (mIsResumed) { runnable.run(); } else { mOnResumeRunnable = new OnResumeRunnable(runnable, this); @@ -170,7 +174,12 @@ public abstract class AbstractSetupData extends BroadcastReceiver implements Set mOnResumeRunnable = null; } + public void onPause() { + mIsResumed = false; + } + public void onResume() { + mIsResumed = true; if (mOnResumeRunnable != null) { mOnResumeRunnable.run(); } diff --git a/src/com/cyanogenmod/setupwizard/setup/CMSetupWizardData.java b/src/com/cyanogenmod/setupwizard/setup/CMSetupWizardData.java index 1987a96..6550bcb 100644 --- a/src/com/cyanogenmod/setupwizard/setup/CMSetupWizardData.java +++ b/src/com/cyanogenmod/setupwizard/setup/CMSetupWizardData.java @@ -35,7 +35,7 @@ public class CMSetupWizardData extends AbstractSetupData { private boolean mTimeSet = false; private boolean mTimeZoneSet = false; - public CMSetupWizardData(SetupWizardActivity context) { + public CMSetupWizardData(Context context) { super(context); } diff --git a/src/com/cyanogenmod/setupwizard/setup/ChooseDataSimPage.java b/src/com/cyanogenmod/setupwizard/setup/ChooseDataSimPage.java index c63ca19..a49ebd2 100644 --- a/src/com/cyanogenmod/setupwizard/setup/ChooseDataSimPage.java +++ b/src/com/cyanogenmod/setupwizard/setup/ChooseDataSimPage.java @@ -37,7 +37,6 @@ import android.widget.TextView; import com.cyanogenmod.setupwizard.R; import com.cyanogenmod.setupwizard.ui.SetupPageFragment; -import com.cyanogenmod.setupwizard.ui.SetupWizardActivity; import java.util.List; @@ -45,7 +44,7 @@ public class ChooseDataSimPage extends SetupPage { public static final String TAG = "ChooseDataSimPage"; - public ChooseDataSimPage(SetupWizardActivity context, SetupDataCallbacks callbacks) { + public ChooseDataSimPage(Context context, SetupDataCallbacks callbacks) { super(context, callbacks); } diff --git a/src/com/cyanogenmod/setupwizard/setup/CyanogenServicesPage.java b/src/com/cyanogenmod/setupwizard/setup/CyanogenServicesPage.java index 13b925a..7db6eca 100644 --- a/src/com/cyanogenmod/setupwizard/setup/CyanogenServicesPage.java +++ b/src/com/cyanogenmod/setupwizard/setup/CyanogenServicesPage.java @@ -25,13 +25,14 @@ import android.app.Activity; import android.app.ActivityOptions; import android.app.Fragment; import android.app.FragmentManager; +import android.content.Context; import android.content.Intent; import android.os.Bundle; import com.cyanogenmod.setupwizard.R; import com.cyanogenmod.setupwizard.SetupWizardApp; import com.cyanogenmod.setupwizard.ui.LoadingFragment; -import com.cyanogenmod.setupwizard.ui.SetupWizardActivity; +import com.cyanogenmod.setupwizard.ui.SetupPageFragment; import com.cyanogenmod.setupwizard.util.SetupWizardUtils; import java.io.IOException; @@ -40,21 +41,23 @@ public class CyanogenServicesPage extends SetupPage { public static final String TAG = "CyanogenServicesPage"; - public CyanogenServicesPage(SetupWizardActivity context, SetupDataCallbacks callbacks) { + private Fragment mFragment; + + public CyanogenServicesPage(Context context, SetupDataCallbacks callbacks) { super(context, callbacks); } @Override public Fragment getFragment(FragmentManager fragmentManager, int action) { - Fragment fragment = fragmentManager.findFragmentByTag(getKey()); - if (fragment == null) { + mFragment = fragmentManager.findFragmentByTag(getKey()); + if (mFragment == null) { Bundle args = new Bundle(); args.putString(Page.KEY_PAGE_ARGUMENT, getKey()); args.putInt(Page.KEY_PAGE_ACTION, action); - fragment = new LoadingFragment(); - fragment.setArguments(args); + mFragment = new LoadingFragment(); + mFragment.setArguments(args); } - return fragment; + return mFragment; } @Override @@ -73,14 +76,13 @@ public class CyanogenServicesPage extends SetupPage { } @Override - public void doLoadAction(SetupWizardActivity context, int action) { + public void doLoadAction(FragmentManager fragmentManager, int action) { if (action == Page.ACTION_PREVIOUS) { getCallbacks().onPreviousPage(); } else { if (!SetupWizardUtils.accountExists(mContext, mContext.getString(R.string.cm_account_type))) { - super.doLoadAction(context, action); - launchCyanogenAccountSetup(context); + super.doLoadAction(fragmentManager, action); } else { getCallbacks().onNextPage(); } @@ -88,6 +90,11 @@ public class CyanogenServicesPage extends SetupPage { } @Override + public void onFragmentReady() { + launchCyanogenAccountSetup(); + } + + @Override public boolean onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == SetupWizardApp.REQUEST_CODE_SETUP_CYANOGEN) { if (resultCode == Activity.RESULT_OK || resultCode == Activity.RESULT_FIRST_USER) { @@ -102,14 +109,13 @@ public class CyanogenServicesPage extends SetupPage { return true; } - - private void launchCyanogenAccountSetup(final Activity activity) { + private void launchCyanogenAccountSetup() { Bundle bundle = new Bundle(); bundle.putBoolean(SetupWizardApp.EXTRA_FIRST_RUN, true); bundle.putBoolean(SetupWizardApp.EXTRA_SHOW_BUTTON_BAR, true); bundle.putBoolean(SetupWizardApp.EXTRA_USE_IMMERSIVE, true); - AccountManager.get(activity) - .addAccount(activity.getString(R.string.cm_account_type), null, null, bundle, + AccountManager.get(mContext) + .addAccount(mContext.getString(R.string.cm_account_type), null, null, bundle, null, new AccountManagerCallback<Bundle>() { @Override public void run(AccountManagerFuture<Bundle> future) { @@ -118,10 +124,10 @@ public class CyanogenServicesPage extends SetupPage { Intent intent = result .getParcelable(AccountManager.KEY_INTENT); ActivityOptions options = - ActivityOptions.makeCustomAnimation(activity, + ActivityOptions.makeCustomAnimation(mContext, android.R.anim.fade_in, android.R.anim.fade_out); - activity.startActivityForResult(intent, + mFragment.startActivityForResult(intent, SetupWizardApp.REQUEST_CODE_SETUP_CYANOGEN, options.toBundle()); } catch (OperationCanceledException e) { diff --git a/src/com/cyanogenmod/setupwizard/setup/CyanogenSettingsPage.java b/src/com/cyanogenmod/setupwizard/setup/CyanogenSettingsPage.java index c35859c..6fce353 100644 --- a/src/com/cyanogenmod/setupwizard/setup/CyanogenSettingsPage.java +++ b/src/com/cyanogenmod/setupwizard/setup/CyanogenSettingsPage.java @@ -20,6 +20,9 @@ import android.app.Fragment; import android.app.FragmentManager; import android.content.Context; import android.content.SharedPreferences; +import android.content.pm.ThemeUtils; +import android.content.res.ThemeConfig; +import android.content.res.ThemeManager; import android.os.Bundle; import android.os.RemoteException; import android.preference.PreferenceManager; @@ -39,7 +42,6 @@ import android.widget.TextView; import com.cyanogenmod.setupwizard.R; import com.cyanogenmod.setupwizard.ui.SetupPageFragment; -import com.cyanogenmod.setupwizard.ui.SetupWizardActivity; import com.cyanogenmod.setupwizard.ui.WebViewDialogFragment; import com.cyanogenmod.setupwizard.util.SetupWizardUtils; import com.cyanogenmod.setupwizard.util.WhisperPushUtils; @@ -56,11 +58,12 @@ public class CyanogenSettingsPage extends SetupPage { public static final String KEY_SEND_METRICS = "send_metrics"; public static final String KEY_REGISTER_WHISPERPUSH = "register"; public static final String KEY_ENABLE_NAV_KEYS = "enable_nav_keys"; + public static final String KEY_APPLY_DEFAULT_THEME = "apply_default_theme"; public static final String SETTING_METRICS = "settings.cyanogen.allow_metrics"; public static final String PRIVACY_POLICY_URI = "https://cyngn.com/legal/privacy-policy"; - public CyanogenSettingsPage(SetupWizardActivity context, SetupDataCallbacks callbacks) { + public CyanogenSettingsPage(Context context, SetupDataCallbacks callbacks) { super(context, callbacks); } @@ -125,6 +128,7 @@ public class CyanogenSettingsPage extends SetupPage { } handleWhisperPushRegistration(); handleEnableMetrics(); + handleDefaultThemeSetup(); } private void handleWhisperPushRegistration() { @@ -146,6 +150,18 @@ public class CyanogenSettingsPage extends SetupPage { } } + private void handleDefaultThemeSetup() { + Bundle privacyData = getData(); + if (!ThemeUtils.getDefaultThemePackageName(mContext).equals(ThemeConfig.SYSTEM_DEFAULT) && + privacyData != null && privacyData.getBoolean(KEY_APPLY_DEFAULT_THEME)) { + Log.i(TAG, "Applying default theme"); + final ThemeManager tm = (ThemeManager) mContext.getSystemService(Context.THEME_SERVICE); + tm.applyDefaultTheme(); + } else { + getCallbacks().finishSetup(); + } + } + private static boolean hideKeyDisabler() { try { return !KeyDisabler.isSupported(); @@ -172,12 +188,18 @@ public class CyanogenSettingsPage extends SetupPage { SetupWizardUtils.isSimMissing(context)); } + protected static boolean hideThemeSwitch(Context context) { + return ThemeUtils.getDefaultThemePackageName(context).equals(ThemeConfig.SYSTEM_DEFAULT); + } + public static class CyanogenSettingsFragment extends SetupPageFragment { private View mMetricsRow; + private View mDefaultThemeRow; private View mNavKeysRow; private View mSecureSmsRow; private CheckBox mMetrics; + private CheckBox mDefaultTheme; private CheckBox mNavKeys; private CheckBox mSecureSms; @@ -191,6 +213,15 @@ public class CyanogenSettingsPage extends SetupPage { } }; + private View.OnClickListener mDefaultThemeClickListener = new View.OnClickListener() { + @Override + public void onClick(View view) { + boolean checked = !mDefaultTheme.isChecked(); + mDefaultTheme.setChecked(checked); + mPage.getData().putBoolean(KEY_APPLY_DEFAULT_THEME, checked); + } + }; + private View.OnClickListener mNavKeysClickListener = new View.OnClickListener() { @Override public void onClick(View view) { @@ -211,6 +242,7 @@ public class CyanogenSettingsPage extends SetupPage { @Override protected void initializePage() { + final Bundle myPageBundle = mPage.getData(); String privacy_policy = getString(R.string.services_privacy_policy); String policySummary = getString(R.string.services_explanation, privacy_policy); SpannableString ss = new SpannableString(policySummary); @@ -242,10 +274,34 @@ public class CyanogenSettingsPage extends SetupPage { metrics.setText(metricsSpan); mMetrics = (CheckBox) mRootView.findViewById(R.id.enable_metrics_checkbox); boolean metricsChecked = - !mPage.getData().containsKey(KEY_SEND_METRICS) || mPage.getData() + !myPageBundle.containsKey(KEY_SEND_METRICS) || myPageBundle .getBoolean(KEY_SEND_METRICS); mMetrics.setChecked(metricsChecked); - mPage.getData().putBoolean(KEY_SEND_METRICS, metricsChecked); + myPageBundle.putBoolean(KEY_SEND_METRICS, metricsChecked); + + mDefaultThemeRow = mRootView.findViewById(R.id.theme); + if (hideThemeSwitch(getActivity())) { + mDefaultThemeRow.setVisibility(View.GONE); + } else { + mDefaultThemeRow.setOnClickListener(mDefaultThemeClickListener); + String defaultTheme = + getString(R.string.services_apply_theme, + getString(R.string.default_theme_name)); + String defaultThemeSummary = getString(R.string.services_apply_theme_label, + defaultTheme); + final SpannableStringBuilder themeSpan = + new SpannableStringBuilder(defaultThemeSummary); + themeSpan.setSpan(new android.text.style.StyleSpan(android.graphics.Typeface.BOLD), + 0, defaultTheme.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); + TextView theme = (TextView) mRootView.findViewById(R.id.enable_theme_summary); + theme.setText(themeSpan); + mDefaultTheme = (CheckBox) mRootView.findViewById(R.id.enable_theme_checkbox); + boolean themesChecked = + !myPageBundle.containsKey(KEY_APPLY_DEFAULT_THEME) || myPageBundle + .getBoolean(KEY_APPLY_DEFAULT_THEME); + mDefaultTheme.setChecked(themesChecked); + myPageBundle.putBoolean(KEY_APPLY_DEFAULT_THEME, themesChecked); + } mNavKeysRow = mRootView.findViewById(R.id.nav_keys); mNavKeysRow.setOnClickListener(mNavKeysClickListener); @@ -279,11 +335,11 @@ public class CyanogenSettingsPage extends SetupPage { mSecureSmsRow.setVisibility(View.GONE); } mSecureSms = (CheckBox) mRootView.findViewById(R.id.secure_sms_checkbox); - boolean smsChecked = mPage.getData().containsKey(KEY_REGISTER_WHISPERPUSH) ? - mPage.getData().getBoolean(KEY_REGISTER_WHISPERPUSH) : + boolean smsChecked = myPageBundle.containsKey(KEY_REGISTER_WHISPERPUSH) ? + myPageBundle.getBoolean(KEY_REGISTER_WHISPERPUSH) : false; mSecureSms.setChecked(smsChecked); - mPage.getData().putBoolean(KEY_REGISTER_WHISPERPUSH, smsChecked); + myPageBundle.putBoolean(KEY_REGISTER_WHISPERPUSH, smsChecked); } @Override diff --git a/src/com/cyanogenmod/setupwizard/setup/DateTimePage.java b/src/com/cyanogenmod/setupwizard/setup/DateTimePage.java index d953a53..0e04fd9 100644 --- a/src/com/cyanogenmod/setupwizard/setup/DateTimePage.java +++ b/src/com/cyanogenmod/setupwizard/setup/DateTimePage.java @@ -43,7 +43,6 @@ import android.widget.TimePicker; import com.cyanogenmod.setupwizard.R; import com.cyanogenmod.setupwizard.ui.SetupPageFragment; -import com.cyanogenmod.setupwizard.ui.SetupWizardActivity; import org.xmlpull.v1.XmlPullParserException; @@ -69,7 +68,7 @@ public class DateTimePage extends SetupPage { private static final int HOURS_1 = 60 * 60000; - public DateTimePage(SetupWizardActivity context, SetupDataCallbacks callbacks) { + public DateTimePage(Context context, SetupDataCallbacks callbacks) { super(context, callbacks); } diff --git a/src/com/cyanogenmod/setupwizard/setup/FinishPage.java b/src/com/cyanogenmod/setupwizard/setup/FinishPage.java index a45a648..fa25bd1 100644 --- a/src/com/cyanogenmod/setupwizard/setup/FinishPage.java +++ b/src/com/cyanogenmod/setupwizard/setup/FinishPage.java @@ -18,11 +18,11 @@ package com.cyanogenmod.setupwizard.setup; import android.app.Fragment; import android.app.FragmentManager; +import android.content.Context; import android.os.Bundle; import com.cyanogenmod.setupwizard.R; import com.cyanogenmod.setupwizard.ui.SetupPageFragment; -import com.cyanogenmod.setupwizard.ui.SetupWizardActivity; public class FinishPage extends SetupPage { @@ -30,7 +30,7 @@ public class FinishPage extends SetupPage { private FinishFragment mFinishFragment; - public FinishPage(SetupWizardActivity context, SetupDataCallbacks callbacks) { + public FinishPage(Context context, SetupDataCallbacks callbacks) { super(context, callbacks); } diff --git a/src/com/cyanogenmod/setupwizard/setup/GmsAccountPage.java b/src/com/cyanogenmod/setupwizard/setup/GmsAccountPage.java index 9cacb0b..20f25c1 100644 --- a/src/com/cyanogenmod/setupwizard/setup/GmsAccountPage.java +++ b/src/com/cyanogenmod/setupwizard/setup/GmsAccountPage.java @@ -27,15 +27,16 @@ 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 android.util.Log; import com.cyanogenmod.setupwizard.R; import com.cyanogenmod.setupwizard.SetupWizardApp; import com.cyanogenmod.setupwizard.ui.LoadingFragment; -import com.cyanogenmod.setupwizard.ui.SetupWizardActivity; import com.cyanogenmod.setupwizard.util.SetupWizardUtils; import java.io.IOException; @@ -55,7 +56,9 @@ public class GmsAccountPage extends SetupPage { private boolean mBackupEnabled = false; - public GmsAccountPage(final SetupWizardActivity context, SetupDataCallbacks callbacks) { + private Fragment mFragment; + + public GmsAccountPage(final Context context, SetupDataCallbacks callbacks) { super(context, callbacks); final ContentResolver res = context.getContentResolver(); mBackupEnabled = Settings.Secure.getInt(res, @@ -78,15 +81,15 @@ public class GmsAccountPage extends SetupPage { @Override public Fragment getFragment(FragmentManager fragmentManager, int action) { - Fragment fragment = fragmentManager.findFragmentByTag(getKey()); - if (fragment == null) { + mFragment = fragmentManager.findFragmentByTag(getKey()); + if (mFragment == null) { Bundle args = new Bundle(); args.putString(Page.KEY_PAGE_ARGUMENT, getKey()); args.putInt(Page.KEY_PAGE_ACTION, action); - fragment = new LoadingFragment(); - fragment.setArguments(args); + mFragment = new LoadingFragment(); + mFragment.setArguments(args); } - return fragment; + return mFragment; } @Override @@ -105,20 +108,24 @@ public class GmsAccountPage extends SetupPage { } @Override - public void doLoadAction(SetupWizardActivity context, int action) { + public void doLoadAction(FragmentManager fragmentManager, int action) { if (action == Page.ACTION_PREVIOUS) { getCallbacks().onPreviousPage(); } else { - launchGmsAccountSetup(context); - super.doLoadAction(context, action); + super.doLoadAction(fragmentManager, action); } } @Override + public void onFragmentReady() { + launchGmsAccountSetup(); + } + + @Override public boolean onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == SetupWizardApp.REQUEST_CODE_SETUP_GMS) { if (!mBackupEnabled && SetupWizardUtils.isOwner() && resultCode == Activity.RESULT_OK) { - launchGmsRestorePage(mContext); + launchGmsRestorePage(); } else { handleResult(resultCode); } @@ -131,10 +138,13 @@ public class GmsAccountPage extends SetupPage { @Override public void onFinishSetup() { - if (mContentQueryMap != null) { - mContentQueryMap.close(); + try { + if (mContentQueryMap != null) { + mContentQueryMap.close(); + } + } catch (Exception e) { + Log.wtf(TAG, e.toString()); } - } private void handleResult(int resultCode) { @@ -148,10 +158,10 @@ public class GmsAccountPage extends SetupPage { } } - private static void launchGmsRestorePage(final SetupWizardActivity activity) { + private void launchGmsRestorePage() { try { // GMS can disable this after logging in sometimes - SetupWizardUtils.enableGMSSetupWizard(activity); + SetupWizardUtils.enableGMSSetupWizard(mContext); Intent intent = new Intent(ACTION_RESTORE); intent.putExtra(SetupWizardApp.EXTRA_ALLOW_SKIP, true); intent.putExtra(SetupWizardApp.EXTRA_USE_IMMERSIVE, true); @@ -161,27 +171,27 @@ public class GmsAccountPage extends SetupPage { // This is necessary to get the material theme on the restore page. intent.putExtra("scriptUri", RESTORE_WIZARD_SCRIPT); ActivityOptions options = - ActivityOptions.makeCustomAnimation(activity, + ActivityOptions.makeCustomAnimation(mContext, android.R.anim.fade_in, android.R.anim.fade_out); - activity.startActivityForResult( + mFragment.startActivityForResult( intent, SetupWizardApp.REQUEST_CODE_RESTORE_GMS, options.toBundle()); } catch (Exception e) { e.printStackTrace(); // XXX: In open source, we don't know what gms version a user has. // Bail if the restore activity is not found. - activity.onNextPage(); + getCallbacks().onNextPage(); } } - private void launchGmsAccountSetup(final SetupWizardActivity activity) { + private void launchGmsAccountSetup() { Bundle bundle = new Bundle(); bundle.putBoolean(SetupWizardApp.EXTRA_FIRST_RUN, true); bundle.putBoolean(SetupWizardApp.EXTRA_ALLOW_SKIP, true); bundle.putBoolean(SetupWizardApp.EXTRA_USE_IMMERSIVE, true); AccountManager - .get(activity).addAccount(SetupWizardApp.ACCOUNT_TYPE_GMS, null, null, + .get(mContext).addAccount(SetupWizardApp.ACCOUNT_TYPE_GMS, null, null, bundle, null, new AccountManagerCallback<Bundle>() { @Override public void run(AccountManagerFuture<Bundle> future) { @@ -190,10 +200,10 @@ public class GmsAccountPage extends SetupPage { Intent intent = result .getParcelable(AccountManager.KEY_INTENT); ActivityOptions options = - ActivityOptions.makeCustomAnimation(activity, + ActivityOptions.makeCustomAnimation(mContext, android.R.anim.fade_in, android.R.anim.fade_out); - activity.startActivityForResult(intent, + mFragment.startActivityForResult(intent, SetupWizardApp.REQUEST_CODE_SETUP_GMS, options.toBundle()); } catch (OperationCanceledException e) { } catch (IOException e) { diff --git a/src/com/cyanogenmod/setupwizard/setup/MobileDataPage.java b/src/com/cyanogenmod/setupwizard/setup/MobileDataPage.java index efa2516..d8866bf 100644 --- a/src/com/cyanogenmod/setupwizard/setup/MobileDataPage.java +++ b/src/com/cyanogenmod/setupwizard/setup/MobileDataPage.java @@ -34,14 +34,13 @@ import android.widget.TextView; import com.cyanogenmod.setupwizard.R; import com.cyanogenmod.setupwizard.ui.SetupPageFragment; -import com.cyanogenmod.setupwizard.ui.SetupWizardActivity; import com.cyanogenmod.setupwizard.util.SetupWizardUtils; public class MobileDataPage extends SetupPage { public static final String TAG = "MobileDataPage"; - public MobileDataPage(SetupWizardActivity context, SetupDataCallbacks callbacks) { + public MobileDataPage(Context context, SetupDataCallbacks callbacks) { super(context, callbacks); } diff --git a/src/com/cyanogenmod/setupwizard/setup/OtherSettingsPage.java b/src/com/cyanogenmod/setupwizard/setup/OtherSettingsPage.java index ee6d206..d6293e9 100644 --- a/src/com/cyanogenmod/setupwizard/setup/OtherSettingsPage.java +++ b/src/com/cyanogenmod/setupwizard/setup/OtherSettingsPage.java @@ -39,7 +39,6 @@ import android.widget.TextView; import com.cyanogenmod.setupwizard.R; import com.cyanogenmod.setupwizard.SetupWizardApp; import com.cyanogenmod.setupwizard.ui.SetupPageFragment; -import com.cyanogenmod.setupwizard.ui.SetupWizardActivity; import com.cyanogenmod.setupwizard.ui.WebViewDialogFragment; import com.cyanogenmod.setupwizard.util.SetupWizardUtils; @@ -53,7 +52,7 @@ public class OtherSettingsPage extends SetupPage { private static final String PRIVACY_POLICY_URI = "https://www.google.com/intl/en/policies/privacy/?fg=1"; - public OtherSettingsPage(SetupWizardActivity context, SetupDataCallbacks callbacks) { + public OtherSettingsPage(Context context, SetupDataCallbacks callbacks) { super(context, callbacks); } diff --git a/src/com/cyanogenmod/setupwizard/setup/Page.java b/src/com/cyanogenmod/setupwizard/setup/Page.java index 4ab2f40..799749a 100644 --- a/src/com/cyanogenmod/setupwizard/setup/Page.java +++ b/src/com/cyanogenmod/setupwizard/setup/Page.java @@ -21,8 +21,6 @@ import android.app.FragmentManager; import android.content.Intent; import android.os.Bundle; -import com.cyanogenmod.setupwizard.ui.SetupWizardActivity; - public interface Page { public static final String KEY_PAGE_ARGUMENT = "key_arg"; @@ -44,7 +42,8 @@ public interface Page { public Page setHidden(boolean hidden); public boolean doPreviousAction(); public boolean doNextAction(); - public void doLoadAction(SetupWizardActivity context, int action); + public void doLoadAction(FragmentManager fragmentManager, int action); + public void onFragmentReady(); public void onFinishSetup(); public boolean onActivityResult(int requestCode, int resultCode, Intent data); public SetupDataCallbacks getCallbacks(); diff --git a/src/com/cyanogenmod/setupwizard/setup/SetupDataCallbacks.java b/src/com/cyanogenmod/setupwizard/setup/SetupDataCallbacks.java index 4deee8d..b4127a6 100644 --- a/src/com/cyanogenmod/setupwizard/setup/SetupDataCallbacks.java +++ b/src/com/cyanogenmod/setupwizard/setup/SetupDataCallbacks.java @@ -22,6 +22,7 @@ public interface SetupDataCallbacks { void onPageLoaded(Page page); void onPageTreeChanged(); void onFinish(); + void finishSetup(); boolean isCurrentPage(Page page); Page getPage(String key); Page getPage(int key); diff --git a/src/com/cyanogenmod/setupwizard/setup/SetupPage.java b/src/com/cyanogenmod/setupwizard/setup/SetupPage.java index fe3fca3..476f911 100644 --- a/src/com/cyanogenmod/setupwizard/setup/SetupPage.java +++ b/src/com/cyanogenmod/setupwizard/setup/SetupPage.java @@ -16,10 +16,6 @@ package com.cyanogenmod.setupwizard.setup; -import com.cyanogenmod.setupwizard.R; -import com.cyanogenmod.setupwizard.ui.SetupWizardActivity; - -import android.app.Activity; import android.app.Fragment; import android.app.FragmentManager; import android.content.Context; @@ -29,6 +25,8 @@ import android.transition.Slide; import android.transition.Transition; import android.view.Gravity; +import com.cyanogenmod.setupwizard.R; + public abstract class SetupPage implements Page { @@ -38,9 +36,9 @@ public abstract class SetupPage implements Page { private boolean mRequired = false; private boolean mHidden = false; - protected final SetupWizardActivity mContext; + protected final Context mContext; - protected SetupPage(SetupWizardActivity context, SetupDataCallbacks callbacks) { + protected SetupPage(Context context, SetupDataCallbacks callbacks) { mContext = context; mCallbacks = callbacks; } @@ -74,9 +72,7 @@ public abstract class SetupPage implements Page { public void onFinishSetup() {} @Override - public void doLoadAction(SetupWizardActivity context, int action) { - if (context == null || context.isFinishing()) { return; } - final FragmentManager fragmentManager = context.getFragmentManager(); + public void doLoadAction(FragmentManager fragmentManager, int action) { Fragment fragment = getFragment(fragmentManager, action); if (action == Page.ACTION_NEXT) { Transition t = new Slide(Gravity.RIGHT); @@ -94,6 +90,9 @@ public abstract class SetupPage implements Page { } @Override + public void onFragmentReady(){} + + @Override public boolean onActivityResult(int requestCode, int resultCode, Intent data) { return false; } diff --git a/src/com/cyanogenmod/setupwizard/setup/SimCardMissingPage.java b/src/com/cyanogenmod/setupwizard/setup/SimCardMissingPage.java index 04d2ce2..36b4a32 100644 --- a/src/com/cyanogenmod/setupwizard/setup/SimCardMissingPage.java +++ b/src/com/cyanogenmod/setupwizard/setup/SimCardMissingPage.java @@ -18,6 +18,7 @@ package com.cyanogenmod.setupwizard.setup; import android.app.Fragment; import android.app.FragmentManager; +import android.content.Context; import android.os.Bundle; import android.widget.ImageView; @@ -33,7 +34,7 @@ public class SimCardMissingPage extends SetupPage { private static final int SIM_SIDE = 1; private static final int SIM_BACK = 2; - public SimCardMissingPage(SetupWizardActivity context, SetupDataCallbacks callbacks) { + public SimCardMissingPage(Context context, SetupDataCallbacks callbacks) { super(context, callbacks); } diff --git a/src/com/cyanogenmod/setupwizard/setup/WelcomePage.java b/src/com/cyanogenmod/setupwizard/setup/WelcomePage.java index bd88d44..e919267 100644 --- a/src/com/cyanogenmod/setupwizard/setup/WelcomePage.java +++ b/src/com/cyanogenmod/setupwizard/setup/WelcomePage.java @@ -19,6 +19,7 @@ package com.cyanogenmod.setupwizard.setup; import android.app.ActivityOptions; import android.app.Fragment; import android.app.FragmentManager; +import android.content.Context; import android.content.Intent; import android.content.res.Configuration; import android.content.res.Resources; @@ -31,7 +32,6 @@ import android.widget.NumberPicker; import com.cyanogenmod.setupwizard.R; import com.cyanogenmod.setupwizard.ui.LocalePicker; import com.cyanogenmod.setupwizard.ui.SetupPageFragment; -import com.cyanogenmod.setupwizard.ui.SetupWizardActivity; import java.util.Locale; @@ -41,7 +41,7 @@ public class WelcomePage extends SetupPage { private static final String ACTION_EMERGENCY_DIAL = "com.android.phone.EmergencyDialer.DIAL"; - public WelcomePage(SetupWizardActivity context, SetupDataCallbacks callbacks) { + public WelcomePage(Context context, SetupDataCallbacks callbacks) { super(context, callbacks); } diff --git a/src/com/cyanogenmod/setupwizard/setup/WifiSetupPage.java b/src/com/cyanogenmod/setupwizard/setup/WifiSetupPage.java index 4a526b3..2dc2c94 100644 --- a/src/com/cyanogenmod/setupwizard/setup/WifiSetupPage.java +++ b/src/com/cyanogenmod/setupwizard/setup/WifiSetupPage.java @@ -18,8 +18,8 @@ package com.cyanogenmod.setupwizard.setup; import android.app.Activity; import android.app.ActivityOptions; -import android.app.Fragment; import android.app.FragmentManager; +import android.content.Context; import android.content.Intent; import android.net.ConnectivityManager; import android.os.Bundle; @@ -30,7 +30,7 @@ import android.util.Log; import com.cyanogenmod.setupwizard.R; import com.cyanogenmod.setupwizard.SetupWizardApp; import com.cyanogenmod.setupwizard.ui.LoadingFragment; -import com.cyanogenmod.setupwizard.ui.SetupWizardActivity; +import com.cyanogenmod.setupwizard.ui.SetupPageFragment; import com.cyanogenmod.setupwizard.util.SetupWizardUtils; import java.io.IOException; @@ -59,10 +59,9 @@ public class WifiSetupPage extends SetupPage { private Runnable mFinishCaptivePortalCheckRunnable = new Runnable() { @Override public void run() { - final SetupWizardActivity activity = mContext; if (mIsCaptivePortal) { try { - int netId = ConnectivityManager.from(activity) + int netId = ConnectivityManager.from(mContext) .getNetworkForType(ConnectivityManager.TYPE_WIFI).netId; Intent intent = new Intent(CAPTIVE_PORTAL_LOGIN_ACTION); intent.putExtra(Intent.EXTRA_TEXT, String.valueOf(netId)); @@ -76,25 +75,25 @@ public class WifiSetupPage extends SetupPage { ActivityOptions.makeCustomAnimation(mContext, android.R.anim.fade_in, android.R.anim.fade_out); - activity.startActivityForResult(intent, + mLoadingFragment.startActivityForResult(intent, SetupWizardApp.REQUEST_CODE_SETUP_CAPTIVE_PORTAL, options.toBundle()); } catch (Exception e) { //Oh well Log.e(TAG, "No captive portal activity found" + e); - if (activity.isCurrentPage(WifiSetupPage.this)) { + if (getCallbacks().isCurrentPage(WifiSetupPage.this)) { getCallbacks().onNextPage(); } } } else { - if (activity.isCurrentPage(WifiSetupPage.this)) { + if (getCallbacks().isCurrentPage(WifiSetupPage.this)) { getCallbacks().onNextPage(); } } } }; - public WifiSetupPage(SetupWizardActivity context, SetupDataCallbacks callbacks) { + public WifiSetupPage(Context context, SetupDataCallbacks callbacks) { super(context, callbacks); String server = Settings.Global.getString(context.getContentResolver(), "captive_portal_server"); if (server == null) server = DEFAULT_SERVER; @@ -106,7 +105,7 @@ public class WifiSetupPage extends SetupPage { } @Override - public Fragment getFragment(FragmentManager fragmentManager, int action) { + public SetupPageFragment getFragment(FragmentManager fragmentManager, int action) { mLoadingFragment = (LoadingFragment)fragmentManager.findFragmentByTag(getKey()); if (mLoadingFragment == null) { Bundle args = new Bundle(); @@ -134,9 +133,8 @@ public class WifiSetupPage extends SetupPage { } @Override - public void doLoadAction(SetupWizardActivity context, int action) { - super.doLoadAction(context, action); - SetupWizardUtils.launchWifiSetup(context); + public void onFragmentReady() { + launchWifiSetup(); } @Override @@ -151,7 +149,7 @@ public class WifiSetupPage extends SetupPage { } } else if (requestCode == SetupWizardApp.REQUEST_CODE_SETUP_CAPTIVE_PORTAL) { if (resultCode == Activity.RESULT_CANCELED) { - SetupWizardUtils.launchWifiSetup(mContext); + launchWifiSetup(); } else { getCallbacks().onNextPage(); } @@ -183,6 +181,11 @@ public class WifiSetupPage extends SetupPage { urlConnection.setUseCaches(false); urlConnection.getInputStream(); // We got a valid response, but not from the real google + final int responseCode = urlConnection.getResponseCode(); + if (responseCode == 408 || responseCode == 504) { + // If we timeout here, we'll try and go through captive portal login + return true; + } return urlConnection.getResponseCode() != 204; } catch (IOException e) { Log.e(TAG, "Captive portal check - probably not a portal: exception " @@ -194,4 +197,20 @@ public class WifiSetupPage extends SetupPage { } } } + + private void launchWifiSetup() { + SetupWizardUtils.tryEnablingWifi(mContext); + Intent intent = new Intent(SetupWizardApp.ACTION_SETUP_WIFI); + intent.putExtra(SetupWizardApp.EXTRA_FIRST_RUN, true); + intent.putExtra(SetupWizardApp.EXTRA_ALLOW_SKIP, true); + intent.putExtra(SetupWizardApp.EXTRA_USE_IMMERSIVE, true); + intent.putExtra(SetupWizardApp.EXTRA_THEME, SetupWizardApp.EXTRA_MATERIAL_LIGHT); + intent.putExtra(SetupWizardApp.EXTRA_AUTO_FINISH, false); + ActivityOptions options = + ActivityOptions.makeCustomAnimation(mContext, + android.R.anim.fade_in, + android.R.anim.fade_out); + mLoadingFragment.startActivityForResult(intent, + SetupWizardApp.REQUEST_CODE_SETUP_WIFI, options.toBundle()); + } } |