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

208 lines
8.8 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/>.
Copyright 2015 by Marcel Bokhorst (M66B)
*/
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;
2015-10-25 16:52:33 +00:00
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-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.util.ArrayList;
import java.util.Collections;
2015-11-01 08:38:51 +00:00
import java.util.HashMap;
2015-10-24 18:01:55 +00:00
import java.util.List;
2015-11-01 08:38:51 +00:00
import java.util.Map;
2015-10-24 18:01:55 +00:00
public class Rule implements Comparable<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;
2015-10-25 21:12:08 +00:00
public boolean disabled;
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-10-26 08:47:49 +00:00
public boolean changed;
2015-11-01 16:15:09 +00:00
2015-10-31 20:25:39 +00:00
public Intent intent;
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;
2015-10-25 21:12:08 +00:00
this.name = info.applicationInfo.loadLabel(pm).toString();
this.internet = (pm.checkPermission("android.permission.INTERNET", info.packageName) == PackageManager.PERMISSION_GRANTED);
2015-11-29 07:34:04 +00:00
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));
}
2015-10-25 21:12:08 +00:00
if (setting == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT)
this.disabled = !info.applicationInfo.enabled;
else
this.disabled = (setting != PackageManager.COMPONENT_ENABLED_STATE_ENABLED);
2015-10-31 20:25:39 +00:00
this.intent = pm.getLaunchIntentForPackage(info.packageName);
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 telephony = Util.hasTelephony(context);
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);
2015-10-26 08:47:49 +00:00
2015-11-01 13:16:57 +00:00
// Get predefined rules
2015-11-01 15:25:54 +00:00
Map<String, Boolean> pre_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<>();
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)
if ("rule".equals(xml.getName())) {
String pkg = xml.getAttributeValue(null, "package");
boolean pblocked = xml.getAttributeBooleanValue(null, "blocked", false);
boolean proaming = xml.getAttributeBooleanValue(null, "roaming", default_roaming);
2015-11-08 09:52:48 +00:00
pre_blocked.put(pkg, pblocked);
pre_roaming.put(pkg, proaming);
2015-11-21 08:48:33 +00:00
Log.d(tag, "Predefined " + 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));
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<>();
2015-10-26 08:47:49 +00:00
for (PackageInfo info : context.getPackageManager().getInstalledPackages(0)) {
2015-10-31 17:07:21 +00:00
boolean system = ((info.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0);
if (!system || manage_system || all) {
Rule rule = new Rule(info, context);
2015-11-01 16:15:09 +00:00
2015-10-31 17:07:21 +00:00
rule.system = system;
2015-11-01 16:15:09 +00:00
rule.wifi_default = (pre_blocked.containsKey(info.packageName) ? pre_blocked.get(info.packageName) : default_wifi);
rule.other_default = (pre_blocked.containsKey(info.packageName) ? pre_blocked.get(info.packageName) : default_other);
2015-11-21 08:48:33 +00:00
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);
2015-11-01 16:15:09 +00:00
rule.wifi_blocked = (system && !manage_system ? false : wifi.getBoolean(info.packageName, rule.wifi_default));
rule.other_blocked = (system && !manage_system ? false : other.getBoolean(info.packageName, rule.other_default));
2015-11-21 08:48:33 +00:00
rule.screen_wifi = screen_wifi.getBoolean(info.packageName, rule.screen_wifi_default);
rule.screen_other = screen_other.getBoolean(info.packageName, rule.screen_other_default);
2015-11-01 16:15:09 +00:00
rule.roaming = roaming.getBoolean(info.packageName, rule.roaming_default);
if (!telephony) {
rule.other_blocked = true;
rule.screen_other = false;
}
2015-11-08 09:52:48 +00:00
if (pre_related.containsKey(info.packageName))
rule.related = pre_related.get(info.packageName);
2015-11-24 17:34:41 +00:00
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-10-24 18:01:55 +00:00
Collections.sort(listRules);
return listRules;
}
2015-11-24 17:34:41 +00:00
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));
}
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-10-24 18:01:55 +00:00
@Override
public int compareTo(Rule other) {
if (changed == other.changed) {
2015-10-26 08:47:49 +00:00
int i = name.compareToIgnoreCase(other.name);
return (i == 0 ? info.packageName.compareTo(other.info.packageName) : i);
}
return (changed ? -1 : 1);
2015-10-24 18:01:55 +00:00
}
}