From 48ca24eb9b92c9b62970c3a12e963f497fc65bbc Mon Sep 17 00:00:00 2001 From: cretin45 Date: Mon, 19 Jan 2015 14:29:43 -0800 Subject: [PATCH] SetupWizard: Add Cyanogen services page Change-Id: I39970b1b659a3a7ed3bb5b4350707aa0e53aba24 --- Android.mk | 2 +- AndroidManifest.xml | 20 +- res/layout/setup_cyanogen_services.xml | 146 +++++++++ res/values/strings.xml | 9 +- .../setupwizard/SetupWizardApp.java | 2 + .../setupwizard/setup/CMSetupWizardData.java | 2 +- .../setup/CyanogenAccountPage.java | 83 ----- .../setup/CyanogenServicesPage.java | 307 ++++++++++++++++++ .../setupwizard/setup/GmsAccountPage.java | 14 +- .../cyanogenmod/setupwizard/setup/Page.java | 3 +- .../setupwizard/setup/SetupPage.java | 2 +- .../setupwizard/setup/WelcomePage.java | 13 + .../setupwizard/ui/SetupWizardActivity.java | 31 ++ .../setupwizard/util/SetupWizardUtils.java | 5 + .../setupwizard/util/WhisperPushUtils.java | 78 +++++ tests/Android.mk | 4 +- tests/res/values/strings.xml | 2 +- .../account/tests/ManualTestActivity.java | 2 +- 18 files changed, 625 insertions(+), 100 deletions(-) create mode 100644 res/layout/setup_cyanogen_services.xml delete mode 100644 src/com/cyanogenmod/setupwizard/setup/CyanogenAccountPage.java create mode 100644 src/com/cyanogenmod/setupwizard/setup/CyanogenServicesPage.java create mode 100644 src/com/cyanogenmod/setupwizard/util/WhisperPushUtils.java diff --git a/Android.mk b/Android.mk index 26b9dfd..8667495 100644 --- a/Android.mk +++ b/Android.mk @@ -5,7 +5,7 @@ LOCAL_SRC_FILES := $(call all-java-files-under, src) LOCAL_MODULE_TAGS := optional -LOCAL_PACKAGE_NAME := CMSetupWizard +LOCAL_PACKAGE_NAME := CyanogenSetupWizard LOCAL_CERTIFICATE := platform LOCAL_PRIVILEGED_MODULE := true diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 8dd4b9b..3b1a3b3 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -16,7 +16,8 @@ --> + android:versionCode="3" + android:sharedUserId="android.uid.system"> @@ -39,6 +40,21 @@ + + + + + + + + + @@ -47,6 +63,8 @@ android:theme="@style/Theme.Setup" android:name=".SetupWizardApp"> + + diff --git a/res/layout/setup_cyanogen_services.xml b/res/layout/setup_cyanogen_services.xml new file mode 100644 index 0000000..2f705ba --- /dev/null +++ b/res/layout/setup_cyanogen_services.xml @@ -0,0 +1,146 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/res/values/strings.xml b/res/values/strings.xml index 60d96f7..bda7eac 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -51,7 +51,7 @@ 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. - Use Google’s location service to help apps determine your location. This means sending annonymous location data to Google, even when no apps are running. + Use Google\'s location service to help apps determine your location. This means sending annonymous location data to Google, even when no apps are running. Turn On Mobile Data No service @@ -63,4 +63,11 @@ SIM %d - %s Emergency Call + + Cyanogen services + These services work for you to extend the capabilities of your device. You can turn them off at anytime. Data will be used in accordance with Cyanogen\'s %s. + Privacy Policy + Help improve Cyanogen OS by automatically sending diagnostic and usage data to Cyanogen. This information won’t be used to identify you and lends a hand to teams working on things like battery life, app performance, and new Cyanogen OS features. + Use on screen navigation keys to move between screens instead of hardware keys. + Use secure SMS to encrypt SMS conversations with other users using secure SMS on a Cyanogen OS device. diff --git a/src/com/cyanogenmod/setupwizard/SetupWizardApp.java b/src/com/cyanogenmod/setupwizard/SetupWizardApp.java index 4df1194..240d4b0 100644 --- a/src/com/cyanogenmod/setupwizard/SetupWizardApp.java +++ b/src/com/cyanogenmod/setupwizard/SetupWizardApp.java @@ -27,6 +27,8 @@ public class SetupWizardApp extends Application { // Leave this off for release public static final boolean DEBUG = false; + public static final String ACTION_FINISHED = "com.cyanogenmod.setupwizard.SETUP_FINISHED"; + public static final String ACCOUNT_TYPE_CYANOGEN = "com.cyanogen"; public static final String ACCOUNT_TYPE_GMS = "com.google"; diff --git a/src/com/cyanogenmod/setupwizard/setup/CMSetupWizardData.java b/src/com/cyanogenmod/setupwizard/setup/CMSetupWizardData.java index e50f1ac..9ebaf78 100644 --- a/src/com/cyanogenmod/setupwizard/setup/CMSetupWizardData.java +++ b/src/com/cyanogenmod/setupwizard/setup/CMSetupWizardData.java @@ -48,7 +48,7 @@ public class CMSetupWizardData extends AbstractSetupData { if (SetupWizardUtils.hasGMS(mContext)) { pages.add(new GmsAccountPage(mContext, this)); } - pages.add(new CyanogenAccountPage(mContext, this)); + pages.add(new CyanogenServicesPage(mContext, this)); pages.add(new LocationSettingsPage(mContext, this)); pages.add(new DateTimePage(mContext, this)); pages.add(new FinishPage(mContext, this)); diff --git a/src/com/cyanogenmod/setupwizard/setup/CyanogenAccountPage.java b/src/com/cyanogenmod/setupwizard/setup/CyanogenAccountPage.java deleted file mode 100644 index dc48faa..0000000 --- a/src/com/cyanogenmod/setupwizard/setup/CyanogenAccountPage.java +++ /dev/null @@ -1,83 +0,0 @@ -/* - * Copyright (C) 2013 The CyanogenMod Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.cyanogenmod.setupwizard.setup; - -import com.cyanogenmod.setupwizard.SetupWizardApp; -import com.cyanogenmod.setupwizard.R; - -import android.accounts.AccountManager; -import android.accounts.AccountManagerCallback; -import android.accounts.AccountManagerFuture; -import android.app.Activity; -import android.content.Context; -import android.os.Bundle; - -public class CyanogenAccountPage extends SetupPage { - - public static final String TAG = "CyanogenAccountPage"; - - public CyanogenAccountPage(Context context, SetupDataCallbacks callbacks) { - super(context, callbacks); - } - - @Override - public int getNextButtonTitleResId() { - return R.string.skip; - } - - @Override - public String getKey() { - return TAG; - } - - @Override - public int getTitleResId() { - return -1; - } - - @Override - public void doLoadAction(Activity context, int action) { - launchCyanogenAccountSetup(context, action); - } - - public void launchCyanogenAccountSetup(final Activity activity, final int action) { - Bundle bundle = new Bundle(); - bundle.putBoolean(SetupWizardApp.EXTRA_FIRST_RUN, true); - AccountManager - .get(activity).addAccount(SetupWizardApp.ACCOUNT_TYPE_CYANOGEN, null, null, bundle, - activity, new AccountManagerCallback() { - @Override - public void run(AccountManagerFuture bundleAccountManagerFuture) { - if (activity == null) return; //There is a chance this activity has been torn down. - if (accountExists(activity, SetupWizardApp.ACCOUNT_TYPE_CYANOGEN)) { - setCompleted(true); - getCallbacks().onNextPage(); - } else { - if (action == Page.ACTION_NEXT) { - getCallbacks().onNextPage(); - } else { - getCallbacks().onPreviousPage(); - } - } - } - }, null); - } - - private boolean accountExists(Activity activity, String accountType) { - return AccountManager.get(activity).getAccountsByType(accountType).length > 0; - } -} diff --git a/src/com/cyanogenmod/setupwizard/setup/CyanogenServicesPage.java b/src/com/cyanogenmod/setupwizard/setup/CyanogenServicesPage.java new file mode 100644 index 0000000..2949b9e --- /dev/null +++ b/src/com/cyanogenmod/setupwizard/setup/CyanogenServicesPage.java @@ -0,0 +1,307 @@ +/* + * Copyright (C) 2013 The CyanogenMod Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.cyanogenmod.setupwizard.setup; + +import android.accounts.AccountManager; +import android.accounts.AccountManagerCallback; +import android.accounts.AccountManagerFuture; +import android.app.Activity; +import android.app.Fragment; +import android.content.Context; +import android.content.SharedPreferences; +import android.os.Bundle; +import android.os.Handler; +import android.os.RemoteException; +import android.preference.PreferenceManager; +import android.provider.Settings; +import android.text.SpannableString; +import android.text.Spanned; +import android.text.method.LinkMovementMethod; +import android.text.style.ClickableSpan; +import android.view.IWindowManager; +import android.view.View; +import android.view.WindowManagerGlobal; +import android.widget.CheckBox; +import android.widget.TextView; + +import com.cyanogenmod.setupwizard.R; +import com.cyanogenmod.setupwizard.SetupWizardApp; +import com.cyanogenmod.setupwizard.ui.SetupPageFragment; +import com.cyanogenmod.setupwizard.util.SetupWizardUtils; + +import com.google.android.gms.common.ConnectionResult; +import com.google.android.gms.common.GooglePlayServicesUtil; + +import org.cyanogenmod.hardware.KeyDisabler; + +public class CyanogenServicesPage extends SetupPage { + + public static final String TAG = "CyanogenServicesPage"; + + public static final String KEY_SEND_METRICS = "send_metrics"; + public static final String KEY_REGISTER_WHISPERPUSH = "register"; + + public static final String SETTING_METRICS = "settings.cyanogen.allow_metrics"; + + public CyanogenServicesPage(Context context, SetupDataCallbacks callbacks) { + super(context, callbacks); + } + + @Override + public Fragment getFragment() { + Bundle args = new Bundle(); + args.putString(Page.KEY_PAGE_ARGUMENT, getKey()); + + CyanogenServicesFragment fragment = new CyanogenServicesFragment(); + fragment.setArguments(args); + return fragment; + } + + @Override + public String getKey() { + return TAG; + } + + @Override + public int getTitleResId() { + return R.string.setup_services; + } + + private static void writeDisableNavkeysOption(Context context, boolean enabled) { + final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); + final int defaultBrightness = context.getResources().getInteger( + com.android.internal.R.integer.config_buttonBrightnessSettingDefault); + + Settings.System.putInt(context.getContentResolver(), + Settings.System.DEV_FORCE_SHOW_NAVBAR, enabled ? 1 : 0); + KeyDisabler.setActive(enabled); + + /* Save/restore button timeouts to disable them in softkey mode */ + SharedPreferences.Editor editor = prefs.edit(); + + if (enabled) { + int currentBrightness = Settings.System.getInt(context.getContentResolver(), + Settings.System.BUTTON_BRIGHTNESS, defaultBrightness); + if (!prefs.contains("pre_navbar_button_backlight")) { + editor.putInt("pre_navbar_button_backlight", currentBrightness); + } + Settings.System.putInt(context.getContentResolver(), + Settings.System.BUTTON_BRIGHTNESS, 0); + } else { + int oldBright = prefs.getInt("pre_navbar_button_backlight", -1); + if (oldBright != -1) { + Settings.System.putInt(context.getContentResolver(), + Settings.System.BUTTON_BRIGHTNESS, oldBright); + editor.remove("pre_navbar_button_backlight"); + } + } + editor.commit(); + } + + private static boolean hideKeyDisabler() { + try { + return !KeyDisabler.isSupported(); + } catch (NoClassDefFoundError e) { + // Hardware abstraction framework not installed + return true; + } + } + + private static boolean hideWhisperPush(Context context) { + final int playServicesAvailable = GooglePlayServicesUtil + .isGooglePlayServicesAvailable(context); + return playServicesAvailable != ConnectionResult.SUCCESS + || (SetupWizardUtils.isGSMPhone(context) && SetupWizardUtils.isSimMissing(context)); + } + + public static class CyanogenServicesFragment extends SetupPageFragment { + + private View mMetricsRow; + private View mNavKeysRow; + private View mSecureSmsRow; + private CheckBox mMetrics; + private CheckBox mNavKeys; + private CheckBox mSecureSms; + + private Runnable mDeferredAction; + + private Handler mHandler; + + + private View.OnClickListener mMetricsClickListener = new View.OnClickListener() { + @Override + public void onClick(View view) { + boolean checked = !mMetrics.isChecked(); + mMetrics.setChecked(checked); + mPage.getData().putBoolean(KEY_SEND_METRICS, checked); + } + }; + + private View.OnClickListener mNavKeysClickListener = new View.OnClickListener() { + @Override + public void onClick(View view) { + mNavKeys.setEnabled(false); + boolean checked = !mNavKeys.isChecked(); + writeDisableNavkeysOption(getActivity(), checked); + updateDisableNavkeysOption(); + mHandler.postDelayed(new Runnable() { + @Override + public void run() { + mNavKeys.setEnabled(true); + } + }, 1000); + } + }; + + private View.OnClickListener mSecureSmsClickListener = new View.OnClickListener() { + @Override + public void onClick(View view) { + boolean checked = !mSecureSms.isChecked(); + mSecureSms.setChecked(checked); + mPage.getData().putBoolean(KEY_REGISTER_WHISPERPUSH, checked); + } + }; + + public CyanogenServicesFragment() { + super(); + mHandler = new Handler(); + } + + @Override + public void onActivityCreated(Bundle savedInstanceState) { + super.onActivityCreated(savedInstanceState); + getActivity().getWindow().setStatusBarColor(getResources().getColor(R.color.primary_dark)); + if (!SetupWizardUtils.accountExists(getActivity(), + SetupWizardApp.ACCOUNT_TYPE_CYANOGEN)) { + launchCyanogenAccountSetup(getActivity()); + } + } + + @Override + protected void initializePage() { + String privacy_policy = getString(R.string.services_privacy_policy); + String summary = getString(R.string.services_explanation, privacy_policy); + SpannableString ss = new SpannableString(summary); + ClickableSpan clickableSpan = new ClickableSpan() { + @Override + public void onClick(View textView) { + //TDB privacy policy + } + }; + ss.setSpan(clickableSpan, + summary.length() - privacy_policy.length() - 1, + summary.length() - 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + TextView textView = (TextView) mRootView.findViewById(R.id.privacy_policy); + textView.setMovementMethod(LinkMovementMethod.getInstance()); + textView.setText(ss); + mMetricsRow = mRootView.findViewById(R.id.metrics); + mMetricsRow.setOnClickListener(mMetricsClickListener); + mMetrics = (CheckBox) mRootView.findViewById(R.id.enable_metrics_checkbox); + boolean metricsChecked = + !mPage.getData().containsKey(KEY_SEND_METRICS) || mPage.getData() + .getBoolean(KEY_SEND_METRICS); + mMetrics.setChecked(metricsChecked); + mPage.getData().putBoolean(KEY_SEND_METRICS, metricsChecked); + mNavKeysRow = mRootView.findViewById(R.id.nav_keys); + mNavKeysRow.setOnClickListener(mNavKeysClickListener); + mNavKeys = (CheckBox) mRootView.findViewById(R.id.nav_keys_checkbox); + boolean needsNavBar = true; + try { + IWindowManager windowManager = WindowManagerGlobal.getWindowManagerService(); + needsNavBar = windowManager.needsNavigationBar(); + } catch (RemoteException e) { + } + if (hideKeyDisabler() || needsNavBar) { + mNavKeysRow.setVisibility(View.GONE); + } else { + boolean navKeysDisabled = + KeyDisabler.isActive(); + mNavKeys.setChecked(navKeysDisabled); + } + mSecureSmsRow = mRootView.findViewById(R.id.secure_sms); + mSecureSmsRow.setOnClickListener(mSecureSmsClickListener); + if (hideWhisperPush(getActivity())) { + 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) : + false; + mSecureSms.setChecked(smsChecked); + mPage.getData().putBoolean(KEY_REGISTER_WHISPERPUSH, smsChecked); + } + + @Override + protected int getLayoutResource() { + return R.layout.setup_cyanogen_services; + } + + @Override + protected int getHeaderLayoutResource() { + return R.layout.header_condensed; + } + + @Override + public void onResume() { + super.onResume(); + updateDisableNavkeysOption(); + runDeferredAction(); + } + + private void runDeferredAction() { + if (mDeferredAction != null) { + mDeferredAction.run(); + mDeferredAction = null; + } + } + + private void updateDisableNavkeysOption() { + boolean enabled = Settings.System.getInt(getActivity().getContentResolver(), + Settings.System.DEV_FORCE_SHOW_NAVBAR, 0) != 0; + + mNavKeys.setChecked(enabled); + } + + private void launchCyanogenAccountSetup(final Activity activity) { + Bundle bundle = new Bundle(); + bundle.putBoolean(SetupWizardApp.EXTRA_FIRST_RUN, true); + AccountManager + .get(activity).addAccount(SetupWizardApp.ACCOUNT_TYPE_CYANOGEN, null, null, bundle, + activity, new AccountManagerCallback() { + @Override + public void run(AccountManagerFuture bundleAccountManagerFuture) { + if (activity == null) return; //There is a chance this activity has been torn down. + Runnable runnable = new Runnable() { + @Override + public void run() { + if (!SetupWizardUtils.accountExists(activity, + SetupWizardApp.ACCOUNT_TYPE_CYANOGEN)) { + mPage.getCallbacks().onNextPage(); + } + } + }; + if (activity.isResumed()) { + runnable.run(); + } else { + mDeferredAction = runnable; + } + } + }, null); + } + + } +} diff --git a/src/com/cyanogenmod/setupwizard/setup/GmsAccountPage.java b/src/com/cyanogenmod/setupwizard/setup/GmsAccountPage.java index a7d2aa8..922dee1 100644 --- a/src/com/cyanogenmod/setupwizard/setup/GmsAccountPage.java +++ b/src/com/cyanogenmod/setupwizard/setup/GmsAccountPage.java @@ -55,10 +55,14 @@ public class GmsAccountPage extends SetupPage { @Override public void doLoadAction(Activity context, int action) { - launchGmsAccountSetup(context, action); + if (action == Page.ACTION_PREVIOUS) { + getCallbacks().onPreviousPage(); + } else { + launchGmsAccountSetup(context); + } } - public void launchGmsAccountSetup(final Activity activity, final int action) { + public void launchGmsAccountSetup(final Activity activity) { Bundle bundle = new Bundle(); bundle.putBoolean(SetupWizardApp.EXTRA_FIRST_RUN, true); bundle.putBoolean(SetupWizardApp.EXTRA_ALLOW_SKIP, true); @@ -79,11 +83,7 @@ public class GmsAccountPage extends SetupPage { if (token != null) { setCompleted(true); } - if (action == Page.ACTION_NEXT) { - getCallbacks().onNextPage(); - } else { - getCallbacks().onPreviousPage(); - } + getCallbacks().onNextPage(); } }, null); } diff --git a/src/com/cyanogenmod/setupwizard/setup/Page.java b/src/com/cyanogenmod/setupwizard/setup/Page.java index 31a02a7..aae4056 100644 --- a/src/com/cyanogenmod/setupwizard/setup/Page.java +++ b/src/com/cyanogenmod/setupwizard/setup/Page.java @@ -42,5 +42,6 @@ public interface Page { public boolean doPreviousAction(); public boolean doNextAction(); public void doLoadAction(Activity context, int action); - public abstract boolean onActivityResult(int requestCode, int resultCode, Intent data); + public boolean onActivityResult(int requestCode, int resultCode, Intent data); + public SetupDataCallbacks getCallbacks(); } diff --git a/src/com/cyanogenmod/setupwizard/setup/SetupPage.java b/src/com/cyanogenmod/setupwizard/setup/SetupPage.java index c607857..4c64d5c 100644 --- a/src/com/cyanogenmod/setupwizard/setup/SetupPage.java +++ b/src/com/cyanogenmod/setupwizard/setup/SetupPage.java @@ -120,7 +120,7 @@ public abstract class SetupPage implements Page { mCallbacks.onPageLoaded(this); } - protected SetupDataCallbacks getCallbacks() { + public SetupDataCallbacks getCallbacks() { return mCallbacks; } } diff --git a/src/com/cyanogenmod/setupwizard/setup/WelcomePage.java b/src/com/cyanogenmod/setupwizard/setup/WelcomePage.java index ca3bf99..569f7b9 100644 --- a/src/com/cyanogenmod/setupwizard/setup/WelcomePage.java +++ b/src/com/cyanogenmod/setupwizard/setup/WelcomePage.java @@ -18,10 +18,12 @@ package com.cyanogenmod.setupwizard.setup; import android.app.Fragment; import android.content.Context; +import android.content.Intent; import android.content.res.Configuration; import android.content.res.Resources; import android.os.Bundle; import android.os.Handler; +import android.os.UserHandle; import android.widget.ArrayAdapter; import android.widget.NumberPicker; @@ -35,6 +37,8 @@ public class WelcomePage extends SetupPage { public static final String TAG = "WelcomePage"; + private static final String ACTION_EMERGENCY_DIAL = "com.android.phone.EmergencyDialer.DIAL"; + public WelcomePage(Context context, SetupDataCallbacks callbacks) { super(context, callbacks); } @@ -54,6 +58,15 @@ public class WelcomePage extends SetupPage { return R.string.setup_welcome; } + @Override + public boolean doPreviousAction() { + Intent intent = new Intent(ACTION_EMERGENCY_DIAL); + intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK + | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); + mContext.startActivity(intent); + return true; + } + @Override public String getKey() { return TAG; diff --git a/src/com/cyanogenmod/setupwizard/ui/SetupWizardActivity.java b/src/com/cyanogenmod/setupwizard/ui/SetupWizardActivity.java index a65c65c..a1c3637 100644 --- a/src/com/cyanogenmod/setupwizard/ui/SetupWizardActivity.java +++ b/src/com/cyanogenmod/setupwizard/ui/SetupWizardActivity.java @@ -22,6 +22,7 @@ import android.content.Intent; import android.content.res.Resources; import android.os.Bundle; import android.provider.Settings; +import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -32,9 +33,11 @@ import com.cyanogenmod.setupwizard.R; import com.cyanogenmod.setupwizard.SetupWizardApp; import com.cyanogenmod.setupwizard.setup.AbstractSetupData; import com.cyanogenmod.setupwizard.setup.CMSetupWizardData; +import com.cyanogenmod.setupwizard.setup.CyanogenServicesPage; import com.cyanogenmod.setupwizard.setup.Page; import com.cyanogenmod.setupwizard.setup.SetupDataCallbacks; import com.cyanogenmod.setupwizard.util.SetupWizardUtils; +import com.cyanogenmod.setupwizard.util.WhisperPushUtils; public class SetupWizardActivity extends Activity implements SetupDataCallbacks { @@ -209,7 +212,35 @@ public class SetupWizardActivity extends Activity implements SetupDataCallbacks finishSetup(); } + private void handleWhisperPushRegistration() { + Page page = getPage(CyanogenServicesPage.TAG); + if (page == null) { + return; + } + Bundle privacyData = page.getData(); + if (privacyData != null && privacyData.getBoolean(CyanogenServicesPage.KEY_REGISTER_WHISPERPUSH)) { + Log.d(TAG, "Registering with WhisperPush"); + WhisperPushUtils.startRegistration(this); + } + } + + public void handleEnableMetrics() { + Page page = getPage(CyanogenServicesPage.TAG); + if (page == null) { + return; + } + Bundle privacyData = page.getData(); + if (privacyData != null + && privacyData.getBoolean(CyanogenServicesPage.KEY_SEND_METRICS)) { + Settings.System.putInt(getContentResolver(), CyanogenServicesPage.SETTING_METRICS, + privacyData.getBoolean(CyanogenServicesPage.KEY_SEND_METRICS) ? 1 : 0); + } + } + private void finishSetup() { + getApplication().sendBroadcast(new Intent(SetupWizardApp.ACTION_FINISHED)); + handleWhisperPushRegistration(); + handleEnableMetrics(); Settings.Global.putInt(getContentResolver(), Settings.Global.DEVICE_PROVISIONED, 1); Settings.Secure.putInt(getContentResolver(), Settings.Secure.USER_SETUP_COMPLETE, 1); ((SetupWizardApp)AppGlobals.getInitialApplication()).enableStatusBar(); diff --git a/src/com/cyanogenmod/setupwizard/util/SetupWizardUtils.java b/src/com/cyanogenmod/setupwizard/util/SetupWizardUtils.java index e31cff0..e37c996 100644 --- a/src/com/cyanogenmod/setupwizard/util/SetupWizardUtils.java +++ b/src/com/cyanogenmod/setupwizard/util/SetupWizardUtils.java @@ -16,6 +16,7 @@ package com.cyanogenmod.setupwizard.util; +import android.accounts.AccountManager; import android.app.Activity; import android.content.ComponentName; import android.content.Context; @@ -138,6 +139,10 @@ public class SetupWizardUtils { ConnectionResult.SERVICE_MISSING; } + public static boolean accountExists(Context context, String accountType) { + return AccountManager.get(context).getAccountsByType(accountType).length > 0; + } + public static void disableSetupWizards(Activity context) { Intent intent = new Intent(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_HOME); diff --git a/src/com/cyanogenmod/setupwizard/util/WhisperPushUtils.java b/src/com/cyanogenmod/setupwizard/util/WhisperPushUtils.java new file mode 100644 index 0000000..4a88f63 --- /dev/null +++ b/src/com/cyanogenmod/setupwizard/util/WhisperPushUtils.java @@ -0,0 +1,78 @@ +/* + * Copyright (C) 2014 The CyanogenMod Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.cyanogenmod.setupwizard.util; + +import android.content.Context; +import android.content.Intent; +import android.telephony.TelephonyManager; +import android.util.Log; + +import com.google.i18n.phonenumbers.NumberParseException; +import com.google.i18n.phonenumbers.PhoneNumberUtil; +import com.google.i18n.phonenumbers.Phonenumber; + +/** + * Utilities for interacting with WhisperPush + * + * @author Chris Soyars + */ +public class WhisperPushUtils { + + private static final String TAG = WhisperPushUtils.class.getSimpleName(); + private static final String ACTION_REGISTER_NUMBER = "org.thoughtcrime.securesms.RegistrationService.REGISTER_NUMBER"; + + private static boolean isEmpty(String value) { + return value == null || value.trim().length() == 0; + } + + private static String getPhoneNumber(Context context) { + String localNumber = ((TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE)) + .getLine1Number(); + + if (!isEmpty(localNumber) && !localNumber.startsWith("+")) { + if (localNumber.length() == 10) { + localNumber = "+1" + localNumber; + } else { + localNumber = "+" + localNumber; + } + } + + try { + if (!isEmpty(localNumber)) { + PhoneNumberUtil numberUtil = PhoneNumberUtil.getInstance(); + Phonenumber.PhoneNumber localNumberObject = numberUtil.parse(localNumber, null); + return numberUtil.format(localNumberObject, PhoneNumberUtil.PhoneNumberFormat.E164); + } + } catch (NumberParseException npe) { + Log.w(TAG, npe); + } + + return null; + } + + public static void startRegistration(Context context) { + String phoneNumber = getPhoneNumber(context); + Log.d(TAG, "Starting WhisperPush registration with number: " + phoneNumber); + if (phoneNumber != null) { + Intent intent = new Intent(); + intent.setAction(ACTION_REGISTER_NUMBER); + intent.setClassName("org.whispersystems.whisperpush", "org.whispersystems.whisperpush.service.RegistrationService"); + intent.putExtra("e164number", phoneNumber); + context.startService(intent); + } + } + +} \ No newline at end of file diff --git a/tests/Android.mk b/tests/Android.mk index c67fc99..9444a01 100644 --- a/tests/Android.mk +++ b/tests/Android.mk @@ -9,9 +9,9 @@ LOCAL_JAVA_LIBRARIES := android.test.runner # Include all test java files. LOCAL_SRC_FILES := $(call all-java-files-under, src) -LOCAL_PACKAGE_NAME := CMSetupWizardTests +LOCAL_PACKAGE_NAME := CyanogenSetupWizardTests LOCAL_CERTIFICATE := platform -LOCAL_INSTRUMENTATION_FOR := CMSetupWizard +LOCAL_INSTRUMENTATION_FOR := CyanogenSetupWizard include $(BUILD_PACKAGE) diff --git a/tests/res/values/strings.xml b/tests/res/values/strings.xml index 94d85cb..b396dd8 100644 --- a/tests/res/values/strings.xml +++ b/tests/res/values/strings.xml @@ -15,7 +15,7 @@ limitations under the License. --> - CMSetupWizard Test + SetupWizard Test Enable Setup Wizard Enable Google Setup Wizard diff --git a/tests/src/com/cyanogenmod/account/tests/ManualTestActivity.java b/tests/src/com/cyanogenmod/account/tests/ManualTestActivity.java index 1e4adef..9caf5a1 100644 --- a/tests/src/com/cyanogenmod/account/tests/ManualTestActivity.java +++ b/tests/src/com/cyanogenmod/account/tests/ManualTestActivity.java @@ -51,7 +51,7 @@ public class ManualTestActivity extends Activity { Intent intent = new Intent("android.intent.action.MAIN"); intent.addCategory("android.intent.category.HOME"); final PackageManager pm = getPackageManager(); - ComponentName componentName = new ComponentName("com.cyanogenmod.account", "com.cyanogenmod.account.ui.SetupWizardActivity"); + ComponentName componentName = new ComponentName("com.cyanogenmod.setupwizard", "com.cyanogenmod.setupwizard.ui.SetupWizardActivity"); pm.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP); componentName = new ComponentName("com.google.android.setupwizard", "com.google.android.setupwizard.SetupWizardActivity"); pm.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);