diff --git a/app/build.gradle b/app/build.gradle index 3c821d6..4e6da2e 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -6,7 +6,6 @@ android { dependencies { compile 'com.android.support:support-annotations:27.0.0' - compile "com.android.support:support-core-utils:27.0.0" } defaultConfig { diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 6c06226..43a73d2 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -25,6 +25,9 @@ + \ No newline at end of file diff --git a/app/src/main/java/com/termux/boot/BootJobService.java b/app/src/main/java/com/termux/boot/BootJobService.java new file mode 100644 index 0000000..6483e64 --- /dev/null +++ b/app/src/main/java/com/termux/boot/BootJobService.java @@ -0,0 +1,46 @@ +package com.termux.boot; + +import android.app.job.JobParameters; +import android.app.job.JobService; +import android.content.Context; +import android.content.Intent; +import android.net.Uri; +import android.os.Build; +import android.os.PersistableBundle; + +public class BootJobService extends JobService { + + public static final String SCRIPT_FILE_PATH = "com.termux.boot.script_path"; + + // Constants from TermuxService. + private static final String TERMUX_SERVICE = "com.termux.app.TermuxService"; + private static final String ACTION_EXECUTE = "com.termux.service_execute"; + private static final String EXTRA_EXECUTE_IN_BACKGROUND = "com.termux.execute.background"; + + @Override + public boolean onStartJob(JobParameters params) { + PersistableBundle extras = params.getExtras(); + String filePath = extras.getString(SCRIPT_FILE_PATH); + + Uri scriptUri = new Uri.Builder().scheme("com.termux.file").path(filePath).build(); + Intent executeIntent = new Intent(ACTION_EXECUTE, scriptUri); + executeIntent.setClassName("com.termux", TERMUX_SERVICE); + executeIntent.putExtra(EXTRA_EXECUTE_IN_BACKGROUND, true); + + Context context = getApplicationContext(); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + // https://developer.android.com/about/versions/oreo/background.html + context.startForegroundService(executeIntent); + } else { + context.startService(executeIntent); + } + + return true; + } + + @Override + public boolean onStopJob(JobParameters params) { + return true; + } + +} diff --git a/app/src/main/java/com/termux/boot/BootReceiver.java b/app/src/main/java/com/termux/boot/BootReceiver.java index 0a32b45..a9fd80a 100644 --- a/app/src/main/java/com/termux/boot/BootReceiver.java +++ b/app/src/main/java/com/termux/boot/BootReceiver.java @@ -1,22 +1,20 @@ package com.termux.boot; import android.annotation.SuppressLint; +import android.app.job.JobInfo; +import android.app.job.JobScheduler; +import android.content.BroadcastReceiver; +import android.content.ComponentName; import android.content.Context; import android.content.Intent; -import android.net.Uri; -import android.support.v4.content.WakefulBroadcastReceiver; +import android.os.PersistableBundle; import android.util.Log; import java.io.File; import java.util.Arrays; import java.util.Comparator; -public class BootReceiver extends WakefulBroadcastReceiver { - - // Constants from TermuxService. - private static final String TERMUX_SERVICE = "com.termux.app.TermuxService"; - private static final String ACTION_EXECUTE = "com.termux.service_execute"; - private static final String EXTRA_EXECUTE_IN_BACKGROUND = "com.termux.execute.background"; +public class BootReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { @@ -43,13 +41,18 @@ public int compare(File f1, File f2) { logMessage.append(file.getName()); ensureFileReadableAndExecutable(file); - Uri scriptUri = new Uri.Builder().scheme("com.termux.file").path(file.getAbsolutePath()).build(); - Intent executeIntent = new Intent(ACTION_EXECUTE, scriptUri); - executeIntent.setClassName("com.termux", TERMUX_SERVICE); - executeIntent.putExtra(EXTRA_EXECUTE_IN_BACKGROUND, true); + PersistableBundle extras = new PersistableBundle(); + extras.putString(BootJobService.SCRIPT_FILE_PATH, file.getAbsolutePath()); - startWakefulService(context, executeIntent); + ComponentName serviceComponent = new ComponentName(context, BootJobService.class); + JobInfo job = new JobInfo.Builder(0, serviceComponent) + .setExtras(extras) + .setOverrideDeadline(3 * 1000) + .build(); + JobScheduler jobScheduler = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE); + assert jobScheduler != null; + jobScheduler.schedule(job); } if (logMessage.length() > 0) {