-
Notifications
You must be signed in to change notification settings - Fork 233
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
51 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |