Home | History | Annotate | Download | only in content
      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                     0 /* delay */, 0 /* flex */, backoff != null ? backoff.first : 0,
     77                     mSyncStorageEngine.getDelayUntilTime(op.account, op.userId, op.authority),
     78                     syncAdapterInfo.type.allowParallelSyncs());
     79             syncOperation.expedited = op.expedited;
     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 
     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      */
     94     private boolean add(SyncOperation operation,
     95             SyncStorageEngine.PendingOperation pop) {
     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.
    101         final String operationKey = operation.key;
    102         final SyncOperation existingOperation = mOperationsMap.get(operationKey);
    103 
    104         if (existingOperation != null) {
    105             boolean changed = false;
    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;
    115             }
    116             return changed;
    117         }
    118 
    119         operation.pendingOperation = pop;
    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.
    123         if (operation.pendingOperation == null) {
    124             pop = new SyncStorageEngine.PendingOperation(
    125                     operation.account, operation.userId, operation.reason, operation.syncSource,
    126                     operation.authority, operation.extras, operation.expedited);
    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 
    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 
    152     /**
    153      * Remove the specified operation if it is in the queue.
    154      * @param operation the operation to remove
    155      */
    156     public void remove(SyncOperation operation) {
    157         SyncOperation operationToRemove = mOperationsMap.remove(operation.key);
    158         if (operationToRemove == null) {
    159             return;
    160         }
    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 
    167     public void onBackoffChanged(Account account, int userId, String providerName, long backoff) {
    168         // for each op that matches the account and provider update its
    169         // backoff and effectiveStartTime
    170         for (SyncOperation op : mOperationsMap.values()) {
    171             if (op.account.equals(account) && op.authority.equals(providerName)
    172                     && op.userId == userId) {
    173                 op.backoff = backoff;
    174                 op.updateEffectiveRunTime();
    175             }
    176         }
    177     }
    178 
    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             }
    187         }
    188     }
    189 
    190     public void remove(Account account, int userId, String authority) {
    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();
    195             if (account != null && !syncOperation.account.equals(account)) {
    196                 continue;
    197             }
    198             if (authority != null && !syncOperation.authority.equals(authority)) {
    199                 continue;
    200             }
    201             if (userId != syncOperation.userId) {
    202                 continue;
    203             }
    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 
    212     public Collection<SyncOperation> getOperations() {
    213         return mOperationsMap.values();
    214     }
    215 
    216     public void dump(StringBuilder sb) {
    217         final long now = SystemClock.elapsedRealtime();
    218         sb.append("SyncQueue: ").append(mOperationsMap.size()).append(" operation(s)\n");
    219         for (SyncOperation operation : mOperationsMap.values()) {
    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(" - ");
    227             sb.append(operation.dump(mPackageManager, false)).append("\n");
    228         }
    229     }
    230 }
    231