package android.content; import com.google.android.collect.Maps; import android.os.Bundle; import android.os.SystemClock; import android.util.Pair; import android.util.Log; import android.accounts.Account; import java.util.HashMap; import java.util.ArrayList; import java.util.Map; import java.util.Iterator; /** * * @hide */ public class SyncQueue { private static final String TAG = "SyncManager"; private SyncStorageEngine mSyncStorageEngine; // A Map of SyncOperations operationKey -> SyncOperation that is designed for // quick lookup of an enqueued SyncOperation. private final HashMap mOperationsMap = Maps.newHashMap(); public SyncQueue(SyncStorageEngine syncStorageEngine) { mSyncStorageEngine = syncStorageEngine; ArrayList ops = mSyncStorageEngine.getPendingOperations(); final int N = ops.size(); for (int i=0; i nextOperation(long now) { SyncOperation lowestOp = null; long lowestOpRunTime = 0; for (SyncOperation op : mOperationsMap.values()) { // effectiveRunTime: // - backoffTime > currentTime : backoffTime // - backoffTime <= currentTime : op.runTime Pair backoff = null; long delayUntilTime = 0; final boolean isManualSync = op.extras.getBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, false); if (!isManualSync) { backoff = mSyncStorageEngine.getBackoff(op.account, op.authority); delayUntilTime = mSyncStorageEngine.getDelayUntilTime(op.account, op.authority); } long backoffTime = Math.max(backoff != null ? backoff.first : 0, delayUntilTime); long opRunTime = backoffTime > now ? backoffTime : op.earliestRunTime; if (lowestOp == null || (lowestOp.expedited == op.expedited ? opRunTime < lowestOpRunTime : op.expedited)) { lowestOp = op; lowestOpRunTime = opRunTime; } } if (lowestOp == null) { return null; } return Pair.create(lowestOp, lowestOpRunTime); } /** * Return when the next SyncOperation will be ready to run or null if there are * none. * @param now the current {@link android.os.SystemClock#elapsedRealtime()}, used to * decide if the sync operation is ready to run * @return when the next SyncOperation will be ready to run, expressed in elapsedRealtime() */ public Long nextRunTime(long now) { Pair nextOpAndRunTime = nextOperation(now); if (nextOpAndRunTime == null) { return null; } return nextOpAndRunTime.second; } /** * Find and return the SyncOperation that should be run next and is ready to run. * @param now the current {@link android.os.SystemClock#elapsedRealtime()}, used to * decide if the sync operation is ready to run * @return the SyncOperation that should be run next and is ready to run. */ public SyncOperation nextReadyToRun(long now) { Pair nextOpAndRunTime = nextOperation(now); if (nextOpAndRunTime == null || nextOpAndRunTime.second > now) { return null; } return nextOpAndRunTime.first; } public void clear(Account account, String authority) { Iterator> entries = mOperationsMap.entrySet().iterator(); while (entries.hasNext()) { Map.Entry entry = entries.next(); SyncOperation syncOperation = entry.getValue(); if (account != null && !syncOperation.account.equals(account)) continue; if (authority != null && !syncOperation.authority.equals(authority)) continue; entries.remove(); if (!mSyncStorageEngine.deleteFromPending(syncOperation.pendingOperation)) { final String errorMessage = "unable to find pending row for " + syncOperation; Log.e(TAG, errorMessage, new IllegalStateException(errorMessage)); } } } public void dump(StringBuilder sb) { sb.append("SyncQueue: ").append(mOperationsMap.size()).append(" operation(s)\n"); for (SyncOperation operation : mOperationsMap.values()) { sb.append(operation).append("\n"); } } }