android: Add a public notification

This commit is contained in:
Tobias Brunner
2016-12-08 17:14:49 +01:00
parent d5070425a0
commit 85059424a7
@@ -290,7 +290,7 @@ public class CharonVpnService extends VpnService implements Runnable, VpnStateSe
private void addNotification() private void addNotification()
{ {
mShowNotification = true; mShowNotification = true;
startForeground(VPN_STATE_NOTIFICATION_ID, buildNotification()); startForeground(VPN_STATE_NOTIFICATION_ID, buildNotification(false));
} }
/** /**
@@ -302,11 +302,10 @@ public class CharonVpnService extends VpnService implements Runnable, VpnStateSe
stopForeground(true); stopForeground(true);
} }
/** /**
* Build a notification matching the current state * Build a notification matching the current state
*/ */
private Notification buildNotification() private Notification buildNotification(boolean publicVersion)
{ {
VpnProfile profile = mService.getProfile(); VpnProfile profile = mService.getProfile();
State state = mService.getState(); State state = mService.getState();
@@ -318,10 +317,10 @@ public class CharonVpnService extends VpnService implements Runnable, VpnStateSe
name = profile.getName(); name = profile.getName();
} }
android.support.v4.app.NotificationCompat.Builder builder = new NotificationCompat.Builder(this) android.support.v4.app.NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setContentText(name)
.setSmallIcon(R.drawable.ic_notification) .setSmallIcon(R.drawable.ic_notification)
.setCategory(NotificationCompat.CATEGORY_SERVICE) .setCategory(NotificationCompat.CATEGORY_SERVICE)
.setVisibility(NotificationCompat.VISIBILITY_SECRET); .setVisibility(publicVersion ? NotificationCompat.VISIBILITY_PUBLIC
: NotificationCompat.VISIBILITY_PRIVATE);
int s = R.string.state_disabled; int s = R.string.state_disabled;
if (error != ErrorState.NO_ERROR) if (error != ErrorState.NO_ERROR)
{ {
@@ -349,6 +348,11 @@ public class CharonVpnService extends VpnService implements Runnable, VpnStateSe
} }
} }
builder.setContentTitle(getString(s)); builder.setContentTitle(getString(s));
if (!publicVersion)
{
builder.setContentText(name);
builder.setPublicVersion(buildNotification(true));
}
Intent intent = new Intent(getApplicationContext(), MainActivity.class); Intent intent = new Intent(getApplicationContext(), MainActivity.class);
PendingIntent pending = PendingIntent.getActivity(getApplicationContext(), 0, intent, PendingIntent pending = PendingIntent.getActivity(getApplicationContext(), 0, intent,
@@ -362,7 +366,7 @@ public class CharonVpnService extends VpnService implements Runnable, VpnStateSe
if (mShowNotification) if (mShowNotification)
{ {
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(VPN_STATE_NOTIFICATION_ID, buildNotification()); manager.notify(VPN_STATE_NOTIFICATION_ID, buildNotification(false));
} }
} }