mirror of
https://github.com/danog/termux-api.git
synced 2024-11-30 04:19:20 +01:00
Support cancel, cancelAll and pending in JobScheduler
This commit is contained in:
parent
dce672f671
commit
51a02f4fd8
@ -7,6 +7,7 @@ import android.content.Context;
|
|||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
import android.os.PersistableBundle;
|
import android.os.PersistableBundle;
|
||||||
|
import android.support.annotation.RequiresApi;
|
||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
@ -57,8 +58,14 @@ public class JobSchedulerAPI {
|
|||||||
|
|
||||||
final String scriptPath = intent.getStringExtra("script");
|
final String scriptPath = intent.getStringExtra("script");
|
||||||
|
|
||||||
final int periodicMillis = intent.getIntExtra("period_ms", 0);
|
|
||||||
final int jobId = intent.getIntExtra("job_id", 0);
|
final int jobId = intent.getIntExtra("job_id", 0);
|
||||||
|
|
||||||
|
final boolean pending = intent.getBooleanExtra("pending", false);
|
||||||
|
|
||||||
|
final boolean cancel = intent.getBooleanExtra("cancel", false);
|
||||||
|
final boolean cancelAll = intent.getBooleanExtra("cancel_all", false);
|
||||||
|
|
||||||
|
final int periodicMillis = intent.getIntExtra("period_ms", 0);
|
||||||
final String networkType = intent.getStringExtra("network");
|
final String networkType = intent.getStringExtra("network");
|
||||||
final boolean batteryNotLow = intent.getBooleanExtra("battery_not_low", true);
|
final boolean batteryNotLow = intent.getBooleanExtra("battery_not_low", true);
|
||||||
final boolean charging = intent.getBooleanExtra("charging", false);
|
final boolean charging = intent.getBooleanExtra("charging", false);
|
||||||
@ -86,6 +93,29 @@ public class JobSchedulerAPI {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
JobScheduler jobScheduler = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE);
|
||||||
|
|
||||||
|
if (pending) {
|
||||||
|
displayPendingJobs(apiReceiver, intent, jobScheduler);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (cancelAll) {
|
||||||
|
displayPendingJobs(apiReceiver, intent, jobScheduler);
|
||||||
|
ResultReturner.returnData(apiReceiver, intent, out -> out.println("Cancelling all jobs"));
|
||||||
|
jobScheduler.cancelAll();
|
||||||
|
return;
|
||||||
|
} else if (cancel) {
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
||||||
|
cancelJob(apiReceiver, intent, jobScheduler, jobId);
|
||||||
|
} else {
|
||||||
|
ResultReturner.returnData(apiReceiver, intent, out -> out.println("Need at least Android N to cancel individual jobs"));
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Schedule new job
|
||||||
if (scriptPath == null) {
|
if (scriptPath == null) {
|
||||||
ResultReturner.returnData(apiReceiver, intent, out -> out.println("No script path given"));
|
ResultReturner.returnData(apiReceiver, intent, out -> out.println("No script path given"));
|
||||||
return;
|
return;
|
||||||
@ -111,12 +141,7 @@ public class JobSchedulerAPI {
|
|||||||
extras.putString(SchedulerJobService.SCRIPT_FILE_PATH, file.getAbsolutePath());
|
extras.putString(SchedulerJobService.SCRIPT_FILE_PATH, file.getAbsolutePath());
|
||||||
|
|
||||||
|
|
||||||
JobScheduler jobScheduler = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE);
|
displayPendingJobs(apiReceiver, intent, jobScheduler);
|
||||||
// Display pending jobs
|
|
||||||
for (JobInfo job : jobScheduler.getAllPendingJobs()) {
|
|
||||||
final JobInfo j = job;
|
|
||||||
ResultReturner.returnData(apiReceiver, intent, out -> out.println(String.format(Locale.ENGLISH, "Pending %s", formatJobInfo(j))));
|
|
||||||
}
|
|
||||||
|
|
||||||
ComponentName serviceComponent = new ComponentName(context, SchedulerJobService.class);
|
ComponentName serviceComponent = new ComponentName(context, SchedulerJobService.class);
|
||||||
JobInfo.Builder builder = new JobInfo.Builder(jobId, serviceComponent)
|
JobInfo.Builder builder = new JobInfo.Builder(jobId, serviceComponent)
|
||||||
@ -125,7 +150,7 @@ public class JobSchedulerAPI {
|
|||||||
.setRequiresCharging(charging)
|
.setRequiresCharging(charging)
|
||||||
.setRequiresDeviceIdle(idle);
|
.setRequiresDeviceIdle(idle);
|
||||||
|
|
||||||
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||||
builder = builder.setRequiresBatteryNotLow(batteryNotLow);
|
builder = builder.setRequiresBatteryNotLow(batteryNotLow);
|
||||||
builder = builder.setRequiresStorageNotLow(storageNotLow);
|
builder = builder.setRequiresStorageNotLow(storageNotLow);
|
||||||
}
|
}
|
||||||
@ -144,4 +169,24 @@ public class JobSchedulerAPI {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void displayPendingJobs(TermuxApiReceiver apiReceiver, Intent intent, JobScheduler jobScheduler) {
|
||||||
|
// Display pending jobs
|
||||||
|
for (JobInfo job : jobScheduler.getAllPendingJobs()) {
|
||||||
|
final JobInfo j = job;
|
||||||
|
ResultReturner.returnData(apiReceiver, intent, out -> out.println(String.format(Locale.ENGLISH, "Pending %s", formatJobInfo(j))));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequiresApi(api = Build.VERSION_CODES.N)
|
||||||
|
private static void cancelJob(TermuxApiReceiver apiReceiver, Intent intent, JobScheduler jobScheduler, int jobId) {
|
||||||
|
final JobInfo jobInfo = jobScheduler.getPendingJob(jobId);
|
||||||
|
if (jobInfo == null) {
|
||||||
|
ResultReturner.returnData(apiReceiver, intent, out -> out.println(String.format(Locale.ENGLISH, "No job %d found", jobId)));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
ResultReturner.returnData(apiReceiver, intent, out -> out.println(String.format(Locale.ENGLISH, "Cancelling %s", formatJobInfo(jobInfo))));
|
||||||
|
jobScheduler.cancel(jobId);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user