Repository navigation
Expand file tree
/
Copy pathAppProtectionWriter.java
More file actions
128 lines (109 loc) · 5.29 KB
/
Copy pathAppProtectionWriter.java
File metadata and controls
128 lines (109 loc) · 5.29 KB
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
/*
* TrackerControl 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.
*/
package net.kollnig.missioncontrol.data;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import java.util.List;
import eu.faircode.netguard.Rule;
import eu.faircode.netguard.ServiceSinkhole;
/**
* Writes the four stores behind the per-app protection state.
*/
public final class AppProtectionWriter {
private AppProtectionWriter() {
}
/**
* Apply a user-selected state. A manual selection always ends a temporary
* pause first; the selected state is the only state this write changes.
*/
public static void applyManual(Context context, String packageName, int uid,
AppProtectionState.Change change) {
if (change == null)
return;
Context appContext = context.getApplicationContext();
PausedApps.cancel(appContext, packageName, uid);
apply(appContext, packageName, uid, change);
}
/**
* Apply a scheduled raw apply value. This path deliberately never cancels
* a pause snapshot, because it is used while sweeping that snapshot.
*/
public static void applyScheduled(Context context, String packageName, int uid,
boolean applyValue) {
Context appContext = context.getApplicationContext();
apply(appContext, packageName, uid, applyValue, null, null, null);
}
/** Apply one scheduled apply value to a UID's packages in one preference edit. */
static void applyScheduled(Context context, List<String> packageNames, boolean applyValue) {
Context appContext = context.getApplicationContext();
SharedPreferences apply = appContext.getSharedPreferences("apply", Context.MODE_PRIVATE);
boolean changed = false;
SharedPreferences.Editor editor = apply.edit();
for (String packageName : packageNames) {
changed |= apply.getBoolean(packageName, true) != applyValue;
editor.putBoolean(packageName, applyValue);
}
editor.apply();
if (changed)
reload(appContext);
}
/**
* Apply a scheduled complete state without cancelling pause snapshots.
*/
public static void applyScheduled(Context context, String packageName, int uid,
AppProtectionState.Change change) {
if (change != null)
apply(context.getApplicationContext(), packageName, uid, change);
}
private static void apply(Context context, String packageName, int uid,
AppProtectionState.Change change) {
apply(context, packageName, uid, change.apply, change.trackerProtect,
change.internetBlocked, change.minimalOnly);
}
private static void apply(Context context, String packageName, int uid,
boolean applyValue, Boolean trackerProtectValue, Boolean internetBlocked,
Boolean minimalOnlyValue) {
SharedPreferences apply = context.getSharedPreferences("apply", Context.MODE_PRIVATE);
SharedPreferences trackerProtect = context.getSharedPreferences("tracker_protect", Context.MODE_PRIVATE);
SharedPreferences minimalOnlyPrefs = context.getSharedPreferences("tracker_essential", Context.MODE_PRIVATE);
InternetBlocklist internetBlocklist = InternetBlocklist.getInstance(context);
boolean applyBefore = apply.getBoolean(packageName, true);
boolean protectBefore = BlockingMode.isTrackerProtectionEnabled(context, trackerProtect, packageName);
boolean internetBefore = internetBlocklist.blockedInternet(uid);
apply.edit().putBoolean(packageName, applyValue).apply();
if (applyValue)
BlockingMode.clearAutoExcludedApp(context, packageName);
if (trackerProtectValue != null)
trackerProtect.edit().putBoolean(packageName, trackerProtectValue).apply();
if (minimalOnlyValue != null)
minimalOnlyPrefs.edit().putBoolean(packageName, minimalOnlyValue).apply();
internetBlocklist.apply(context, uid, internetBlocked);
// The service reads this per-package flag through the live preferences
// handle, so changing it alone needs no service reload.
if (!shouldReloadAfterChange(applyValue, applyBefore, trackerProtectValue,
protectBefore, internetBlocked, internetBefore))
return;
AsyncTask.execute(() -> {
Rule.clearCache(context);
ServiceSinkhole.reload("app protection changed", context, false);
});
}
private static void reload(Context context) {
AsyncTask.execute(() -> {
Rule.clearCache(context);
ServiceSinkhole.reload("app protection changed", context, false);
});
}
static boolean shouldReloadAfterChange(boolean applyValue, boolean applyBefore,
Boolean trackerProtectValue, boolean protectBefore, Boolean internetBlocked,
boolean internetBefore) {
return applyValue != applyBefore
|| (trackerProtectValue != null && trackerProtectValue != protectBefore)
|| (internetBlocked != null && internetBlocked != internetBefore);
}
}