mirror of
https://github.com/M66B/NetGuard.git
synced 2025-01-01 12:54:07 +00:00
Keep notification on action
This commit is contained in:
parent
d12faf81d1
commit
4cfa2cf9cc
4 changed files with 76 additions and 68 deletions
|
@ -75,7 +75,6 @@
|
|||
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/jniLibs" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/manifests" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/pre-dexed" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/proguard-rules" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/res" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/rs" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/symbols" />
|
||||
|
|
|
@ -46,71 +46,11 @@ public class Receiver extends BroadcastReceiver {
|
|||
|
||||
if (Intent.ACTION_PACKAGE_ADDED.equals(intent.getAction())) {
|
||||
// Application added
|
||||
if (!intent.getBooleanExtra(Intent.EXTRA_REPLACING, false))
|
||||
if (!intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) {
|
||||
// Show notification
|
||||
try {
|
||||
// Get package info
|
||||
int uid = intent.getIntExtra(Intent.EXTRA_UID, 0);
|
||||
PackageManager pm = context.getPackageManager();
|
||||
String[] packages = pm.getPackagesForUid(uid);
|
||||
if (packages.length < 1)
|
||||
throw new PackageManager.NameNotFoundException(Integer.toString(uid));
|
||||
String name = (String) pm.getApplicationLabel(pm.getApplicationInfo(packages[0], 0));
|
||||
|
||||
// Build notification
|
||||
Intent main = new Intent(context, ActivityMain.class);
|
||||
PendingIntent pi = PendingIntent.getActivity(context, 0, main, PendingIntent.FLAG_UPDATE_CURRENT);
|
||||
|
||||
NotificationCompat.Builder notification = new NotificationCompat.Builder(context)
|
||||
.setSmallIcon(R.drawable.ic_security_white_24dp)
|
||||
.setContentTitle(context.getString(R.string.app_name))
|
||||
.setContentText(context.getString(R.string.msg_installed, name))
|
||||
.setContentIntent(pi)
|
||||
.setCategory(Notification.CATEGORY_STATUS)
|
||||
.setVisibility(Notification.VISIBILITY_SECRET)
|
||||
.setColor(ContextCompat.getColor(context, R.color.colorPrimary))
|
||||
.setAutoCancel(true);
|
||||
|
||||
// Get defaults
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
boolean default_wifi = prefs.getBoolean("whitelist_wifi", true);
|
||||
boolean default_other = prefs.getBoolean("whitelist_other", true);
|
||||
|
||||
// Build Wi-Fi action
|
||||
Intent riWifi = new Intent(context, SinkholeService.class);
|
||||
riWifi.putExtra(SinkholeService.EXTRA_COMMAND, SinkholeService.Command.set);
|
||||
riWifi.putExtra(SinkholeService.EXTRA_NETWORK, "wifi");
|
||||
riWifi.putExtra(SinkholeService.EXTRA_UID, uid);
|
||||
riWifi.putExtra(SinkholeService.EXTRA_PACKAGE, packages[0]);
|
||||
riWifi.putExtra(SinkholeService.EXTRA_BLOCKED, !default_wifi);
|
||||
|
||||
PendingIntent piWifi = PendingIntent.getService(context, 0, riWifi, PendingIntent.FLAG_UPDATE_CURRENT);
|
||||
notification.addAction(
|
||||
default_wifi ? R.drawable.wifi_on : R.drawable.wifi_off,
|
||||
context.getString(default_wifi ? R.string.title_allow : R.string.title_block),
|
||||
piWifi
|
||||
);
|
||||
|
||||
// Build mobile action
|
||||
Intent riOther = new Intent(context, SinkholeService.class);
|
||||
riOther.putExtra(SinkholeService.EXTRA_COMMAND, SinkholeService.Command.set);
|
||||
riOther.putExtra(SinkholeService.EXTRA_NETWORK, "other");
|
||||
riOther.putExtra(SinkholeService.EXTRA_UID, uid);
|
||||
riOther.putExtra(SinkholeService.EXTRA_PACKAGE, packages[0]);
|
||||
riOther.putExtra(SinkholeService.EXTRA_BLOCKED, !default_other);
|
||||
PendingIntent piOther = PendingIntent.getService(context, 1, riOther, PendingIntent.FLAG_UPDATE_CURRENT);
|
||||
notification.addAction(
|
||||
default_other ? R.drawable.other_on : R.drawable.other_off,
|
||||
context.getString(default_other ? R.string.title_allow : R.string.title_block),
|
||||
piOther
|
||||
);
|
||||
|
||||
// Show notification
|
||||
NotificationManagerCompat.from(context).notify(uid, notification.build());
|
||||
|
||||
} catch (PackageManager.NameNotFoundException ex) {
|
||||
Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
|
||||
}
|
||||
int uid = intent.getIntExtra(Intent.EXTRA_UID, 0);
|
||||
notifyApplication(uid, context);
|
||||
}
|
||||
|
||||
} else if (Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction())) {
|
||||
// Application removed
|
||||
|
@ -150,6 +90,72 @@ public class Receiver extends BroadcastReceiver {
|
|||
}
|
||||
}
|
||||
|
||||
public static void notifyApplication(int uid, Context context) {
|
||||
try {
|
||||
PackageManager pm = context.getPackageManager();
|
||||
String[] packages = pm.getPackagesForUid(uid);
|
||||
if (packages.length < 1)
|
||||
throw new PackageManager.NameNotFoundException(Integer.toString(uid));
|
||||
String name = (String) pm.getApplicationLabel(pm.getApplicationInfo(packages[0], 0));
|
||||
|
||||
// Build notification
|
||||
Intent main = new Intent(context, ActivityMain.class);
|
||||
PendingIntent pi = PendingIntent.getActivity(context, 0, main, PendingIntent.FLAG_UPDATE_CURRENT);
|
||||
|
||||
NotificationCompat.Builder notification = new NotificationCompat.Builder(context)
|
||||
.setSmallIcon(R.drawable.ic_security_white_24dp)
|
||||
.setContentTitle(context.getString(R.string.app_name))
|
||||
.setContentText(context.getString(R.string.msg_installed, name))
|
||||
.setContentIntent(pi)
|
||||
.setCategory(Notification.CATEGORY_STATUS)
|
||||
.setVisibility(Notification.VISIBILITY_SECRET)
|
||||
.setColor(ContextCompat.getColor(context, R.color.colorPrimary))
|
||||
.setAutoCancel(true);
|
||||
|
||||
// Get defaults
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
SharedPreferences prefs_wifi = context.getSharedPreferences("wifi", Context.MODE_PRIVATE);
|
||||
SharedPreferences prefs_other = context.getSharedPreferences("other", Context.MODE_PRIVATE);
|
||||
boolean wifi = prefs_wifi.getBoolean(packages[0], prefs.getBoolean("whitelist_wifi", true));
|
||||
boolean other = prefs_other.getBoolean(packages[0], prefs.getBoolean("whitelist_other", true));
|
||||
|
||||
// Build Wi-Fi action
|
||||
Intent riWifi = new Intent(context, SinkholeService.class);
|
||||
riWifi.putExtra(SinkholeService.EXTRA_COMMAND, SinkholeService.Command.set);
|
||||
riWifi.putExtra(SinkholeService.EXTRA_NETWORK, "wifi");
|
||||
riWifi.putExtra(SinkholeService.EXTRA_UID, uid);
|
||||
riWifi.putExtra(SinkholeService.EXTRA_PACKAGE, packages[0]);
|
||||
riWifi.putExtra(SinkholeService.EXTRA_BLOCKED, !wifi);
|
||||
|
||||
PendingIntent piWifi = PendingIntent.getService(context, 0, riWifi, PendingIntent.FLAG_UPDATE_CURRENT);
|
||||
notification.addAction(
|
||||
wifi ? R.drawable.wifi_on : R.drawable.wifi_off,
|
||||
context.getString(wifi ? R.string.title_allow : R.string.title_block),
|
||||
piWifi
|
||||
);
|
||||
|
||||
// Build mobile action
|
||||
Intent riOther = new Intent(context, SinkholeService.class);
|
||||
riOther.putExtra(SinkholeService.EXTRA_COMMAND, SinkholeService.Command.set);
|
||||
riOther.putExtra(SinkholeService.EXTRA_NETWORK, "other");
|
||||
riOther.putExtra(SinkholeService.EXTRA_UID, uid);
|
||||
riOther.putExtra(SinkholeService.EXTRA_PACKAGE, packages[0]);
|
||||
riOther.putExtra(SinkholeService.EXTRA_BLOCKED, !other);
|
||||
PendingIntent piOther = PendingIntent.getService(context, 1, riOther, PendingIntent.FLAG_UPDATE_CURRENT);
|
||||
notification.addAction(
|
||||
other ? R.drawable.other_on : R.drawable.other_off,
|
||||
context.getString(other ? R.string.title_allow : R.string.title_block),
|
||||
piOther
|
||||
);
|
||||
|
||||
// Show notification
|
||||
NotificationManagerCompat.from(context).notify(uid, notification.build());
|
||||
|
||||
} catch (PackageManager.NameNotFoundException ex) {
|
||||
Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
|
||||
}
|
||||
}
|
||||
|
||||
public static void upgrade(boolean initialized, Context context) {
|
||||
synchronized (context.getApplicationContext()) {
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
|
|
|
@ -33,6 +33,7 @@ import android.net.Uri;
|
|||
import android.os.Build;
|
||||
import android.os.SystemClock;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.support.v4.app.NotificationManagerCompat;
|
||||
import android.support.v4.content.ContextCompat;
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
import android.util.Log;
|
||||
|
@ -465,6 +466,8 @@ public class RuleAdapter extends RecyclerView.Adapter<RuleAdapter.ViewHolder> im
|
|||
prefs.edit().putBoolean(rule.info.packageName, blocked).apply();
|
||||
}
|
||||
}
|
||||
|
||||
NotificationManagerCompat.from(context).cancel(rule.info.applicationInfo.uid);
|
||||
}
|
||||
|
||||
private void updateScreenWifi(Rule rule, boolean enabled) {
|
||||
|
|
|
@ -471,7 +471,7 @@ public class SinkholeService extends VpnService {
|
|||
boolean blocked = intent.getBooleanExtra(EXTRA_BLOCKED, false);
|
||||
Log.i(TAG, "Set " + pkg + " " + network + "=" + blocked);
|
||||
|
||||
// Get defailts
|
||||
// Get defaults
|
||||
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(SinkholeService.this);
|
||||
boolean default_wifi = settings.getBoolean("whitelist_wifi", true);
|
||||
boolean default_other = settings.getBoolean("whitelist_other", true);
|
||||
|
@ -483,8 +483,8 @@ public class SinkholeService extends VpnService {
|
|||
else
|
||||
prefs.edit().putBoolean(pkg, blocked).apply();
|
||||
|
||||
// Remove notification
|
||||
NotificationManagerCompat.from(SinkholeService.this).cancel(uid);
|
||||
// Update notification
|
||||
Receiver.notifyApplication(uid, SinkholeService.this);
|
||||
|
||||
// Update UI
|
||||
Intent ruleset = new Intent(ActivityMain.ACTION_RULES_CHANGED);
|
||||
|
|
Loading…
Reference in a new issue