forked from mozilla-mobile/focus-android
/
SettingsFragment.java
174 lines (145 loc) · 8.11 KB
/
SettingsFragment.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.mozilla.focus.settings;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceManager;
import android.preference.PreferenceScreen;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.widget.ListView;
import org.mozilla.focus.R;
import org.mozilla.focus.activity.SettingsActivity;
import org.mozilla.focus.autocomplete.AutocompleteSettingsFragment;
import org.mozilla.focus.browser.LocalizedContent;
import org.mozilla.focus.locale.LocaleManager;
import org.mozilla.focus.locale.Locales;
import org.mozilla.focus.session.SessionManager;
import org.mozilla.focus.session.Source;
import org.mozilla.focus.telemetry.TelemetryWrapper;
import org.mozilla.focus.utils.AppConstants;
import org.mozilla.focus.utils.SupportUtils;
import org.mozilla.focus.widget.DefaultBrowserPreference;
import java.util.Locale;
public class SettingsFragment extends BaseSettingsFragment implements SharedPreferences.OnSharedPreferenceChangeListener {
private boolean localeUpdated;
public static SettingsFragment newInstance() {
return new SettingsFragment();
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
}
@Override
public void onResume() {
super.onResume();
getPreferenceManager().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
final DefaultBrowserPreference preference = (DefaultBrowserPreference) findPreference(getString(R.string.pref_key_default_browser));
if (preference != null) {
preference.update();
}
// Update title and icons when returning to fragments.
final ActionBarUpdater updater = (ActionBarUpdater) getActivity();
updater.updateTitle(R.string.menu_settings);
updater.updateIcon(R.drawable.ic_back);
}
@Override
public void onPause() {
getPreferenceManager().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
super.onPause();
}
@Override
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
final Resources resources = getResources();
// AppCompatActivity has a Toolbar that is used as the ActionBar, and it conflicts with the ActionBar
// used by PreferenceScreen to create the headers (with title, back navigation), so we wrap all these
// "preference screens" into separate activities.
if (preference.getKey().equals(resources.getString(R.string.pref_key_about))) {
SessionManager.getInstance().createSession(Source.MENU, LocalizedContent.URL_ABOUT);
getActivity().onBackPressed();
} else if (preference.getKey().equals(resources.getString(R.string.pref_key_help))) {
SessionManager.getInstance().createSession(Source.MENU, SupportUtils.HELP_URL);
getActivity().onBackPressed();
} else if (preference.getKey().equals(resources.getString(R.string.pref_key_rights))) {
SessionManager.getInstance().createSession(Source.MENU, LocalizedContent.URL_RIGHTS);
getActivity().onBackPressed();
} else if (preference.getKey().equals(resources.getString(R.string.pref_key_privacy_notice))) {
SessionManager.getInstance().createSession(Source.MENU, AppConstants.isKlarBuild() ?
SupportUtils.PRIVACY_NOTICE_KLAR_URL : SupportUtils.PRIVACY_NOTICE_URL);
getActivity().onBackPressed();
} else if (preference.getKey().equals(resources.getString(R.string.pref_key_search_engine))) {
navigateToFragment(new InstalledSearchEnginesSettingsFragment());
TelemetryWrapper.openSearchSettingsEvent();
} else if (preference.getKey().equals(resources.getString(R.string.pref_key_screen_autocomplete))) {
navigateToFragment(new AutocompleteSettingsFragment());
} else if (preference.getKey().equals(resources.getString(R.string.pref_key_reset_default_browser))) {
// Reset browser settings to default
PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext())
.edit()
.clear()
.apply();
// Reset locale to default - US
final LocaleManager localeManager = LocaleManager.getInstance();
localeManager.setSelectedLocale(getActivity(), String.valueOf(Locale.US));
localeManager.updateConfiguration(getActivity(), Locale.US);
// Update the language selection (in dialog)
final ListPreference languagePreference = (ListPreference) findPreference(getString(R.string.pref_key_locale));
languagePreference.setValue(String.valueOf(Locale.US));
// Scroll to selected item in ListView.
final ListView mListView = getActivity().findViewById(android.R.id.list);
mListView.smoothScrollToPosition(0);
mListView.setVerticalScrollBarEnabled(false);
// Half a second delay for better animation
new CountDownTimer(500, 1000) {
public void onTick(long millisUntilFinished) {}
public void onFinish() {
// Manually notify SettingsActivity of browser settings reset
if (mListView != null) {
mListView.setVerticalScrollBarEnabled(true);
}
if (getActivity() != null) {
addPreferencesFromResource(R.xml.settings);
}
}
}.start();
}
return super.onPreferenceTreeClick(preferenceScreen, preference);
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
TelemetryWrapper.settingsEvent(key, String.valueOf(sharedPreferences.getAll().get(key)));
if (!localeUpdated && key.equals(getString(R.string.pref_key_locale))) {
// Updating the locale leads to onSharedPreferenceChanged being triggered again in some
// cases. To avoid an infinite loop we won't update the preference a second time. This
// fragment gets replaced at the end of this method anyways.
localeUpdated = true;
final ListPreference languagePreference = (ListPreference) findPreference(getString(R.string.pref_key_locale));
final String value = languagePreference.getValue();
final LocaleManager localeManager = LocaleManager.getInstance();
final Locale locale;
if (TextUtils.isEmpty(value)) {
localeManager.resetToSystemLocale(getActivity());
locale = localeManager.getCurrentLocale(getActivity());
} else {
locale = Locales.parseLocaleCode(value);
localeManager.setSelectedLocale(getActivity(), value);
}
localeManager.updateConfiguration(getActivity(), locale);
// Manually notify SettingsActivity of locale changes (in most other cases activities
// will detect changes in onActivityResult(), but that doesn't apply to SettingsActivity).
getActivity().onConfigurationChanged(getActivity().getResources().getConfiguration());
// And ensure that the calling LocaleAware*Activity knows that the locale changed:
getActivity().setResult(SettingsActivity.ACTIVITY_RESULT_LOCALE_CHANGED);
// The easiest way to ensure we update the language is by replacing the entire fragment:
getFragmentManager().beginTransaction()
.replace(R.id.container, SettingsFragment.newInstance())
.commit();
}
}
}