1 /* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.server.content; 18 19 import android.accounts.Account; 20 import android.content.pm.PackageManager; 21 import android.content.pm.RegisteredServicesCache; 22 import android.content.SyncAdapterType; 23 import android.content.SyncAdaptersCache; 24 import android.content.pm.RegisteredServicesCache.ServiceInfo; 25 import android.os.SystemClock; 26 import android.text.format.DateUtils; 27 import android.util.Log; 28 import android.util.Pair; 29 30 import com.google.android.collect.Maps; 31 32 import java.util.ArrayList; 33 import java.util.Collection; 34 import java.util.HashMap; 35 import java.util.Iterator; 36 import java.util.Map; 37 38 /** 39 * Queue of pending sync operations. Not inherently thread safe, external 40 * callers are responsible for locking. 41 * 42 * @hide 43 */ 44 public class SyncQueue { 45 private static final String TAG = "SyncManager"; 46 private final SyncStorageEngine mSyncStorageEngine; 47 private final SyncAdaptersCache mSyncAdapters; 48 private final PackageManager mPackageManager; 49 50 // A Map of SyncOperations operationKey -> SyncOperation that is designed for 51 // quick lookup of an enqueued SyncOperation. 52 private final HashMap<String, SyncOperation> mOperationsMap = Maps.newHashMap(); 53 54 public SyncQueue(PackageManager packageManager, SyncStorageEngine syncStorageEngine, 55 final SyncAdaptersCache syncAdapters) { 56 mPackageManager = packageManager; 57 mSyncStorageEngine = syncStorageEngine; 58 mSyncAdapters = syncAdapters; 59 } 60 61 public void addPendingOperations(int userId) { 62 for (SyncStorageEngine.PendingOperation op : mSyncStorageEngine.getPendingOperations()) { 63 if (op.userId != userId) continue; 64 65 final Pair<Long, Long> backoff = mSyncStorageEngine.getBackoff( 66 op.account, op.userId, op.authority); 67 final ServiceInfo<SyncAdapterType> syncAdapterInfo = mSyncAdapters.getServiceInfo( 68 SyncAdapterType.newKey(op.authority, op.account.type), op.userId); 69 if (syncAdapterInfo == null) { 70 Log.w(TAG, "Missing sync adapter info for authority " + op.authority + ", userId " 71 + op.userId); 72 continue; 73 } 74 SyncOperation syncOperation = new SyncOperation( 75 op.account, op.userId, op.reason, op.syncSource, op.authority, op.extras, 76 op.expedited ? -1: 0 /* delay */, 0 /* flex */, backoff != null ? backoff.first : 0, 77 mSyncStorageEngine.getDelayUntilTime(op.account, op.userId, op.authority), 78 syncAdapterInfo.type.allowParallelSyncs()); 79 syncOperation.pendingOperation = op; 80 add(syncOperation, op); 81 } 82 } 83 84 public boolean add(SyncOperation operation) { 85 return add(operation, null /* this is not coming from the database */); 86 } 87 88 /** 89 * Adds a SyncOperation to the queue and creates a PendingOperation object to track that sync. 90 * If an operation is added that already exists, the existing operation is updated if the newly 91 * added operation occurs before (or the interval overlaps). 92 */ 93 private boolean add(SyncOperation operation, 94 SyncStorageEngine.PendingOperation pop) { 95 // If an operation with the same key exists and this one should run sooner/overlaps, 96 // replace the run interval of the existing operation with this new one. 97 // Complications: what if the existing operation is expedited but the new operation has an 98 // earlier run time? Will not be a problem for periodic syncs (no expedited flag), and for 99 // one-off syncs we only change it if the new sync is sooner. 100 final String operationKey = operation.key; 101 final SyncOperation existingOperation = mOperationsMap.get(operationKey); 102 103 if (existingOperation != null) { 104 boolean changed = false; 105 if (operation.compareTo(existingOperation) <= 0 ) { 106 long newRunTime = 107 Math.min(existingOperation.latestRunTime, operation.latestRunTime); 108 // Take smaller runtime. 109 existingOperation.latestRunTime = newRunTime; 110 // Take newer flextime. 111 existingOperation.flexTime = operation.flexTime; 112 changed = true; 113 } 114 return changed; 115 } 116 117 operation.pendingOperation = pop; 118 // Don't update the PendingOp if one already exists. This really is just a placeholder, 119 // no actual scheduling info is placed here. 120 // TODO: Change this to support service components. 121 if (operation.pendingOperation == null) { 122 pop = new SyncStorageEngine.PendingOperation( 123 operation.account, operation.userId, operation.reason, operation.syncSource, 124 operation.authority, operation.extras, operation.isExpedited()); 125 pop = mSyncStorageEngine.insertIntoPending(pop); 126 if (pop == null) { 127 throw new IllegalStateException("error adding pending sync operation " 128 + operation); 129 } 130 operation.pendingOperation = pop; 131 } 132 133 mOperationsMap.put(operationKey, operation); 134 return true; 135 } 136 137 public void removeUser(int userId) { 138 ArrayList<SyncOperation> opsToRemove = new ArrayList<SyncOperation>(); 139 for (SyncOperation op : mOperationsMap.values()) { 140 if (op.userId == userId) { 141 opsToRemove.add(op); 142 } 143 } 144 145 for (SyncOperation op : opsToRemove) { 146 remove(op); 147 } 148 } 149 150 /** 151 * Remove the specified operation if it is in the queue. 152 * @param operation the operation to remove 153 */ 154 public void remove(SyncOperation operation) { 155 SyncOperation operationToRemove = mOperationsMap.remove(operation.key); 156 if (operationToRemove == null) { 157 return; 158 } 159 if (!mSyncStorageEngine.deleteFromPending(operationToRemove.pendingOperation)) { 160 final String errorMessage = "unable to find pending row for " + operationToRemove; 161 Log.e(TAG, errorMessage, new IllegalStateException(errorMessage)); 162 } 163 } 164 165 public void onBackoffChanged(Account account, int userId, String providerName, long backoff) { 166 // for each op that matches the account and provider update its 167 // backoff and effectiveStartTime 168 for (SyncOperation op : mOperationsMap.values()) { 169 if (op.account.equals(account) && op.authority.equals(providerName) 170 && op.userId == userId) { 171 op.backoff = backoff; 172 op.updateEffectiveRunTime(); 173 } 174 } 175 } 176 177 public void onDelayUntilTimeChanged(Account account, String providerName, long delayUntil) { 178 // for each op that matches the account and provider update its 179 // delayUntilTime and effectiveStartTime 180 for (SyncOperation op : mOperationsMap.values()) { 181 if (op.account.equals(account) && op.authority.equals(providerName)) { 182 op.delayUntil = delayUntil; 183 op.updateEffectiveRunTime(); 184 } 185 } 186 } 187 188 public void remove(Account account, int userId, String authority) { 189 Iterator<Map.Entry<String, SyncOperation>> entries = mOperationsMap.entrySet().iterator(); 190 while (entries.hasNext()) { 191 Map.Entry<String, SyncOperation> entry = entries.next(); 192 SyncOperation syncOperation = entry.getValue(); 193 if (account != null && !syncOperation.account.equals(account)) { 194 continue; 195 } 196 if (authority != null && !syncOperation.authority.equals(authority)) { 197 continue; 198 } 199 if (userId != syncOperation.userId) { 200 continue; 201 } 202 entries.remove(); 203 if (!mSyncStorageEngine.deleteFromPending(syncOperation.pendingOperation)) { 204 final String errorMessage = "unable to find pending row for " + syncOperation; 205 Log.e(TAG, errorMessage, new IllegalStateException(errorMessage)); 206 } 207 } 208 } 209 210 public Collection<SyncOperation> getOperations() { 211 return mOperationsMap.values(); 212 } 213 214 public void dump(StringBuilder sb) { 215 final long now = SystemClock.elapsedRealtime(); 216 sb.append("SyncQueue: ").append(mOperationsMap.size()).append(" operation(s)\n"); 217 for (SyncOperation operation : mOperationsMap.values()) { 218 sb.append(" "); 219 if (operation.effectiveRunTime <= now) { 220 sb.append("READY"); 221 } else { 222 sb.append(DateUtils.formatElapsedTime((operation.effectiveRunTime - now) / 1000)); 223 } 224 sb.append(" - "); 225 sb.append(operation.dump(mPackageManager, false)).append("\n"); 226 } 227 } 228 } 229