Show notification

Do not translate yet, texts may change
This commit is contained in:
M66B 2015-11-04 20:47:07 +01:00
parent a2f5d8f084
commit 28eb3f20a8
2 changed files with 56 additions and 1 deletions

View File

@ -19,6 +19,7 @@ package eu.faircode.netguard;
Copyright 2015 by Marcel Bokhorst (M66B) Copyright 2015 by Marcel Bokhorst (M66B)
*/ */
import android.app.Notification;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
@ -49,7 +50,10 @@ public class SinkholeService extends VpnService {
private boolean debug = false; private boolean debug = false;
private Thread thread = null; private Thread thread = null;
private static final int NOTIFY_DISABLED = 1; private static final int NOTIFY_STARTED = 1;
private static final int NOTIFY_STOPPED = 2;
private static final int NOTIFY_DISABLED = 3;
private static final String EXTRA_COMMAND = "Command"; private static final String EXTRA_COMMAND = "Command";
private enum Command {start, reload, stop} private enum Command {start, reload, stop}
@ -74,6 +78,8 @@ public class SinkholeService extends VpnService {
last_roaming = Util.isRoaming(SinkholeService.this); last_roaming = Util.isRoaming(SinkholeService.this);
vpn = startVPN(); vpn = startVPN();
startDebug(vpn); startDebug(vpn);
startForeground(NOTIFY_STARTED, getForegroundNotification());
removeStoppedNotification();
removeDisabledNotification(); removeDisabledNotification();
prefs.edit().putBoolean("enabled", true).apply(); prefs.edit().putBoolean("enabled", true).apply();
} }
@ -94,6 +100,8 @@ public class SinkholeService extends VpnService {
stopDebug(); stopDebug();
stopVPN(vpn); stopVPN(vpn);
vpn = null; vpn = null;
stopForeground(true);
showStoppedNotification();
} }
prefs.edit().putBoolean("enabled", false).apply(); prefs.edit().putBoolean("enabled", false).apply();
stopSelf(); stopSelf();
@ -341,6 +349,49 @@ public class SinkholeService extends VpnService {
super.onRevoke(); super.onRevoke();
} }
private Notification getForegroundNotification() {
Intent riMain = new Intent(this, ActivityMain.class);
PendingIntent piMain = PendingIntent.getActivity(this, 0, riMain, PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Builder notification = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(getString(R.string.app_name))
.setContentText(getString(R.string.msg_started))
.setContentIntent(piMain);
Intent intent = new Intent(this, SinkholeService.class);
intent.putExtra(EXTRA_COMMAND, Command.stop);
PendingIntent pi = PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.addAction(android.R.drawable.ic_menu_close_clear_cancel, getString(R.string.title_stop), pi);
return notification.build();
}
private void showStoppedNotification() {
Intent riMain = new Intent(this, ActivityMain.class);
PendingIntent piMain = PendingIntent.getActivity(this, 0, riMain, PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Builder notification = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(getString(R.string.app_name))
.setContentText(getString(R.string.msg_stopped))
.setContentIntent(piMain)
.setAutoCancel(false);
Intent intent = new Intent(this, SinkholeService.class);
intent.putExtra(EXTRA_COMMAND, Command.start);
PendingIntent pi = PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.addAction(android.R.drawable.ic_media_play, getString(R.string.title_start), pi);
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(NOTIFY_STOPPED, notification.build());
}
private void removeStoppedNotification() {
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nm.cancel(NOTIFY_STOPPED);
}
private void showDisabledNotification() { private void showDisabledNotification() {
Intent riMain = new Intent(this, ActivityMain.class); Intent riMain = new Intent(this, ActivityMain.class);
PendingIntent piMain = PendingIntent.getActivity(this, 0, riMain, PendingIntent.FLAG_CANCEL_CURRENT); PendingIntent piMain = PendingIntent.getActivity(this, 0, riMain, PendingIntent.FLAG_CANCEL_CURRENT);

View File

@ -21,12 +21,16 @@ This is caused by bugs in Android, or in the software provided by the manufactur
<string name="menu_about">About</string> <string name="menu_about">About</string>
<string name="msg_sure">Are you sure?</string> <string name="msg_sure">Are you sure?</string>
<string name="msg_started">Running</string>
<string name="msg_stopped">Stopped</string>
<string name="msg_disabled">NetGuard is disabled, use the switch above to enable NetGuard</string> <string name="msg_disabled">NetGuard is disabled, use the switch above to enable NetGuard</string>
<string name="msg_revoked">NetGuard has been disabled</string> <string name="msg_revoked">NetGuard has been disabled</string>
<string name="msg_completed">Action completed</string> <string name="msg_completed">Action completed</string>
<string name="msg_vpn">NetGuard uses a local VPN as a sinkhole for blocked internet traffic, <string name="msg_vpn">NetGuard uses a local VPN as a sinkhole for blocked internet traffic,
for this reason you should allow a VPN connection in the next dialog</string> for this reason you should allow a VPN connection in the next dialog</string>
<string name="title_start">Start</string>
<string name="title_stop">Stop</string>
<string name="title_using">Allow when device in use</string> <string name="title_using">Allow when device in use</string>
<string name="title_roaming">Block when roaming</string> <string name="title_roaming">Block when roaming</string>
<string name="title_launch">Start application</string> <string name="title_launch">Start application</string>