From 5e80515785232a00f0c3af9733823dc5462d200c Mon Sep 17 00:00:00 2001 From: M66B Date: Fri, 19 Feb 2016 16:49:32 +0100 Subject: [PATCH] Store notify indication by package name --- .../faircode/netguard/ActivitySettings.java | 11 +++++++-- .../eu/faircode/netguard/AdapterRule.java | 19 +++++++++++---- .../eu/faircode/netguard/SinkholeService.java | 23 +++++++++++++++---- 3 files changed, 42 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/eu/faircode/netguard/ActivitySettings.java b/app/src/main/java/eu/faircode/netguard/ActivitySettings.java index c76ed426..33695582 100644 --- a/app/src/main/java/eu/faircode/netguard/ActivitySettings.java +++ b/app/src/main/java/eu/faircode/netguard/ActivitySettings.java @@ -828,8 +828,6 @@ public class ActivitySettings extends AppCompatActivity implements SharedPrefere return intent; } - // TODO translate uid to package name for notify. setting - private void handleExport(final Intent data) { new AsyncTask() { @Override @@ -999,6 +997,10 @@ public class ActivitySettings extends AppCompatActivity implements SharedPrefere xmlExport(getSharedPreferences("screen_other", Context.MODE_PRIVATE), serializer); serializer.endTag(null, "screen_other"); + serializer.startTag(null, "notify"); + xmlExport(getSharedPreferences("notify", Context.MODE_PRIVATE), serializer); + serializer.endTag(null, "notify"); + serializer.startTag(null, "filter"); filterExport(serializer); serializer.endTag(null, "filter"); @@ -1101,6 +1103,7 @@ public class ActivitySettings extends AppCompatActivity implements SharedPrefere xmlImport(handler.screen_wifi, getSharedPreferences("screen_wifi", Context.MODE_PRIVATE)); xmlImport(handler.screen_other, getSharedPreferences("screen_other", Context.MODE_PRIVATE)); xmlImport(handler.roaming, getSharedPreferences("roaming", Context.MODE_PRIVATE)); + xmlImport(handler.notify, getSharedPreferences("notify", Context.MODE_PRIVATE)); // Upgrade imported settings Receiver.upgrade(true, this); @@ -1146,6 +1149,7 @@ public class ActivitySettings extends AppCompatActivity implements SharedPrefere public Map screen_wifi = new HashMap<>(); public Map screen_other = new HashMap<>(); public Map roaming = new HashMap<>(); + public Map notify = new HashMap<>(); private Map current = null; private List listUid = new ArrayList<>(); @@ -1179,6 +1183,9 @@ public class ActivitySettings extends AppCompatActivity implements SharedPrefere else if (qName.equals("roaming")) current = roaming; + else if (qName.equals("notify")) + current = notify; + else if (qName.equals("filter")) current = null; diff --git a/app/src/main/java/eu/faircode/netguard/AdapterRule.java b/app/src/main/java/eu/faircode/netguard/AdapterRule.java index 273d9f90..44d9cf74 100644 --- a/app/src/main/java/eu/faircode/netguard/AdapterRule.java +++ b/app/src/main/java/eu/faircode/netguard/AdapterRule.java @@ -563,15 +563,24 @@ public class AdapterRule extends RecyclerView.Adapter im }); // Show disable access notifications setting - boolean notify = prefs.getBoolean("notify_access", false); - final String key = "notify_" + rule.info.applicationInfo.uid; + final SharedPreferences nprefs = context.getSharedPreferences("notify", Context.MODE_PRIVATE); holder.cbNotify.setOnCheckedChangeListener(null); - holder.cbNotify.setEnabled(notify); - holder.cbNotify.setChecked(prefs.getBoolean(key, true)); + holder.cbNotify.setEnabled(prefs.getBoolean("notify_access", false)); + holder.cbNotify.setChecked(nprefs.getBoolean(rule.info.packageName, true)); holder.cbNotify.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) { - prefs.edit().putBoolean(key, isChecked).apply(); + if (isChecked) + nprefs.edit().remove(rule.info.packageName).apply(); + else + nprefs.edit().putBoolean(rule.info.packageName, isChecked).apply(); + + for (String pkg : rule.related) + if (isChecked) + nprefs.edit().remove(pkg).apply(); + else + nprefs.edit().putBoolean(pkg, isChecked).apply(); + SinkholeService.reload("notify changed", context); } }); diff --git a/app/src/main/java/eu/faircode/netguard/SinkholeService.java b/app/src/main/java/eu/faircode/netguard/SinkholeService.java index ec992a2b..72ac3be4 100644 --- a/app/src/main/java/eu/faircode/netguard/SinkholeService.java +++ b/app/src/main/java/eu/faircode/netguard/SinkholeService.java @@ -30,6 +30,7 @@ import android.content.Intent; import android.content.IntentFilter; import android.content.SharedPreferences; import android.content.pm.ApplicationInfo; +import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.database.Cursor; import android.graphics.Bitmap; @@ -104,6 +105,7 @@ public class SinkholeService extends VpnService implements SharedPreferences.OnS private Map mapUidKnown = new HashMap<>(); private Map> mapUidIPFilters = new HashMap<>(); private Map mapForward = new HashMap<>(); + private Map mapNoNotify = new HashMap<>(); private volatile Looper commandLooper; private volatile Looper logLooper; @@ -526,8 +528,6 @@ public class SinkholeService extends VpnService implements SharedPreferences.OnS SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(SinkholeService.this); boolean log = prefs.getBoolean("log", false); boolean log_app = prefs.getBoolean("log_app", false); - boolean notify = prefs.getBoolean("notify_access", false); - boolean system = prefs.getBoolean("manage_system", false); DatabaseHelper dh = DatabaseHelper.getInstance(SinkholeService.this); @@ -543,8 +543,7 @@ public class SinkholeService extends VpnService implements SharedPreferences.OnS if (!(packet.protocol == 6 /* TCP */ || packet.protocol == 17 /* UDP */)) packet.dport = 0; if (dh.updateAccess(packet, dname, -1)) - if (notify && prefs.getBoolean("notify_" + packet.uid, true) && - (system || !Util.isSystem(packet.uid, SinkholeService.this))) + if (mapNoNotify.containsKey(packet.uid) && mapNoNotify.get(packet.uid)) showAccessNotification(packet.uid); } } @@ -960,6 +959,7 @@ public class SinkholeService extends VpnService implements SharedPreferences.OnS prepareHostsBlocked(); prepareUidIPFilters(); prepareForwarding(); + prepareNotify(listRule); } else unprepare(); @@ -990,6 +990,7 @@ public class SinkholeService extends VpnService implements SharedPreferences.OnS mapHostsBlocked.clear(); mapUidIPFilters.clear(); mapForward.clear(); + mapNoNotify.clear(); } private void prepareUidAllowed(List listAllowed, List listRule) { @@ -1142,6 +1143,20 @@ public class SinkholeService extends VpnService implements SharedPreferences.OnS cursor.close(); } + private void prepareNotify(List listRule) { + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); + SharedPreferences nprefs = getSharedPreferences("notify", Context.MODE_PRIVATE); + boolean notify = prefs.getBoolean("notify_access", false); + boolean system = prefs.getBoolean("manage_system", false); + + mapNoNotify.clear(); + + if (notify) + for (Rule rule : listRule) + if (nprefs.getBoolean(rule.info.packageName, true) && (system || !rule.system)) + mapNoNotify.put(rule.info.applicationInfo.uid, true); + } + private void cleanupDNS() { // Keep records for a week DatabaseHelper.getInstance(SinkholeService.this).cleanupDns(new Date().getTime() - 7 * 24 * 3600 * 1000L);