-
-
Notifications
You must be signed in to change notification settings - Fork 25
/
MainConfigurablePreferences.java
executable file
·251 lines (214 loc) · 9.88 KB
/
MainConfigurablePreferences.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package org.commcare.preferences;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import androidx.annotation.NonNull;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import org.commcare.CommCareApp;
import org.commcare.CommCareApplication;
import org.commcare.activities.CommCarePreferenceActivity;
import org.commcare.activities.SessionAwarePreferenceActivity;
import org.commcare.dalvik.R;
import org.commcare.fragments.CommCarePreferenceFragment;
import org.commcare.views.PasswordShow;
import org.commcare.views.dialogs.StandardAlertDialog;
import org.javarosa.core.services.locale.Localization;
import java.util.HashMap;
import java.util.Map;
public class MainConfigurablePreferences
extends CommCarePreferenceFragment
implements OnSharedPreferenceChangeListener {
// Preferences the user has direct control over within CommCare
public final static String PREFS_PRINT_DOC_LOCATION = "print-doc-location";
private final static String PREFS_FUZZY_SEARCH_KEY = "cc-fuzzy-search-enabled";
public final static String GRID_MENUS_ENABLED = "cc-grid-menus";
public final static String UPDATE_TARGET = "cc-update-target";
public final static String AUTO_UPDATE_FREQUENCY = "cc-autoup-freq";
public final static String SHOW_PASSWORD_OPTION = "cc-password-entry-show-behavior";
public final static String PREFS_LOCALE_KEY = "cur_locale";
public final static String ANALYTICS_ENABLED = "cc-analytics-enabled";
public final static String INTENT_CALLOUT_FOR_SCANNER = "cc-intent-callout-for-scanner";
public final static String ENABLE_TEXT_TO_SPEECH = "cc-enable-tts";
// Fake settings that really act as buttons to open a new activity or choice dialog
private final static String DEVELOPER_SETTINGS = "developer-settings-button";
private final static String DISABLE_ANALYTICS = "disable-analytics-button";
private static final int REQUEST_DEVELOPER_PREFERENCES = 1;
private final static Map<String, String> keyToTitleMap = new HashMap<>();
static {
keyToTitleMap.put(DEVELOPER_SETTINGS, "settings.developer.options");
keyToTitleMap.put(DISABLE_ANALYTICS, "home.menu.disable.analytics");
}
@NonNull
@Override
protected String getTitle() {
return Localization.get("settings.main.title");
}
@Override
protected Map<String, String> getPrefKeyTitleMap() {
return keyToTitleMap;
}
@Override
protected int getPreferencesResource() {
return R.xml.main_preferences;
}
@Override
protected boolean isPersistentAppPreference() {
return true;
}
@Override
public void onStart() {
super.onStart();
setVisibilityOfUpdateOptionsPref();
}
private void setVisibilityOfUpdateOptionsPref() {
Preference updateOptionsPref = getPreferenceManager().findPreference(UPDATE_TARGET);
if (!DeveloperPreferences.shouldShowUpdateOptionsSetting() && updateOptionsPref != null) {
// If the pref is showing and it shouldn't be
PreferenceScreen prefScreen = getPreferenceScreen();
prefScreen.removePreference(updateOptionsPref);
} else if (DeveloperPreferences.shouldShowUpdateOptionsSetting() &&
updateOptionsPref == null) {
// If the pref isn't showing and it should be
reset();
}
}
@Override
protected void setupPrefClickListeners() {
configureDevPreferencesButton();
Preference analyticsButton = findPreference(DISABLE_ANALYTICS);
if (MainConfigurablePreferences.isAnalyticsEnabled()) {
analyticsButton.setOnPreferenceClickListener(preference -> {
showAnalyticsOptOutDialog();
return true;
});
} else {
getPreferenceScreen().removePreference(analyticsButton);
}
}
private void configureDevPreferencesButton() {
Preference developerSettingsButton = findPreference(DEVELOPER_SETTINGS);
if (DeveloperPreferences.isSuperuserEnabled()) {
developerSettingsButton.setOnPreferenceClickListener(preference -> {
startDeveloperOptions();
return true;
});
} else {
getPreferenceScreen().removePreference(developerSettingsButton);
}
}
private void startDeveloperOptions() {
Intent intent = new Intent(getActivity(), SessionAwarePreferenceActivity.class);
intent.putExtra(CommCarePreferenceActivity.EXTRA_PREF_TYPE, CommCarePreferenceActivity.PREF_TYPE_DEVELOPER);
startActivityForResult(intent, REQUEST_DEVELOPER_PREFERENCES);
}
private void showAnalyticsOptOutDialog() {
StandardAlertDialog f = new StandardAlertDialog(getActivity(),
Localization.get("analytics.opt.out.title"),
Localization.get("analytics.opt.out.message"));
f.setPositiveButton(Localization.get("analytics.disable.button"),
(dialog, which) -> {
dialog.dismiss();
MainConfigurablePreferences.disableAnalytics();
});
f.setNegativeButton(Localization.get("option.cancel"),
(dialog, which) -> dialog.dismiss());
f.showNonPersistentDialog();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_DEVELOPER_PREFERENCES) {
if (resultCode == DeveloperPreferences.RESULT_SYNC_CUSTOM) {
getActivity().setResult(DeveloperPreferences.RESULT_SYNC_CUSTOM);
getActivity().finish();
} else if (resultCode == DeveloperPreferences.RESULT_DEV_OPTIONS_DISABLED) {
configureDevPreferencesButton();
}
}
}
public static PasswordShow.PasswordShowOption getPasswordDisplayOption() {
SharedPreferences properties = CommCareApplication.instance().getCurrentApp().getAppPreferences();
return PasswordShow.PasswordShowOption.fromString(properties.getString(SHOW_PASSWORD_OPTION, ""));
}
public static boolean isGridMenuEnabled() {
SharedPreferences properties = CommCareApplication.instance().getCurrentApp().getAppPreferences();
return properties.getString(GRID_MENUS_ENABLED, PrefValues.NO).equals(PrefValues.YES);
}
public static boolean isFuzzySearchEnabled() {
SharedPreferences properties = CommCareApplication.instance().getCurrentApp().getAppPreferences();
return properties.getString(PREFS_FUZZY_SEARCH_KEY, PrefValues.NO).equals(PrefValues.YES);
}
public static boolean isAnalyticsEnabled() {
CommCareApp app = CommCareApplication.instance().getCurrentApp();
if (app == null) {
return true;
}
return app.getAppPreferences().getBoolean(ANALYTICS_ENABLED, true);
}
public static boolean useIntentCalloutForScanner() {
CommCareApp app = CommCareApplication.instance().getCurrentApp();
if (app == null) {
return false;
}
return app.getAppPreferences().getString(INTENT_CALLOUT_FOR_SCANNER, PrefValues.NO).equals(PrefValues.YES);
}
public static boolean isTTSEnabled() {
CommCareApp app = CommCareApplication.instance().getCurrentApp();
if (app == null) {
return false;
}
return app.getAppPreferences().getString(ENABLE_TEXT_TO_SPEECH, PrefValues.NO).equals(PrefValues.YES);
}
public static void disableAnalytics() {
CommCareApp app = CommCareApplication.instance().getCurrentApp();
if (app == null) {
return;
}
app.getAppPreferences().edit().putBoolean(ANALYTICS_ENABLED, false).apply();
}
public static String getUpdateTargetParam() {
String updateTarget = getUpdateTarget();
if (PrefValues.UPDATE_TARGET_BUILD.equals(updateTarget)
|| PrefValues.UPDATE_TARGET_SAVED.equals(updateTarget)) {
// We only need to add a query param to the update URL if the target is set to
// something other than the default
return updateTarget;
} else {
return "";
}
}
/**
* @return the update target set by the user, or default to latest starred build if none set.
* The 3 options are:
* 1. Latest starred build (this is the default)
* 2. Latest build, starred or un-starred
* 3. Latest saved version (whether or not a build has been created it for it)
*/
private static String getUpdateTarget() {
SharedPreferences properties = CommCareApplication.instance().getCurrentApp().getAppPreferences();
return properties.getString(UPDATE_TARGET, PrefValues.UPDATE_TARGET_STARRED);
}
public static void setUpdateTarget(String updateTargetValue) {
if (PrefValues.UPDATE_TARGET_BUILD.equals(updateTargetValue) ||
PrefValues.UPDATE_TARGET_SAVED.equals(updateTargetValue) ||
PrefValues.UPDATE_TARGET_STARRED.equals(updateTargetValue)) {
CommCareApplication.instance().getCurrentApp().getAppPreferences()
.edit()
.putString(UPDATE_TARGET, updateTargetValue)
.apply();
}
}
public static String getGlobalTemplatePath() {
SharedPreferences prefs = CommCareApplication.instance().getCurrentApp().getAppPreferences();
String path = prefs.getString(MainConfigurablePreferences.PREFS_PRINT_DOC_LOCATION, "");
if ("".equals(path)) {
return null;
} else {
return path;
}
}
public static void setCurrentLocale(String currentLocale) {
SharedPreferences prefs = CommCareApplication.instance().getCurrentApp().getAppPreferences();
prefs.edit().putString(PREFS_LOCALE_KEY, currentLocale).apply();
}
}