Skip to content

Commit

Permalink
Add back BootJobService
Browse files Browse the repository at this point in the history
  • Loading branch information
fornwall committed Oct 17, 2023
1 parent 3f9a106 commit e7d2abd
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions app/src/main/java/com/termux/boot/BootJobService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
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;
import android.util.Log;

public class BootJobService extends JobService {

public static final String SCRIPT_FILE_PATH = "com.termux.boot.script_path";

private static final String TAG = "termux";

// 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) {
Log.i(TAG, "Executing job " + params.getJobId() + ".");

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 false; // offloaded to Termux; job is done
}

@Override
public boolean onStopJob(JobParameters params) {
Log.i(TAG, "Execution of job " + params.getJobId() + " has been cancelled.");
return false; // do not reschedule
}
}

0 comments on commit e7d2abd

Please sign in to comment.