OLD | NEW |
---|---|
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_public.BlimpClientContext; |
24 import org.chromium.components.sync.signin.ChromeSigninController; | |
20 | 25 |
21 /** | 26 /** |
22 * Blimp preferences page in Chrome. | 27 * Blimp preferences page in embedder. |
23 */ | 28 */ |
29 @JNINamespace("blimp::client") | |
24 public class AboutBlimpPreferences extends PreferenceFragment { | 30 public class AboutBlimpPreferences extends PreferenceFragment { |
31 /** | |
32 * If this fragment is waiting for user sign in. | |
33 */ | |
34 @VisibleForTesting | |
35 protected boolean mWaitForSignIn = false; | |
25 | 36 |
26 /** | 37 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 | 38 |
35 private static BlimpSettingsCallbacks sSettingsCallback; | 39 private long mNativeBlimpSettingsAndroid; |
36 | 40 |
37 /** | 41 /** |
38 * Attach the blimp setting preferences to a {@link PreferenceFragment}. | 42 * Attach the blimp setting preferences to a {@link PreferenceFragment}. |
39 * @param fragment The fragment that blimp setting attach to. | 43 * @param fragment The fragment that blimp setting attach to. |
44 * @param blimpContext {@link BlimpClientContext} object. | |
40 */ | 45 */ |
41 public static void addBlimpPreferences(PreferenceFragment fragment) { | 46 public static void addBlimpPreferences( |
47 PreferenceFragment fragment, BlimpClientContext blimpContext) { | |
David Trainor- moved to gerrit
2016/08/30 20:46:29
Make this BlimpPreferencesDelegate?
xingliu
2016/08/30 22:27:36
Make sense, fixed.
| |
48 addBlimpPreferences(fragment); | |
49 setContext(blimpContext); | |
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 BlimpClientContext}. |
55 * @param callback | |
56 */ | 66 */ |
57 public static void registerCallback(BlimpSettingsCallbacks callback) { | 67 @VisibleForTesting |
58 sSettingsCallback = callback; | 68 protected static void setContext(BlimpClientContext blimpContext) { |
69 sPreferencesDelegate = (BlimpPreferencesDelegate) blimpContext; | |
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 super.onDestroy(); | |
89 destroyNative(); | |
David Trainor- moved to gerrit
2016/08/30 20:46:29
Should we destroy native before calling super.onDe
xingliu
2016/08/30 22:27:36
Done.
| |
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 // If user has signed in and the switch is turned on, start auth entication. | |
130 sPreferencesDelegate.connect(); | |
David Trainor- moved to gerrit
2016/08/30 20:46:29
assert sPreferencesDialog != null here too?
xingliu
2016/08/30 22:27:36
Done, also added in another call site.
| |
131 } else { | |
132 // If user didn't sign in, show a dialog to let the user sign in . | |
133 showSignInDialog(); | |
134 return false; | |
135 } | |
136 } | |
137 return true; | |
138 } | |
139 | |
140 /** | |
141 * Show sign in dialog, it will show AccountSigninView to let user to sign i n. | |
142 * | |
143 * If the user signed in after clicking the confirm button, turn on the Blim p switch and connect | |
144 * to the engine. | |
145 */ | |
146 private void showSignInDialog() { | |
147 final Context context = getActivity(); | |
148 new AlertDialog.Builder(context) | |
149 .setTitle(R.string.blimp_sign_in_title) | |
150 .setMessage(R.string.blimp_sign_in_msg) | |
151 .setPositiveButton(R.string.blimp_sign_in_btn, | |
152 new DialogInterface.OnClickListener() { | |
153 @Override | |
154 public void onClick(DialogInterface dialog, int whic h) { | |
155 startUserSignInFlow(); | |
156 } | |
157 }) | |
158 .create() | |
159 .show(); | |
160 } | |
161 | |
162 /** | |
92 * When the user taps on the current assigner, a list of available assigners pops up. | 163 * 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. | 164 * 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. | 165 * A dialog is displayed which prompts the user to restart the application. |
95 * | 166 * |
96 * Use {@link PreferencesUtil#getLastUsedAssigner} to retrieve the assigner URL. | 167 * Use {@link PreferencesUtil#getLastUsedAssigner} to retrieve the assigner URL. |
97 */ | 168 */ |
98 private void setupAssignerPreferences() { | 169 private void setupAssignerPreferences() { |
99 final Activity activity = getActivity(); | 170 final Activity activity = getActivity(); |
100 | 171 |
101 final ListPreference listPreference = (ListPreference) findPreference(PR EF_ASSIGNER_URL); | 172 final ListPreference listPreference = |
173 (ListPreference) findPreference(PreferencesUtil.PREF_ASSIGNER_UR L); | |
174 | |
175 // Set to default assigner URL on first time loading this UI. | |
176 listPreference.setValue(PreferencesUtil.getLastUsedAssigner()); | |
177 | |
102 listPreference.setSummary(listPreference.getValue()); | 178 listPreference.setSummary(listPreference.getValue()); |
103 | 179 |
104 listPreference.setOnPreferenceChangeListener(new Preference.OnPreference ChangeListener() { | 180 listPreference.setOnPreferenceChangeListener(new Preference.OnPreference ChangeListener() { |
105 @Override | 181 @Override |
106 public boolean onPreferenceChange(Preference preference, Object newV alue) { | 182 public boolean onPreferenceChange(Preference preference, Object newV alue) { |
107 String newAssignmentUrl = (String) newValue; | 183 String newAssignmentUrl = (String) newValue; |
108 listPreference.setSummary(newAssignmentUrl); | 184 listPreference.setSummary(newAssignmentUrl); |
109 showRestartDialog(activity, R.string.blimp_assigner_changed_plea se_restart); | 185 showRestartDialog(activity, R.string.blimp_assigner_changed_plea se_restart); |
110 return true; | 186 return true; |
111 } | 187 } |
112 }); | 188 }); |
113 } | 189 } |
114 | 190 |
115 /** | 191 /** |
116 * Show restart browser dialog. | 192 * Show restart browser dialog. |
117 * @param context The context where we display the restart browser dialog. | 193 * @param context The context where we display the restart browser dialog. |
118 * @param message The message shown to the user. | 194 * @param message The message shown to the user. |
119 */ | 195 */ |
120 private static void showRestartDialog(final Context context, int message) { | 196 private void showRestartDialog(final Context context, int message) { |
121 new AlertDialog.Builder(context) | 197 new AlertDialog.Builder(context) |
122 .setTitle(R.string.blimp_restart_blimp) | 198 .setTitle(R.string.blimp_restart_blimp) |
123 .setMessage(message) | 199 .setMessage(message) |
200 .setCancelable(false) | |
124 .setPositiveButton(R.string.blimp_restart_now, | 201 .setPositiveButton(R.string.blimp_restart_now, |
125 new DialogInterface.OnClickListener() { | 202 new DialogInterface.OnClickListener() { |
126 @Override | 203 @Override |
127 public void onClick(DialogInterface dialog, int whic h) { | 204 public void onClick(DialogInterface dialog, int whic h) { |
128 restartBrowser(); | 205 restartBrowser(); |
129 } | 206 } |
130 }) | 207 }) |
131 .create() | 208 .create() |
132 .show(); | 209 .show(); |
133 } | 210 } |
134 | 211 |
135 /** | 212 /** |
136 * Restart the browser. | 213 * Restart the browser. |
137 */ | 214 */ |
138 @VisibleForTesting | 215 @VisibleForTesting |
139 protected static void restartBrowser() { | 216 protected void restartBrowser() { |
140 if (sSettingsCallback != null) { | 217 assert sPreferencesDelegate != null; |
141 sSettingsCallback.onRestartBrowserRequested(); | 218 sPreferencesDelegate.getDelegate().restartBrowser(); |
219 } | |
220 | |
221 /** | |
222 * Start user sign in flow to let the user pick an existing account or creat e new account. | |
223 */ | |
224 private void startUserSignInFlow() { | |
225 mWaitForSignIn = true; | |
226 assert sPreferencesDelegate != null; | |
227 sPreferencesDelegate.getDelegate().startUserSignInFlow(getActivity()); | |
228 } | |
229 | |
230 private boolean isSignedIn() { | |
231 return ChromeSigninController.get(ContextUtils.getApplicationContext()). isSignedIn(); | |
232 } | |
233 | |
234 @VisibleForTesting | |
235 @CalledByNative | |
236 protected void onSignedOut() { | |
237 // If user signed out, turn off the switch. We also do a sign in state c heck on | |
238 // {@link AboutBlimpPreferences#updateSettingPage()}. | |
239 final SwitchPreference pref = | |
240 (SwitchPreference) findPreference(PreferencesUtil.PREF_BLIMP_SWI TCH); | |
241 pref.setChecked(false); | |
242 showRestartDialog(getActivity(), R.string.blimp_sign_out_restart); | |
243 } | |
244 | |
245 @VisibleForTesting | |
246 @CalledByNative | |
247 protected void onSignedIn() { | |
248 // If user came back from sign in flow, turn on the switch and connect t o engine. | |
249 // This logic won't trigger the {@link OnPreferenceChangeListener} call. | |
250 if (mWaitForSignIn) { | |
251 final SwitchPreference pref = | |
252 (SwitchPreference) findPreference(PreferencesUtil.PREF_BLIMP _SWITCH); | |
253 pref.setChecked(true); | |
254 | |
255 sPreferencesDelegate.connect(); | |
256 mWaitForSignIn = false; | |
142 } | 257 } |
143 } | 258 } |
259 | |
260 @VisibleForTesting | |
261 protected void initializeNative() { | |
262 mNativeBlimpSettingsAndroid = nativeInit(); | |
263 assert sPreferencesDelegate != null && mNativeBlimpSettingsAndroid != 0; | |
264 | |
265 // Initialize in native code. | |
266 sPreferencesDelegate.initSettingsPage(this); | |
267 } | |
268 | |
269 @VisibleForTesting | |
270 protected void destroyNative() { | |
271 nativeDestroy(mNativeBlimpSettingsAndroid); | |
272 } | |
273 | |
274 @CalledByNative | |
275 private void clearNativePtr() { | |
276 mNativeBlimpSettingsAndroid = 0; | |
277 } | |
278 | |
279 @CalledByNative | |
280 private long getNativePtr() { | |
281 assert mNativeBlimpSettingsAndroid != 0; | |
282 return mNativeBlimpSettingsAndroid; | |
283 } | |
284 | |
285 private native long nativeInit(); | |
286 private native void nativeDestroy(long nativeBlimpSettingsAndroid); | |
144 } | 287 } |
OLD | NEW |