Chromium Code Reviews| Index: blimp/client/core/android/java/src/org/chromium/blimp/core/settings/AboutBlimpPreferences.java |
| diff --git a/blimp/client/core/android/java/src/org/chromium/blimp/core/settings/AboutBlimpPreferences.java b/blimp/client/core/android/java/src/org/chromium/blimp/core/settings/AboutBlimpPreferences.java |
| index c03d0829f50d32d15bfe36edec2d2e18e3c75ae1..c840a451d1c9c350941ce4cd811b06a0ebda1aba 100644 |
| --- a/blimp/client/core/android/java/src/org/chromium/blimp/core/settings/AboutBlimpPreferences.java |
| +++ b/blimp/client/core/android/java/src/org/chromium/blimp/core/settings/AboutBlimpPreferences.java |
| @@ -12,27 +12,32 @@ import android.preference.ListPreference; |
| import android.preference.Preference; |
| import android.preference.PreferenceFragment; |
| import android.preference.PreferenceScreen; |
| +import android.preference.SwitchPreference; |
| import android.support.v7.app.AlertDialog; |
| +import org.chromium.base.ContextUtils; |
| import org.chromium.base.VisibleForTesting; |
| +import org.chromium.base.annotations.CalledByNative; |
| +import org.chromium.base.annotations.JNINamespace; |
| import org.chromium.blimp.R; |
| -import org.chromium.blimp_public.BlimpSettingsCallbacks; |
| +import org.chromium.blimp.core.common.BlimpClientContextInternal; |
| +import org.chromium.blimp_public.BlimpClientContext; |
| +import org.chromium.components.sync.signin.ChromeSigninController; |
| /** |
| - * Blimp preferences page in Chrome. |
| + * Blimp preferences page in embedder. |
| */ |
| +@JNINamespace("blimp::client") |
| public class AboutBlimpPreferences extends PreferenceFragment { |
| - |
| - /** |
| - * Blimp switch preference key, also the key for this PreferenceFragment. |
| - */ |
| - public static final String PREF_BLIMP_SWITCH = "blimp_switch"; |
| /** |
| - * Blimp assigner URL preference key. |
| + * If this fragment is waiting for user sign in. |
| */ |
| - public static final String PREF_ASSIGNER_URL = "blimp_assigner_url"; |
| + @VisibleForTesting |
| + protected boolean mWaitForSignIn = false; |
| - private static BlimpSettingsCallbacks sSettingsCallback; |
| + private static BlimpClientContext sBlimpClientContext; |
| + |
| + private long mNativeBlimpSettingsAndroid; |
| /** |
| * Attach the blimp setting preferences to a {@link PreferenceFragment}. |
| @@ -44,24 +49,43 @@ public class AboutBlimpPreferences extends PreferenceFragment { |
| Preference blimpSetting = new Preference(fragment.getActivity()); |
| blimpSetting.setTitle(R.string.blimp_about_blimp_preferences); |
| blimpSetting.setFragment(AboutBlimpPreferences.class.getName()); |
| - blimpSetting.setKey(AboutBlimpPreferences.PREF_BLIMP_SWITCH); |
| + blimpSetting.setKey(PreferencesUtil.PREF_BLIMP_SWITCH); |
| screen.addPreference(blimpSetting); |
| fragment.setPreferenceScreen(screen); |
| } |
| /** |
| - * Register Chrome callback related to Blimp settings. |
| - * @param callback |
| + * Register the {@link BlimpClientContext}. |
| */ |
| - public static void registerCallback(BlimpSettingsCallbacks callback) { |
| - sSettingsCallback = callback; |
| + public static void setContext(BlimpClientContext blimpContext) { |
| + sBlimpClientContext = blimpContext; |
| } |
| @Override |
| public void onCreate(Bundle savedInstanceState) { |
| super.onCreate(savedInstanceState); |
| + initNativeLayer(); |
| getActivity().setTitle(R.string.blimp_about_blimp_preferences); |
| + updateSettingPage(); |
| + } |
| + |
| + @Override |
| + public void onResume() { |
| + super.onResume(); |
| + updateSettingPage(); |
| + } |
| + |
| + @Override |
| + public void onDestroy() { |
| + super.onDestroy(); |
| + destroyNativeLayer(); |
| + } |
| + |
| + // Reload the items in preferences list. |
| + private void updateSettingPage() { |
| + PreferenceScreen screen = getPreferenceScreen(); |
| + if (screen != null) screen.removeAll(); |
| addPreferencesFromResource(R.xml.blimp_preferences); |
| setupBlimpSwitch(); |
| @@ -69,26 +93,66 @@ public class AboutBlimpPreferences extends PreferenceFragment { |
| } |
| /** |
| - * Setup the switch preference for blimp. |
| + * Setup the switch preference for Blimp. |
| */ |
| private void setupBlimpSwitch() { |
| // TODO(xingliu): Use {@link ChromeSwitchPreference} after move this class to Chrome. |
| // http://crbug.com/630675 |
| - final Preference pref = findPreference(PREF_BLIMP_SWITCH); |
| + final SwitchPreference pref = |
| + (SwitchPreference) findPreference(PreferencesUtil.PREF_BLIMP_SWITCH); |
| + |
| + if (!isSignedIn()) pref.setChecked(false); |
| pref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { |
| @Override |
| public boolean onPreferenceChange(Preference preference, Object newValue) { |
| - // For blimp client 0.6, if blimp switch changed, show restart dialog. |
| - // TODO(xingliu): Figure out if we need to close all tabs before restart or reset |
| - // the settings before restarting. |
| - showRestartDialog(getActivity(), R.string.blimp_switch_changed_please_restart); |
| - return true; |
| + return onBlimpSwitchPreferenceChange((boolean) newValue); |
| } |
| }); |
| } |
| /** |
| + * Handles switch preference change. |
| + * @param switchValue The new value of the preference. |
| + * @return If the new value will be persisted. |
| + */ |
| + private boolean onBlimpSwitchPreferenceChange(boolean switchValue) { |
| + if (switchValue) { |
| + if (isSignedIn()) { |
| + // If user has signed in and the switch is turned on, start authentication. |
| + sBlimpClientContext.connect(); |
| + } else { |
| + // If user didn't sign in, show a dialog to let the user sign in. |
| + showPleaseSignInDialog(); |
| + return false; |
| + } |
| + } |
| + return true; |
| + } |
| + |
| + /** |
| + * Show please sign in dialog, it will show AccountSigninView to let user to sign in. |
|
David Trainor- moved to gerrit
2016/08/29 05:12:24
Probably don't need "please" in this flow :).
xingliu
2016/08/30 04:47:42
Done.
|
| + * |
| + * If the user signed in after clicking the confirm button, turn on the Blimp switch and connect |
| + * to the engine. |
| + */ |
| + private void showPleaseSignInDialog() { |
| + final Context context = getActivity(); |
| + new AlertDialog.Builder(context) |
| + .setTitle(R.string.blimp_sign_in_title) |
| + .setMessage(R.string.blimp_sign_in_msg) |
| + .setPositiveButton(R.string.blimp_sign_in_btn, |
| + new DialogInterface.OnClickListener() { |
| + @Override |
| + public void onClick(DialogInterface dialog, int which) { |
| + startUserSignInFlow(); |
| + } |
| + }) |
| + .create() |
| + .show(); |
| + } |
| + |
| + /** |
| * When the user taps on the current assigner, a list of available assigners pops up. |
| * User is allowed to change the assigner which is saved to shared preferences. |
| * A dialog is displayed which prompts the user to restart the application. |
| @@ -98,7 +162,13 @@ public class AboutBlimpPreferences extends PreferenceFragment { |
| private void setupAssignerPreferences() { |
| final Activity activity = getActivity(); |
| - final ListPreference listPreference = (ListPreference) findPreference(PREF_ASSIGNER_URL); |
| + final ListPreference listPreference = |
| + (ListPreference) findPreference(PreferencesUtil.PREF_ASSIGNER_URL); |
| + |
| + // Set to default assigner URL on first time loading this UI. |
| + if (listPreference.getValue() == null) { |
| + listPreference.setValue(PreferencesUtil.DEFAULT_ASSIGNER_URL); |
|
David Trainor- moved to gerrit
2016/08/29 05:12:24
Can we use getLastUsedAssigner()?
xingliu
2016/08/30 04:47:42
Done. Make sense.
|
| + } |
| listPreference.setSummary(listPreference.getValue()); |
| listPreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { |
| @@ -117,7 +187,7 @@ public class AboutBlimpPreferences extends PreferenceFragment { |
| * @param context The context where we display the restart browser dialog. |
| * @param message The message shown to the user. |
| */ |
| - private static void showRestartDialog(final Context context, int message) { |
| + private void showRestartDialog(final Context context, int message) { |
| new AlertDialog.Builder(context) |
| .setTitle(R.string.blimp_restart_blimp) |
| .setMessage(message) |
| @@ -136,9 +206,71 @@ public class AboutBlimpPreferences extends PreferenceFragment { |
| * Restart the browser. |
| */ |
| @VisibleForTesting |
| - protected static void restartBrowser() { |
| - if (sSettingsCallback != null) { |
| - sSettingsCallback.onRestartBrowserRequested(); |
| + protected void restartBrowser() { |
| + assert sBlimpClientContext != null; |
| + BlimpClientContextInternal contextInternal = |
| + (BlimpClientContextInternal) sBlimpClientContext; |
| + |
| + contextInternal.getDelegate().restartBrowser(); |
| + } |
| + |
| + /** |
| + * Start user sign in flow to let the user pick an existing account or create new account. |
| + */ |
| + private void startUserSignInFlow() { |
| + mWaitForSignIn = true; |
| + assert sBlimpClientContext != null; |
| + BlimpClientContextInternal contextInternal = |
| + (BlimpClientContextInternal) sBlimpClientContext; |
| + |
| + contextInternal.getDelegate().startUserSignInFlow(getActivity()); |
| + } |
| + |
| + private boolean isSignedIn() { |
| + return ChromeSigninController.get(ContextUtils.getApplicationContext()).isSignedIn(); |
| + } |
| + |
| + @VisibleForTesting |
| + @CalledByNative |
| + protected void onSignedOut() { |
| + // If user signed out, turn off the switch. We also do a sign in state check on |
| + // {@link AboutBlimpPreferences#updateSettingPage()}. |
| + final SwitchPreference pref = |
| + (SwitchPreference) findPreference(PreferencesUtil.PREF_BLIMP_SWITCH); |
| + pref.setChecked(false); |
|
David Trainor- moved to gerrit
2016/08/29 05:12:24
If we were on should we alert the user and shut do
xingliu
2016/08/30 04:47:42
This logic is mostly not get called since the UI i
David Trainor- moved to gerrit
2016/08/30 20:46:29
Acknowledged.
|
| + } |
| + |
| + @VisibleForTesting |
| + @CalledByNative |
| + protected void onSignedIn() { |
| + // If user came back from sign in flow, turn on the switch and connect to engine. |
| + // This logic won't trigger the {@link OnPreferenceChangeListener} call. |
| + if (mWaitForSignIn) { |
| + final SwitchPreference pref = |
| + (SwitchPreference) findPreference(PreferencesUtil.PREF_BLIMP_SWITCH); |
| + pref.setChecked(true); |
| + |
| + sBlimpClientContext.connect(); |
| + mWaitForSignIn = false; |
| } |
| } |
| + |
| + @VisibleForTesting |
| + protected void initNativeLayer() { |
|
David Trainor- moved to gerrit
2016/08/29 05:12:24
Maybe call this initializeNative or just initializ
xingliu
2016/08/30 04:47:42
Done.
|
| + mNativeBlimpSettingsAndroid = nativeInit(); |
| + assert sBlimpClientContext != null && mNativeBlimpSettingsAndroid != 0; |
| + |
| + // Set identity source pointer in native code. |
| + BlimpClientContextInternal contextInternal = |
| + (BlimpClientContextInternal) sBlimpClientContext; |
| + contextInternal.initSettingsPage(mNativeBlimpSettingsAndroid); |
|
David Trainor- moved to gerrit
2016/08/29 05:12:24
Just pass this class in and allow the native side
xingliu
2016/08/30 04:47:42
Done. Now, it's similar to other Jni bridges.
|
| + } |
| + |
| + @VisibleForTesting |
| + protected void destroyNativeLayer() { |
| + nativeDestroy(mNativeBlimpSettingsAndroid); |
| + } |
| + |
| + private native long nativeInit(); |
| + private native void nativeDestroy(long nativeBlimpSettingsAndroid); |
| } |