Removed dependency on application info

This commit is contained in:
M66B 2017-11-05 15:17:09 +01:00
parent eb7334814c
commit 9f0cf42983
12 changed files with 118 additions and 55 deletions

View File

@ -223,7 +223,7 @@
</service>
<receiver
android:name=".Receiver"
android:name=".ReceiverAutostart"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
@ -231,6 +231,13 @@
</intent-filter>
</receiver>
<receiver android:name=".ReceiverPackageRemoved">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_FULLY_REMOVED" />
<data android:scheme="package" />
</intent-filter>
</receiver>
<receiver
android:name=".WidgetMain"
android:label="@string/app_name">

View File

@ -192,7 +192,7 @@ public class ActivityForwarding extends AppCompatActivity {
final int dport = Integer.parseInt(etDPort.getText().toString());
final String raddr = etRAddr.getText().toString();
final int rport = Integer.parseInt(etRPort.getText().toString());
final int ruid = ((Rule) spRuid.getSelectedItem()).info.applicationInfo.uid;
final int ruid = ((Rule) spRuid.getSelectedItem()).uid;
InetAddress iraddr = InetAddress.getByName(raddr);
if (rport < 1024 && (iraddr.isLoopbackAddress() || iraddr.isAnyLocalAddress()))

View File

@ -335,7 +335,7 @@ public class ActivityLog extends AppCompatActivity implements SharedPreferences.
if (query != null && query.length() > 0) {
for (Rule rule : Rule.getRules(true, false, ActivityLog.this))
if (rule.name != null && rule.name.toLowerCase().contains(query.toLowerCase())) {
String newQuery = Integer.toString(rule.info.applicationInfo.uid);
String newQuery = Integer.toString(rule.uid);
Log.i(TAG, "Search " + query + " found " + rule.name + " new " + newQuery);
return newQuery;
}

View File

@ -149,7 +149,7 @@ public class ActivityMain extends AppCompatActivity implements SharedPreferences
boolean initialized = prefs.getBoolean("initialized", false);
// Upgrade
Receiver.upgrade(initialized, this);
ReceiverAutostart.upgrade(initialized, this);
if (!getIntent().hasExtra(EXTRA_APPROVE)) {
if (enabled)

View File

@ -1196,7 +1196,7 @@ public class ActivitySettings extends AppCompatActivity implements SharedPrefere
xmlImport(handler.notify, getSharedPreferences("notify", Context.MODE_PRIVATE));
// Upgrade imported settings
Receiver.upgrade(true, this);
ReceiverAutostart.upgrade(true, this);
DatabaseHelper.clearCache();

View File

@ -344,7 +344,7 @@ public class AdapterRule extends RecyclerView.Adapter<AdapterRule.ViewHolder> im
holder.ivExpander.setImageLevel(rule.expanded ? 1 : 0);
// Show application icon
if (rule.info.applicationInfo.icon <= 0)
if (rule.icon <= 0)
holder.ivIcon.setImageResource(android.R.drawable.sym_def_app_icon);
else {
holder.iconLoader = new IconLoader(holder, rule);
@ -437,9 +437,9 @@ public class AdapterRule extends RecyclerView.Adapter<AdapterRule.ViewHolder> im
holder.llConfiguration.setVisibility(rule.expanded ? View.VISIBLE : View.GONE);
// Show application details
holder.tvUid.setText(rule.info.applicationInfo == null ? "?" : Integer.toString(rule.info.applicationInfo.uid));
holder.tvPackage.setText(rule.info.packageName);
holder.tvVersion.setText(rule.info.versionName + '/' + rule.info.versionCode);
holder.tvUid.setText(Integer.toString(rule.uid));
holder.tvPackage.setText(rule.packageName);
holder.tvVersion.setText(rule.version);
holder.tvDescription.setVisibility(rule.description == null ? View.GONE : View.VISIBLE);
holder.tvDescription.setText(rule.description);
@ -453,7 +453,7 @@ public class AdapterRule extends RecyclerView.Adapter<AdapterRule.ViewHolder> im
@Override
public void onClick(View view) {
Intent main = new Intent(context, ActivityMain.class);
main.putExtra(ActivityMain.EXTRA_SEARCH, Integer.toString(rule.info.applicationInfo.uid));
main.putExtra(ActivityMain.EXTRA_SEARCH, Integer.toString(rule.uid));
main.putExtra(ActivityMain.EXTRA_RELATED, true);
context.startActivity(main);
}
@ -671,7 +671,7 @@ public class AdapterRule extends RecyclerView.Adapter<AdapterRule.ViewHolder> im
if (rule.expanded) {
// Access the database when expanded only
final AdapterAccess badapter = new AdapterAccess(context,
DatabaseHelper.getInstance(context).getAccess(rule.info.applicationInfo.uid, rule.info.firstInstallTime));
DatabaseHelper.getInstance(context).getAccess(rule.uid));
holder.lvAccess.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, final int bposition, long bid) {
@ -772,7 +772,7 @@ public class AdapterRule extends RecyclerView.Adapter<AdapterRule.ViewHolder> im
new AsyncTask<Object, Object, Long>() {
@Override
protected Long doInBackground(Object... objects) {
return DatabaseHelper.getInstance(context).getHostCount(rule.info.applicationInfo.uid, false);
return DatabaseHelper.getInstance(context).getHostCount(rule.uid, false);
}
@Override
@ -808,7 +808,7 @@ public class AdapterRule extends RecyclerView.Adapter<AdapterRule.ViewHolder> im
Util.areYouSure(view.getContext(), R.string.msg_reset_access, new Util.DoubtListener() {
@Override
public void onSure() {
DatabaseHelper.getInstance(context).clearAccess(rule.info.applicationInfo.uid, true);
DatabaseHelper.getInstance(context).clearAccess(rule.uid, true);
if (!live)
notifyDataSetChanged();
if (rv != null)
@ -867,44 +867,44 @@ public class AdapterRule extends RecyclerView.Adapter<AdapterRule.ViewHolder> im
SharedPreferences notify = context.getSharedPreferences("notify", Context.MODE_PRIVATE);
if (rule.wifi_blocked == rule.wifi_default)
wifi.edit().remove(rule.info.packageName).apply();
wifi.edit().remove(rule.packageName).apply();
else
wifi.edit().putBoolean(rule.info.packageName, rule.wifi_blocked).apply();
wifi.edit().putBoolean(rule.packageName, rule.wifi_blocked).apply();
if (rule.other_blocked == rule.other_default)
other.edit().remove(rule.info.packageName).apply();
other.edit().remove(rule.packageName).apply();
else
other.edit().putBoolean(rule.info.packageName, rule.other_blocked).apply();
other.edit().putBoolean(rule.packageName, rule.other_blocked).apply();
if (rule.apply)
apply.edit().remove(rule.info.packageName).apply();
apply.edit().remove(rule.packageName).apply();
else
apply.edit().putBoolean(rule.info.packageName, rule.apply).apply();
apply.edit().putBoolean(rule.packageName, rule.apply).apply();
if (rule.screen_wifi == rule.screen_wifi_default)
screen_wifi.edit().remove(rule.info.packageName).apply();
screen_wifi.edit().remove(rule.packageName).apply();
else
screen_wifi.edit().putBoolean(rule.info.packageName, rule.screen_wifi).apply();
screen_wifi.edit().putBoolean(rule.packageName, rule.screen_wifi).apply();
if (rule.screen_other == rule.screen_other_default)
screen_other.edit().remove(rule.info.packageName).apply();
screen_other.edit().remove(rule.packageName).apply();
else
screen_other.edit().putBoolean(rule.info.packageName, rule.screen_other).apply();
screen_other.edit().putBoolean(rule.packageName, rule.screen_other).apply();
if (rule.roaming == rule.roaming_default)
roaming.edit().remove(rule.info.packageName).apply();
roaming.edit().remove(rule.packageName).apply();
else
roaming.edit().putBoolean(rule.info.packageName, rule.roaming).apply();
roaming.edit().putBoolean(rule.packageName, rule.roaming).apply();
if (rule.lockdown)
lockdown.edit().putBoolean(rule.info.packageName, rule.lockdown).apply();
lockdown.edit().putBoolean(rule.packageName, rule.lockdown).apply();
else
lockdown.edit().remove(rule.info.packageName).apply();
lockdown.edit().remove(rule.packageName).apply();
if (rule.notify)
notify.edit().remove(rule.info.packageName).apply();
notify.edit().remove(rule.packageName).apply();
else
notify.edit().putBoolean(rule.info.packageName, rule.notify).apply();
notify.edit().putBoolean(rule.packageName, rule.notify).apply();
rule.updateChanged(context);
Log.i(TAG, "Updated " + rule);
@ -912,7 +912,7 @@ public class AdapterRule extends RecyclerView.Adapter<AdapterRule.ViewHolder> im
List<Rule> listModified = new ArrayList<>();
for (String pkg : rule.related) {
for (Rule related : listAll)
if (related.info.packageName.equals(pkg)) {
if (related.packageName.equals(pkg)) {
related.wifi_blocked = rule.wifi_blocked;
related.other_blocked = rule.other_blocked;
related.apply = rule.apply;
@ -934,7 +934,7 @@ public class AdapterRule extends RecyclerView.Adapter<AdapterRule.ViewHolder> im
if (root) {
notifyDataSetChanged();
NotificationManagerCompat.from(context).cancel(rule.info.applicationInfo.uid);
NotificationManagerCompat.from(context).cancel(rule.uid);
ServiceSinkhole.reload("rule changed", context, false);
}
}
@ -956,8 +956,8 @@ public class AdapterRule extends RecyclerView.Adapter<AdapterRule.ViewHolder> im
uid = -1;
}
for (Rule rule : listAll)
if (rule.info.applicationInfo.uid == uid ||
rule.info.packageName.toLowerCase().contains(query) ||
if (rule.uid == uid ||
rule.packageName.toLowerCase().contains(query) ||
(rule.name != null && rule.name.toLowerCase().contains(query)))
listResult.add(rule);
}
@ -991,7 +991,7 @@ public class AdapterRule extends RecyclerView.Adapter<AdapterRule.ViewHolder> im
@Override
public long getItemId(int position) {
Rule rule = listFiltered.get(position);
return rule.info.packageName.hashCode() * 100000L + rule.info.applicationInfo.uid;
return rule.packageName.hashCode() * 100000L + rule.uid;
}
@Override
@ -1022,12 +1022,12 @@ public class AdapterRule extends RecyclerView.Adapter<AdapterRule.ViewHolder> im
if (cancelled)
throw new InterruptedException();
Resources res = context.getPackageManager().getResourcesForApplication(rule.info.packageName);
Drawable drawable = res.getDrawable(rule.info.applicationInfo.icon, null);
Resources res = context.getPackageManager().getResourcesForApplication(rule.packageName);
Drawable drawable = res.getDrawable(rule.icon, null);
final Drawable scaledDrawable;
if (drawable instanceof BitmapDrawable) {
Bitmap scaled = Util.decodeSampledBitmapFromResource(res, rule.info.applicationInfo.icon, iconSize, iconSize);
Bitmap scaled = Util.decodeSampledBitmapFromResource(res, rule.icon, iconSize, iconSize);
scaledDrawable = new BitmapDrawable(context.getResources(), scaled);
} else
scaledDrawable = drawable;

View File

@ -686,7 +686,7 @@ public class DatabaseHelper extends SQLiteOpenHelper {
notifyAccessChanged();
}
public Cursor getAccess(int uid, long since) {
public Cursor getAccess(int uid) {
lock.readLock().lock();
try {
SQLiteDatabase db = this.getReadableDatabase();
@ -696,10 +696,9 @@ public class DatabaseHelper extends SQLiteOpenHelper {
query += ", (SELECT COUNT(DISTINCT d.qname) FROM dns d WHERE d.resource IN (SELECT d1.resource FROM dns d1 WHERE d1.qname = a.daddr)) count";
query += " FROM access a";
query += " WHERE a.uid = ?";
query += " AND a.time >= ?";
query += " ORDER BY a.time DESC";
query += " LIMIT 50";
return db.rawQuery(query, new String[]{Integer.toString(uid), Long.toString(since)});
return db.rawQuery(query, new String[]{Integer.toString(uid)});
} finally {
lock.readLock().unlock();
}

View File

@ -29,7 +29,7 @@ import android.util.Log;
import java.util.Map;
public class Receiver extends BroadcastReceiver {
public class ReceiverAutostart extends BroadcastReceiver {
private static final String TAG = "NetGuard.Receiver";
@Override

View File

@ -0,0 +1,51 @@
package eu.faircode.netguard;
/*
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-2017 by Marcel Bokhorst (M66B)
*/
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.preference.PreferenceManager;
import android.support.v4.app.NotificationManagerCompat;
import android.util.Log;
import java.util.Map;
public class ReceiverPackageRemoved extends BroadcastReceiver {
private static final String TAG = "NetGuard.Receiver";
@Override
public void onReceive(final Context context, Intent intent) {
Log.i(TAG, "Received " + intent);
Util.logExtras(intent);
int uid = intent.getIntExtra(Intent.EXTRA_UID, 0);
if (uid > 0) {
DatabaseHelper dh = DatabaseHelper.getInstance(context);
dh.clearLog(uid);
dh.clearAccess(uid, false);
NotificationManagerCompat.from(context).cancel(uid); // installed notification
NotificationManagerCompat.from(context).cancel(uid + 10000); // access notification
}
}
}

View File

@ -48,9 +48,12 @@ import java.util.Map;
public class Rule {
private static final String TAG = "NetGuard.Rule";
public PackageInfo info;
public int uid;
public String packageName;
public int icon;
public String name;
public String description;
public String version;
public boolean system;
public boolean internet;
public boolean enabled;
@ -192,7 +195,10 @@ public class Rule {
}
private Rule(PackageInfo info, boolean service, Context context) {
this.info = info;
this.uid = info.applicationInfo.uid;
this.packageName = info.packageName;
this.icon = info.applicationInfo.icon;
this.version = info.versionName;
if (info.applicationInfo.uid == 0) {
this.name = context.getString(R.string.title_root);
this.description = null;
@ -409,7 +415,7 @@ public class Rule {
if (service)
rule.hosts = -1;
else
rule.hosts = dh.getHostCount(rule.info.applicationInfo.uid, true);
rule.hosts = dh.getHostCount(rule.uid, true);
rule.updateChanged(default_wifi, default_other, default_roaming);
@ -429,13 +435,13 @@ public class Rule {
Collections.sort(listRules, new Comparator<Rule>() {
@Override
public int compare(Rule rule, Rule other) {
if (rule.info.applicationInfo.uid < other.info.applicationInfo.uid)
if (rule.uid < other.uid)
return -1;
else if (rule.info.applicationInfo.uid > other.info.applicationInfo.uid)
else if (rule.uid > other.uid)
return 1;
else {
int i = collator.compare(rule.name, other.name);
return (i == 0 ? rule.info.packageName.compareTo(other.info.packageName) : i);
return (i == 0 ? rule.packageName.compareTo(other.packageName) : i);
}
}
});
@ -445,7 +451,7 @@ public class Rule {
public int compare(Rule rule, Rule other) {
if (all || rule.changed == other.changed) {
int i = collator.compare(rule.name, other.name);
return (i == 0 ? rule.info.packageName.compareTo(other.info.packageName) : i);
return (i == 0 ? rule.packageName.compareTo(other.packageName) : i);
}
return (rule.changed ? -1 : 1);
}

View File

@ -1341,7 +1341,7 @@ public class ServiceSinkhole extends VpnService implements SharedPreferences.OnS
if (last_connected && !filter)
for (Rule rule : listAllowed)
try {
builder.addDisallowedApplication(rule.info.packageName);
builder.addDisallowedApplication(rule.packageName);
} catch (PackageManager.NameNotFoundException ex) {
Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
}
@ -1354,8 +1354,8 @@ public class ServiceSinkhole extends VpnService implements SharedPreferences.OnS
for (Rule rule : listRule)
if (!rule.apply || (!system && rule.system))
try {
Log.i(TAG, "Not routing " + rule.info.packageName);
builder.addDisallowedApplication(rule.info.packageName);
Log.i(TAG, "Not routing " + rule.packageName);
builder.addDisallowedApplication(rule.packageName);
} catch (PackageManager.NameNotFoundException ex) {
Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
}
@ -1443,11 +1443,11 @@ public class ServiceSinkhole extends VpnService implements SharedPreferences.OnS
mapUidAllowed.clear();
for (Rule rule : listAllowed)
mapUidAllowed.put(rule.info.applicationInfo.uid, true);
mapUidAllowed.put(rule.uid, true);
mapUidKnown.clear();
for (Rule rule : listRule)
mapUidKnown.put(rule.info.applicationInfo.uid, rule.info.applicationInfo.uid);
mapUidKnown.put(rule.uid, rule.uid);
lock.writeLock().unlock();
}
@ -1628,7 +1628,7 @@ public class ServiceSinkhole extends VpnService implements SharedPreferences.OnS
lock.writeLock().lock();
mapNotify.clear();
for (Rule rule : listRule)
mapNotify.put(rule.info.applicationInfo.uid, notify && rule.notify && (system || !rule.system));
mapNotify.put(rule.uid, notify && rule.notify && (system || !rule.system));
lock.writeLock().unlock();
}

View File

@ -32,7 +32,7 @@ import android.util.Log;
import java.util.Date;
public class WidgetAdmin extends Receiver {
public class WidgetAdmin extends ReceiverAutostart {
private static final String TAG = "NetGuard.Widget";
public static final String INTENT_ON = "eu.faircode.netguard.ON";