From f31e3e567efb2cbcd0c898c83555c071ea52945f Mon Sep 17 00:00:00 2001 From: hyb1996 <946994919@qq.com> Date: Sun, 16 Dec 2018 11:00:52 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BB=B6=E9=95=BF=20=E5=AE=9A=E6=97=B6?= =?UTF-8?q?=E4=BB=BB=E5=8A=A1=E7=9A=84=E8=B0=83=E5=BA=A6=E6=9C=80=E5=B0=8F?= =?UTF-8?q?=E6=97=B6=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../autojs/timing/TimedTaskManager.java | 9 ++-- .../autojs/timing/TimedTaskScheduler.java | 44 +++++++------------ 2 files changed, 20 insertions(+), 33 deletions(-) diff --git a/app/src/main/java/org/autojs/autojs/timing/TimedTaskManager.java b/app/src/main/java/org/autojs/autojs/timing/TimedTaskManager.java index 4ad701ac5..d955cac55 100644 --- a/app/src/main/java/org/autojs/autojs/timing/TimedTaskManager.java +++ b/app/src/main/java/org/autojs/autojs/timing/TimedTaskManager.java @@ -17,7 +17,6 @@ import io.reactivex.Flowable; import io.reactivex.Observable; -import io.reactivex.functions.Consumer; /** * Created by Stardust on 2017/11/27. @@ -61,7 +60,7 @@ public void notifyTaskFinished(long id) { @SuppressLint("CheckResult") public void removeTask(TimedTask timedTask) { - TimedTaskScheduler.cancel(mContext, timedTask); + TimedTaskScheduler.cancel(timedTask); mTimedTaskDatabase.delete(timedTask) .subscribe(Observers.emptyConsumer(), Throwable::printStackTrace); } @@ -71,7 +70,7 @@ public void addTask(TimedTask timedTask) { mTimedTaskDatabase.insert(timedTask) .subscribe(id -> { timedTask.setId(id); - TimedTaskScheduler.scheduleTaskIfNeeded(mContext, timedTask); + TimedTaskScheduler.scheduleTaskIfNeeded(mContext, timedTask, false); }, Throwable::printStackTrace); } @@ -130,8 +129,8 @@ public TimedTask getTimedTask(long taskId) { public void updateTask(TimedTask task) { mTimedTaskDatabase.update(task) .subscribe(Observers.emptyConsumer(), Throwable::printStackTrace); - TimedTaskScheduler.cancel(mContext, task); - TimedTaskScheduler.scheduleTaskIfNeeded(mContext, task); + TimedTaskScheduler.cancel(task); + TimedTaskScheduler.scheduleTaskIfNeeded(mContext, task, false); } @SuppressLint("CheckResult") diff --git a/app/src/main/java/org/autojs/autojs/timing/TimedTaskScheduler.java b/app/src/main/java/org/autojs/autojs/timing/TimedTaskScheduler.java index 159108923..7250b2fdd 100644 --- a/app/src/main/java/org/autojs/autojs/timing/TimedTaskScheduler.java +++ b/app/src/main/java/org/autojs/autojs/timing/TimedTaskScheduler.java @@ -1,33 +1,20 @@ package org.autojs.autojs.timing; import android.annotation.SuppressLint; -import android.app.AlarmManager; -import android.app.PendingIntent; -import android.app.job.JobInfo; -import android.app.job.JobScheduler; -import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; -import android.os.Build; -import android.provider.AlarmClock; import android.util.Log; import com.evernote.android.job.Job; -import com.evernote.android.job.JobCreator; import com.evernote.android.job.JobManager; import com.evernote.android.job.JobRequest; -import com.evernote.android.job.util.support.PersistableBundleCompat; -import org.autojs.autojs.App; import org.autojs.autojs.external.ScriptIntents; -import org.autojs.autojs.storage.database.TimedTaskDatabase; import org.jetbrains.annotations.NotNull; import java.util.concurrent.TimeUnit; import androidx.annotation.NonNull; -import androidx.annotation.Nullable; -import androidx.annotation.RequiresApi; import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.schedulers.Schedulers; @@ -39,51 +26,52 @@ public class TimedTaskScheduler { private static final String LOG_TAG = "TimedTaskScheduler"; - private static final long ONE_HOUR = TimeUnit.HOURS.toMillis(1); + private static final long SCHEDULE_TASK_MIN_TIME = TimeUnit.DAYS.toMillis(2); private static final String JOB_TAG_CHECK_TASKS = "checkTasks"; @SuppressLint("CheckResult") - public static void checkTasks(Context context) { - Log.d(LOG_TAG, "check tasks"); + public static void checkTasks(Context context, boolean force) { + Log.d(LOG_TAG, "check tasks: force = " + force); TimedTaskManager.getInstance().getAllTasks() .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) - .subscribe(timedTask -> scheduleTaskIfNeeded(context, timedTask)); + .subscribe(timedTask -> scheduleTaskIfNeeded(context, timedTask, force)); } - public static void scheduleTaskIfNeeded(Context context, TimedTask timedTask) { + public static void scheduleTaskIfNeeded(Context context, TimedTask timedTask, boolean force) { long millis = timedTask.getNextTime(); - if (timedTask.isScheduled() || millis - System.currentTimeMillis() > ONE_HOUR) { + if ((!force && timedTask.isScheduled()) || millis - System.currentTimeMillis() > SCHEDULE_TASK_MIN_TIME) { return; } - scheduleTask(context, timedTask, millis); + scheduleTask(context, timedTask, millis, force); TimedTaskManager.getInstance() .notifyTaskScheduled(timedTask); } - private synchronized static void scheduleTask(Context context, TimedTask timedTask, long millis) { - if (timedTask.isScheduled()) { + private synchronized static void scheduleTask(Context context, TimedTask timedTask, long millis, boolean force) { + if (!force && timedTask.isScheduled()) { return; } long timeWindow = millis - System.currentTimeMillis(); - Log.d(LOG_TAG, "schedule task: task = " + timedTask + ", millis = " + millis + ", timeWindow = " + timeWindow); timedTask.setScheduled(true); TimedTaskManager.getInstance().updateTaskWithoutReScheduling(timedTask); if (timeWindow <= 0) { runTask(context, timedTask); return; } + cancel(timedTask); + Log.d(LOG_TAG, "schedule task: task = " + timedTask + ", millis = " + millis + ", timeWindow = " + timeWindow); new JobRequest.Builder(String.valueOf(timedTask.getId())) .setExact(timeWindow) .build() .schedule(); } - public static void cancel(Context context, TimedTask timedTask) { - Log.d(LOG_TAG, "cancel task: task = " + timedTask); - JobManager.instance().cancelAllForTag(String.valueOf(timedTask.getId())); + public static void cancel(TimedTask timedTask) { + int cancelCount = JobManager.instance().cancelAllForTag(String.valueOf(timedTask.getId())); + Log.d(LOG_TAG, "cancel task: task = " + timedTask + ", cancel = " + cancelCount); } public static void init(@NotNull Context context) { @@ -98,7 +86,7 @@ public static void init(@NotNull Context context) { .setPeriodic(TimeUnit.MINUTES.toMillis(20)) .build() .scheduleAsync(); - checkTasks(context); + checkTasks(context, true); } private static void runTask(Context context, TimedTask task) { @@ -140,7 +128,7 @@ private static class CheckTasksJob extends Job { @NonNull @Override protected Result onRunJob(@NonNull Params params) { - checkTasks(mContext); + checkTasks(mContext, false); return Result.SUCCESS; } }