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.
master
Greg Alexander 5 years ago
parent c1bdacd4d6
commit 823591055a

@ -3,27 +3,13 @@ package org.galexander.sshd;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
import android.content.Intent; import android.content.Intent;
import android.content.Context; import android.content.Context;
import android.os.Build;
import android.widget.Toast;
public class BootReceiver extends BroadcastReceiver { public class BootReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
Prefs.init(context); Prefs.init(context);
if (Prefs.get_onboot()) { if (Prefs.get_onboot()) {
Intent i = new Intent(context, SimpleSSHDService.class); SimpleSSHDService.my_startService(context,
if (Build.VERSION.SDK_INT >= new Intent(context, SimpleSSHDService.class));
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);
}
} }
} }
} }

@ -10,6 +10,7 @@ import android.content.Context;
import android.os.Build; import android.os.Build;
import android.os.IBinder; import android.os.IBinder;
import android.widget.RemoteViews; import android.widget.RemoteViews;
import android.widget.Toast;
import androidx.core.app.NotificationCompat; import androidx.core.app.NotificationCompat;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.File; import java.io.File;
@ -210,4 +211,20 @@ public class SimpleSSHDService extends Service {
static { static {
System.loadLibrary("simplesshd-jni"); 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);
}
}
} }

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

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

Loading…
Cancel
Save