1
0
mirror of http://galexander.org/git/simplesshd.git synced 2024-12-29 09:28:07 +00:00

Factor out the idiom for startService() vs startForegroundService(), and

use it in StartReceive/StopReceiver as well as BootReceiver, so that the
sshd service can be started and stopped even if the UI is closed.
This commit is contained in:
Greg Alexander 2019-09-01 15:42:09 -04:00
parent c1bdacd4d6
commit 823591055a
4 changed files with 21 additions and 18 deletions

View File

@ -3,27 +3,13 @@ package org.galexander.sshd;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.Context;
import android.os.Build;
import android.widget.Toast;
public class BootReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
Prefs.init(context);
if (Prefs.get_onboot()) {
Intent i = new Intent(context, SimpleSSHDService.class);
if (Build.VERSION.SDK_INT >=
Build.VERSION_CODES.O) {
if (Prefs.get_foreground()) {
context.startForegroundService(i);
} else {
Toast.makeText(context,
"SimpleSSHD cannot start background at boot since Oreo (see Settings).",
Toast.LENGTH_LONG).show();
}
} else {
context.startService(i);
}
SimpleSSHDService.my_startService(context,
new Intent(context, SimpleSSHDService.class));
}
}
}

View File

@ -10,6 +10,7 @@ import android.content.Context;
import android.os.Build;
import android.os.IBinder;
import android.widget.RemoteViews;
import android.widget.Toast;
import androidx.core.app.NotificationCompat;
import java.io.BufferedReader;
import java.io.File;
@ -210,4 +211,20 @@ public class SimpleSSHDService extends Service {
static {
System.loadLibrary("simplesshd-jni");
}
public static void my_startService(Context ctx, Intent i) {
Prefs.init(ctx);
if (Build.VERSION.SDK_INT >=
Build.VERSION_CODES.O) {
if (Prefs.get_foreground()) {
ctx.startForegroundService(i);
} else {
Toast.makeText(ctx,
"SimpleSSHD cannot start in background since Oreo (enable Settings -> Foreground Service).",
Toast.LENGTH_LONG).show();
}
} else {
ctx.startService(i);
}
}
}

View File

@ -6,7 +6,7 @@ import android.content.Context;
public class StartReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
context.startService(
SimpleSSHDService.my_startService(context,
new Intent(context, SimpleSSHDService.class));
}
}

View File

@ -6,7 +6,7 @@ import android.content.Context;
public class StopReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
context.startService(
SimpleSSHDService.my_startService(context,
new Intent(context, SimpleSSHDService.class)
.putExtra("stop", true));
}