NetGuard/app/src/main/java/eu/faircode/netguard/Rule.java

311 lines
14 KiB
Java
Raw Normal View History

2015-10-24 18:01:55 +00:00
package eu.faircode.netguard;
2015-11-03 17:57:29 +00:00
/*
This file is part of NetGuard.
NetGuard 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.
NetGuard 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 NetGuard. If not, see <http://www.gnu.org/licenses/>.
2016-01-01 13:52:09 +00:00
Copyright 2015-2016 by Marcel Bokhorst (M66B)
2015-11-03 17:57:29 +00:00
*/
2015-10-24 18:01:55 +00:00
import android.content.Context;
2015-10-31 20:25:39 +00:00
import android.content.Intent;
2015-10-24 18:01:55 +00:00
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
2015-10-24 18:01:55 +00:00
import android.content.pm.PackageInfo;
2015-10-25 21:12:08 +00:00
import android.content.pm.PackageManager;
2015-11-01 08:38:51 +00:00
import android.content.res.XmlResourceParser;
2015-12-28 08:09:07 +00:00
import android.net.TrafficStats;
2016-02-06 06:35:02 +00:00
import android.os.Build;
2016-01-22 15:13:33 +00:00
import android.os.Process;
2015-12-28 08:09:07 +00:00
import android.os.SystemClock;
2015-10-25 22:04:10 +00:00
import android.preference.PreferenceManager;
2015-11-08 09:52:48 +00:00
import android.text.TextUtils;
2015-11-01 08:38:51 +00:00
import android.util.Log;
import org.xmlpull.v1.XmlPullParser;
2015-10-24 18:01:55 +00:00
import java.text.Collator;
2015-10-24 18:01:55 +00:00
import java.util.ArrayList;
import java.util.Collections;
2015-12-28 08:09:07 +00:00
import java.util.Comparator;
2015-11-01 08:38:51 +00:00
import java.util.HashMap;
2015-10-24 18:01:55 +00:00
import java.util.List;
import java.util.Locale;
2015-11-01 08:38:51 +00:00
import java.util.Map;
2015-10-24 18:01:55 +00:00
2015-12-28 08:09:07 +00:00
public class Rule {
2015-11-29 07:34:04 +00:00
private static final String TAG = "NetGuard.Rule";
2015-10-24 18:01:55 +00:00
public PackageInfo info;
public String name;
2015-10-25 16:52:33 +00:00
public boolean system;
public boolean internet;
public boolean enabled;
public Intent intent;
2015-11-01 16:15:09 +00:00
public boolean wifi_default;
public boolean other_default;
2015-11-21 08:48:33 +00:00
public boolean screen_wifi_default;
public boolean screen_other_default;
2015-11-01 16:15:09 +00:00
public boolean roaming_default;
2015-10-24 18:01:55 +00:00
public boolean wifi_blocked;
public boolean other_blocked;
2015-11-21 08:48:33 +00:00
public boolean screen_wifi;
public boolean screen_other;
2015-11-01 13:16:57 +00:00
public boolean roaming;
2015-11-01 16:15:09 +00:00
2015-11-08 09:52:48 +00:00
public String[] related = null;
2015-12-28 08:09:07 +00:00
public float upspeed;
public float downspeed;
public float totalbytes;
2015-10-26 08:47:49 +00:00
public boolean changed;
2015-11-01 16:15:09 +00:00
public boolean expanded = false;
2015-10-29 08:56:22 +00:00
2015-10-31 17:07:21 +00:00
private Rule(PackageInfo info, Context context) {
2015-10-25 21:12:08 +00:00
PackageManager pm = context.getPackageManager();
2015-10-31 20:25:39 +00:00
2015-10-24 18:01:55 +00:00
this.info = info;
if (info.applicationInfo.uid == 0) {
this.name = context.getString(R.string.title_root);
this.system = true;
this.internet = true;
this.enabled = true;
this.intent = null;
2016-02-06 06:35:02 +00:00
} else if (info.applicationInfo.uid == 1013) {
this.name = context.getString(R.string.title_mediaserver);
this.system = true;
this.internet = true;
this.enabled = true;
this.intent = null;
} else {
this.name = info.applicationInfo.loadLabel(pm).toString();
this.system = Util.isSystem(info.packageName, context);
this.internet = Util.hasInternet(info.packageName, context);
int setting;
try {
setting = pm.getApplicationEnabledSetting(info.packageName);
} catch (IllegalArgumentException ex) {
setting = PackageManager.COMPONENT_ENABLED_STATE_DEFAULT;
Log.w(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
}
if (setting == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT)
this.enabled = info.applicationInfo.enabled;
else
this.enabled = (setting == PackageManager.COMPONENT_ENABLED_STATE_ENABLED);
this.intent = pm.getLaunchIntentForPackage(info.packageName);
2015-11-29 07:34:04 +00:00
}
2015-10-24 18:01:55 +00:00
}
2015-11-01 08:38:51 +00:00
public static List<Rule> getRules(boolean all, String tag, Context context) {
2015-10-25 22:04:10 +00:00
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
2015-10-24 18:01:55 +00:00
SharedPreferences wifi = context.getSharedPreferences("wifi", Context.MODE_PRIVATE);
SharedPreferences other = context.getSharedPreferences("other", Context.MODE_PRIVATE);
2015-11-21 08:48:33 +00:00
SharedPreferences screen_wifi = context.getSharedPreferences("screen_wifi", Context.MODE_PRIVATE);
SharedPreferences screen_other = context.getSharedPreferences("screen_other", Context.MODE_PRIVATE);
2015-11-01 13:16:57 +00:00
SharedPreferences roaming = context.getSharedPreferences("roaming", Context.MODE_PRIVATE);
2015-10-24 18:01:55 +00:00
2015-11-01 08:38:51 +00:00
// Get settings
boolean default_wifi = prefs.getBoolean("whitelist_wifi", true);
boolean default_other = prefs.getBoolean("whitelist_other", true);
2015-11-21 08:48:33 +00:00
boolean default_screen_wifi = prefs.getBoolean("screen_wifi", true);
boolean default_screen_other = prefs.getBoolean("screen_other", true);
boolean default_roaming = prefs.getBoolean("whitelist_roaming", true);
2015-10-31 17:07:21 +00:00
boolean manage_system = prefs.getBoolean("manage_system", false);
boolean show_user = prefs.getBoolean("show_user", true);
boolean show_system = prefs.getBoolean("show_system", false);
boolean show_nointernet = prefs.getBoolean("show_nointernet", true);
boolean show_disabled = prefs.getBoolean("show_disabled", true);
2015-10-26 08:47:49 +00:00
2015-12-28 08:09:07 +00:00
long now = SystemClock.elapsedRealtime();
2015-11-01 13:16:57 +00:00
// Get predefined rules
2015-12-31 19:03:03 +00:00
Map<String, Boolean> pre_wifi_blocked = new HashMap<>();
Map<String, Boolean> pre_other_blocked = new HashMap<>();
2015-11-01 16:15:09 +00:00
Map<String, Boolean> pre_roaming = new HashMap<>();
2015-11-08 09:52:48 +00:00
Map<String, String[]> pre_related = new HashMap<>();
2016-01-31 06:50:37 +00:00
Map<String, Boolean> pre_system = new HashMap<>();
2015-11-01 08:38:51 +00:00
try {
XmlResourceParser xml = context.getResources().getXml(R.xml.predefined);
int eventType = xml.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
2015-11-08 09:52:48 +00:00
if (eventType == XmlPullParser.START_TAG)
2015-12-31 19:03:03 +00:00
if ("wifi".equals(xml.getName())) {
String pkg = xml.getAttributeValue(null, "package");
boolean pblocked = xml.getAttributeBooleanValue(null, "blocked", false);
pre_wifi_blocked.put(pkg, pblocked);
Log.d(tag, "Wifi " + pkg + " blocked=" + pblocked);
} else if ("other".equals(xml.getName())) {
2015-11-08 09:52:48 +00:00
String pkg = xml.getAttributeValue(null, "package");
boolean pblocked = xml.getAttributeBooleanValue(null, "blocked", false);
boolean proaming = xml.getAttributeBooleanValue(null, "roaming", default_roaming);
2015-12-31 19:03:03 +00:00
pre_other_blocked.put(pkg, pblocked);
2015-11-08 09:52:48 +00:00
pre_roaming.put(pkg, proaming);
2015-12-31 19:03:03 +00:00
Log.d(tag, "Other " + pkg + " blocked=" + pblocked + " roaming=" + proaming);
2015-11-08 09:52:48 +00:00
} else if ("relation".equals(xml.getName())) {
String pkg = xml.getAttributeValue(null, "package");
String[] rel = xml.getAttributeValue(null, "related").split(",");
pre_related.put(pkg, rel);
2015-11-21 08:48:33 +00:00
Log.d(tag, "Relation " + pkg + " related=" + TextUtils.join(",", rel));
2016-01-31 06:50:37 +00:00
} else if ("type".equals(xml.getName())) {
String pkg = xml.getAttributeValue(null, "package");
boolean system = xml.getAttributeBooleanValue(null, "system", true);
pre_system.put(pkg, system);
Log.d(tag, "Type " + pkg + " system=" + system);
2015-11-08 09:52:48 +00:00
}
2015-11-01 08:38:51 +00:00
eventType = xml.next();
}
} catch (Throwable ex) {
Log.e(tag, ex.toString() + "\n" + Log.getStackTraceString(ex));
2015-11-20 09:10:22 +00:00
Util.sendCrashReport(ex, context);
2015-11-01 08:38:51 +00:00
}
// Build rule list
2015-10-24 18:01:55 +00:00
List<Rule> listRules = new ArrayList<>();
List<PackageInfo> listPI = context.getPackageManager().getInstalledPackages(0);
2016-02-06 06:35:02 +00:00
// Add root
PackageInfo root = new PackageInfo();
root.packageName = "root";
2016-02-06 06:35:02 +00:00
root.versionCode = Build.VERSION.SDK_INT;
root.versionName = Build.VERSION.RELEASE;
root.applicationInfo = new ApplicationInfo();
root.applicationInfo.uid = 0;
root.applicationInfo.icon = 0;
listPI.add(root);
2016-02-06 06:35:02 +00:00
// Add mediaserver
PackageInfo media = new PackageInfo();
media.packageName = "android.media";
media.versionCode = Build.VERSION.SDK_INT;
media.versionName = Build.VERSION.RELEASE;
media.applicationInfo = new ApplicationInfo();
media.applicationInfo.uid = 1013;
media.applicationInfo.icon = 0;
listPI.add(media);
for (PackageInfo info : listPI) {
Rule rule = new Rule(info, context);
2016-01-31 06:50:37 +00:00
if (pre_system.containsKey(info.packageName))
rule.system = pre_system.get(info.packageName);
if (all ||
((rule.system ? show_system : show_user) &&
(show_nointernet || rule.internet) &&
(show_disabled || rule.enabled) &&
info.applicationInfo.uid != Process.myUid())) {
if (info.applicationInfo.uid == Process.myUid()) {
// Internet access is needed to resolve host names
rule.wifi_default = false;
rule.other_default = false;
rule.screen_wifi_default = false;
rule.screen_other_default = false;
rule.roaming_default = false;
rule.wifi_blocked = false;
rule.other_blocked = false;
2015-12-08 16:00:40 +00:00
rule.screen_wifi = false;
rule.screen_other = false;
rule.roaming = false;
} else {
rule.wifi_default = (pre_wifi_blocked.containsKey(info.packageName) ? pre_wifi_blocked.get(info.packageName) : default_wifi);
rule.other_default = (pre_other_blocked.containsKey(info.packageName) ? pre_other_blocked.get(info.packageName) : default_other);
rule.screen_wifi_default = default_screen_wifi;
rule.screen_other_default = default_screen_other;
rule.roaming_default = (pre_roaming.containsKey(info.packageName) ? pre_roaming.get(info.packageName) : default_roaming);
rule.wifi_blocked = (!(rule.system && !manage_system) && wifi.getBoolean(info.packageName, rule.wifi_default));
rule.other_blocked = (!(rule.system && !manage_system) && other.getBoolean(info.packageName, rule.other_default));
rule.screen_wifi = screen_wifi.getBoolean(info.packageName, rule.screen_wifi_default);
rule.screen_other = screen_other.getBoolean(info.packageName, rule.screen_other_default);
rule.roaming = roaming.getBoolean(info.packageName, rule.roaming_default);
}
2015-11-08 09:52:48 +00:00
if (pre_related.containsKey(info.packageName))
rule.related = pre_related.get(info.packageName);
2015-12-28 08:09:07 +00:00
long up = TrafficStats.getUidTxBytes(rule.info.applicationInfo.uid);
long down = TrafficStats.getUidRxBytes(rule.info.applicationInfo.uid);
rule.totalbytes = up + down;
rule.upspeed = (float) up * 24 * 3600 * 1000 / 1024f / 1024f / now;
rule.downspeed = (float) down * 24 * 3600 * 1000 / 1024f / 1024f / now;
rule.updateChanged(default_wifi, default_other, default_roaming);
2015-11-01 16:15:09 +00:00
2015-10-31 17:07:21 +00:00
listRules.add(rule);
}
2015-10-26 08:47:49 +00:00
}
2015-10-24 18:01:55 +00:00
2015-11-01 08:38:51 +00:00
// Sort rule list
2015-12-28 08:09:07 +00:00
String sort = prefs.getString("sort", "name");
if ("data".equals(sort))
Collections.sort(listRules, new Comparator<Rule>() {
@Override
public int compare(Rule rule, Rule other) {
if (rule.totalbytes < other.totalbytes)
return 1;
else if (rule.totalbytes > other.totalbytes)
return -1;
else
return 0;
}
});
else
Collections.sort(listRules, new Comparator<Rule>() {
@Override
public int compare(Rule rule, Rule other) {
if (rule.changed == other.changed) {
Collator collator = Collator.getInstance(Locale.getDefault());
collator.setStrength(Collator.SECONDARY); // Case sensitive, process accents etc
int i = collator.compare(rule.name, other.name);
2015-12-28 08:09:07 +00:00
return (i == 0 ? rule.info.packageName.compareTo(other.info.packageName) : i);
}
return (rule.changed ? -1 : 1);
}
});
2015-10-24 18:01:55 +00:00
return listRules;
}
private void updateChanged(boolean default_wifi, boolean default_other, boolean default_roaming) {
changed = (wifi_blocked != default_wifi ||
(other_blocked != default_other) ||
(wifi_blocked && screen_wifi != screen_wifi_default) ||
(other_blocked && screen_other != screen_other_default) ||
((!other_blocked || screen_other) && roaming != default_roaming));
2015-11-24 17:34:41 +00:00
}
public void updateChanged(Context context) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean default_wifi = prefs.getBoolean("whitelist_wifi", true);
boolean default_other = prefs.getBoolean("whitelist_other", true);
boolean default_roaming = prefs.getBoolean("whitelist_roaming", true);
updateChanged(default_wifi, default_other, default_roaming);
2015-11-24 17:34:41 +00:00
}
2015-10-24 18:01:55 +00:00
}