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