This repository has been archived by the owner on Aug 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
SettingsFragment.java
370 lines (337 loc) · 16.6 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
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/*
* Copyright (c) 2015
*
* ApkTrack is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ApkTrack is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with ApkTrack. If not, see <http://www.gnu.org/licenses/>.
*/
package fr.kwiatkowski.apktrack.ui;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceFragmentCompat;
import android.util.Log;
import android.widget.Toast;
import fr.kwiatkowski.apktrack.MainActivity;
import fr.kwiatkowski.apktrack.R;
import fr.kwiatkowski.apktrack.model.InstalledApp;
import fr.kwiatkowski.apktrack.service.EventBusHelper;
import fr.kwiatkowski.apktrack.service.message.ModelModifiedMessage;
import fr.kwiatkowski.apktrack.service.utils.CapabilitiesHelper;
import fr.kwiatkowski.apktrack.service.utils.ProxyHelper;
import java.util.List;
public class SettingsFragment extends PreferenceFragmentCompat
{
public final static String KEY_PREF_SEARCH_ENGINE = "pref_search_engine";
public final static String KEY_PREF_SORT_TYPE = "pref_sort_type";
public final static String KEY_PREF_BACKGROUND_CHECKS = "pref_background_checks";
public final static String KEY_PREF_WIFI_ONLY = "pref_wifi_only";
public final static String KEY_PREF_DOWNLOAD_APKS = "pref_automatic_downloads";
public final static String KEY_PREF_PROXY_TYPE = "pref_proxy_type";
public final static String KEY_PREF_PROXY_ADDRESS = "pref_proxy_address";
public final static String KEY_PREF_PROXY_WARNING = "pref_proxy_warning";
public final static String KEY_PREF_CLEAN_APKS = "action_clean_downloads";
public final static String KEY_PREF_REFRESH_APPS = "action_refresh_installed_apps";
public final static String KEY_PREF_RESET_IGNORED = "pref_reset_ignored_apps";
public final static String KEY_PREF_IGNORE_SYSTEM_APPS = "pref_ignore_system_apps";
public final static String KEY_PREF_IGNORE_XPOSED_APPS = "pref_ignore_xposed_apps";
public final static String KEY_PREF_IGNORE_UNKNOWN_APPS = "pref_ignore_unknown_apps";
public final static String ALPHA_SORT = "alpha";
public final static String STATUS_SORT = "status";
// Not displayed in the settings screen: the user interacts with it from the top menu.
public final static String KEY_PREF_SHOW_SYSTEM = "pref_show_system";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
// --------------------------------------------------------------------------------------------
@Override
public void onResume()
{
super.onResume();
_enable_buttons();
}
// --------------------------------------------------------------------------------------------
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey)
{
addPreferencesFromResource(R.xml.preferences);
final Preference reset = findPreference(KEY_PREF_RESET_IGNORED);
final Preference ignore_system = findPreference(KEY_PREF_IGNORE_SYSTEM_APPS);
final Preference ignore_xposed = findPreference(KEY_PREF_IGNORE_XPOSED_APPS);
final Preference ignore_unknown = findPreference(KEY_PREF_IGNORE_UNKNOWN_APPS);
final Preference privacy = findPreference("action_privacy_policy");
final Preference clean_apks = findPreference(KEY_PREF_CLEAN_APKS);
final Preference proxy_type = findPreference(KEY_PREF_PROXY_TYPE);
final Preference proxy_address = findPreference(KEY_PREF_PROXY_ADDRESS);
final Preference refresh_apps = findPreference(KEY_PREF_REFRESH_APPS);
if (reset == null || privacy == null || ignore_system == null || ignore_xposed == null ||
proxy_type == null || proxy_address == null || clean_apks == null || refresh_apps == null)
{
Log.v(MainActivity.TAG, "The preferences are malformed!");
return;
}
_enable_buttons();
// Add a click listener to unignore apps.
reset.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(final Preference preference)
{
new AlertDialog.Builder(preference.getContext())
.setTitle(R.string.app_name)
.setMessage(R.string.confirm_reset_ignored)
.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i)
{
InstalledApp.executeQuery("UPDATE installed_app SET _isignored = 0");
_enable_buttons();
// Re-enable the "show system apps" button if it was disabled
Activity activity = getActivity();
if (activity != null) {
activity.invalidateOptionsMenu();
}
}
})
.setNegativeButton(R.string.cancel, null).show();
_enable_buttons();
return false;
}
});
// Add a click listener to ignore system apps.
ignore_system.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference)
{
InstalledApp.executeQuery("UPDATE installed_app SET _isignored = 1 WHERE " +
"_systemapp = 1");
// Disable the "show system apps" button
Activity activity = getActivity();
if (activity != null) {
activity.invalidateOptionsMenu();
}
_enable_buttons();
return false;
}
});
// Add a click listener to ignore unknown apps.
ignore_unknown.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference)
{
InstalledApp.executeQuery("UPDATE installed_app SET _isignored = 1 WHERE " +
"_updatesource is NULL");
_enable_buttons();
return false;
}
});
// Add a click listener to ignore xposed apps.
ignore_xposed.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference)
{
InstalledApp.executeQuery("UPDATE installed_app SET _isignored = 1 WHERE " +
"_updatesource LIKE 'Xposed%'");
_enable_buttons();
return false;
}
});
// Add a click listener to open the privacy policy
privacy.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference)
{
if (CapabilitiesHelper.check_browser_available(getContext())) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://apktrack.kwiatkowski.fr/privacy")));
}
else // No browser is present on the device.
{
privacy.setSummary(getContext().getString(R.string.cant_handle_view));
privacy.setEnabled(false);
}
return false;
}
});
// Set the right summary and enabled status for proxy options
render_proxy_type_preference(null);
set_proxy_address_summary(null);
// Add a change listener to disable so this function is called when the value is updated.
proxy_type.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue)
{
render_proxy_type_preference(newValue.toString());
return true;
}
});
proxy_address.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue)
{
// Assert that the input value is valid.
if (!ProxyHelper.test_proxy_address(newValue.toString()))
{
Toast.makeText(getContext(), R.string.invalid_proxy_address, Toast.LENGTH_SHORT).show();
return false;
}
set_proxy_address_summary(newValue.toString());
return true;
}
});
// Setup the description and click listener for the "Delete APKs downloaded by ApkTrack" setting.
final List<InstalledApp> downloaded = InstalledApp.find(InstalledApp.class, "_downloadid != 0");
clean_apks.setSummary(getResources().getString(R.string.clean_downloads_description, downloaded.size()));
if (downloaded.size() == 0) {
clean_apks.setEnabled(false);
}
else {
clean_apks.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference)
{
for (InstalledApp app : downloaded)
{
app.clean_downloads(getContext());
EventBusHelper.post_sticky(ModelModifiedMessage.event_type.APP_UPDATED, app.get_package_name());
}
clean_apks.setSummary(getResources().getString(R.string.clean_downloads_description, 0));
clean_apks.setEnabled(false);
return true;
}
});
}
// Setup the listener for the "Refresh App" setting.
refresh_apps.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
refresh_apps.setEnabled(false);
refresh_apps.setSummary(R.string.refresh_installed_apps_desc_2);
// Do not perform the detection in the UI thread.
new Thread(() -> {
Activity activity = getActivity();
if (activity == null) {
return;
}
int[] detection_result = InstalledApp.update_applist(activity.getPackageManager());
// Update the button from the UI thread.
activity.runOnUiThread(() -> {
refresh_apps.setSummary(getResources().getString(R.string.refresh_installed_apps_desc_3,
detection_result[0], detection_result[1], detection_result[2]));
refresh_apps.setEnabled(true);
});
}).start();
return true;
}
});
}
// --------------------------------------------------------------------------------------------
/**
* Called to determine whether the "ignore [category of apps]" actions should be
* enabled or disabled, depending on whether there are actually apps to be
* ignored in the category.
*/
private void _enable_buttons()
{
final Preference reset = findPreference(KEY_PREF_RESET_IGNORED);
final Preference ignore_system = findPreference(KEY_PREF_IGNORE_SYSTEM_APPS);
final Preference ignore_xposed = findPreference(KEY_PREF_IGNORE_XPOSED_APPS);
final Preference ignore_unknown = findPreference(KEY_PREF_IGNORE_UNKNOWN_APPS);
if (reset != null)
{
long ignored_apps = InstalledApp.count(InstalledApp.class, "_isignored = 1", null);
reset.setEnabled(ignored_apps != 0);
}
if (ignore_xposed != null)
{
long xposed_apps = InstalledApp.count(InstalledApp.class,
"_updatesource LIKE 'Xposed%' AND _isignored = 0",
null);
ignore_xposed.setEnabled(xposed_apps != 0);
}
if (ignore_system != null) {
ignore_system.setEnabled(InstalledApp.check_system_apps_tracked());
}
if (ignore_unknown != null) {
long unknown_apps = InstalledApp.count(InstalledApp.class,
"_updatesource is NULL AND _isignored = 0",
null);
ignore_unknown.setEnabled(unknown_apps != 0);
}
}
// --------------------------------------------------------------------------------------------
/**
* This function is used to set the right summary for the proxy type setting, and
* disable the proxy address setting if no proxy type is selected.
* @param new_type The new value for this setting (may be null if we're initializing and not
* updating).
*/
private void render_proxy_type_preference(String new_type)
{
Preference proxy_type = findPreference(KEY_PREF_PROXY_TYPE);
Preference proxy_address = findPreference(KEY_PREF_PROXY_ADDRESS);
Preference proxy_warning = findPreference(KEY_PREF_PROXY_WARNING);
if (proxy_type == null || proxy_address == null || proxy_warning == null)
{
Log.v(MainActivity.TAG, "The preferences are malformed!");
return;
}
// new_type will be set if this function is called from the PropertyChangeListener, but
// will be null when called from onCreatePreferences. Get the stored value in that case.
if (new_type == null) {
new_type = proxy_type.getSharedPreferences().getString(KEY_PREF_PROXY_TYPE, "DIRECT");
}
switch (new_type)
{
case "DIRECT":
proxy_type.setSummary(R.string.no_proxy_summary);
proxy_address.setEnabled(false);
proxy_warning.setEnabled(false);
break;
case "HTTP":
proxy_type.setSummary(R.string.http_proxy_summary);
proxy_address.setEnabled(true);
proxy_warning.setEnabled(true);
break;
case "SOCKS":
proxy_type.setSummary(R.string.socks_proxy_summary);
proxy_address.setEnabled(true);
proxy_warning.setEnabled(true);
break;
}
}
// --------------------------------------------------------------------------------------------
/**
* This function is used to set the right summary for the proxy address setting.
* The following code would not exist if "%s" were supported by EditTextPreference.
* @param new_value The new value for this setting (may be null if we're initializing and not
* updating).
*/
private void set_proxy_address_summary(String new_value)
{
Preference proxy_address = findPreference(KEY_PREF_PROXY_ADDRESS);
if (proxy_address == null)
{
Log.v(MainActivity.TAG, "The preferences are malformed!");
return;
}
// Set the summary manually, since "%s" is not supported for EditTextPreferences.
String address = new_value;
if (address == null) {
address = proxy_address.getSharedPreferences().getString(KEY_PREF_PROXY_ADDRESS, "127.0.0.1:9050");
}
proxy_address.setSummary(address);
}
}