| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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.chrome.browser.preferences; | 5 package org.chromium.chrome.browser.preferences; |
| 6 | 6 |
| 7 import android.content.Context; |
| 8 import android.content.Intent; |
| 7 import android.os.Bundle; | 9 import android.os.Bundle; |
| 8 import android.preference.Preference; | 10 import android.preference.Preference; |
| 9 import android.preference.Preference.OnPreferenceChangeListener; | 11 import android.preference.Preference.OnPreferenceChangeListener; |
| 10 import android.preference.PreferenceFragment; | 12 import android.preference.PreferenceFragment; |
| 13 import android.support.annotation.IntDef; |
| 11 import android.view.Menu; | 14 import android.view.Menu; |
| 12 import android.view.MenuInflater; | 15 import android.view.MenuInflater; |
| 13 import android.view.MenuItem; | 16 import android.view.MenuItem; |
| 14 | 17 |
| 18 import org.chromium.base.metrics.RecordUserAction; |
| 15 import org.chromium.chrome.R; | 19 import org.chromium.chrome.R; |
| 16 import org.chromium.chrome.browser.ChromeFeatureList; | 20 import org.chromium.chrome.browser.ChromeFeatureList; |
| 17 import org.chromium.chrome.browser.help.HelpAndFeedback; | 21 import org.chromium.chrome.browser.help.HelpAndFeedback; |
| 22 import org.chromium.chrome.browser.ntp.ContentSuggestionsNotificationHelper; |
| 23 import org.chromium.chrome.browser.ntp.snippets.ContentSuggestionsNotificationAc
tion; |
| 24 import org.chromium.chrome.browser.ntp.snippets.ContentSuggestionsNotificationOp
tOut; |
| 18 import org.chromium.chrome.browser.ntp.snippets.SnippetsBridge; | 25 import org.chromium.chrome.browser.ntp.snippets.SnippetsBridge; |
| 19 import org.chromium.chrome.browser.profiles.Profile; | 26 import org.chromium.chrome.browser.profiles.Profile; |
| 20 | 27 |
| 28 import java.lang.annotation.Retention; |
| 29 import java.lang.annotation.RetentionPolicy; |
| 30 |
| 21 /** | 31 /** |
| 22 * Settings fragment that allows the user to configure content suggestions. | 32 * Settings fragment that allows the user to configure content suggestions. |
| 23 */ | 33 */ |
| 24 public class ContentSuggestionsPreferences extends PreferenceFragment { | 34 public class ContentSuggestionsPreferences extends PreferenceFragment { |
| 25 private static final String PREF_MAIN_SWITCH = "suggestions_switch"; | 35 private static final String PREF_MAIN_SWITCH = "suggestions_switch"; |
| 26 private static final String PREF_NOTIFICATIONS_SWITCH = "suggestions_notific
ations_switch"; | 36 private static final String PREF_NOTIFICATIONS_SWITCH = "suggestions_notific
ations_switch"; |
| 27 private static final String PREF_CAVEATS = "suggestions_caveats"; | 37 private static final String PREF_CAVEATS = "suggestions_caveats"; |
| 28 private static final String PREF_LEARN_MORE = "suggestions_learn_more"; | 38 private static final String PREF_LEARN_MORE = "suggestions_learn_more"; |
| 29 | 39 |
| 40 private static final String LAUNCH_SOURCE_EXTRA = "source"; |
| 41 |
| 42 @IntDef({LAUNCH_SOURCE_SETTINGS, LAUNCH_SOURCE_NOTIFICATION}) |
| 43 @Retention(RetentionPolicy.SOURCE) |
| 44 public @interface LaunchSource {} |
| 45 public static final int LAUNCH_SOURCE_SETTINGS = 0; |
| 46 public static final int LAUNCH_SOURCE_NOTIFICATION = 1; |
| 47 |
| 30 private boolean mIsEnabled; | 48 private boolean mIsEnabled; |
| 31 | 49 |
| 32 // Preferences, modified as the state of the screen changes. | 50 // Preferences, modified as the state of the screen changes. |
| 33 private ChromeSwitchPreference mFeatureSwitch; | 51 private ChromeSwitchPreference mFeatureSwitch; |
| 34 private ChromeSwitchPreference mNotificationsSwitch; | 52 private ChromeSwitchPreference mNotificationsSwitch; |
| 35 private Preference mCaveatsDescription; | 53 private Preference mCaveatsDescription; |
| 36 private Preference mLearnMoreButton; | 54 private Preference mLearnMoreButton; |
| 37 | 55 |
| 56 /** |
| 57 * Creates an intent for launching the content suggestions settings page. |
| 58 * @param context The current Activity, or an application context if no Acti
vity is available. |
| 59 * @param source Where the intent is going to be launched from. See {@link L
aunchSource} |
| 60 */ |
| 61 public static Intent createLaunchIntent(Context context, @LaunchSource int s
ource) { |
| 62 Intent intent = PreferencesLauncher.createIntentForSettingsPage( |
| 63 context, ContentSuggestionsPreferences.class.getName()); |
| 64 intent.putExtra(LAUNCH_SOURCE_EXTRA, source); |
| 65 return intent; |
| 66 } |
| 67 |
| 38 @Override | 68 @Override |
| 39 public void onCreate(Bundle savedInstanceState) { | 69 public void onCreate(Bundle savedInstanceState) { |
| 40 super.onCreate(savedInstanceState); | 70 super.onCreate(savedInstanceState); |
| 41 | 71 |
| 42 addPreferencesFromResource(R.xml.suggestions_preferences); | 72 addPreferencesFromResource(R.xml.suggestions_preferences); |
| 43 setHasOptionsMenu(true); | 73 setHasOptionsMenu(true); |
| 44 finishSwitchInitialisation(); | 74 finishSwitchInitialisation(); |
| 45 | 75 |
| 46 boolean isEnabled = SnippetsBridge.isRemoteSuggestionsServiceEnabled(); | 76 boolean isEnabled = SnippetsBridge.areRemoteSuggestionsEnabled(); |
| 47 mIsEnabled = !isEnabled; // Opposite so that we trigger side effects bel
ow. | 77 mIsEnabled = !isEnabled; // Opposite so that we trigger side effects bel
ow. |
| 48 updatePreferences(isEnabled); | 78 updatePreferences(isEnabled); |
| 79 |
| 80 @LaunchSource |
| 81 int launchSource = |
| 82 getActivity().getIntent().getIntExtra(LAUNCH_SOURCE_EXTRA, LAUNC
H_SOURCE_SETTINGS); |
| 83 if (launchSource == LAUNCH_SOURCE_NOTIFICATION) { |
| 84 ContentSuggestionsNotificationHelper.recordNotificationAction( |
| 85 ContentSuggestionsNotificationAction.OPEN_SETTINGS); |
| 86 } |
| 49 } | 87 } |
| 50 | 88 |
| 51 @Override | 89 @Override |
| 52 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { | 90 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { |
| 53 menu.clear(); | 91 menu.clear(); |
| 54 MenuItem help = | 92 MenuItem help = |
| 55 menu.add(Menu.NONE, R.id.menu_id_targeted_help, Menu.NONE, R.str
ing.menu_help); | 93 menu.add(Menu.NONE, R.id.menu_id_targeted_help, Menu.NONE, R.str
ing.menu_help); |
| 56 help.setIcon(R.drawable.ic_help_and_feedback); | 94 help.setIcon(R.drawable.ic_help_and_feedback); |
| 57 } | 95 } |
| 58 | 96 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 95 if (!ChromeFeatureList.isEnabled(ChromeFeatureList.CONTENT_SUGGESTIONS_N
OTIFICATIONS)) { | 133 if (!ChromeFeatureList.isEnabled(ChromeFeatureList.CONTENT_SUGGESTIONS_N
OTIFICATIONS)) { |
| 96 return false; | 134 return false; |
| 97 } | 135 } |
| 98 return mIsEnabled; | 136 return mIsEnabled; |
| 99 } | 137 } |
| 100 | 138 |
| 101 private void setNotificationsPrefState(boolean visible) { | 139 private void setNotificationsPrefState(boolean visible) { |
| 102 if (visible) { | 140 if (visible) { |
| 103 if (findPreference(PREF_NOTIFICATIONS_SWITCH) != null) return; | 141 if (findPreference(PREF_NOTIFICATIONS_SWITCH) != null) return; |
| 104 getPreferenceScreen().addPreference(mNotificationsSwitch); | 142 getPreferenceScreen().addPreference(mNotificationsSwitch); |
| 105 | |
| 106 } else { | 143 } else { |
| 107 getPreferenceScreen().removePreference(mNotificationsSwitch); | 144 getPreferenceScreen().removePreference(mNotificationsSwitch); |
| 108 } | 145 } |
| 109 } | 146 } |
| 110 | 147 |
| 111 private void setCaveatsPrefState(boolean visible) { | 148 private void setCaveatsPrefState(boolean visible) { |
| 112 if (visible) { | 149 if (visible) { |
| 113 if (findPreference(PREF_CAVEATS) != null) return; | 150 if (findPreference(PREF_CAVEATS) != null) return; |
| 114 getPreferenceScreen().addPreference(mCaveatsDescription); | 151 getPreferenceScreen().addPreference(mCaveatsDescription); |
| 115 getPreferenceScreen().addPreference(mLearnMoreButton); | 152 getPreferenceScreen().addPreference(mLearnMoreButton); |
| 116 } else { | 153 } else { |
| 117 getPreferenceScreen().removePreference(mCaveatsDescription); | 154 getPreferenceScreen().removePreference(mCaveatsDescription); |
| 118 getPreferenceScreen().removePreference(mLearnMoreButton); | 155 getPreferenceScreen().removePreference(mLearnMoreButton); |
| 119 } | 156 } |
| 120 } | 157 } |
| 121 | 158 |
| 122 private void finishSwitchInitialisation() { | 159 private void finishSwitchInitialisation() { |
| 123 mFeatureSwitch = (ChromeSwitchPreference) findPreference(PREF_MAIN_SWITC
H); | 160 mFeatureSwitch = (ChromeSwitchPreference) findPreference(PREF_MAIN_SWITC
H); |
| 124 mNotificationsSwitch = (ChromeSwitchPreference) findPreference(PREF_NOTI
FICATIONS_SWITCH); | 161 mNotificationsSwitch = (ChromeSwitchPreference) findPreference(PREF_NOTI
FICATIONS_SWITCH); |
| 125 mCaveatsDescription = findPreference(PREF_CAVEATS); | 162 mCaveatsDescription = findPreference(PREF_CAVEATS); |
| 126 mLearnMoreButton = findPreference(PREF_LEARN_MORE); | 163 mLearnMoreButton = findPreference(PREF_LEARN_MORE); |
| 127 | 164 |
| 128 mFeatureSwitch.setOnPreferenceChangeListener(new OnPreferenceChangeListe
ner() { | 165 mFeatureSwitch.setOnPreferenceChangeListener(new OnPreferenceChangeListe
ner() { |
| 129 @Override | 166 @Override |
| 130 public boolean onPreferenceChange(Preference preference, Object newV
alue) { | 167 public boolean onPreferenceChange(Preference preference, Object newV
alue) { |
| 131 SnippetsBridge.setRemoteSuggestionsServiceEnabled((boolean) newV
alue); | 168 boolean isEnabled = (boolean) newValue; |
| 169 SnippetsBridge.setRemoteSuggestionsEnabled(isEnabled); |
| 132 // TODO(dgn): Is there a way to have a visual feedback of when t
he remote | 170 // TODO(dgn): Is there a way to have a visual feedback of when t
he remote |
| 133 // suggestions service has completed being turned on or off? | 171 // suggestions service has completed being turned on or off? |
| 134 ContentSuggestionsPreferences.this.updatePreferences((boolean) n
ewValue); | 172 updatePreferences(isEnabled); |
| 173 |
| 174 if (isEnabled) { |
| 175 RecordUserAction.record("ContentSuggestions.RemoteSuggestion
sPreferenceOn"); |
| 176 } else { |
| 177 RecordUserAction.record("ContentSuggestions.RemoteSuggestion
sPreferenceOff"); |
| 178 } |
| 179 |
| 135 return true; | 180 return true; |
| 136 } | 181 } |
| 137 }); | 182 }); |
| 138 mFeatureSwitch.setManagedPreferenceDelegate(new ManagedPreferenceDelegat
e() { | 183 mFeatureSwitch.setManagedPreferenceDelegate(new ManagedPreferenceDelegat
e() { |
| 139 @Override | 184 @Override |
| 140 public boolean isPreferenceControlledByPolicy(Preference preference)
{ | 185 public boolean isPreferenceControlledByPolicy(Preference preference)
{ |
| 141 return SnippetsBridge.isRemoteSuggestionsServiceManaged(); | 186 return SnippetsBridge.areRemoteSuggestionsManaged(); |
| 142 } | 187 } |
| 143 | 188 |
| 144 @Override | 189 @Override |
| 145 public boolean isPreferenceControlledByCustodian(Preference preferen
ce) { | 190 public boolean isPreferenceControlledByCustodian(Preference preferen
ce) { |
| 146 return SnippetsBridge.isRemoteSuggestionsServiceManagedByCustodi
an(); | 191 return SnippetsBridge.areRemoteSuggestionsManagedByCustodian(); |
| 147 } | 192 } |
| 148 }); | 193 }); |
| 149 | 194 |
| 150 mNotificationsSwitch.setOnPreferenceChangeListener(new OnPreferenceChang
eListener() { | 195 mNotificationsSwitch.setOnPreferenceChangeListener(new OnPreferenceChang
eListener() { |
| 151 @Override | 196 @Override |
| 152 public boolean onPreferenceChange(Preference preference, Object newV
alue) { | 197 public boolean onPreferenceChange(Preference preference, Object newV
alue) { |
| 153 SnippetsBridge.setContentSuggestionsNotificationsEnabled((boolea
n) newValue); | 198 boolean isEnabled = (boolean) newValue; |
| 199 SnippetsBridge.setContentSuggestionsNotificationsEnabled(isEnabl
ed); |
| 200 |
| 201 if (isEnabled) { |
| 202 RecordUserAction.record("ContentSuggestions.NotificationsPre
ferenceOn"); |
| 203 } else { |
| 204 RecordUserAction.record("ContentSuggestions.NotificationsPre
ferenceOff"); |
| 205 ContentSuggestionsNotificationHelper.recordNotificationOptOu
t( |
| 206 ContentSuggestionsNotificationOptOut.EXPLICIT); |
| 207 } |
| 208 |
| 154 return true; | 209 return true; |
| 155 } | 210 } |
| 156 }); | 211 }); |
| 157 } | 212 } |
| 158 } | 213 } |
| OLD | NEW |