SetupWizard: Add Cyanogen services page

Change-Id: I39970b1b659a3a7ed3bb5b4350707aa0e53aba24
This commit is contained in:
cretin45
2015-01-19 14:29:43 -08:00
parent 74ca6faf3a
commit 48ca24eb9b
18 changed files with 625 additions and 100 deletions

View File

@@ -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";

View File

@@ -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));

View File

@@ -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<Bundle>() {
@Override
public void run(AccountManagerFuture<Bundle> 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;
}
}

View File

@@ -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<Bundle>() {
@Override
public void run(AccountManagerFuture<Bundle> 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);
}
}
}

View File

@@ -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);
}

View File

@@ -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();
}

View File

@@ -120,7 +120,7 @@ public abstract class SetupPage implements Page {
mCallbacks.onPageLoaded(this);
}
protected SetupDataCallbacks getCallbacks() {
public SetupDataCallbacks getCallbacks() {
return mCallbacks;
}
}

View File

@@ -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;

View File

@@ -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();

View File

@@ -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);

View File

@@ -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);
}
}
}