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.os.Bundle; | 7 import android.os.Bundle; |
8 import android.preference.Preference; | 8 import android.preference.Preference; |
9 import android.preference.Preference.OnPreferenceChangeListener; | 9 import android.preference.Preference.OnPreferenceChangeListener; |
10 import android.preference.PreferenceFragment; | 10 import android.preference.PreferenceFragment; |
11 import android.view.Menu; | 11 import android.view.Menu; |
12 import android.view.MenuInflater; | 12 import android.view.MenuInflater; |
13 import android.view.MenuItem; | 13 import android.view.MenuItem; |
14 | 14 |
| 15 import org.chromium.base.metrics.RecordUserAction; |
15 import org.chromium.chrome.R; | 16 import org.chromium.chrome.R; |
16 import org.chromium.chrome.browser.help.HelpAndFeedback; | 17 import org.chromium.chrome.browser.help.HelpAndFeedback; |
17 import org.chromium.chrome.browser.ntp.snippets.SnippetsBridge; | 18 import org.chromium.chrome.browser.ntp.snippets.SnippetsBridge; |
18 import org.chromium.chrome.browser.profiles.Profile; | 19 import org.chromium.chrome.browser.profiles.Profile; |
| 20 import org.chromium.chrome.browser.suggestions.ContentSuggestionsUma; |
19 | 21 |
20 /** | 22 /** |
21 * Settings fragment that allows the user to configure content suggestions. | 23 * Settings fragment that allows the user to configure content suggestions. |
22 */ | 24 */ |
23 public class ContentSuggestionsPreferences extends PreferenceFragment { | 25 public class ContentSuggestionsPreferences extends PreferenceFragment { |
24 private static final String PREF_MAIN_SWITCH = "suggestions_switch"; | 26 private static final String PREF_MAIN_SWITCH = "suggestions_switch"; |
25 private static final String PREF_NOTIFICATIONS_SWITCH = "suggestions_notific
ations_switch"; | 27 private static final String PREF_NOTIFICATIONS_SWITCH = "suggestions_notific
ations_switch"; |
26 private static final String PREF_CAVEATS = "suggestions_caveats"; | 28 private static final String PREF_CAVEATS = "suggestions_caveats"; |
27 private static final String PREF_LEARN_MORE = "suggestions_learn_more"; | 29 private static final String PREF_LEARN_MORE = "suggestions_learn_more"; |
28 | 30 |
| 31 private boolean mWasEnabledAtCreation; |
29 private boolean mIsEnabled; | 32 private boolean mIsEnabled; |
30 | 33 |
31 // Preferences, modified as the state of the screen changes. | 34 // Preferences, modified as the state of the screen changes. |
32 private ChromeSwitchPreference mFeatureSwitch; | 35 private ChromeSwitchPreference mFeatureSwitch; |
33 private ChromeSwitchPreference mNotificationsSwitch; | 36 private ChromeSwitchPreference mNotificationsSwitch; |
34 private Preference mCaveatsDescription; | 37 private Preference mCaveatsDescription; |
35 private Preference mLearnMoreButton; | 38 private Preference mLearnMoreButton; |
36 | 39 |
37 @Override | 40 @Override |
38 public void onCreate(Bundle savedInstanceState) { | 41 public void onCreate(Bundle savedInstanceState) { |
39 super.onCreate(savedInstanceState); | 42 super.onCreate(savedInstanceState); |
40 | 43 |
41 addPreferencesFromResource(R.xml.suggestions_preferences); | 44 addPreferencesFromResource(R.xml.suggestions_preferences); |
42 setHasOptionsMenu(true); | 45 setHasOptionsMenu(true); |
43 finishSwitchInitialisation(); | 46 finishSwitchInitialisation(); |
44 | 47 |
45 boolean isEnabled = SnippetsBridge.isRemoteSuggestionsServiceEnabled(); | 48 mWasEnabledAtCreation = SnippetsBridge.isRemoteSuggestionsServiceEnabled
(); |
46 mIsEnabled = !isEnabled; // Opposite so that we trigger side effects bel
ow. | 49 mIsEnabled = !mWasEnabledAtCreation; // Opposite so that we trigger side
effects below. |
47 updatePreferences(isEnabled); | 50 updatePreferences(mWasEnabledAtCreation); |
48 } | 51 } |
49 | 52 |
50 @Override | 53 @Override |
51 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { | 54 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { |
52 menu.clear(); | 55 menu.clear(); |
53 MenuItem help = | 56 MenuItem help = |
54 menu.add(Menu.NONE, R.id.menu_id_targeted_help, Menu.NONE, R.str
ing.menu_help); | 57 menu.add(Menu.NONE, R.id.menu_id_targeted_help, Menu.NONE, R.str
ing.menu_help); |
55 help.setIcon(R.drawable.ic_help_and_feedback); | 58 help.setIcon(R.drawable.ic_help_and_feedback); |
56 } | 59 } |
57 | 60 |
58 @Override | 61 @Override |
59 public boolean onOptionsItemSelected(MenuItem item) { | 62 public boolean onOptionsItemSelected(MenuItem item) { |
60 if (item.getItemId() == R.id.menu_id_targeted_help) { | 63 if (item.getItemId() == R.id.menu_id_targeted_help) { |
61 // TODO(dgn): The help page needs to be added and the context reserv
ed. | 64 // TODO(dgn): The help page needs to be added and the context reserv
ed. |
62 HelpAndFeedback.getInstance(getActivity()) | 65 HelpAndFeedback.getInstance(getActivity()) |
63 .show(getActivity(), getString(R.string.help_context_suggest
ions), | 66 .show(getActivity(), getString(R.string.help_context_suggest
ions), |
64 Profile.getLastUsedProfile(), null); | 67 Profile.getLastUsedProfile(), null); |
65 return true; | 68 return true; |
66 } | 69 } |
67 return false; | 70 return false; |
68 } | 71 } |
69 | 72 |
| 73 @Override |
| 74 public void onDestroy() { |
| 75 super.onDestroy(); |
| 76 |
| 77 int statusChange; |
| 78 if (mWasEnabledAtCreation) { |
| 79 statusChange = mIsEnabled ? ContentSuggestionsUma.PREFERENCE_TOGGLE_
ON_TO_ON |
| 80 : ContentSuggestionsUma.PREFERENCE_TOGGLE_
ON_TO_OFF; |
| 81 } else { |
| 82 statusChange = mIsEnabled ? ContentSuggestionsUma.PREFERENCE_TOGGLE_
OFF_TO_ON |
| 83 : ContentSuggestionsUma.PREFERENCE_TOGGLE_
OFF_TO_OFF; |
| 84 } |
| 85 ContentSuggestionsUma.remoteSuggestionsPreferencesUIAction(statusChange)
; |
| 86 } |
| 87 |
70 /** | 88 /** |
71 * Switches preference screens depending on whether the remote suggestions a
re enabled/disabled. | 89 * Switches preference screens depending on whether the remote suggestions a
re enabled/disabled. |
72 * @param isEnabled Indicates whether the remote suggestions are enabled. | 90 * @param isEnabled Indicates whether the remote suggestions are enabled. |
73 */ | 91 */ |
74 public void updatePreferences(boolean isEnabled) { | 92 public void updatePreferences(boolean isEnabled) { |
75 if (mIsEnabled == isEnabled) return; | 93 if (mIsEnabled == isEnabled) return; |
76 | 94 |
77 mFeatureSwitch.setChecked(isEnabled); | 95 mFeatureSwitch.setChecked(isEnabled); |
78 | 96 |
79 if (isEnabled) { | 97 if (isEnabled) { |
80 setNotificationsPrefState(true); | 98 setNotificationsPrefState(true); |
81 setCaveatsPrefState(false); | 99 setCaveatsPrefState(false); |
82 } else { | 100 } else { |
83 setNotificationsPrefState(false); | 101 setNotificationsPrefState(false); |
84 setCaveatsPrefState(true); | 102 setCaveatsPrefState(true); |
85 } | 103 } |
86 mIsEnabled = isEnabled; | 104 mIsEnabled = isEnabled; |
87 } | 105 } |
88 | 106 |
89 private void setNotificationsPrefState(boolean visible) { | 107 private void setNotificationsPrefState(boolean visible) { |
90 if (visible) { | 108 if (visible) { |
| 109 RecordUserAction.record("ContentSuggestions.RemoteSuggestionsPrefere
nceOn"); |
91 if (findPreference(PREF_NOTIFICATIONS_SWITCH) != null) return; | 110 if (findPreference(PREF_NOTIFICATIONS_SWITCH) != null) return; |
92 getPreferenceScreen().addPreference(mNotificationsSwitch); | 111 getPreferenceScreen().addPreference(mNotificationsSwitch); |
93 mNotificationsSwitch.setChecked(true); // TODO(dgn): initialise prop
erly. | 112 mNotificationsSwitch.setChecked(true); // TODO(dgn): initialise prop
erly. |
94 } else { | 113 } else { |
| 114 RecordUserAction.record("ContentSuggestions.RemoteSuggestionsPrefere
nceOff"); |
95 getPreferenceScreen().removePreference(mNotificationsSwitch); | 115 getPreferenceScreen().removePreference(mNotificationsSwitch); |
96 } | 116 } |
97 } | 117 } |
98 | 118 |
99 private void setCaveatsPrefState(boolean visible) { | 119 private void setCaveatsPrefState(boolean visible) { |
100 if (visible) { | 120 if (visible) { |
101 if (findPreference(PREF_CAVEATS) != null) return; | 121 if (findPreference(PREF_CAVEATS) != null) return; |
102 getPreferenceScreen().addPreference(mCaveatsDescription); | 122 getPreferenceScreen().addPreference(mCaveatsDescription); |
103 getPreferenceScreen().addPreference(mLearnMoreButton); | 123 getPreferenceScreen().addPreference(mLearnMoreButton); |
104 } else { | 124 } else { |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
137 | 157 |
138 mNotificationsSwitch.setOnPreferenceChangeListener(new OnPreferenceChang
eListener() { | 158 mNotificationsSwitch.setOnPreferenceChangeListener(new OnPreferenceChang
eListener() { |
139 @Override | 159 @Override |
140 public boolean onPreferenceChange(Preference preference, Object newV
alue) { | 160 public boolean onPreferenceChange(Preference preference, Object newV
alue) { |
141 // TODO(dgn) implement preference change. | 161 // TODO(dgn) implement preference change. |
142 return true; | 162 return true; |
143 } | 163 } |
144 }); | 164 }); |
145 } | 165 } |
146 } | 166 } |
OLD | NEW |