Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(77)

Side by Side Diff: blimp/client/core/android/java/src/org/chromium/blimp/core/settings/AboutBlimpPreferences.java

Issue 2261273002: Integrate UI with authentication flow. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Misc fixes/ Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 package org.chromium.blimp.core.settings; 5 package org.chromium.blimp.core.settings;
6 6
7 import android.app.Activity; 7 import android.app.Activity;
8 import android.content.Context; 8 import android.content.Context;
9 import android.content.DialogInterface; 9 import android.content.DialogInterface;
10 import android.os.Bundle; 10 import android.os.Bundle;
11 import android.preference.ListPreference; 11 import android.preference.ListPreference;
12 import android.preference.Preference; 12 import android.preference.Preference;
13 import android.preference.PreferenceFragment; 13 import android.preference.PreferenceFragment;
14 import android.preference.PreferenceScreen; 14 import android.preference.PreferenceScreen;
15 import android.preference.SwitchPreference;
15 import android.support.v7.app.AlertDialog; 16 import android.support.v7.app.AlertDialog;
16 17
18 import org.chromium.base.ContextUtils;
17 import org.chromium.base.VisibleForTesting; 19 import org.chromium.base.VisibleForTesting;
20 import org.chromium.base.annotations.CalledByNative;
21 import org.chromium.base.annotations.JNINamespace;
18 import org.chromium.blimp.R; 22 import org.chromium.blimp.R;
19 import org.chromium.blimp_public.BlimpSettingsCallbacks; 23 import org.chromium.components.sync.signin.ChromeSigninController;
20 24
21 /** 25 /**
22 * Blimp preferences page in Chrome. 26 * Blimp preferences page in embedder.
23 */ 27 */
28 @JNINamespace("blimp::client")
24 public class AboutBlimpPreferences extends PreferenceFragment { 29 public class AboutBlimpPreferences extends PreferenceFragment {
30 /**
31 * If this fragment is waiting for user sign in.
32 */
33 @VisibleForTesting
34 protected boolean mWaitForSignIn = false;
25 35
26 /** 36 private static BlimpPreferencesDelegate sPreferencesDelegate;
27 * Blimp switch preference key, also the key for this PreferenceFragment.
28 */
29 public static final String PREF_BLIMP_SWITCH = "blimp_switch";
30 /**
31 * Blimp assigner URL preference key.
32 */
33 public static final String PREF_ASSIGNER_URL = "blimp_assigner_url";
34 37
35 private static BlimpSettingsCallbacks sSettingsCallback; 38 private long mNativeBlimpSettingsAndroid;
36 39
37 /** 40 /**
38 * Attach the blimp setting preferences to a {@link PreferenceFragment}. 41 * Attach the blimp setting preferences to a {@link PreferenceFragment}.
42 * And Set the delegate.
39 * @param fragment The fragment that blimp setting attach to. 43 * @param fragment The fragment that blimp setting attach to.
44 * @param delegate {@link BlimpPreferencesDelegate} implemented by BlimpClie ntContextImpl.
40 */ 45 */
41 public static void addBlimpPreferences(PreferenceFragment fragment) { 46 public static void addBlimpPreferences(
47 PreferenceFragment fragment, BlimpPreferencesDelegate delegate) {
48 addBlimpPreferences(fragment);
49 setDelegate(delegate);
50 }
51
52 private static void addBlimpPreferences(PreferenceFragment fragment) {
42 PreferenceScreen screen = fragment.getPreferenceScreen(); 53 PreferenceScreen screen = fragment.getPreferenceScreen();
43 54
44 Preference blimpSetting = new Preference(fragment.getActivity()); 55 Preference blimpSetting = new Preference(fragment.getActivity());
45 blimpSetting.setTitle(R.string.blimp_about_blimp_preferences); 56 blimpSetting.setTitle(R.string.blimp_about_blimp_preferences);
46 blimpSetting.setFragment(AboutBlimpPreferences.class.getName()); 57 blimpSetting.setFragment(AboutBlimpPreferences.class.getName());
47 blimpSetting.setKey(AboutBlimpPreferences.PREF_BLIMP_SWITCH); 58 blimpSetting.setKey(PreferencesUtil.PREF_BLIMP_SWITCH);
48 59
49 screen.addPreference(blimpSetting); 60 screen.addPreference(blimpSetting);
50 fragment.setPreferenceScreen(screen); 61 fragment.setPreferenceScreen(screen);
51 } 62 }
52 63
53 /** 64 /**
54 * Register Chrome callback related to Blimp settings. 65 * Set {@link BlimpPreferencesDelegate}.
55 * @param callback
56 */ 66 */
57 public static void registerCallback(BlimpSettingsCallbacks callback) { 67 @VisibleForTesting
58 sSettingsCallback = callback; 68 protected static void setDelegate(BlimpPreferencesDelegate delegate) {
69 sPreferencesDelegate = delegate;
59 } 70 }
60 71
61 @Override 72 @Override
62 public void onCreate(Bundle savedInstanceState) { 73 public void onCreate(Bundle savedInstanceState) {
63 super.onCreate(savedInstanceState); 74 super.onCreate(savedInstanceState);
75 initializeNative();
64 getActivity().setTitle(R.string.blimp_about_blimp_preferences); 76 getActivity().setTitle(R.string.blimp_about_blimp_preferences);
77 updateSettingPage();
78 }
79
80 @Override
81 public void onResume() {
82 super.onResume();
83 updateSettingPage();
84 }
85
86 @Override
87 public void onDestroy() {
88 destroyNative();
89 super.onDestroy();
90 }
91
92 // Reload the items in preferences list.
93 private void updateSettingPage() {
94 PreferenceScreen screen = getPreferenceScreen();
95 if (screen != null) screen.removeAll();
65 addPreferencesFromResource(R.xml.blimp_preferences); 96 addPreferencesFromResource(R.xml.blimp_preferences);
66 97
67 setupBlimpSwitch(); 98 setupBlimpSwitch();
68 setupAssignerPreferences(); 99 setupAssignerPreferences();
69 } 100 }
70 101
71 /** 102 /**
72 * Setup the switch preference for blimp. 103 * Setup the switch preference for Blimp.
73 */ 104 */
74 private void setupBlimpSwitch() { 105 private void setupBlimpSwitch() {
75 // TODO(xingliu): Use {@link ChromeSwitchPreference} after move this cla ss to Chrome. 106 // TODO(xingliu): Use {@link ChromeSwitchPreference} after move this cla ss to Chrome.
76 // http://crbug.com/630675 107 // http://crbug.com/630675
77 final Preference pref = findPreference(PREF_BLIMP_SWITCH); 108 final SwitchPreference pref =
109 (SwitchPreference) findPreference(PreferencesUtil.PREF_BLIMP_SWI TCH);
110
111 if (!isSignedIn()) pref.setChecked(false);
78 112
79 pref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeList ener() { 113 pref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeList ener() {
80 @Override 114 @Override
81 public boolean onPreferenceChange(Preference preference, Object newV alue) { 115 public boolean onPreferenceChange(Preference preference, Object newV alue) {
82 // For blimp client 0.6, if blimp switch changed, show restart d ialog. 116 return onBlimpSwitchPreferenceChange((boolean) newValue);
83 // TODO(xingliu): Figure out if we need to close all tabs before restart or reset
84 // the settings before restarting.
85 showRestartDialog(getActivity(), R.string.blimp_switch_changed_p lease_restart);
86 return true;
87 } 117 }
88 }); 118 });
89 } 119 }
90 120
91 /** 121 /**
122 * Handles switch preference change.
123 * @param switchValue The new value of the preference.
124 * @return If the new value will be persisted.
125 */
126 private boolean onBlimpSwitchPreferenceChange(boolean switchValue) {
127 if (switchValue) {
128 if (isSignedIn()) {
129 assert sPreferencesDelegate != null;
130
131 // If user has signed in and the switch is turned on, start auth entication.
132 sPreferencesDelegate.connect();
133 } else {
134 // If user didn't sign in, show a dialog to let the user sign in .
135 showSignInDialog();
136 return false;
137 }
138 }
139 return true;
140 }
141
142 /**
143 * Show sign in dialog, it will show AccountSigninView to let user to sign i n.
144 *
145 * If the user signed in after clicking the confirm button, turn on the Blim p switch and connect
146 * to the engine.
147 */
148 private void showSignInDialog() {
149 final Context context = getActivity();
150 new AlertDialog.Builder(context)
151 .setTitle(R.string.blimp_sign_in_title)
152 .setMessage(R.string.blimp_sign_in_msg)
153 .setPositiveButton(R.string.blimp_sign_in_btn,
154 new DialogInterface.OnClickListener() {
155 @Override
156 public void onClick(DialogInterface dialog, int whic h) {
157 startUserSignInFlow();
158 }
159 })
160 .create()
161 .show();
162 }
163
164 /**
92 * When the user taps on the current assigner, a list of available assigners pops up. 165 * When the user taps on the current assigner, a list of available assigners pops up.
93 * User is allowed to change the assigner which is saved to shared preferenc es. 166 * User is allowed to change the assigner which is saved to shared preferenc es.
94 * A dialog is displayed which prompts the user to restart the application. 167 * A dialog is displayed which prompts the user to restart the application.
95 * 168 *
96 * Use {@link PreferencesUtil#getLastUsedAssigner} to retrieve the assigner URL. 169 * Use {@link PreferencesUtil#getLastUsedAssigner} to retrieve the assigner URL.
97 */ 170 */
98 private void setupAssignerPreferences() { 171 private void setupAssignerPreferences() {
99 final Activity activity = getActivity(); 172 final Activity activity = getActivity();
100 173
101 final ListPreference listPreference = (ListPreference) findPreference(PR EF_ASSIGNER_URL); 174 final ListPreference listPreference =
175 (ListPreference) findPreference(PreferencesUtil.PREF_ASSIGNER_UR L);
176
177 // Set to default assigner URL on first time loading this UI.
178 listPreference.setValue(PreferencesUtil.getLastUsedAssigner());
179
102 listPreference.setSummary(listPreference.getValue()); 180 listPreference.setSummary(listPreference.getValue());
103 181
104 listPreference.setOnPreferenceChangeListener(new Preference.OnPreference ChangeListener() { 182 listPreference.setOnPreferenceChangeListener(new Preference.OnPreference ChangeListener() {
105 @Override 183 @Override
106 public boolean onPreferenceChange(Preference preference, Object newV alue) { 184 public boolean onPreferenceChange(Preference preference, Object newV alue) {
107 String newAssignmentUrl = (String) newValue; 185 String newAssignmentUrl = (String) newValue;
108 listPreference.setSummary(newAssignmentUrl); 186 listPreference.setSummary(newAssignmentUrl);
109 showRestartDialog(activity, R.string.blimp_assigner_changed_plea se_restart); 187 showRestartDialog(activity, R.string.blimp_assigner_changed_plea se_restart);
110 return true; 188 return true;
111 } 189 }
112 }); 190 });
113 } 191 }
114 192
115 /** 193 /**
116 * Show restart browser dialog. 194 * Show restart browser dialog.
117 * @param context The context where we display the restart browser dialog. 195 * @param context The context where we display the restart browser dialog.
118 * @param message The message shown to the user. 196 * @param message The message shown to the user.
119 */ 197 */
120 private static void showRestartDialog(final Context context, int message) { 198 private void showRestartDialog(final Context context, int message) {
121 new AlertDialog.Builder(context) 199 new AlertDialog.Builder(context)
122 .setTitle(R.string.blimp_restart_blimp) 200 .setTitle(R.string.blimp_restart_blimp)
123 .setMessage(message) 201 .setMessage(message)
202 .setCancelable(false)
124 .setPositiveButton(R.string.blimp_restart_now, 203 .setPositiveButton(R.string.blimp_restart_now,
125 new DialogInterface.OnClickListener() { 204 new DialogInterface.OnClickListener() {
126 @Override 205 @Override
127 public void onClick(DialogInterface dialog, int whic h) { 206 public void onClick(DialogInterface dialog, int whic h) {
128 restartBrowser(); 207 restartBrowser();
129 } 208 }
130 }) 209 })
131 .create() 210 .create()
132 .show(); 211 .show();
133 } 212 }
134 213
135 /** 214 /**
136 * Restart the browser. 215 * Restart the browser.
137 */ 216 */
138 @VisibleForTesting 217 @VisibleForTesting
139 protected static void restartBrowser() { 218 protected void restartBrowser() {
140 if (sSettingsCallback != null) { 219 assert sPreferencesDelegate != null;
141 sSettingsCallback.onRestartBrowserRequested(); 220 sPreferencesDelegate.getDelegate().restartBrowser();
221 }
222
223 /**
224 * Start user sign in flow to let the user pick an existing account or creat e new account.
225 */
226 private void startUserSignInFlow() {
227 mWaitForSignIn = true;
228 assert sPreferencesDelegate != null;
229 sPreferencesDelegate.getDelegate().startUserSignInFlow(getActivity());
230 }
231
232 private boolean isSignedIn() {
233 return ChromeSigninController.get(ContextUtils.getApplicationContext()). isSignedIn();
234 }
235
236 @VisibleForTesting
237 @CalledByNative
238 protected void onSignedOut() {
239 // If user signed out, turn off the switch. We also do a sign in state c heck on
240 // {@link AboutBlimpPreferences#updateSettingPage()}.
241 final SwitchPreference pref =
242 (SwitchPreference) findPreference(PreferencesUtil.PREF_BLIMP_SWI TCH);
243 pref.setChecked(false);
244 showRestartDialog(getActivity(), R.string.blimp_sign_out_restart);
245 }
246
247 @VisibleForTesting
248 @CalledByNative
249 protected void onSignedIn() {
250 // If user came back from sign in flow, turn on the switch and connect t o engine.
251 // This logic won't trigger the {@link OnPreferenceChangeListener} call.
252 if (mWaitForSignIn) {
253 final SwitchPreference pref =
254 (SwitchPreference) findPreference(PreferencesUtil.PREF_BLIMP _SWITCH);
255 pref.setChecked(true);
256
257 assert sPreferencesDelegate != null;
258 sPreferencesDelegate.connect();
259 mWaitForSignIn = false;
142 } 260 }
143 } 261 }
262
263 @VisibleForTesting
264 protected void initializeNative() {
265 mNativeBlimpSettingsAndroid = nativeInit();
266 assert sPreferencesDelegate != null && mNativeBlimpSettingsAndroid != 0;
267
268 // Initialize in native code.
269 sPreferencesDelegate.initSettingsPage(this);
270 }
271
272 @VisibleForTesting
273 protected void destroyNative() {
274 nativeDestroy(mNativeBlimpSettingsAndroid);
275 }
276
277 @CalledByNative
278 private void clearNativePtr() {
279 mNativeBlimpSettingsAndroid = 0;
280 }
281
282 @CalledByNative
283 private long getNativePtr() {
284 assert mNativeBlimpSettingsAndroid != 0;
285 return mNativeBlimpSettingsAndroid;
286 }
287
288 private native long nativeInit();
289 private native void nativeDestroy(long nativeBlimpSettingsAndroid);
144 } 290 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698