2015-10-24 18:01:55 +00:00
|
|
|
package eu.faircode.netguard;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
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-10-24 18:50:27 +00:00
|
|
|
import android.graphics.drawable.Drawable;
|
2015-10-25 22:04:10 +00:00
|
|
|
import android.preference.PreferenceManager;
|
2015-10-24 18:01:55 +00:00
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
public class Rule implements Comparable<Rule> {
|
|
|
|
public PackageInfo info;
|
|
|
|
public String name;
|
2015-10-25 16:52:33 +00:00
|
|
|
public boolean system;
|
2015-10-25 21:12:08 +00:00
|
|
|
public boolean disabled;
|
2015-10-24 18:01:55 +00:00
|
|
|
public boolean wifi_blocked;
|
|
|
|
public boolean other_blocked;
|
2015-10-26 08:47:49 +00:00
|
|
|
public boolean changed;
|
2015-10-24 18:01:55 +00:00
|
|
|
|
2015-10-26 08:47:49 +00:00
|
|
|
private Rule(PackageInfo info, boolean wifi_blocked, boolean other_blocked, boolean changed, Context context) {
|
2015-10-25 21:12:08 +00:00
|
|
|
PackageManager pm = context.getPackageManager();
|
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();
|
2015-10-25 16:52:33 +00:00
|
|
|
this.system = ((info.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0);
|
2015-10-25 21:12:08 +00:00
|
|
|
|
|
|
|
int setting = pm.getApplicationEnabledSetting(info.packageName);
|
|
|
|
if (setting == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT)
|
|
|
|
this.disabled = !info.applicationInfo.enabled;
|
|
|
|
else
|
|
|
|
this.disabled = (setting != PackageManager.COMPONENT_ENABLED_STATE_ENABLED);
|
|
|
|
|
2015-10-24 18:01:55 +00:00
|
|
|
this.wifi_blocked = wifi_blocked;
|
|
|
|
this.other_blocked = other_blocked;
|
2015-10-26 08:47:49 +00:00
|
|
|
this.changed = changed;
|
2015-10-24 18:01:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static List<Rule> getRules(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-10-26 08:47:49 +00:00
|
|
|
boolean wlWifi = prefs.getBoolean("whitelist_wifi", true);
|
|
|
|
boolean wlOther = prefs.getBoolean("whitelist_other", true);
|
|
|
|
|
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)) {
|
|
|
|
boolean blWifi = wifi.getBoolean(info.packageName, wlWifi);
|
|
|
|
boolean blOther = other.getBoolean(info.packageName, wlOther);
|
|
|
|
boolean changed = (blWifi != wlWifi || blOther != wlOther);
|
|
|
|
listRules.add(new Rule(info, blWifi, blOther, changed, context));
|
|
|
|
}
|
2015-10-24 18:01:55 +00:00
|
|
|
|
|
|
|
Collections.sort(listRules);
|
|
|
|
|
|
|
|
return listRules;
|
|
|
|
}
|
|
|
|
|
2015-10-24 18:50:27 +00:00
|
|
|
public Drawable getIcon(Context context) {
|
|
|
|
return info.applicationInfo.loadIcon(context.getPackageManager());
|
|
|
|
}
|
|
|
|
|
2015-10-24 18:01:55 +00:00
|
|
|
@Override
|
|
|
|
public int compareTo(Rule other) {
|
2015-10-26 08:47:49 +00:00
|
|
|
if (changed == other.changed) {
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|