From dd9e4833431d1d09e409642d5669c35c5f4284f7 Mon Sep 17 00:00:00 2001 From: jokubasdargissc Date: Tue, 12 Sep 2017 14:20:00 -0700 Subject: [PATCH 1/2] [Service] Start as foreground --- .../com/viper/simplert/TetherService.java | 19 +++++++++++++++++++ .../app/src/main/res/values/strings.xml | 1 + 2 files changed, 20 insertions(+) diff --git a/simple-rt-android/app/src/main/java/com/viper/simplert/TetherService.java b/simple-rt-android/app/src/main/java/com/viper/simplert/TetherService.java index 7a94e2e..093d80e 100644 --- a/simple-rt-android/app/src/main/java/com/viper/simplert/TetherService.java +++ b/simple-rt-android/app/src/main/java/com/viper/simplert/TetherService.java @@ -32,6 +32,8 @@ import android.net.VpnService; import android.os.Build; import android.os.ParcelFileDescriptor; +import android.support.v4.app.NotificationCompat; +import android.support.v4.app.NotificationManagerCompat; import android.util.Log; import android.widget.Toast; @@ -40,6 +42,7 @@ public class TetherService extends VpnService { private static final String TAG = "TetherService"; private static final String ACTION_USB_PERMISSION = "com.viper.simplert.TetherService.action.USB_PERMISSION"; + private static final int FOREGROUND_NOTIFICATION_ID = 16; private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() { public void onReceive(Context context, Intent intent) { @@ -55,6 +58,8 @@ public void onReceive(Context context, Intent intent) { } }; + + @Override public int onStartCommand(final Intent intent, int flags, final int startId) { Log.w(TAG, "onStartCommand"); @@ -121,9 +126,23 @@ public int onStartCommand(final Intent intent, int flags, final int startId) { Native.start(tunFd.detachFd(), accessoryFd.detachFd()); setAsUnderlyingNetwork(ipAddr + "/" + prefixLength); + + startForeground(FOREGROUND_NOTIFICATION_ID, new NotificationCompat.Builder(this) + .setOngoing(true) + .setContentTitle(getString(R.string.app_name)) + .setContentText(getString(R.string.description_service_running)) + .setSmallIcon(android.R.drawable.ic_secure) + .build()); + return START_NOT_STICKY; } + @Override + public void onDestroy() { + NotificationManagerCompat.from(this).cancel(FOREGROUND_NOTIFICATION_ID); + super.onDestroy(); + } + private void setAsUnderlyingNetwork(String Address) { if (Build.VERSION.SDK_INT >= 22) { Network vpnNetwork = findVpnNetwork(Address); diff --git a/simple-rt-android/app/src/main/res/values/strings.xml b/simple-rt-android/app/src/main/res/values/strings.xml index bb9f93f..635a1f6 100644 --- a/simple-rt-android/app/src/main/res/values/strings.xml +++ b/simple-rt-android/app/src/main/res/values/strings.xml @@ -3,4 +3,5 @@ MainActivity Seems like you device doesn\'t support Android Open Accessory protocol! Seems like you device doesn\'t support Android VpnApi! Check out tun.ko app. + Service running From 1074d684f30944ca5b11f8880ecb3fda894cfd1c Mon Sep 17 00:00:00 2001 From: jokubasdargissc Date: Tue, 12 Sep 2017 14:21:34 -0700 Subject: [PATCH 2/2] [Nit] Remove linebreaks --- .../app/src/main/java/com/viper/simplert/TetherService.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/simple-rt-android/app/src/main/java/com/viper/simplert/TetherService.java b/simple-rt-android/app/src/main/java/com/viper/simplert/TetherService.java index 093d80e..08ce262 100644 --- a/simple-rt-android/app/src/main/java/com/viper/simplert/TetherService.java +++ b/simple-rt-android/app/src/main/java/com/viper/simplert/TetherService.java @@ -58,8 +58,6 @@ public void onReceive(Context context, Intent intent) { } }; - - @Override public int onStartCommand(final Intent intent, int flags, final int startId) { Log.w(TAG, "onStartCommand");