Store notify indication by package name

This commit is contained in:
M66B 2016-02-19 16:49:32 +01:00
parent b4e7a7a1ef
commit 5e80515785
3 changed files with 42 additions and 11 deletions

View File

@ -828,8 +828,6 @@ public class ActivitySettings extends AppCompatActivity implements SharedPrefere
return intent;
}
// TODO translate uid to package name for notify.<uid> setting
private void handleExport(final Intent data) {
new AsyncTask<Object, Object, Throwable>() {
@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<String, Object> screen_wifi = new HashMap<>();
public Map<String, Object> screen_other = new HashMap<>();
public Map<String, Object> roaming = new HashMap<>();
public Map<String, Object> notify = new HashMap<>();
private Map<String, Object> current = null;
private List<Integer> 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;

View File

@ -563,15 +563,24 @@ public class AdapterRule extends RecyclerView.Adapter<AdapterRule.ViewHolder> 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);
}
});

View File

@ -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<Integer, Integer> mapUidKnown = new HashMap<>();
private Map<Long, Map<InetAddress, Boolean>> mapUidIPFilters = new HashMap<>();
private Map<Integer, Forward> mapForward = new HashMap<>();
private Map<Integer, Boolean> 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<Rule> listAllowed, List<Rule> listRule) {
@ -1142,6 +1143,20 @@ public class SinkholeService extends VpnService implements SharedPreferences.OnS
cursor.close();
}
private void prepareNotify(List<Rule> 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);