Skip to content

Commit

Permalink
Improve force stop feature on long back button press
Browse files Browse the repository at this point in the history
  • Loading branch information
Gedsh committed Dec 9, 2019
1 parent bb4a4df commit 1a4e04e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -776,7 +776,8 @@ public boolean onKeyLongPress(int keyCode, KeyEvent event) {
@Override
public void run() {
Intent intent = new Intent(MainActivity.this, ModulesService.class);
stopService(intent);
intent.setAction(ModulesService.actionStopService);
startService(intent);
}
}, 3000);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ public class ModulesService extends Service {
public static final String actionDismissNotification = "pan.alexander.tordnscrypt.action.DISMISS_NOTIFICATION";
public static final int DEFAULT_NOTIFICATION_ID = 101;

public static final String actionStopService = "pan.alexander.tordnscrypt.action.STOP_SERVICE";

static final String actionStartDnsCrypt = "pan.alexander.tordnscrypt.action.START_DNSCRYPT";
static final String actionStartTor = "pan.alexander.tordnscrypt.action.START_TOR";
static final String actionStartITPD = "pan.alexander.tordnscrypt.action.START_ITPD";
Expand Down Expand Up @@ -150,6 +152,9 @@ public int onStartCommand(Intent intent, int flags, int startId) {
case actionRecoverService:
setAllModulesStateStopped();
break;
case actionStopService:
stopModulesService();
break;
}

return START_REDELIVER_INTENT;
Expand Down Expand Up @@ -507,6 +512,14 @@ private void setAllModulesStateStopped() {
modulesStatus.setItpdState(STOPPED);
}

private void stopModulesService() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
stopForeground(true);
}

stopSelf();
}

private void makeDelay() {
try {
TimeUnit.SECONDS.sleep(10);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
Copyright 2019 by Garmatin Oleksandr [email protected]
*/

import android.os.Build;
import android.util.Log;

import java.util.TimerTask;
Expand Down Expand Up @@ -235,6 +236,10 @@ private void restoreModulesSavedState() {
}

private void safeStopModulesService() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
modulesService.stopForeground(true);
}

modulesService.stopSelf();
}

Expand Down

0 comments on commit 1a4e04e

Please sign in to comment.