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