blob: 6f3fe6e1d37bc53a641211b0fc11326ecd08351a [file] [log] [blame]
Kenny Root15a4d2f2010-03-11 18:20:12 -08001/*
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 Sharkey7a96c392012-11-15 14:01:46 -080017package com.android.server.content;
Fred Quintana307da1a2010-01-21 14:24:20 -080018
Fred Quintana307da1a2010-01-21 14:24:20 -080019import android.accounts.Account;
Alon Albert57286f92012-10-09 14:21:38 -070020import android.content.pm.PackageManager;
21import android.content.pm.RegisteredServicesCache;
Jeff Sharkey7a96c392012-11-15 14:01:46 -080022import android.content.SyncAdapterType;
23import android.content.SyncAdaptersCache;
Jeff Sharkey6ab72d72012-10-08 16:44:37 -070024import android.content.pm.RegisteredServicesCache.ServiceInfo;
25import android.os.SystemClock;
Jeff Sharkey6ab72d72012-10-08 16:44:37 -070026import android.text.format.DateUtils;
27import android.util.Log;
28import android.util.Pair;
29
30import com.google.android.collect.Maps;
Fred Quintana307da1a2010-01-21 14:24:20 -080031
Fred Quintana307da1a2010-01-21 14:24:20 -080032import java.util.ArrayList;
Jeff Sharkeya706e2f2012-10-16 12:02:42 -070033import java.util.Collection;
Fred Quintana918339a2010-10-05 14:00:39 -070034import java.util.HashMap;
Fred Quintana307da1a2010-01-21 14:24:20 -080035import java.util.Iterator;
Fred Quintana918339a2010-10-05 14:00:39 -070036import java.util.Map;
Fred Quintana307da1a2010-01-21 14:24:20 -080037
38/**
Jeff Sharkeya706e2f2012-10-16 12:02:42 -070039 * Queue of pending sync operations. Not inherently thread safe, external
40 * callers are responsible for locking.
Fred Quintana307da1a2010-01-21 14:24:20 -080041 *
42 * @hide
43 */
44public class SyncQueue {
45 private static final String TAG = "SyncManager";
Jeff Sharkey6ab72d72012-10-08 16:44:37 -070046 private final SyncStorageEngine mSyncStorageEngine;
47 private final SyncAdaptersCache mSyncAdapters;
Alon Albert57286f92012-10-09 14:21:38 -070048 private final PackageManager mPackageManager;
Fred Quintana307da1a2010-01-21 14:24:20 -080049
50 // A Map of SyncOperations operationKey -> SyncOperation that is designed for
51 // quick lookup of an enqueued SyncOperation.
Jeff Sharkeya706e2f2012-10-16 12:02:42 -070052 private final HashMap<String, SyncOperation> mOperationsMap = Maps.newHashMap();
Fred Quintana307da1a2010-01-21 14:24:20 -080053
Alon Albert57286f92012-10-09 14:21:38 -070054 public SyncQueue(PackageManager packageManager, SyncStorageEngine syncStorageEngine,
55 final SyncAdaptersCache syncAdapters) {
56 mPackageManager = packageManager;
Fred Quintana307da1a2010-01-21 14:24:20 -080057 mSyncStorageEngine = syncStorageEngine;
Jeff Sharkey6ab72d72012-10-08 16:44:37 -070058 mSyncAdapters = syncAdapters;
Jeff Sharkey6ab72d72012-10-08 16:44:37 -070059 }
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 Quintana0c4d04a2010-11-03 17:02:55 -070069 if (syncAdapterInfo == null) {
Jeff Sharkey6ab72d72012-10-08 16:44:37 -070070 Log.w(TAG, "Missing sync adapter info for authority " + op.authority + ", userId "
71 + op.userId);
Fred Quintana0c4d04a2010-11-03 17:02:55 -070072 continue;
73 }
Fred Quintana307da1a2010-01-21 14:24:20 -080074 SyncOperation syncOperation = new SyncOperation(
Alon Albert57286f92012-10-09 14:21:38 -070075 op.account, op.userId, op.reason, op.syncSource, op.authority, op.extras,
Matthew Williamsfa774182013-06-18 15:44:11 -070076 0 /* delay */, 0 /* flex */, backoff != null ? backoff.first : 0,
Jeff Sharkey6ab72d72012-10-08 16:44:37 -070077 mSyncStorageEngine.getDelayUntilTime(op.account, op.userId, op.authority),
Fred Quintana0c4d04a2010-11-03 17:02:55 -070078 syncAdapterInfo.type.allowParallelSyncs());
Fred Quintanac5d1c6d2010-01-27 12:17:49 -080079 syncOperation.expedited = op.expedited;
Fred Quintana307da1a2010-01-21 14:24:20 -080080 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 Williamsfa774182013-06-18 15:44:11 -070089 /**
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 Quintana307da1a2010-01-21 14:24:20 -080094 private boolean add(SyncOperation operation,
95 SyncStorageEngine.PendingOperation pop) {
Matthew Williamsfa774182013-06-18 15:44:11 -070096 // 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 Quintana307da1a2010-01-21 14:24:20 -0800101 final String operationKey = operation.key;
102 final SyncOperation existingOperation = mOperationsMap.get(operationKey);
103
104 if (existingOperation != null) {
105 boolean changed = false;
Matthew Williamsfa774182013-06-18 15:44:11 -0700106 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 Quintana307da1a2010-01-21 14:24:20 -0800115 }
116 return changed;
117 }
118
119 operation.pendingOperation = pop;
Matthew Williamsfa774182013-06-18 15:44:11 -0700120 // 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 Quintana307da1a2010-01-21 14:24:20 -0800123 if (operation.pendingOperation == null) {
124 pop = new SyncStorageEngine.PendingOperation(
Alon Albert57286f92012-10-09 14:21:38 -0700125 operation.account, operation.userId, operation.reason, operation.syncSource,
Amith Yamasani04e0d262012-02-14 11:50:53 -0800126 operation.authority, operation.extras, operation.expedited);
Fred Quintana307da1a2010-01-21 14:24:20 -0800127 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 Yamasani13593602012-03-22 16:16:17 -0700139 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 Quintanac5d1c6d2010-01-27 12:17:49 -0800152 /**
153 * Remove the specified operation if it is in the queue.
154 * @param operation the operation to remove
155 */
Fred Quintana307da1a2010-01-21 14:24:20 -0800156 public void remove(SyncOperation operation) {
157 SyncOperation operationToRemove = mOperationsMap.remove(operation.key);
Fred Quintanac5d1c6d2010-01-27 12:17:49 -0800158 if (operationToRemove == null) {
159 return;
160 }
Fred Quintana307da1a2010-01-21 14:24:20 -0800161 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 Yamasani04e0d262012-02-14 11:50:53 -0800167 public void onBackoffChanged(Account account, int userId, String providerName, long backoff) {
Fred Quintana918339a2010-10-05 14:00:39 -0700168 // for each op that matches the account and provider update its
169 // backoff and effectiveStartTime
Fred Quintana307da1a2010-01-21 14:24:20 -0800170 for (SyncOperation op : mOperationsMap.values()) {
Amith Yamasani04e0d262012-02-14 11:50:53 -0800171 if (op.account.equals(account) && op.authority.equals(providerName)
172 && op.userId == userId) {
Fred Quintana918339a2010-10-05 14:00:39 -0700173 op.backoff = backoff;
174 op.updateEffectiveRunTime();
Fred Quintana307da1a2010-01-21 14:24:20 -0800175 }
176 }
Fred Quintana307da1a2010-01-21 14:24:20 -0800177 }
178
Fred Quintana918339a2010-10-05 14:00:39 -0700179 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 Quintana307da1a2010-01-21 14:24:20 -0800187 }
Fred Quintana307da1a2010-01-21 14:24:20 -0800188 }
189
Amith Yamasani04e0d262012-02-14 11:50:53 -0800190 public void remove(Account account, int userId, String authority) {
Fred Quintana307da1a2010-01-21 14:24:20 -0800191 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 Quintanac5d1c6d2010-01-27 12:17:49 -0800195 if (account != null && !syncOperation.account.equals(account)) {
196 continue;
197 }
198 if (authority != null && !syncOperation.authority.equals(authority)) {
199 continue;
200 }
Amith Yamasani04e0d262012-02-14 11:50:53 -0800201 if (userId != syncOperation.userId) {
202 continue;
203 }
Fred Quintana307da1a2010-01-21 14:24:20 -0800204 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 Sharkeya706e2f2012-10-16 12:02:42 -0700212 public Collection<SyncOperation> getOperations() {
213 return mOperationsMap.values();
214 }
215
Fred Quintana307da1a2010-01-21 14:24:20 -0800216 public void dump(StringBuilder sb) {
Fred Quintana918339a2010-10-05 14:00:39 -0700217 final long now = SystemClock.elapsedRealtime();
Fred Quintana307da1a2010-01-21 14:24:20 -0800218 sb.append("SyncQueue: ").append(mOperationsMap.size()).append(" operation(s)\n");
219 for (SyncOperation operation : mOperationsMap.values()) {
Fred Quintana918339a2010-10-05 14:00:39 -0700220 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 Albert57286f92012-10-09 14:21:38 -0700227 sb.append(operation.dump(mPackageManager, false)).append("\n");
Fred Quintana307da1a2010-01-21 14:24:20 -0800228 }
229 }
230}