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

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 fix. Created 4 years, 4 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.blimp.core.common.BlimpClientContextInternal;
24 import org.chromium.blimp_public.BlimpClientContext;
25 import org.chromium.components.sync.signin.ChromeSigninController;
20 26
21 /** 27 /**
22 * Blimp preferences page in Chrome. 28 * Blimp preferences page in embedder.
23 */ 29 */
30 @JNINamespace("blimp::client")
24 public class AboutBlimpPreferences extends PreferenceFragment { 31 public class AboutBlimpPreferences extends PreferenceFragment {
32 /**
33 * If this fragment is waiting for user sign in.
34 */
35 @VisibleForTesting
36 protected boolean mWaitForSignIn = false;
25 37
26 /** 38 private static BlimpClientContext sBlimpClientContext;
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 39
35 private static BlimpSettingsCallbacks sSettingsCallback; 40 private long mNativeBlimpSettingsAndroid;
36 41
37 /** 42 /**
38 * Attach the blimp setting preferences to a {@link PreferenceFragment}. 43 * Attach the blimp setting preferences to a {@link PreferenceFragment}.
39 * @param fragment The fragment that blimp setting attach to. 44 * @param fragment The fragment that blimp setting attach to.
40 */ 45 */
41 public static void addBlimpPreferences(PreferenceFragment fragment) { 46 public static void addBlimpPreferences(PreferenceFragment fragment) {
42 PreferenceScreen screen = fragment.getPreferenceScreen(); 47 PreferenceScreen screen = fragment.getPreferenceScreen();
43 48
44 Preference blimpSetting = new Preference(fragment.getActivity()); 49 Preference blimpSetting = new Preference(fragment.getActivity());
45 blimpSetting.setTitle(R.string.blimp_about_blimp_preferences); 50 blimpSetting.setTitle(R.string.blimp_about_blimp_preferences);
46 blimpSetting.setFragment(AboutBlimpPreferences.class.getName()); 51 blimpSetting.setFragment(AboutBlimpPreferences.class.getName());
47 blimpSetting.setKey(AboutBlimpPreferences.PREF_BLIMP_SWITCH); 52 blimpSetting.setKey(PreferencesUtil.PREF_BLIMP_SWITCH);
48 53
49 screen.addPreference(blimpSetting); 54 screen.addPreference(blimpSetting);
50 fragment.setPreferenceScreen(screen); 55 fragment.setPreferenceScreen(screen);
51 } 56 }
52 57
53 /** 58 /**
54 * Register Chrome callback related to Blimp settings. 59 * Register the {@link BlimpClientContext}.
55 * @param callback
56 */ 60 */
57 public static void registerCallback(BlimpSettingsCallbacks callback) { 61 public static void setContext(BlimpClientContext blimpContext) {
58 sSettingsCallback = callback; 62 sBlimpClientContext = blimpContext;
59 } 63 }
60 64
61 @Override 65 @Override
62 public void onCreate(Bundle savedInstanceState) { 66 public void onCreate(Bundle savedInstanceState) {
63 super.onCreate(savedInstanceState); 67 super.onCreate(savedInstanceState);
68 initNativeLayer();
64 getActivity().setTitle(R.string.blimp_about_blimp_preferences); 69 getActivity().setTitle(R.string.blimp_about_blimp_preferences);
70 updateSettingPage();
71 }
72
73 @Override
74 public void onResume() {
75 super.onResume();
76 updateSettingPage();
77 }
78
79 @Override
80 public void onDestroy() {
81 super.onDestroy();
82 destroyNativeLayer();
83 }
84
85 // Reload the items in preferences list.
86 private void updateSettingPage() {
87 PreferenceScreen screen = getPreferenceScreen();
88 if (screen != null) screen.removeAll();
65 addPreferencesFromResource(R.xml.blimp_preferences); 89 addPreferencesFromResource(R.xml.blimp_preferences);
66 90
67 setupBlimpSwitch(); 91 setupBlimpSwitch();
68 setupAssignerPreferences(); 92 setupAssignerPreferences();
69 } 93 }
70 94
71 /** 95 /**
72 * Setup the switch preference for blimp. 96 * Setup the switch preference for Blimp.
73 */ 97 */
74 private void setupBlimpSwitch() { 98 private void setupBlimpSwitch() {
75 // TODO(xingliu): Use {@link ChromeSwitchPreference} after move this cla ss to Chrome. 99 // TODO(xingliu): Use {@link ChromeSwitchPreference} after move this cla ss to Chrome.
76 // http://crbug.com/630675 100 // http://crbug.com/630675
77 final Preference pref = findPreference(PREF_BLIMP_SWITCH); 101 final SwitchPreference pref =
102 (SwitchPreference) findPreference(PreferencesUtil.PREF_BLIMP_SWI TCH);
103
104 if (!isSignedIn()) pref.setChecked(false);
78 105
79 pref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeList ener() { 106 pref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeList ener() {
80 @Override 107 @Override
81 public boolean onPreferenceChange(Preference preference, Object newV alue) { 108 public boolean onPreferenceChange(Preference preference, Object newV alue) {
82 // For blimp client 0.6, if blimp switch changed, show restart d ialog. 109 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 } 110 }
88 }); 111 });
89 } 112 }
90 113
91 /** 114 /**
115 * Handles switch preference change.
116 * @param switchValue The new value of the preference.
117 * @return If the new value will be persisted.
118 */
119 private boolean onBlimpSwitchPreferenceChange(boolean switchValue) {
120 if (switchValue) {
121 if (isSignedIn()) {
122 // If user has signed in and the switch is turned on, start auth entication.
123 sBlimpClientContext.connect();
124 } else {
125 // If user didn't sign in, show a dialog to let the user sign in .
126 showPleaseSignInDialog();
127 return false;
128 }
129 }
130 return true;
131 }
132
133 /**
134 * 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.
135 *
136 * If the user signed in after clicking the confirm button, turn on the Blim p switch and connect
137 * to the engine.
138 */
139 private void showPleaseSignInDialog() {
140 final Context context = getActivity();
141 new AlertDialog.Builder(context)
142 .setTitle(R.string.blimp_sign_in_title)
143 .setMessage(R.string.blimp_sign_in_msg)
144 .setPositiveButton(R.string.blimp_sign_in_btn,
145 new DialogInterface.OnClickListener() {
146 @Override
147 public void onClick(DialogInterface dialog, int whic h) {
148 startUserSignInFlow();
149 }
150 })
151 .create()
152 .show();
153 }
154
155 /**
92 * When the user taps on the current assigner, a list of available assigners pops up. 156 * 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. 157 * 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. 158 * A dialog is displayed which prompts the user to restart the application.
95 * 159 *
96 * Use {@link PreferencesUtil#getLastUsedAssigner} to retrieve the assigner URL. 160 * Use {@link PreferencesUtil#getLastUsedAssigner} to retrieve the assigner URL.
97 */ 161 */
98 private void setupAssignerPreferences() { 162 private void setupAssignerPreferences() {
99 final Activity activity = getActivity(); 163 final Activity activity = getActivity();
100 164
101 final ListPreference listPreference = (ListPreference) findPreference(PR EF_ASSIGNER_URL); 165 final ListPreference listPreference =
166 (ListPreference) findPreference(PreferencesUtil.PREF_ASSIGNER_UR L);
167
168 // Set to default assigner URL on first time loading this UI.
169 if (listPreference.getValue() == null) {
170 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.
171 }
102 listPreference.setSummary(listPreference.getValue()); 172 listPreference.setSummary(listPreference.getValue());
103 173
104 listPreference.setOnPreferenceChangeListener(new Preference.OnPreference ChangeListener() { 174 listPreference.setOnPreferenceChangeListener(new Preference.OnPreference ChangeListener() {
105 @Override 175 @Override
106 public boolean onPreferenceChange(Preference preference, Object newV alue) { 176 public boolean onPreferenceChange(Preference preference, Object newV alue) {
107 String newAssignmentUrl = (String) newValue; 177 String newAssignmentUrl = (String) newValue;
108 listPreference.setSummary(newAssignmentUrl); 178 listPreference.setSummary(newAssignmentUrl);
109 showRestartDialog(activity, R.string.blimp_assigner_changed_plea se_restart); 179 showRestartDialog(activity, R.string.blimp_assigner_changed_plea se_restart);
110 return true; 180 return true;
111 } 181 }
112 }); 182 });
113 } 183 }
114 184
115 /** 185 /**
116 * Show restart browser dialog. 186 * Show restart browser dialog.
117 * @param context The context where we display the restart browser dialog. 187 * @param context The context where we display the restart browser dialog.
118 * @param message The message shown to the user. 188 * @param message The message shown to the user.
119 */ 189 */
120 private static void showRestartDialog(final Context context, int message) { 190 private void showRestartDialog(final Context context, int message) {
121 new AlertDialog.Builder(context) 191 new AlertDialog.Builder(context)
122 .setTitle(R.string.blimp_restart_blimp) 192 .setTitle(R.string.blimp_restart_blimp)
123 .setMessage(message) 193 .setMessage(message)
124 .setPositiveButton(R.string.blimp_restart_now, 194 .setPositiveButton(R.string.blimp_restart_now,
125 new DialogInterface.OnClickListener() { 195 new DialogInterface.OnClickListener() {
126 @Override 196 @Override
127 public void onClick(DialogInterface dialog, int whic h) { 197 public void onClick(DialogInterface dialog, int whic h) {
128 restartBrowser(); 198 restartBrowser();
129 } 199 }
130 }) 200 })
131 .create() 201 .create()
132 .show(); 202 .show();
133 } 203 }
134 204
135 /** 205 /**
136 * Restart the browser. 206 * Restart the browser.
137 */ 207 */
138 @VisibleForTesting 208 @VisibleForTesting
139 protected static void restartBrowser() { 209 protected void restartBrowser() {
140 if (sSettingsCallback != null) { 210 assert sBlimpClientContext != null;
141 sSettingsCallback.onRestartBrowserRequested(); 211 BlimpClientContextInternal contextInternal =
212 (BlimpClientContextInternal) sBlimpClientContext;
213
214 contextInternal.getDelegate().restartBrowser();
215 }
216
217 /**
218 * Start user sign in flow to let the user pick an existing account or creat e new account.
219 */
220 private void startUserSignInFlow() {
221 mWaitForSignIn = true;
222 assert sBlimpClientContext != null;
223 BlimpClientContextInternal contextInternal =
224 (BlimpClientContextInternal) sBlimpClientContext;
225
226 contextInternal.getDelegate().startUserSignInFlow(getActivity());
227 }
228
229 private boolean isSignedIn() {
230 return ChromeSigninController.get(ContextUtils.getApplicationContext()). isSignedIn();
231 }
232
233 @VisibleForTesting
234 @CalledByNative
235 protected void onSignedOut() {
236 // If user signed out, turn off the switch. We also do a sign in state c heck on
237 // {@link AboutBlimpPreferences#updateSettingPage()}.
238 final SwitchPreference pref =
239 (SwitchPreference) findPreference(PreferencesUtil.PREF_BLIMP_SWI TCH);
240 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.
241 }
242
243 @VisibleForTesting
244 @CalledByNative
245 protected void onSignedIn() {
246 // If user came back from sign in flow, turn on the switch and connect t o engine.
247 // This logic won't trigger the {@link OnPreferenceChangeListener} call.
248 if (mWaitForSignIn) {
249 final SwitchPreference pref =
250 (SwitchPreference) findPreference(PreferencesUtil.PREF_BLIMP _SWITCH);
251 pref.setChecked(true);
252
253 sBlimpClientContext.connect();
254 mWaitForSignIn = false;
142 } 255 }
143 } 256 }
257
258 @VisibleForTesting
259 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.
260 mNativeBlimpSettingsAndroid = nativeInit();
261 assert sBlimpClientContext != null && mNativeBlimpSettingsAndroid != 0;
262
263 // Set identity source pointer in native code.
264 BlimpClientContextInternal contextInternal =
265 (BlimpClientContextInternal) sBlimpClientContext;
266 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.
267 }
268
269 @VisibleForTesting
270 protected void destroyNativeLayer() {
271 nativeDestroy(mNativeBlimpSettingsAndroid);
272 }
273
274 private native long nativeInit();
275 private native void nativeDestroy(long nativeBlimpSettingsAndroid);
144 } 276 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698