public synchronized int nextJobId() { if (mJobCounter == null) { mJobCounter = new AtomicInteger(getMaxJobId()); } int id = mJobCounter.incrementAndGet(); int offset = JobConfig.getJobIdOffset(); if (id < offset || id >= JobIdsInternal.RESERVED_JOB_ID_RANGE_START) { /* * An overflow occurred. It'll happen rarely, but just in case reset the ID and start from scratch. * Existing jobs will be treated as orphaned and will be overwritten. */ mJobCounter.set(offset); id = mJobCounter.incrementAndGet(); } mPreferences.edit().putInt(JOB_ID_COUNTER, id).apply(); return id; }
@VisibleForTesting /*package*/ int getMaxJobId() { SQLiteDatabase database = null; Cursor cursor = null; int jobId = 0; try { database = getDatabase(); cursor = database.rawQuery("SELECT MAX(" + COLUMN_ID + ") FROM " + JOB_TABLE_NAME, null); if (cursor != null && cursor.moveToFirst()) { jobId = cursor.getInt(0); } } catch (Exception e) { CAT.e(e); } finally { closeCursor(cursor); closeDatabase(database); } return Math.max(JobConfig.getJobIdOffset(), Math.max(jobId, mPreferences.getInt(JOB_ID_COUNTER, 0))); }
public synchronized int nextJobId() { if (mJobCounter == null) { mJobCounter = new AtomicInteger(getMaxJobId()); } int id = mJobCounter.incrementAndGet(); int offset = JobConfig.getJobIdOffset(); if (id < offset || id >= JobIdsInternal.RESERVED_JOB_ID_RANGE_START) { /* * An overflow occurred. It'll happen rarely, but just in case reset the ID and start from scratch. * Existing jobs will be treated as orphaned and will be overwritten. */ mJobCounter.set(offset); id = mJobCounter.incrementAndGet(); } mPreferences.edit().putInt(JOB_ID_COUNTER, id).apply(); return id; }
@VisibleForTesting /*package*/ int getMaxJobId() { SQLiteDatabase database = null; Cursor cursor = null; int jobId = 0; try { database = getDatabase(); cursor = database.rawQuery("SELECT MAX(" + COLUMN_ID + ") FROM " + JOB_TABLE_NAME, null); if (cursor != null && cursor.moveToFirst()) { jobId = cursor.getInt(0); } } catch (Exception e) { CAT.e(e); } finally { closeCursor(cursor); closeDatabase(database); } return Math.max(JobConfig.getJobIdOffset(), Math.max(jobId, mPreferences.getInt(JOB_ID_COUNTER, 0))); }