Do not use getPendingJob()

Also make the duplicate detection better and add some more debug logging for now.
This commit is contained in:
Adrian Ulrich 2017-10-25 12:21:35 +02:00
parent feed3100be
commit b6ba98dcd0

View File

@ -29,6 +29,10 @@ import android.content.ComponentName;
import android.database.ContentObserver;
import android.os.Build;
import android.util.Log;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.io.FileWriter;
@TargetApi(21)
public class ScheduledLibraryUpdate extends JobService {
@ -55,9 +59,6 @@ public class ScheduledLibraryUpdate extends JobService {
JobScheduler scheduler = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE);
if (scheduler.getPendingJob(JOB_ID_UPDATE) != null)
return false; // no need to re-schedule the job
ComponentName componentName = new ComponentName(context, ScheduledLibraryUpdate.class);
JobInfo job = new JobInfo.Builder(JOB_ID_UPDATE, componentName)
.setRequiresCharging(true)
@ -65,11 +66,34 @@ public class ScheduledLibraryUpdate extends JobService {
.setPeriodic(3600000 * 32) // run at most every ~32 hours
.build();
for (JobInfo pj : scheduler.getAllPendingJobs()) {
if (jobsEqual(pj, job)) {
xlog("scheduleUpdate: Job "+JOB_ID_UPDATE+" already scheduled, returning. Job="+pj.toString());
return false;
}
}
xlog("scheduling new job "+JOB_ID_UPDATE);
scheduler.schedule(job);
return true;
}
/**
* Compare two jobs, returns `true' if the values we care about
* are equal
*
* @param a the first job to compare
* @param b the second job to compare
* @return true if `a' and `b' are equal jobs
*/
private static boolean jobsEqual(JobInfo a, JobInfo b) {
return (a.getId() == b.getId() &&
a.getIntervalMillis() == b.getIntervalMillis() &&
a.isRequireCharging() == b.isRequireCharging() &&
a.isRequireDeviceIdle() == b.isRequireDeviceIdle() &&
a.isPeriodic() == b.isPeriodic());
}
/**
* Called by the scheduler to launch the job
@ -88,6 +112,7 @@ public class ScheduledLibraryUpdate extends JobService {
MediaLibrary.registerContentObserver(mObserver);
MediaLibrary.startLibraryScan(this, fullScan, false);
xlog("starting library scann. full? = "+fullScan);
return true;
}
@ -99,6 +124,7 @@ public class ScheduledLibraryUpdate extends JobService {
*/
@Override
public boolean onStopJob(JobParameters params) {
xlog("onStopJob was called");
finalizeScan();
return false;
}
@ -110,6 +136,7 @@ public class ScheduledLibraryUpdate extends JobService {
MediaLibrary.unregisterContentObserver(mObserver);
MediaLibrary.abortLibraryScan(this);
mJobParams = null;
xlog("finalized scan");
}
/**
@ -121,10 +148,23 @@ public class ScheduledLibraryUpdate extends JobService {
private final ContentObserver mObserver = new ContentObserver(null) {
@Override
public void onChange(boolean ongoing) {
xlog("onChange! ongoing="+ongoing);
if (!ongoing) {
jobFinished(mJobParams, false);
finalizeScan();
}
}
};
private static void xlog(String str) {
Log.v("VanillaMusic", str);
try {
String sdf = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS")).format(new Date());
FileWriter fw = new FileWriter("/sdcard/vanilla-log.txt", true);
fw.write(String.format("%s: %s\n", sdf, str));
fw.close();
} catch(Exception e) {
Log.v("VanillaMusic", "LOGFAIL: "+e);
}
}
}