Kenny Root | 15a4d2f | 2010-03-11 18:20:12 -0800 | [diff] [blame] | 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 | |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 17 | package com.android.server.content; |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 18 | |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 19 | import android.content.pm.PackageManager; |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 20 | import android.content.SyncAdapterType; |
| 21 | import android.content.SyncAdaptersCache; |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 22 | import android.content.pm.RegisteredServicesCache.ServiceInfo; |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 23 | import android.os.Bundle; |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 24 | import android.os.SystemClock; |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 25 | import android.text.format.DateUtils; |
| 26 | import android.util.Log; |
| 27 | import android.util.Pair; |
| 28 | |
| 29 | import com.google.android.collect.Maps; |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 30 | |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 31 | import java.util.ArrayList; |
Jeff Sharkey | a706e2f | 2012-10-16 12:02:42 -0700 | [diff] [blame] | 32 | import java.util.Collection; |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 33 | import java.util.HashMap; |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 34 | import java.util.Iterator; |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 35 | import java.util.Map; |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 36 | |
| 37 | /** |
Jeff Sharkey | a706e2f | 2012-10-16 12:02:42 -0700 | [diff] [blame] | 38 | * Queue of pending sync operations. Not inherently thread safe, external |
| 39 | * callers are responsible for locking. |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 40 | * |
| 41 | * @hide |
| 42 | */ |
| 43 | public class SyncQueue { |
| 44 | private static final String TAG = "SyncManager"; |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 45 | private final SyncStorageEngine mSyncStorageEngine; |
| 46 | private final SyncAdaptersCache mSyncAdapters; |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 47 | private final PackageManager mPackageManager; |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 48 | |
| 49 | // A Map of SyncOperations operationKey -> SyncOperation that is designed for |
| 50 | // quick lookup of an enqueued SyncOperation. |
Jeff Sharkey | a706e2f | 2012-10-16 12:02:42 -0700 | [diff] [blame] | 51 | private final HashMap<String, SyncOperation> mOperationsMap = Maps.newHashMap(); |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 52 | |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 53 | public SyncQueue(PackageManager packageManager, SyncStorageEngine syncStorageEngine, |
| 54 | final SyncAdaptersCache syncAdapters) { |
| 55 | mPackageManager = packageManager; |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 56 | mSyncStorageEngine = syncStorageEngine; |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 57 | mSyncAdapters = syncAdapters; |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | public void addPendingOperations(int userId) { |
| 61 | for (SyncStorageEngine.PendingOperation op : mSyncStorageEngine.getPendingOperations()) { |
Matthew Williams | 8ef2204 | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 62 | final SyncStorageEngine.EndPoint info = op.target; |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 63 | if (info.userId != userId) continue; |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 64 | |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 65 | final Pair<Long, Long> backoff = mSyncStorageEngine.getBackoff(info); |
| 66 | SyncOperation operationToAdd; |
| 67 | if (info.target_provider) { |
| 68 | final ServiceInfo<SyncAdapterType> syncAdapterInfo = mSyncAdapters.getServiceInfo( |
| 69 | SyncAdapterType.newKey(info.provider, info.account.type), info.userId); |
| 70 | if (syncAdapterInfo == null) { |
| 71 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
Matthew Williams | 8ef2204 | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 72 | Log.v(TAG, "Missing sync adapter info for authority " + op.target); |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 73 | } |
| 74 | continue; |
| 75 | } |
| 76 | operationToAdd = new SyncOperation( |
| 77 | info.account, info.userId, op.reason, op.syncSource, info.provider, |
| 78 | op.extras, |
| 79 | 0 /* delay */, |
| 80 | 0 /* flex */, |
| 81 | backoff != null ? backoff.first : 0, |
| 82 | mSyncStorageEngine.getDelayUntilTime(info), |
| 83 | syncAdapterInfo.type.allowParallelSyncs()); |
| 84 | operationToAdd.expedited = op.expedited; |
| 85 | operationToAdd.pendingOperation = op; |
| 86 | add(operationToAdd, op); |
| 87 | } else if (info.target_service) { |
| 88 | try { |
| 89 | mPackageManager.getServiceInfo(info.service, 0); |
| 90 | } catch (PackageManager.NameNotFoundException e) { |
| 91 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
Matthew Williams | 8ef2204 | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 92 | Log.w(TAG, "Missing sync service for authority " + op.target); |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 93 | } |
| 94 | continue; |
| 95 | } |
| 96 | operationToAdd = new SyncOperation( |
| 97 | info.service, info.userId, op.reason, op.syncSource, |
| 98 | op.extras, |
| 99 | 0 /* delay */, |
| 100 | 0 /* flex */, |
| 101 | backoff != null ? backoff.first : 0, |
| 102 | mSyncStorageEngine.getDelayUntilTime(info)); |
| 103 | operationToAdd.expedited = op.expedited; |
| 104 | operationToAdd.pendingOperation = op; |
| 105 | add(operationToAdd, op); |
Fred Quintana | 0c4d04a | 2010-11-03 17:02:55 -0700 | [diff] [blame] | 106 | } |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | |
| 110 | public boolean add(SyncOperation operation) { |
| 111 | return add(operation, null /* this is not coming from the database */); |
| 112 | } |
| 113 | |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 114 | /** |
| 115 | * Adds a SyncOperation to the queue and creates a PendingOperation object to track that sync. |
| 116 | * If an operation is added that already exists, the existing operation is updated if the newly |
| 117 | * added operation occurs before (or the interval overlaps). |
| 118 | */ |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 119 | private boolean add(SyncOperation operation, |
| 120 | SyncStorageEngine.PendingOperation pop) { |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 121 | // If an operation with the same key exists and this one should run sooner/overlaps, |
| 122 | // replace the run interval of the existing operation with this new one. |
| 123 | // Complications: what if the existing operation is expedited but the new operation has an |
| 124 | // earlier run time? Will not be a problem for periodic syncs (no expedited flag), and for |
| 125 | // one-off syncs we only change it if the new sync is sooner. |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 126 | final String operationKey = operation.key; |
| 127 | final SyncOperation existingOperation = mOperationsMap.get(operationKey); |
| 128 | |
| 129 | if (existingOperation != null) { |
| 130 | boolean changed = false; |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 131 | if (operation.compareTo(existingOperation) <= 0 ) { |
| 132 | existingOperation.expedited = operation.expedited; |
| 133 | long newRunTime = |
| 134 | Math.min(existingOperation.latestRunTime, operation.latestRunTime); |
| 135 | // Take smaller runtime. |
| 136 | existingOperation.latestRunTime = newRunTime; |
| 137 | // Take newer flextime. |
| 138 | existingOperation.flexTime = operation.flexTime; |
| 139 | changed = true; |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 140 | } |
| 141 | return changed; |
| 142 | } |
| 143 | |
| 144 | operation.pendingOperation = pop; |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 145 | // Don't update the PendingOp if one already exists. This really is just a placeholder, |
| 146 | // no actual scheduling info is placed here. |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 147 | if (operation.pendingOperation == null) { |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 148 | pop = mSyncStorageEngine.insertIntoPending(operation); |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 149 | if (pop == null) { |
| 150 | throw new IllegalStateException("error adding pending sync operation " |
| 151 | + operation); |
| 152 | } |
| 153 | operation.pendingOperation = pop; |
| 154 | } |
| 155 | |
| 156 | mOperationsMap.put(operationKey, operation); |
| 157 | return true; |
| 158 | } |
| 159 | |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 160 | public void removeUserLocked(int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 161 | ArrayList<SyncOperation> opsToRemove = new ArrayList<SyncOperation>(); |
| 162 | for (SyncOperation op : mOperationsMap.values()) { |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 163 | if (op.target.userId == userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 164 | opsToRemove.add(op); |
| 165 | } |
| 166 | } |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 167 | for (SyncOperation op : opsToRemove) { |
| 168 | remove(op); |
| 169 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 170 | } |
| 171 | |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 172 | /** |
| 173 | * Remove the specified operation if it is in the queue. |
| 174 | * @param operation the operation to remove |
| 175 | */ |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 176 | public void remove(SyncOperation operation) { |
Matthew Williams | 8ef2204 | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 177 | boolean isLoggable = Log.isLoggable(TAG, Log.VERBOSE); |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 178 | SyncOperation operationToRemove = mOperationsMap.remove(operation.key); |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 179 | if (isLoggable) { |
Matthew Williams | 8ef2204 | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 180 | Log.v(TAG, "Attempting to remove: " + operation.key); |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 181 | } |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 182 | if (operationToRemove == null) { |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 183 | if (isLoggable) { |
Matthew Williams | 8ef2204 | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 184 | Log.v(TAG, "Could not find: " + operation.key); |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 185 | } |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 186 | return; |
| 187 | } |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 188 | if (!mSyncStorageEngine.deleteFromPending(operationToRemove.pendingOperation)) { |
| 189 | final String errorMessage = "unable to find pending row for " + operationToRemove; |
| 190 | Log.e(TAG, errorMessage, new IllegalStateException(errorMessage)); |
| 191 | } |
| 192 | } |
| 193 | |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 194 | /** Reset backoffs for all operations in the queue. */ |
| 195 | public void clearBackoffs() { |
| 196 | for (SyncOperation op : mOperationsMap.values()) { |
| 197 | op.backoff = 0L; |
| 198 | op.updateEffectiveRunTime(); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | public void onBackoffChanged(SyncStorageEngine.EndPoint target, long backoff) { |
Matthew Williams | 8ef2204 | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 203 | // For each op that matches the target of the changed op, update its |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 204 | // backoff and effectiveStartTime |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 205 | for (SyncOperation op : mOperationsMap.values()) { |
Matthew Williams | 8ef2204 | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 206 | if (op.target.matchesSpec(target)) { |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 207 | op.backoff = backoff; |
| 208 | op.updateEffectiveRunTime(); |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 209 | } |
| 210 | } |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 211 | } |
| 212 | |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 213 | public void onDelayUntilTimeChanged(SyncStorageEngine.EndPoint target, long delayUntil) { |
Matthew Williams | 8ef2204 | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 214 | // for each op that matches the target info of the provided op, change the delay time. |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 215 | for (SyncOperation op : mOperationsMap.values()) { |
Matthew Williams | 8ef2204 | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 216 | if (op.target.matchesSpec(target)) { |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 217 | op.delayUntil = delayUntil; |
| 218 | op.updateEffectiveRunTime(); |
| 219 | } |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 220 | } |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 221 | } |
| 222 | |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 223 | /** |
| 224 | * Remove all of the SyncOperations associated with a given target. |
| 225 | * |
| 226 | * @param info target object provided here can have null Account/provider. This is the case |
| 227 | * where you want to remove all ops associated with a provider (null Account) or all ops |
| 228 | * associated with an account (null provider). |
| 229 | * @param extras option bundle to include to further specify which operation to remove. If this |
| 230 | * bundle contains sync settings flags, they are ignored. |
| 231 | */ |
| 232 | public void remove(final SyncStorageEngine.EndPoint info, Bundle extras) { |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 233 | Iterator<Map.Entry<String, SyncOperation>> entries = mOperationsMap.entrySet().iterator(); |
| 234 | while (entries.hasNext()) { |
| 235 | Map.Entry<String, SyncOperation> entry = entries.next(); |
| 236 | SyncOperation syncOperation = entry.getValue(); |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 237 | final SyncStorageEngine.EndPoint opInfo = syncOperation.target; |
Matthew Williams | 8ef2204 | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 238 | if (!opInfo.matchesSpec(info)) { |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 239 | continue; |
| 240 | } |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 241 | if (extras != null |
| 242 | && !SyncManager.syncExtrasEquals( |
| 243 | syncOperation.extras, |
| 244 | extras, |
| 245 | false /* no config flags*/)) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 246 | continue; |
| 247 | } |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 248 | entries.remove(); |
| 249 | if (!mSyncStorageEngine.deleteFromPending(syncOperation.pendingOperation)) { |
| 250 | final String errorMessage = "unable to find pending row for " + syncOperation; |
| 251 | Log.e(TAG, errorMessage, new IllegalStateException(errorMessage)); |
| 252 | } |
| 253 | } |
| 254 | } |
| 255 | |
Jeff Sharkey | a706e2f | 2012-10-16 12:02:42 -0700 | [diff] [blame] | 256 | public Collection<SyncOperation> getOperations() { |
| 257 | return mOperationsMap.values(); |
| 258 | } |
| 259 | |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 260 | public void dump(StringBuilder sb) { |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 261 | final long now = SystemClock.elapsedRealtime(); |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 262 | sb.append("SyncQueue: ").append(mOperationsMap.size()).append(" operation(s)\n"); |
| 263 | for (SyncOperation operation : mOperationsMap.values()) { |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 264 | sb.append(" "); |
| 265 | if (operation.effectiveRunTime <= now) { |
| 266 | sb.append("READY"); |
| 267 | } else { |
| 268 | sb.append(DateUtils.formatElapsedTime((operation.effectiveRunTime - now) / 1000)); |
| 269 | } |
| 270 | sb.append(" - "); |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 271 | sb.append(operation.dump(mPackageManager, false)).append("\n"); |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 272 | } |
| 273 | } |
| 274 | } |