protected JobProxy14(Context context, String logTag) { mContext = context; mCat = new JobCat(logTag); }
/** * @return Whether logging is enabled for this library. The default value is {@code true}. */ public static boolean isLogcatEnabled() { return JobCat.isLogcatEnabled(); }
/** * Resets all adjustments in the config. */ public static void reset() { for (JobApi api : JobApi.values()) { ENABLED_APIS.put(api, Boolean.TRUE); } allowSmallerIntervals = false; forceAllowApi14 = false; jobReschedulePause = DEFAULT_JOB_RESCHEDULE_PAUSE; skipJobReschedule = false; jobIdOffset = 0; forceRtc = false; clock = Clock.DEFAULT; executorService = DEFAULT_EXECUTOR_SERVICE; closeDatabase = false; JobCat.setLogcatEnabled(true); JobCat.clearLogger(); } }
/*package*/ void deleteDatabaseFile(String fileName) { if (fileName.equalsIgnoreCase(":memory:") || fileName.trim().length() == 0) { return; } CAT.e("deleting the database file: " + fileName); try { File databaseFile = new File(fileName); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { deleteApi16(databaseFile); } else { deleteApi14(JobManager.instance().getContext(), databaseFile); } } catch (Exception e) { /* print warning and ignore exception */ CAT.w(e, "delete failed: " + e.getMessage()); } }
@Override protected void onHandleWork(@NonNull Intent intent) { /* * Delay this slightly. This avoids a race condition if the app was launched by the * AlarmManager. Then the alarm was already removed, but the JobRequest might still * be available in the storage. We still catch this case, because we never execute * a job with the same ID twice. Nonetheless, add the delay to save resources. */ try { CAT.d("Reschedule service started"); SystemClock.sleep(JobConfig.getJobReschedulePause()); JobManager manager; try { manager = JobManager.create(this); } catch (Exception e) { return; } Set<JobRequest> requests = manager.getAllJobRequests(null, true, true); int rescheduledCount = rescheduleJobs(manager, requests); CAT.d("Reschedule %d jobs of %d jobs", rescheduledCount, requests.size()); } finally { if (latch != null) { // latch can be null, if the service was restarted after a process death latch.countDown(); } } }
/** * <b>WARNING:</b> Please use this method carefully. It's only meant to be used for testing purposes * and could break how the library works. * <br> * <br> * Programmatic switch to enable or disable the given API. This only has an impact for new scheduled jobs. * * @param api The API which should be enabled or disabled. * @param enabled Whether the API should be enabled or disabled. */ public static void setApiEnabled(@NonNull JobApi api, boolean enabled) { ENABLED_APIS.put(api, enabled); CAT.w("setApiEnabled - %s, %b", api, enabled); }
mCat.w("Running JobRequest on a main thread, this could cause stutter or ANR in your app."); mCat.d("Run job, %s, waited %s, %s", request, JobUtil.timeToString(waited), timeWindow); JobExecutor jobExecutor = mJobManager.getJobExecutor(); Job job = null; mCat.d("Finished job, %s %s", request, result); return result; mCat.e(e); mCat.e("Canceled %s", request);
public void i(@NonNull String message) { log(Log.INFO, mTag, message, null); }
@Nullable protected AlarmManager getAlarmManager() { if (mAlarmManager == null) { mAlarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE); } if (mAlarmManager == null) { // https://gist.github.com/vRallev/5daef6e8a3b0d4a7c366 mCat.e("AlarmManager is null"); } return mAlarmManager; } }
@Test public void testIsLogcatEnabled() { // first test in class, so resetValue() hasn't been called, yet assertThat(mResetValueCalled).isFalse(); assertThat(JobCat.isLogcatEnabled()).isTrue(); JobCat.setLogcatEnabled(false); assertThat(JobCat.isLogcatEnabled()).isFalse(); }
public synchronized Future<Job.Result> execute(@NonNull Context context, @NonNull JobRequest request, @Nullable Job job, @NonNull Bundle transientExtras) { mStartingRequests.remove(request); if (job == null) { CAT.w("JobCreator returned null for tag %s", request.getTag()); return null; } if (job.isFinished()) { throw new IllegalStateException(String.format(Locale.ENGLISH, "Job for tag %s was already run, a creator should always create a new Job instance", request.getTag())); } job.setContext(context).setRequest(request, transientExtras); CAT.i("Executing %s, context %s", request, context.getClass().getSimpleName()); mJobs.put(request.getJobId(), job); return JobConfig.getExecutorService().submit(new JobCallable(job)); }
private boolean cancelInner(@Nullable Job job) { if (job != null && job.cancel(true)) { CAT.i("Cancel running %s", job); return true; } else { return false; } }
/** * Global switch to enable or disable printing log messages to Logcat. * * @param enabled Whether or not to print all log messages. The default value is {@code true}. */ public static void setLogcatEnabled(boolean enabled) { JobCat.setLogcatEnabled(enabled); }
/** * Add a global logger for the job library, which will be notified about each log statement. * * @param logger Your desired logger. * @return {@code true} if the logger was added. Returns {@code false} if the logger was * already added. */ public static synchronized boolean addLogger(@NonNull JobLogger logger) { return JobCat.addLogger(logger); }
@Override public boolean onStopJob(JobParameters params) { Job job = JobManager.create(this).getJob(params.getJobId()); if (job != null) { job.cancel(); CAT.d("Called onStopJob for %s", job); } else { CAT.d("Called onStopJob, job %d not found", params.getJobId()); } // do not reschedule return false; }
@Override public void plantPeriodicFlexSupport(JobRequest request) { mCat.w("plantPeriodicFlexSupport called although flex is supported"); super.plantPeriodicFlexSupport(request); }