Skip to content

Commit

Permalink
Merge pull request robinpaulson#1 from eleventigerssc/jd/foreground
Browse files Browse the repository at this point in the history
[Service] Start as foreground
  • Loading branch information
eugene-sevostianov-sc authored Sep 13, 2017
2 parents 1356bbe + 1074d68 commit ebd6a08
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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) {
Expand Down Expand Up @@ -121,9 +124,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);
Expand Down
1 change: 1 addition & 0 deletions simple-rt-android/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
<string name="title_activity_main">MainActivity</string>
<string name="accessory_error">Seems like you device doesn\'t support Android Open Accessory protocol!</string>
<string name="tun_error">Seems like you device doesn\'t support Android VpnApi! Check out tun.ko app.</string>
<string name="description_service_running">Service running</string>
</resources>

0 comments on commit ebd6a08

Please sign in to comment.