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 |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..d0d8c7d0e92fadf3168b4bb6c9ed59ee5b9e93c8 |
| --- /dev/null |
| +++ b/blimp/client/core/android/java/src/org/chromium/blimp/core/settings/AboutBlimpPreferences.java |
| @@ -0,0 +1,136 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package org.chromium.blimp.core.settings; |
| + |
| +import android.app.Activity; |
| +import android.content.Context; |
| +import android.content.DialogInterface; |
| +import android.os.Bundle; |
| +import android.preference.ListPreference; |
| +import android.preference.Preference; |
| +import android.preference.PreferenceFragment; |
| +import android.preference.PreferenceScreen; |
| +import android.support.v7.app.AlertDialog; |
| + |
| +import org.chromium.blimp.R; |
| +import org.chromium.blimp_public.BlimpSettingsCallbacks; |
| + |
| +// import org.chromium.chrome.browser.ApplicationLifetime; |
|
David Trainor- moved to gerrit
2016/07/22 16:51:42
?
xingliu
2016/07/23 23:50:36
Done.
|
| + |
| +/** |
| + * Blimp preferences page in Chrome. |
| + */ |
| +public class AboutBlimpPreferences extends PreferenceFragment { |
| + |
| + public static final String PREF_BLIMP_SWITCH = "blimp_switch"; |
| + private static final String PREF_ASSIGNER_URL = "blimp_assigner_url"; |
|
David Trainor- moved to gerrit
2016/07/22 16:51:42
Public as well. And javadocs for both :). Or hel
xingliu
2016/07/23 23:50:36
Done.
|
| + |
| + private static BlimpSettingsCallbacks sSettingsCallback; |
| + |
| + /** |
| + * Attach the blimp setting preferences to a {@link PreferenceFragment}. |
| + * @param fragment The fragment that blimp setting attach to. |
| + */ |
| + public static void addBlimpPreferences(PreferenceFragment fragment) { |
| + PreferenceScreen screen = fragment.getPreferenceScreen(); |
| + |
| + 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); |
| + |
| + screen.addPreference(blimpSetting); |
| + fragment.setPreferenceScreen(screen); |
| + } |
| + |
| + /** |
| + * Register Chrome callback related to Blimp settings. |
| + * @param callback |
| + */ |
| + public static void registerCallback(BlimpSettingsCallbacks callback) { |
| + sSettingsCallback = callback; |
| + } |
| + |
| + @Override |
| + public void onCreate(Bundle savedInstanceState) { |
| + super.onCreate(savedInstanceState); |
| + getActivity().setTitle(R.string.blimp_about_blimp_preferences); |
| + addPreferencesFromResource(R.xml.blimp_preferences); |
| + |
| + setupBlimpSwitch(); |
| + setupAssignerPreferences(); |
| + } |
| + |
| + /** |
| + * Setup the switch preference for blimp. |
| + */ |
| + private void setupBlimpSwitch() { |
| + // An alternative is to use {@link ChromeSwitchPreference}. |
|
David Trainor- moved to gerrit
2016/07/22 16:51:42
Can you add a TODO and a bug for when this is move
xingliu
2016/07/23 23:50:36
Done.
|
| + final Preference pref = findPreference(PREF_BLIMP_SWITCH); |
| + |
| + 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. |
| + showRestartDialog(getActivity(), R.string.blimp_switch_changed_please_restart); |
| + return true; |
| + } |
| + }); |
| + } |
| + |
| + /** |
| + * 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. |
| + */ |
| + private void setupAssignerPreferences() { |
| + final Activity activity = getActivity(); |
| + |
| + final ListPreference lp = (ListPreference) findPreference(PREF_ASSIGNER_URL); |
| + lp.setSummary(lp.getValue()); |
| + |
| + lp.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { |
| + @Override |
| + public boolean onPreferenceChange(Preference preference, Object newValue) { |
| + String newAssignmentUrl = (String) newValue; |
| + lp.setSummary(newAssignmentUrl); |
| + showRestartDialog(activity, R.string.blimp_assigner_changed_please_restart); |
| + return true; |
| + } |
| + }); |
| + } |
| + |
| + /** |
| + * Show restart browser dialog. |
| + * @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) { |
| + new AlertDialog.Builder(context) |
| + .setTitle(R.string.blimp_restart_blimp) |
| + .setMessage(message) |
| + .setPositiveButton(R.string.blimp_restart_now, |
| + new DialogInterface.OnClickListener() { |
| + @Override |
| + public void onClick(DialogInterface dialog, int which) { |
| + restartBrowser(context); |
| + } |
| + }) |
| + .create() |
| + .show(); |
| + } |
| + |
| + /** |
| + * Restart the browser. |
| + * @param context The context of restart application dialog. |
| + */ |
| + private static void restartBrowser(Context context) { |
|
David Trainor- moved to gerrit
2016/07/22 16:51:42
Do we need the context?
xingliu
2016/07/23 23:50:36
Done.
|
| + if (sSettingsCallback != null) { |
| + sSettingsCallback.onRestartBrowserRequested(); |
|
David Trainor- moved to gerrit
2016/07/22 16:51:42
Just to confirm, there's no way to avoid restartin
xingliu
2016/07/23 23:50:36
Not sure why we need undo the preferences, is it s
David Trainor- moved to gerrit
2016/07/27 17:31:34
I just meant the restart dialog (and subsequent re
xingliu
2016/07/27 23:52:04
Oh, restart will be triggered only if user clicks
|
| + } |
| + } |
| +} |