fixed job service

This commit is contained in:
Mariotaku Lee 2017-02-08 00:38:33 +08:00
parent 8e83ae326a
commit 5cb691a54a
No known key found for this signature in database
GPG Key ID: 15C10F89D7C33535
5 changed files with 16 additions and 15 deletions

View File

@ -30,8 +30,6 @@ import android.annotation.TargetApi;
import android.app.job.JobParameters;
import android.os.Build;
import org.mariotaku.twidere.util.Analyzer;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
@ -52,15 +50,15 @@ public class JobServiceSupport {
getCallbackMethod.setAccessible(true);
final Object callback = getCallbackMethod.invoke(params);
if (callback == null) return false;
final Class<?> callbackCls = callback.getClass();
final Method acknowledgeStopMessageMethod = callbackCls.getDeclaredMethod("acknowledgeStopMessage",
final Class<?> callbackCls = Class.forName("android.app.job.IJobCallback");
final Method acknowledgeStopMessageMethod = callbackCls.getMethod("acknowledgeStopMessage",
int.class, boolean.class);
acknowledgeStopMessageMethod.setAccessible(true);
// Once method returned true successfully, remove it's callback.
// Due to Android's Binder implementation, IJobCallback.Stub.asInterface(null) will
// return null rather than crash
try {
acknowledgeStopMessageMethod.invoke(callbackCls, params.getJobId(), reschedule);
acknowledgeStopMessageMethod.invoke(callback, params.getJobId(), reschedule);
return true;
} catch (NullPointerException npe) {
// Treat as handled
@ -69,13 +67,14 @@ public class JobServiceSupport {
} catch (NoSuchMethodException e) {
// Framework version mismatch, skip
return false;
} catch (ClassNotFoundException e) {
// Framework version mismatch, skip
return false;
} catch (IllegalAccessException e) {
// This shouldn't happen, skip
Analyzer.Companion.logException(e);
return false;
} catch (InvocationTargetException e) {
// Internal error, skip
Analyzer.Companion.logException(e);
return false;
}
}
@ -89,11 +88,9 @@ public class JobServiceSupport {
return true;
} catch (NoSuchFieldException e) {
// Framework version mismatch, skip
Analyzer.Companion.logException(e);
return false;
} catch (IllegalAccessException e) {
// This shouldn't happen, skip
Analyzer.Companion.logException(e);
return false;
}
}

View File

@ -52,7 +52,6 @@ class JobSchedulerAutoRefreshController(
fun scheduleJob(jobId: Int, periodMillis: Long = TimeUnit.MINUTES.toMillis(kPreferences[refreshIntervalKey]), persisted: Boolean = true) {
val builder = JobInfo.Builder(jobId, ComponentName(context, JobTaskService::class.java))
builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
builder.setPeriodic(periodMillis)
builder.setPersisted(persisted)
try {

View File

@ -41,7 +41,7 @@ class LegacyAutoRefreshController(
override fun rescheduleAll() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
removeAllJobs(context)
removeAllJobs(context, JOB_IDS_REFRESH)
}
super.rescheduleAll()
}
@ -69,12 +69,12 @@ class LegacyAutoRefreshController(
PendingIntent.getService(context, 0, intent, 0))
}
private companion object {
companion object {
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
fun removeAllJobs(context: Context) {
fun removeAllJobs(context: Context, jobIds: IntArray) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) return
val jobService = context.getSystemService(Context.JOB_SCHEDULER_SERVICE) as JobScheduler
JOB_IDS_REFRESH.forEach { id ->
jobIds.forEach { id ->
jobService.cancel(id)
}
}

View File

@ -28,7 +28,6 @@ class JobSchedulerSyncController(context: Context) : SyncController(context) {
fun scheduleJob(jobId: Int, persisted: Boolean = true) {
val builder = JobInfo.Builder(jobId, ComponentName(context, JobTaskService::class.java))
builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
builder.setPeriodic(TimeUnit.HOURS.toMillis(4))
builder.setPersisted(persisted)
try {

View File

@ -4,10 +4,13 @@ import android.app.AlarmManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.SystemClock
import android.support.v4.util.ArrayMap
import org.mariotaku.twidere.service.JobTaskService
import org.mariotaku.twidere.service.LegacyTaskService
import org.mariotaku.twidere.util.TaskServiceRunner
import org.mariotaku.twidere.util.refresh.LegacyAutoRefreshController
import java.util.concurrent.TimeUnit
/**
@ -28,6 +31,9 @@ class LegacySyncController(context: Context) : SyncController(context) {
}
override fun appStarted() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
LegacyAutoRefreshController.removeAllJobs(context, JobTaskService.JOB_IDS_REFRESH)
}
for ((action, pendingIntent) in pendingIntents) {
alarmManager.cancel(pendingIntent)
val interval = TimeUnit.HOURS.toMillis(4)