Home | History | Annotate | Download | only in content
      1 /*
      2  * Copyright (C) 2009 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 android.content;
     18 
     19 import com.android.internal.os.AtomicFile;
     20 import com.android.internal.util.ArrayUtils;
     21 import com.android.internal.util.FastXmlSerializer;
     22 
     23 import org.xmlpull.v1.XmlPullParser;
     24 import org.xmlpull.v1.XmlPullParserException;
     25 import org.xmlpull.v1.XmlSerializer;
     26 
     27 import android.accounts.Account;
     28 import android.database.Cursor;
     29 import android.database.sqlite.SQLiteDatabase;
     30 import android.database.sqlite.SQLiteException;
     31 import android.database.sqlite.SQLiteQueryBuilder;
     32 import android.os.Bundle;
     33 import android.os.Environment;
     34 import android.os.Handler;
     35 import android.os.Message;
     36 import android.os.Parcel;
     37 import android.os.RemoteCallbackList;
     38 import android.os.RemoteException;
     39 import android.util.Log;
     40 import android.util.SparseArray;
     41 import android.util.Xml;
     42 import android.util.Pair;
     43 
     44 import java.io.File;
     45 import java.io.FileInputStream;
     46 import java.io.FileOutputStream;
     47 import java.util.ArrayList;
     48 import java.util.Calendar;
     49 import java.util.HashMap;
     50 import java.util.Iterator;
     51 import java.util.TimeZone;
     52 import java.util.List;
     53 
     54 /**
     55  * Singleton that tracks the sync data and overall sync
     56  * history on the device.
     57  *
     58  * @hide
     59  */
     60 public class SyncStorageEngine extends Handler {
     61     private static final String TAG = "SyncManager";
     62     private static final boolean DEBUG_FILE = false;
     63 
     64     private static final long DEFAULT_POLL_FREQUENCY_SECONDS = 60 * 60 * 24; // One day
     65 
     66     // @VisibleForTesting
     67     static final long MILLIS_IN_4WEEKS = 1000L * 60 * 60 * 24 * 7 * 4;
     68 
     69     /** Enum value for a sync start event. */
     70     public static final int EVENT_START = 0;
     71 
     72     /** Enum value for a sync stop event. */
     73     public static final int EVENT_STOP = 1;
     74 
     75     // TODO: i18n -- grab these out of resources.
     76     /** String names for the sync event types. */
     77     public static final String[] EVENTS = { "START", "STOP" };
     78 
     79     /** Enum value for a server-initiated sync. */
     80     public static final int SOURCE_SERVER = 0;
     81 
     82     /** Enum value for a local-initiated sync. */
     83     public static final int SOURCE_LOCAL = 1;
     84     /**
     85      * Enum value for a poll-based sync (e.g., upon connection to
     86      * network)
     87      */
     88     public static final int SOURCE_POLL = 2;
     89 
     90     /** Enum value for a user-initiated sync. */
     91     public static final int SOURCE_USER = 3;
     92 
     93     /** Enum value for a periodic sync. */
     94     public static final int SOURCE_PERIODIC = 4;
     95 
     96     public static final long NOT_IN_BACKOFF_MODE = -1;
     97 
     98     public static final Intent SYNC_CONNECTION_SETTING_CHANGED_INTENT =
     99             new Intent("com.android.sync.SYNC_CONN_STATUS_CHANGED");
    100 
    101     // TODO: i18n -- grab these out of resources.
    102     /** String names for the sync source types. */
    103     public static final String[] SOURCES = { "SERVER",
    104                                              "LOCAL",
    105                                              "POLL",
    106                                              "USER",
    107                                              "PERIODIC" };
    108 
    109     // The MESG column will contain one of these or one of the Error types.
    110     public static final String MESG_SUCCESS = "success";
    111     public static final String MESG_CANCELED = "canceled";
    112 
    113     public static final int MAX_HISTORY = 100;
    114 
    115     private static final int MSG_WRITE_STATUS = 1;
    116     private static final long WRITE_STATUS_DELAY = 1000*60*10; // 10 minutes
    117 
    118     private static final int MSG_WRITE_STATISTICS = 2;
    119     private static final long WRITE_STATISTICS_DELAY = 1000*60*30; // 1/2 hour
    120 
    121     private static final boolean SYNC_ENABLED_DEFAULT = false;
    122 
    123     // the version of the accounts xml file format
    124     private static final int ACCOUNTS_VERSION = 2;
    125 
    126     private static HashMap<String, String> sAuthorityRenames;
    127 
    128     static {
    129         sAuthorityRenames = new HashMap<String, String>();
    130         sAuthorityRenames.put("contacts", "com.android.contacts");
    131         sAuthorityRenames.put("calendar", "com.android.calendar");
    132     }
    133 
    134     public static class PendingOperation {
    135         final Account account;
    136         final int syncSource;
    137         final String authority;
    138         final Bundle extras;        // note: read-only.
    139         final boolean expedited;
    140 
    141         int authorityId;
    142         byte[] flatExtras;
    143 
    144         PendingOperation(Account account, int source,
    145                 String authority, Bundle extras, boolean expedited) {
    146             this.account = account;
    147             this.syncSource = source;
    148             this.authority = authority;
    149             this.extras = extras != null ? new Bundle(extras) : extras;
    150             this.expedited = expedited;
    151             this.authorityId = -1;
    152         }
    153 
    154         PendingOperation(PendingOperation other) {
    155             this.account = other.account;
    156             this.syncSource = other.syncSource;
    157             this.authority = other.authority;
    158             this.extras = other.extras;
    159             this.authorityId = other.authorityId;
    160             this.expedited = other.expedited;
    161         }
    162     }
    163 
    164     static class AccountInfo {
    165         final Account account;
    166         final HashMap<String, AuthorityInfo> authorities =
    167                 new HashMap<String, AuthorityInfo>();
    168 
    169         AccountInfo(Account account) {
    170             this.account = account;
    171         }
    172     }
    173 
    174     public static class AuthorityInfo {
    175         final Account account;
    176         final String authority;
    177         final int ident;
    178         boolean enabled;
    179         int syncable;
    180         long backoffTime;
    181         long backoffDelay;
    182         long delayUntil;
    183         final ArrayList<Pair<Bundle, Long>> periodicSyncs;
    184 
    185         AuthorityInfo(Account account, String authority, int ident) {
    186             this.account = account;
    187             this.authority = authority;
    188             this.ident = ident;
    189             enabled = SYNC_ENABLED_DEFAULT;
    190             syncable = -1; // default to "unknown"
    191             backoffTime = -1; // if < 0 then we aren't in backoff mode
    192             backoffDelay = -1; // if < 0 then we aren't in backoff mode
    193             periodicSyncs = new ArrayList<Pair<Bundle, Long>>();
    194             periodicSyncs.add(Pair.create(new Bundle(), DEFAULT_POLL_FREQUENCY_SECONDS));
    195         }
    196     }
    197 
    198     public static class SyncHistoryItem {
    199         int authorityId;
    200         int historyId;
    201         long eventTime;
    202         long elapsedTime;
    203         int source;
    204         int event;
    205         long upstreamActivity;
    206         long downstreamActivity;
    207         String mesg;
    208     }
    209 
    210     public static class DayStats {
    211         public final int day;
    212         public int successCount;
    213         public long successTime;
    214         public int failureCount;
    215         public long failureTime;
    216 
    217         public DayStats(int day) {
    218             this.day = day;
    219         }
    220     }
    221 
    222     // Primary list of all syncable authorities.  Also our global lock.
    223     private final SparseArray<AuthorityInfo> mAuthorities =
    224             new SparseArray<AuthorityInfo>();
    225 
    226     private final HashMap<Account, AccountInfo> mAccounts =
    227         new HashMap<Account, AccountInfo>();
    228 
    229     private final ArrayList<PendingOperation> mPendingOperations =
    230             new ArrayList<PendingOperation>();
    231 
    232     private SyncInfo mCurrentSync;
    233 
    234     private final SparseArray<SyncStatusInfo> mSyncStatus =
    235             new SparseArray<SyncStatusInfo>();
    236 
    237     private final ArrayList<SyncHistoryItem> mSyncHistory =
    238             new ArrayList<SyncHistoryItem>();
    239 
    240     private final RemoteCallbackList<ISyncStatusObserver> mChangeListeners
    241             = new RemoteCallbackList<ISyncStatusObserver>();
    242 
    243     private int mNextAuthorityId = 0;
    244 
    245     // We keep 4 weeks of stats.
    246     private final DayStats[] mDayStats = new DayStats[7*4];
    247     private final Calendar mCal;
    248     private int mYear;
    249     private int mYearInDays;
    250 
    251     private final Context mContext;
    252 
    253     private static volatile SyncStorageEngine sSyncStorageEngine = null;
    254 
    255     /**
    256      * This file contains the core engine state: all accounts and the
    257      * settings for them.  It must never be lost, and should be changed
    258      * infrequently, so it is stored as an XML file.
    259      */
    260     private final AtomicFile mAccountInfoFile;
    261 
    262     /**
    263      * This file contains the current sync status.  We would like to retain
    264      * it across boots, but its loss is not the end of the world, so we store
    265      * this information as binary data.
    266      */
    267     private final AtomicFile mStatusFile;
    268 
    269     /**
    270      * This file contains sync statistics.  This is purely debugging information
    271      * so is written infrequently and can be thrown away at any time.
    272      */
    273     private final AtomicFile mStatisticsFile;
    274 
    275     /**
    276      * This file contains the pending sync operations.  It is a binary file,
    277      * which must be updated every time an operation is added or removed,
    278      * so we have special handling of it.
    279      */
    280     private final AtomicFile mPendingFile;
    281     private static final int PENDING_FINISH_TO_WRITE = 4;
    282     private int mNumPendingFinished = 0;
    283 
    284     private int mNextHistoryId = 0;
    285     private boolean mMasterSyncAutomatically = true;
    286 
    287     private SyncStorageEngine(Context context, File dataDir) {
    288         mContext = context;
    289         sSyncStorageEngine = this;
    290 
    291         mCal = Calendar.getInstance(TimeZone.getTimeZone("GMT+0"));
    292 
    293         File systemDir = new File(dataDir, "system");
    294         File syncDir = new File(systemDir, "sync");
    295         syncDir.mkdirs();
    296         mAccountInfoFile = new AtomicFile(new File(syncDir, "accounts.xml"));
    297         mStatusFile = new AtomicFile(new File(syncDir, "status.bin"));
    298         mPendingFile = new AtomicFile(new File(syncDir, "pending.bin"));
    299         mStatisticsFile = new AtomicFile(new File(syncDir, "stats.bin"));
    300 
    301         readAccountInfoLocked();
    302         readStatusLocked();
    303         readPendingOperationsLocked();
    304         readStatisticsLocked();
    305         readAndDeleteLegacyAccountInfoLocked();
    306         writeAccountInfoLocked();
    307         writeStatusLocked();
    308         writePendingOperationsLocked();
    309         writeStatisticsLocked();
    310     }
    311 
    312     public static SyncStorageEngine newTestInstance(Context context) {
    313         return new SyncStorageEngine(context, context.getFilesDir());
    314     }
    315 
    316     public static void init(Context context) {
    317         if (sSyncStorageEngine != null) {
    318             return;
    319         }
    320         // This call will return the correct directory whether Encrypted File Systems is
    321         // enabled or not.
    322         File dataDir = Environment.getSecureDataDirectory();
    323         sSyncStorageEngine = new SyncStorageEngine(context, dataDir);
    324     }
    325 
    326     public static SyncStorageEngine getSingleton() {
    327         if (sSyncStorageEngine == null) {
    328             throw new IllegalStateException("not initialized");
    329         }
    330         return sSyncStorageEngine;
    331     }
    332 
    333     @Override public void handleMessage(Message msg) {
    334         if (msg.what == MSG_WRITE_STATUS) {
    335             synchronized (mAuthorities) {
    336                 writeStatusLocked();
    337             }
    338         } else if (msg.what == MSG_WRITE_STATISTICS) {
    339             synchronized (mAuthorities) {
    340                 writeStatisticsLocked();
    341             }
    342         }
    343     }
    344 
    345     public void addStatusChangeListener(int mask, ISyncStatusObserver callback) {
    346         synchronized (mAuthorities) {
    347             mChangeListeners.register(callback, mask);
    348         }
    349     }
    350 
    351     public void removeStatusChangeListener(ISyncStatusObserver callback) {
    352         synchronized (mAuthorities) {
    353             mChangeListeners.unregister(callback);
    354         }
    355     }
    356 
    357     private void reportChange(int which) {
    358         ArrayList<ISyncStatusObserver> reports = null;
    359         synchronized (mAuthorities) {
    360             int i = mChangeListeners.beginBroadcast();
    361             while (i > 0) {
    362                 i--;
    363                 Integer mask = (Integer)mChangeListeners.getBroadcastCookie(i);
    364                 if ((which & mask.intValue()) == 0) {
    365                     continue;
    366                 }
    367                 if (reports == null) {
    368                     reports = new ArrayList<ISyncStatusObserver>(i);
    369                 }
    370                 reports.add(mChangeListeners.getBroadcastItem(i));
    371             }
    372             mChangeListeners.finishBroadcast();
    373         }
    374 
    375         if (Log.isLoggable(TAG, Log.VERBOSE)) {
    376             Log.v(TAG, "reportChange " + which + " to: " + reports);
    377         }
    378 
    379         if (reports != null) {
    380             int i = reports.size();
    381             while (i > 0) {
    382                 i--;
    383                 try {
    384                     reports.get(i).onStatusChanged(which);
    385                 } catch (RemoteException e) {
    386                     // The remote callback list will take care of this for us.
    387                 }
    388             }
    389         }
    390     }
    391 
    392     public boolean getSyncAutomatically(Account account, String providerName) {
    393         synchronized (mAuthorities) {
    394             if (account != null) {
    395                 AuthorityInfo authority = getAuthorityLocked(account, providerName,
    396                         "getSyncAutomatically");
    397                 return authority != null && authority.enabled;
    398             }
    399 
    400             int i = mAuthorities.size();
    401             while (i > 0) {
    402                 i--;
    403                 AuthorityInfo authority = mAuthorities.valueAt(i);
    404                 if (authority.authority.equals(providerName)
    405                         && authority.enabled) {
    406                     return true;
    407                 }
    408             }
    409             return false;
    410         }
    411     }
    412 
    413     public void setSyncAutomatically(Account account, String providerName, boolean sync) {
    414         Log.d(TAG, "setSyncAutomatically: " + /*account +*/ ", provider " + providerName
    415                 + " -> " + sync);
    416         synchronized (mAuthorities) {
    417             AuthorityInfo authority = getOrCreateAuthorityLocked(account, providerName, -1, false);
    418             if (authority.enabled == sync) {
    419                 Log.d(TAG, "setSyncAutomatically: already set to " + sync + ", doing nothing");
    420                 return;
    421             }
    422             authority.enabled = sync;
    423             writeAccountInfoLocked();
    424         }
    425 
    426         if (sync) {
    427             ContentResolver.requestSync(account, providerName, new Bundle());
    428         }
    429         reportChange(ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS);
    430     }
    431 
    432     public int getIsSyncable(Account account, String providerName) {
    433         synchronized (mAuthorities) {
    434             if (account != null) {
    435                 AuthorityInfo authority = getAuthorityLocked(account, providerName,
    436                         "getIsSyncable");
    437                 if (authority == null) {
    438                     return -1;
    439                 }
    440                 return authority.syncable;
    441             }
    442 
    443             int i = mAuthorities.size();
    444             while (i > 0) {
    445                 i--;
    446                 AuthorityInfo authority = mAuthorities.valueAt(i);
    447                 if (authority.authority.equals(providerName)) {
    448                     return authority.syncable;
    449                 }
    450             }
    451             return -1;
    452         }
    453     }
    454 
    455     public void setIsSyncable(Account account, String providerName, int syncable) {
    456         if (syncable > 1) {
    457             syncable = 1;
    458         } else if (syncable < -1) {
    459             syncable = -1;
    460         }
    461         Log.d(TAG, "setIsSyncable: " + account + ", provider " + providerName + " -> " + syncable);
    462         synchronized (mAuthorities) {
    463             AuthorityInfo authority = getOrCreateAuthorityLocked(account, providerName, -1, false);
    464             if (authority.syncable == syncable) {
    465                 Log.d(TAG, "setIsSyncable: already set to " + syncable + ", doing nothing");
    466                 return;
    467             }
    468             authority.syncable = syncable;
    469             writeAccountInfoLocked();
    470         }
    471 
    472         if (syncable > 0) {
    473             ContentResolver.requestSync(account, providerName, new Bundle());
    474         }
    475         reportChange(ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS);
    476     }
    477 
    478     public Pair<Long, Long> getBackoff(Account account, String providerName) {
    479         synchronized (mAuthorities) {
    480             AuthorityInfo authority = getAuthorityLocked(account, providerName, "getBackoff");
    481             if (authority == null || authority.backoffTime < 0) {
    482                 return null;
    483             }
    484             return Pair.create(authority.backoffTime, authority.backoffDelay);
    485         }
    486     }
    487 
    488     public void setBackoff(Account account, String providerName,
    489             long nextSyncTime, long nextDelay) {
    490         if (Log.isLoggable(TAG, Log.VERBOSE)) {
    491             Log.v(TAG, "setBackoff: " + account + ", provider " + providerName
    492                     + " -> nextSyncTime " + nextSyncTime + ", nextDelay " + nextDelay);
    493         }
    494         boolean changed = false;
    495         synchronized (mAuthorities) {
    496             if (account == null || providerName == null) {
    497                 for (AccountInfo accountInfo : mAccounts.values()) {
    498                     if (account != null && !account.equals(accountInfo.account)) continue;
    499                     for (AuthorityInfo authorityInfo : accountInfo.authorities.values()) {
    500                         if (providerName != null && !providerName.equals(authorityInfo.authority)) {
    501                             continue;
    502                         }
    503                         if (authorityInfo.backoffTime != nextSyncTime
    504                                 || authorityInfo.backoffDelay != nextDelay) {
    505                             authorityInfo.backoffTime = nextSyncTime;
    506                             authorityInfo.backoffDelay = nextDelay;
    507                             changed = true;
    508                         }
    509                     }
    510                 }
    511             } else {
    512                 AuthorityInfo authority =
    513                         getOrCreateAuthorityLocked(account, providerName, -1 /* ident */, true);
    514                 if (authority.backoffTime == nextSyncTime && authority.backoffDelay == nextDelay) {
    515                     return;
    516                 }
    517                 authority.backoffTime = nextSyncTime;
    518                 authority.backoffDelay = nextDelay;
    519                 changed = true;
    520             }
    521         }
    522 
    523         if (changed) {
    524             reportChange(ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS);
    525         }
    526     }
    527 
    528     public void clearAllBackoffs() {
    529         boolean changed = false;
    530         synchronized (mAuthorities) {
    531             for (AccountInfo accountInfo : mAccounts.values()) {
    532                 for (AuthorityInfo authorityInfo : accountInfo.authorities.values()) {
    533                     if (authorityInfo.backoffTime != NOT_IN_BACKOFF_MODE
    534                             || authorityInfo.backoffDelay != NOT_IN_BACKOFF_MODE) {
    535                         if (Log.isLoggable(TAG, Log.VERBOSE)) {
    536                             Log.v(TAG, "clearAllBackoffs:"
    537                                     + " authority:" + authorityInfo.authority
    538                                     + " account:" + accountInfo.account.name
    539                                     + " backoffTime was: " + authorityInfo.backoffTime
    540                                     + " backoffDelay was: " + authorityInfo.backoffDelay);
    541                         }
    542                         authorityInfo.backoffTime = NOT_IN_BACKOFF_MODE;
    543                         authorityInfo.backoffDelay = NOT_IN_BACKOFF_MODE;
    544                         changed = true;
    545                     }
    546                 }
    547             }
    548         }
    549 
    550         if (changed) {
    551             reportChange(ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS);
    552         }
    553     }
    554     public void setDelayUntilTime(Account account, String providerName, long delayUntil) {
    555         if (Log.isLoggable(TAG, Log.VERBOSE)) {
    556             Log.v(TAG, "setDelayUntil: " + account + ", provider " + providerName
    557                     + " -> delayUntil " + delayUntil);
    558         }
    559         synchronized (mAuthorities) {
    560             AuthorityInfo authority = getOrCreateAuthorityLocked(
    561                     account, providerName, -1 /* ident */, true);
    562             if (authority.delayUntil == delayUntil) {
    563                 return;
    564             }
    565             authority.delayUntil = delayUntil;
    566         }
    567 
    568         reportChange(ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS);
    569     }
    570 
    571     public long getDelayUntilTime(Account account, String providerName) {
    572         synchronized (mAuthorities) {
    573             AuthorityInfo authority = getAuthorityLocked(account, providerName, "getDelayUntil");
    574             if (authority == null) {
    575                 return 0;
    576             }
    577             return authority.delayUntil;
    578         }
    579     }
    580 
    581     private void updateOrRemovePeriodicSync(Account account, String providerName, Bundle extras,
    582             long period, boolean add) {
    583         if (period <= 0) {
    584             period = 0;
    585         }
    586         if (extras == null) {
    587             extras = new Bundle();
    588         }
    589         if (Log.isLoggable(TAG, Log.VERBOSE)) {
    590             Log.v(TAG, "addOrRemovePeriodicSync: " + account + ", provider " + providerName
    591                     + " -> period " + period + ", extras " + extras);
    592         }
    593         synchronized (mAuthorities) {
    594             try {
    595                 AuthorityInfo authority =
    596                         getOrCreateAuthorityLocked(account, providerName, -1, false);
    597                 if (add) {
    598                     // add this periodic sync if one with the same extras doesn't already
    599                     // exist in the periodicSyncs array
    600                     boolean alreadyPresent = false;
    601                     for (int i = 0, N = authority.periodicSyncs.size(); i < N; i++) {
    602                         Pair<Bundle, Long> syncInfo = authority.periodicSyncs.get(i);
    603                         final Bundle existingExtras = syncInfo.first;
    604                         if (equals(existingExtras, extras)) {
    605                             if (syncInfo.second == period) {
    606                                 return;
    607                             }
    608                             authority.periodicSyncs.set(i, Pair.create(extras, period));
    609                             alreadyPresent = true;
    610                             break;
    611                         }
    612                     }
    613                     // if we added an entry to the periodicSyncs array also add an entry to
    614                     // the periodic syncs status to correspond to it
    615                     if (!alreadyPresent) {
    616                         authority.periodicSyncs.add(Pair.create(extras, period));
    617                         SyncStatusInfo status = getOrCreateSyncStatusLocked(authority.ident);
    618                         status.setPeriodicSyncTime(authority.periodicSyncs.size() - 1, 0);
    619                     }
    620                 } else {
    621                     // remove any periodic syncs that match the authority and extras
    622                     SyncStatusInfo status = mSyncStatus.get(authority.ident);
    623                     boolean changed = false;
    624                     Iterator<Pair<Bundle, Long>> iterator = authority.periodicSyncs.iterator();
    625                     int i = 0;
    626                     while (iterator.hasNext()) {
    627                         Pair<Bundle, Long> syncInfo = iterator.next();
    628                         if (equals(syncInfo.first, extras)) {
    629                             iterator.remove();
    630                             changed = true;
    631                             // if we removed an entry from the periodicSyncs array also
    632                             // remove the corresponding entry from the status
    633                             if (status != null) {
    634                                 status.removePeriodicSyncTime(i);
    635                             }
    636                         } else {
    637                             i++;
    638                         }
    639                     }
    640                     if (!changed) {
    641                         return;
    642                     }
    643                 }
    644             } finally {
    645                 writeAccountInfoLocked();
    646                 writeStatusLocked();
    647             }
    648         }
    649 
    650         reportChange(ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS);
    651     }
    652 
    653     public void addPeriodicSync(Account account, String providerName, Bundle extras,
    654             long pollFrequency) {
    655         updateOrRemovePeriodicSync(account, providerName, extras, pollFrequency, true /* add */);
    656     }
    657 
    658     public void removePeriodicSync(Account account, String providerName, Bundle extras) {
    659         updateOrRemovePeriodicSync(account, providerName, extras, 0 /* period, ignored */,
    660                 false /* remove */);
    661     }
    662 
    663     public List<PeriodicSync> getPeriodicSyncs(Account account, String providerName) {
    664         ArrayList<PeriodicSync> syncs = new ArrayList<PeriodicSync>();
    665         synchronized (mAuthorities) {
    666             AuthorityInfo authority = getAuthorityLocked(account, providerName, "getPeriodicSyncs");
    667             if (authority != null) {
    668                 for (Pair<Bundle, Long> item : authority.periodicSyncs) {
    669                     syncs.add(new PeriodicSync(account, providerName, item.first, item.second));
    670                 }
    671             }
    672         }
    673         return syncs;
    674     }
    675 
    676     public void setMasterSyncAutomatically(boolean flag) {
    677         synchronized (mAuthorities) {
    678             if (mMasterSyncAutomatically == flag) {
    679                 return;
    680             }
    681             mMasterSyncAutomatically = flag;
    682             writeAccountInfoLocked();
    683         }
    684         if (flag) {
    685             ContentResolver.requestSync(null, null, new Bundle());
    686         }
    687         reportChange(ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS);
    688         mContext.sendBroadcast(SYNC_CONNECTION_SETTING_CHANGED_INTENT);
    689     }
    690 
    691     public boolean getMasterSyncAutomatically() {
    692         synchronized (mAuthorities) {
    693             return mMasterSyncAutomatically;
    694         }
    695     }
    696 
    697     public AuthorityInfo getOrCreateAuthority(Account account, String authority) {
    698         synchronized (mAuthorities) {
    699             return getOrCreateAuthorityLocked(account, authority,
    700                     -1 /* assign a new identifier if creating a new authority */,
    701                     true /* write to storage if this results in a change */);
    702         }
    703     }
    704 
    705     public void removeAuthority(Account account, String authority) {
    706         synchronized (mAuthorities) {
    707             removeAuthorityLocked(account, authority, true /* doWrite */);
    708         }
    709     }
    710 
    711     public AuthorityInfo getAuthority(int authorityId) {
    712         synchronized (mAuthorities) {
    713             return mAuthorities.get(authorityId);
    714         }
    715     }
    716 
    717     /**
    718      * Returns true if there is currently a sync operation for the given
    719      * account or authority in the pending list, or actively being processed.
    720      */
    721     public boolean isSyncActive(Account account, String authority) {
    722         synchronized (mAuthorities) {
    723             int i = mPendingOperations.size();
    724             while (i > 0) {
    725                 i--;
    726                 // TODO(fredq): this probably shouldn't be considering
    727                 // pending operations.
    728                 PendingOperation op = mPendingOperations.get(i);
    729                 if (op.account.equals(account) && op.authority.equals(authority)) {
    730                     return true;
    731                 }
    732             }
    733 
    734             if (mCurrentSync != null) {
    735                 AuthorityInfo ainfo = getAuthority(mCurrentSync.authorityId);
    736                 if (ainfo != null && ainfo.account.equals(account)
    737                         && ainfo.authority.equals(authority)) {
    738                     return true;
    739                 }
    740             }
    741         }
    742 
    743         return false;
    744     }
    745 
    746     public PendingOperation insertIntoPending(PendingOperation op) {
    747         synchronized (mAuthorities) {
    748             if (Log.isLoggable(TAG, Log.VERBOSE)) {
    749                 Log.v(TAG, "insertIntoPending: account=" + op.account
    750                     + " auth=" + op.authority
    751                     + " src=" + op.syncSource
    752                     + " extras=" + op.extras);
    753             }
    754 
    755             AuthorityInfo authority = getOrCreateAuthorityLocked(op.account,
    756                     op.authority,
    757                     -1 /* desired identifier */,
    758                     true /* write accounts to storage */);
    759             if (authority == null) {
    760                 return null;
    761             }
    762 
    763             op = new PendingOperation(op);
    764             op.authorityId = authority.ident;
    765             mPendingOperations.add(op);
    766             appendPendingOperationLocked(op);
    767 
    768             SyncStatusInfo status = getOrCreateSyncStatusLocked(authority.ident);
    769             status.pending = true;
    770         }
    771 
    772         reportChange(ContentResolver.SYNC_OBSERVER_TYPE_PENDING);
    773         return op;
    774     }
    775 
    776     public boolean deleteFromPending(PendingOperation op) {
    777         boolean res = false;
    778         synchronized (mAuthorities) {
    779             if (Log.isLoggable(TAG, Log.VERBOSE)) {
    780                 Log.v(TAG, "deleteFromPending: account=" + op.account
    781                     + " auth=" + op.authority
    782                     + " src=" + op.syncSource
    783                     + " extras=" + op.extras);
    784             }
    785             if (mPendingOperations.remove(op)) {
    786                 if (mPendingOperations.size() == 0
    787                         || mNumPendingFinished >= PENDING_FINISH_TO_WRITE) {
    788                     writePendingOperationsLocked();
    789                     mNumPendingFinished = 0;
    790                 } else {
    791                     mNumPendingFinished++;
    792                 }
    793 
    794                 AuthorityInfo authority = getAuthorityLocked(op.account, op.authority,
    795                         "deleteFromPending");
    796                 if (authority != null) {
    797                     if (Log.isLoggable(TAG, Log.VERBOSE)) Log.v(TAG, "removing - " + authority);
    798                     final int N = mPendingOperations.size();
    799                     boolean morePending = false;
    800                     for (int i=0; i<N; i++) {
    801                         PendingOperation cur = mPendingOperations.get(i);
    802                         if (cur.account.equals(op.account)
    803                                 && cur.authority.equals(op.authority)) {
    804                             morePending = true;
    805                             break;
    806                         }
    807                     }
    808 
    809                     if (!morePending) {
    810                         if (Log.isLoggable(TAG, Log.VERBOSE)) Log.v(TAG, "no more pending!");
    811                         SyncStatusInfo status = getOrCreateSyncStatusLocked(authority.ident);
    812                         status.pending = false;
    813                     }
    814                 }
    815 
    816                 res = true;
    817             }
    818         }
    819 
    820         reportChange(ContentResolver.SYNC_OBSERVER_TYPE_PENDING);
    821         return res;
    822     }
    823 
    824     public int clearPending() {
    825         int num;
    826         synchronized (mAuthorities) {
    827             if (Log.isLoggable(TAG, Log.VERBOSE)) {
    828                 Log.v(TAG, "clearPending");
    829             }
    830             num = mPendingOperations.size();
    831             mPendingOperations.clear();
    832             final int N = mSyncStatus.size();
    833             for (int i=0; i<N; i++) {
    834                 mSyncStatus.valueAt(i).pending = false;
    835             }
    836             writePendingOperationsLocked();
    837         }
    838         reportChange(ContentResolver.SYNC_OBSERVER_TYPE_PENDING);
    839         return num;
    840     }
    841 
    842     /**
    843      * Return a copy of the current array of pending operations.  The
    844      * PendingOperation objects are the real objects stored inside, so that
    845      * they can be used with deleteFromPending().
    846      */
    847     public ArrayList<PendingOperation> getPendingOperations() {
    848         synchronized (mAuthorities) {
    849             return new ArrayList<PendingOperation>(mPendingOperations);
    850         }
    851     }
    852 
    853     /**
    854      * Return the number of currently pending operations.
    855      */
    856     public int getPendingOperationCount() {
    857         synchronized (mAuthorities) {
    858             return mPendingOperations.size();
    859         }
    860     }
    861 
    862     /**
    863      * Called when the set of account has changed, given the new array of
    864      * active accounts.
    865      */
    866     public void doDatabaseCleanup(Account[] accounts) {
    867         synchronized (mAuthorities) {
    868             if (Log.isLoggable(TAG, Log.VERBOSE)) Log.w(TAG, "Updating for new accounts...");
    869             SparseArray<AuthorityInfo> removing = new SparseArray<AuthorityInfo>();
    870             Iterator<AccountInfo> accIt = mAccounts.values().iterator();
    871             while (accIt.hasNext()) {
    872                 AccountInfo acc = accIt.next();
    873                 if (!ArrayUtils.contains(accounts, acc.account)) {
    874                     // This account no longer exists...
    875                     if (Log.isLoggable(TAG, Log.VERBOSE)) {
    876                         Log.w(TAG, "Account removed: " + acc.account);
    877                     }
    878                     for (AuthorityInfo auth : acc.authorities.values()) {
    879                         removing.put(auth.ident, auth);
    880                     }
    881                     accIt.remove();
    882                 }
    883             }
    884 
    885             // Clean out all data structures.
    886             int i = removing.size();
    887             if (i > 0) {
    888                 while (i > 0) {
    889                     i--;
    890                     int ident = removing.keyAt(i);
    891                     mAuthorities.remove(ident);
    892                     int j = mSyncStatus.size();
    893                     while (j > 0) {
    894                         j--;
    895                         if (mSyncStatus.keyAt(j) == ident) {
    896                             mSyncStatus.remove(mSyncStatus.keyAt(j));
    897                         }
    898                     }
    899                     j = mSyncHistory.size();
    900                     while (j > 0) {
    901                         j--;
    902                         if (mSyncHistory.get(j).authorityId == ident) {
    903                             mSyncHistory.remove(j);
    904                         }
    905                     }
    906                 }
    907                 writeAccountInfoLocked();
    908                 writeStatusLocked();
    909                 writePendingOperationsLocked();
    910                 writeStatisticsLocked();
    911             }
    912         }
    913     }
    914 
    915     /**
    916      * Called when the currently active sync is changing (there can only be
    917      * one at a time).  Either supply a valid ActiveSyncContext with information
    918      * about the sync, or null to stop the currently active sync.
    919      */
    920     public void setActiveSync(SyncManager.ActiveSyncContext activeSyncContext) {
    921         synchronized (mAuthorities) {
    922             if (activeSyncContext != null) {
    923                 if (Log.isLoggable(TAG, Log.VERBOSE)) {
    924                     Log.v(TAG, "setActiveSync: account="
    925                         + activeSyncContext.mSyncOperation.account
    926                         + " auth=" + activeSyncContext.mSyncOperation.authority
    927                         + " src=" + activeSyncContext.mSyncOperation.syncSource
    928                         + " extras=" + activeSyncContext.mSyncOperation.extras);
    929                 }
    930                 if (mCurrentSync != null) {
    931                     Log.w(TAG, "setActiveSync called with existing active sync!");
    932                 }
    933                 AuthorityInfo authority = getAuthorityLocked(
    934                         activeSyncContext.mSyncOperation.account,
    935                         activeSyncContext.mSyncOperation.authority,
    936                         "setActiveSync");
    937                 if (authority == null) {
    938                     return;
    939                 }
    940                 mCurrentSync = new SyncInfo(authority.ident,
    941                         authority.account, authority.authority,
    942                         activeSyncContext.mStartTime);
    943             } else {
    944                 if (Log.isLoggable(TAG, Log.VERBOSE)) Log.v(TAG, "setActiveSync: null");
    945                 mCurrentSync = null;
    946             }
    947         }
    948 
    949         reportChange(ContentResolver.SYNC_OBSERVER_TYPE_ACTIVE);
    950     }
    951 
    952     /**
    953      * To allow others to send active change reports, to poke clients.
    954      */
    955     public void reportActiveChange() {
    956         reportChange(ContentResolver.SYNC_OBSERVER_TYPE_ACTIVE);
    957     }
    958 
    959     /**
    960      * Note that sync has started for the given account and authority.
    961      */
    962     public long insertStartSyncEvent(Account accountName, String authorityName,
    963             long now, int source) {
    964         long id;
    965         synchronized (mAuthorities) {
    966             if (Log.isLoggable(TAG, Log.VERBOSE)) {
    967                 Log.v(TAG, "insertStartSyncEvent: account=" + accountName
    968                     + " auth=" + authorityName + " source=" + source);
    969             }
    970             AuthorityInfo authority = getAuthorityLocked(accountName, authorityName,
    971                     "insertStartSyncEvent");
    972             if (authority == null) {
    973                 return -1;
    974             }
    975             SyncHistoryItem item = new SyncHistoryItem();
    976             item.authorityId = authority.ident;
    977             item.historyId = mNextHistoryId++;
    978             if (mNextHistoryId < 0) mNextHistoryId = 0;
    979             item.eventTime = now;
    980             item.source = source;
    981             item.event = EVENT_START;
    982             mSyncHistory.add(0, item);
    983             while (mSyncHistory.size() > MAX_HISTORY) {
    984                 mSyncHistory.remove(mSyncHistory.size()-1);
    985             }
    986             id = item.historyId;
    987             if (Log.isLoggable(TAG, Log.VERBOSE)) Log.v(TAG, "returning historyId " + id);
    988         }
    989 
    990         reportChange(ContentResolver.SYNC_OBSERVER_TYPE_STATUS);
    991         return id;
    992     }
    993 
    994     public static boolean equals(Bundle b1, Bundle b2) {
    995         if (b1.size() != b2.size()) {
    996             return false;
    997         }
    998         if (b1.isEmpty()) {
    999             return true;
   1000         }
   1001         for (String key : b1.keySet()) {
   1002             if (!b2.containsKey(key)) {
   1003                 return false;
   1004             }
   1005             if (!b1.get(key).equals(b2.get(key))) {
   1006                 return false;
   1007             }
   1008         }
   1009         return true;
   1010     }
   1011 
   1012     public void stopSyncEvent(long historyId, long elapsedTime, String resultMessage,
   1013             long downstreamActivity, long upstreamActivity) {
   1014         synchronized (mAuthorities) {
   1015             if (Log.isLoggable(TAG, Log.VERBOSE)) {
   1016                 Log.v(TAG, "stopSyncEvent: historyId=" + historyId);
   1017             }
   1018             SyncHistoryItem item = null;
   1019             int i = mSyncHistory.size();
   1020             while (i > 0) {
   1021                 i--;
   1022                 item = mSyncHistory.get(i);
   1023                 if (item.historyId == historyId) {
   1024                     break;
   1025                 }
   1026                 item = null;
   1027             }
   1028 
   1029             if (item == null) {
   1030                 Log.w(TAG, "stopSyncEvent: no history for id " + historyId);
   1031                 return;
   1032             }
   1033 
   1034             item.elapsedTime = elapsedTime;
   1035             item.event = EVENT_STOP;
   1036             item.mesg = resultMessage;
   1037             item.downstreamActivity = downstreamActivity;
   1038             item.upstreamActivity = upstreamActivity;
   1039 
   1040             SyncStatusInfo status = getOrCreateSyncStatusLocked(item.authorityId);
   1041 
   1042             status.numSyncs++;
   1043             status.totalElapsedTime += elapsedTime;
   1044             switch (item.source) {
   1045                 case SOURCE_LOCAL:
   1046                     status.numSourceLocal++;
   1047                     break;
   1048                 case SOURCE_POLL:
   1049                     status.numSourcePoll++;
   1050                     break;
   1051                 case SOURCE_USER:
   1052                     status.numSourceUser++;
   1053                     break;
   1054                 case SOURCE_SERVER:
   1055                     status.numSourceServer++;
   1056                     break;
   1057                 case SOURCE_PERIODIC:
   1058                     status.numSourcePeriodic++;
   1059                     break;
   1060             }
   1061 
   1062             boolean writeStatisticsNow = false;
   1063             int day = getCurrentDayLocked();
   1064             if (mDayStats[0] == null) {
   1065                 mDayStats[0] = new DayStats(day);
   1066             } else if (day != mDayStats[0].day) {
   1067                 System.arraycopy(mDayStats, 0, mDayStats, 1, mDayStats.length-1);
   1068                 mDayStats[0] = new DayStats(day);
   1069                 writeStatisticsNow = true;
   1070             } else if (mDayStats[0] == null) {
   1071             }
   1072             final DayStats ds = mDayStats[0];
   1073 
   1074             final long lastSyncTime = (item.eventTime + elapsedTime);
   1075             boolean writeStatusNow = false;
   1076             if (MESG_SUCCESS.equals(resultMessage)) {
   1077                 // - if successful, update the successful columns
   1078                 if (status.lastSuccessTime == 0 || status.lastFailureTime != 0) {
   1079                     writeStatusNow = true;
   1080                 }
   1081                 status.lastSuccessTime = lastSyncTime;
   1082                 status.lastSuccessSource = item.source;
   1083                 status.lastFailureTime = 0;
   1084                 status.lastFailureSource = -1;
   1085                 status.lastFailureMesg = null;
   1086                 status.initialFailureTime = 0;
   1087                 ds.successCount++;
   1088                 ds.successTime += elapsedTime;
   1089             } else if (!MESG_CANCELED.equals(resultMessage)) {
   1090                 if (status.lastFailureTime == 0) {
   1091                     writeStatusNow = true;
   1092                 }
   1093                 status.lastFailureTime = lastSyncTime;
   1094                 status.lastFailureSource = item.source;
   1095                 status.lastFailureMesg = resultMessage;
   1096                 if (status.initialFailureTime == 0) {
   1097                     status.initialFailureTime = lastSyncTime;
   1098                 }
   1099                 ds.failureCount++;
   1100                 ds.failureTime += elapsedTime;
   1101             }
   1102 
   1103             if (writeStatusNow) {
   1104                 writeStatusLocked();
   1105             } else if (!hasMessages(MSG_WRITE_STATUS)) {
   1106                 sendMessageDelayed(obtainMessage(MSG_WRITE_STATUS),
   1107                         WRITE_STATUS_DELAY);
   1108             }
   1109             if (writeStatisticsNow) {
   1110                 writeStatisticsLocked();
   1111             } else if (!hasMessages(MSG_WRITE_STATISTICS)) {
   1112                 sendMessageDelayed(obtainMessage(MSG_WRITE_STATISTICS),
   1113                         WRITE_STATISTICS_DELAY);
   1114             }
   1115         }
   1116 
   1117         reportChange(ContentResolver.SYNC_OBSERVER_TYPE_STATUS);
   1118     }
   1119 
   1120     /**
   1121      * Return the currently active sync information, or null if there is no
   1122      * active sync.  Note that the returned object is the real, live active
   1123      * sync object, so be careful what you do with it.
   1124      */
   1125     public SyncInfo getCurrentSync() {
   1126         synchronized (mAuthorities) {
   1127             return mCurrentSync;
   1128         }
   1129     }
   1130 
   1131     /**
   1132      * Return an array of the current sync status for all authorities.  Note
   1133      * that the objects inside the array are the real, live status objects,
   1134      * so be careful what you do with them.
   1135      */
   1136     public ArrayList<SyncStatusInfo> getSyncStatus() {
   1137         synchronized (mAuthorities) {
   1138             final int N = mSyncStatus.size();
   1139             ArrayList<SyncStatusInfo> ops = new ArrayList<SyncStatusInfo>(N);
   1140             for (int i=0; i<N; i++) {
   1141                 ops.add(mSyncStatus.valueAt(i));
   1142             }
   1143             return ops;
   1144         }
   1145     }
   1146 
   1147     /**
   1148      * Return an array of the current authorities. Note
   1149      * that the objects inside the array are the real, live objects,
   1150      * so be careful what you do with them.
   1151      */
   1152     public ArrayList<AuthorityInfo> getAuthorities() {
   1153         synchronized (mAuthorities) {
   1154             final int N = mAuthorities.size();
   1155             ArrayList<AuthorityInfo> infos = new ArrayList<AuthorityInfo>(N);
   1156             for (int i=0; i<N; i++) {
   1157                 infos.add(mAuthorities.valueAt(i));
   1158             }
   1159             return infos;
   1160         }
   1161     }
   1162 
   1163     /**
   1164      * Returns the status that matches the authority and account.
   1165      *
   1166      * @param account the account we want to check
   1167      * @param authority the authority whose row should be selected
   1168      * @return the SyncStatusInfo for the authority
   1169      */
   1170     public SyncStatusInfo getStatusByAccountAndAuthority(Account account, String authority) {
   1171         if (account == null || authority == null) {
   1172           throw new IllegalArgumentException();
   1173         }
   1174         synchronized (mAuthorities) {
   1175             final int N = mSyncStatus.size();
   1176             for (int i=0; i<N; i++) {
   1177                 SyncStatusInfo cur = mSyncStatus.valueAt(i);
   1178                 AuthorityInfo ainfo = mAuthorities.get(cur.authorityId);
   1179 
   1180                 if (ainfo != null && ainfo.authority.equals(authority) &&
   1181                     account.equals(ainfo.account)) {
   1182                   return cur;
   1183                 }
   1184             }
   1185             return null;
   1186         }
   1187     }
   1188 
   1189     /**
   1190      * Return true if the pending status is true of any matching authorities.
   1191      */
   1192     public boolean isSyncPending(Account account, String authority) {
   1193         synchronized (mAuthorities) {
   1194             final int N = mSyncStatus.size();
   1195             for (int i=0; i<N; i++) {
   1196                 SyncStatusInfo cur = mSyncStatus.valueAt(i);
   1197                 AuthorityInfo ainfo = mAuthorities.get(cur.authorityId);
   1198                 if (ainfo == null) {
   1199                     continue;
   1200                 }
   1201                 if (account != null && !ainfo.account.equals(account)) {
   1202                     continue;
   1203                 }
   1204                 if (ainfo.authority.equals(authority) && cur.pending) {
   1205                     return true;
   1206                 }
   1207             }
   1208             return false;
   1209         }
   1210     }
   1211 
   1212     /**
   1213      * Return an array of the current sync status for all authorities.  Note
   1214      * that the objects inside the array are the real, live status objects,
   1215      * so be careful what you do with them.
   1216      */
   1217     public ArrayList<SyncHistoryItem> getSyncHistory() {
   1218         synchronized (mAuthorities) {
   1219             final int N = mSyncHistory.size();
   1220             ArrayList<SyncHistoryItem> items = new ArrayList<SyncHistoryItem>(N);
   1221             for (int i=0; i<N; i++) {
   1222                 items.add(mSyncHistory.get(i));
   1223             }
   1224             return items;
   1225         }
   1226     }
   1227 
   1228     /**
   1229      * Return an array of the current per-day statistics.  Note
   1230      * that the objects inside the array are the real, live status objects,
   1231      * so be careful what you do with them.
   1232      */
   1233     public DayStats[] getDayStatistics() {
   1234         synchronized (mAuthorities) {
   1235             DayStats[] ds = new DayStats[mDayStats.length];
   1236             System.arraycopy(mDayStats, 0, ds, 0, ds.length);
   1237             return ds;
   1238         }
   1239     }
   1240 
   1241     /**
   1242      * If sync is failing for any of the provider/accounts then determine the time at which it
   1243      * started failing and return the earliest time over all the provider/accounts. If none are
   1244      * failing then return 0.
   1245      */
   1246     public long getInitialSyncFailureTime() {
   1247         synchronized (mAuthorities) {
   1248             if (!mMasterSyncAutomatically) {
   1249                 return 0;
   1250             }
   1251 
   1252             long oldest = 0;
   1253             int i = mSyncStatus.size();
   1254             while (i > 0) {
   1255                 i--;
   1256                 SyncStatusInfo stats = mSyncStatus.valueAt(i);
   1257                 AuthorityInfo authority = mAuthorities.get(stats.authorityId);
   1258                 if (authority != null && authority.enabled) {
   1259                     if (oldest == 0 || stats.initialFailureTime < oldest) {
   1260                         oldest = stats.initialFailureTime;
   1261                     }
   1262                 }
   1263             }
   1264 
   1265             return oldest;
   1266         }
   1267     }
   1268 
   1269     private int getCurrentDayLocked() {
   1270         mCal.setTimeInMillis(System.currentTimeMillis());
   1271         final int dayOfYear = mCal.get(Calendar.DAY_OF_YEAR);
   1272         if (mYear != mCal.get(Calendar.YEAR)) {
   1273             mYear = mCal.get(Calendar.YEAR);
   1274             mCal.clear();
   1275             mCal.set(Calendar.YEAR, mYear);
   1276             mYearInDays = (int)(mCal.getTimeInMillis()/86400000);
   1277         }
   1278         return dayOfYear + mYearInDays;
   1279     }
   1280 
   1281     /**
   1282      * Retrieve an authority, returning null if one does not exist.
   1283      *
   1284      * @param accountName The name of the account for the authority.
   1285      * @param authorityName The name of the authority itself.
   1286      * @param tag If non-null, this will be used in a log message if the
   1287      * requested authority does not exist.
   1288      */
   1289     private AuthorityInfo getAuthorityLocked(Account accountName, String authorityName,
   1290             String tag) {
   1291         AccountInfo account = mAccounts.get(accountName);
   1292         if (account == null) {
   1293             if (tag != null) {
   1294                 if (Log.isLoggable(TAG, Log.VERBOSE)) {
   1295                     Log.v(TAG, tag + ": unknown account " + accountName);
   1296                 }
   1297             }
   1298             return null;
   1299         }
   1300         AuthorityInfo authority = account.authorities.get(authorityName);
   1301         if (authority == null) {
   1302             if (tag != null) {
   1303                 if (Log.isLoggable(TAG, Log.VERBOSE)) {
   1304                     Log.v(TAG, tag + ": unknown authority " + authorityName);
   1305                 }
   1306             }
   1307             return null;
   1308         }
   1309 
   1310         return authority;
   1311     }
   1312 
   1313     private AuthorityInfo getOrCreateAuthorityLocked(Account accountName,
   1314             String authorityName, int ident, boolean doWrite) {
   1315         AccountInfo account = mAccounts.get(accountName);
   1316         if (account == null) {
   1317             account = new AccountInfo(accountName);
   1318             mAccounts.put(accountName, account);
   1319         }
   1320         AuthorityInfo authority = account.authorities.get(authorityName);
   1321         if (authority == null) {
   1322             if (ident < 0) {
   1323                 ident = mNextAuthorityId;
   1324                 mNextAuthorityId++;
   1325                 doWrite = true;
   1326             }
   1327             if (Log.isLoggable(TAG, Log.VERBOSE)) {
   1328                 Log.v(TAG, "created a new AuthorityInfo for " + accountName
   1329                     + ", provider " + authorityName);
   1330             }
   1331             authority = new AuthorityInfo(accountName, authorityName, ident);
   1332             account.authorities.put(authorityName, authority);
   1333             mAuthorities.put(ident, authority);
   1334             if (doWrite) {
   1335                 writeAccountInfoLocked();
   1336             }
   1337         }
   1338 
   1339         return authority;
   1340     }
   1341 
   1342     private void removeAuthorityLocked(Account account, String authorityName, boolean doWrite) {
   1343         AccountInfo accountInfo = mAccounts.get(account);
   1344         if (accountInfo != null) {
   1345             final AuthorityInfo authorityInfo = accountInfo.authorities.remove(authorityName);
   1346             if (authorityInfo != null) {
   1347                 mAuthorities.remove(authorityInfo.ident);
   1348                 if (doWrite) {
   1349                     writeAccountInfoLocked();
   1350                 }
   1351             }
   1352         }
   1353     }
   1354 
   1355     public SyncStatusInfo getOrCreateSyncStatus(AuthorityInfo authority) {
   1356         synchronized (mAuthorities) {
   1357             return getOrCreateSyncStatusLocked(authority.ident);
   1358         }
   1359     }
   1360 
   1361     private SyncStatusInfo getOrCreateSyncStatusLocked(int authorityId) {
   1362         SyncStatusInfo status = mSyncStatus.get(authorityId);
   1363         if (status == null) {
   1364             status = new SyncStatusInfo(authorityId);
   1365             mSyncStatus.put(authorityId, status);
   1366         }
   1367         return status;
   1368     }
   1369 
   1370     public void writeAllState() {
   1371         synchronized (mAuthorities) {
   1372             // Account info is always written so no need to do it here.
   1373 
   1374             if (mNumPendingFinished > 0) {
   1375                 // Only write these if they are out of date.
   1376                 writePendingOperationsLocked();
   1377             }
   1378 
   1379             // Just always write these...  they are likely out of date.
   1380             writeStatusLocked();
   1381             writeStatisticsLocked();
   1382         }
   1383     }
   1384 
   1385     /**
   1386      * public for testing
   1387      */
   1388     public void clearAndReadState() {
   1389         synchronized (mAuthorities) {
   1390             mAuthorities.clear();
   1391             mAccounts.clear();
   1392             mPendingOperations.clear();
   1393             mSyncStatus.clear();
   1394             mSyncHistory.clear();
   1395 
   1396             readAccountInfoLocked();
   1397             readStatusLocked();
   1398             readPendingOperationsLocked();
   1399             readStatisticsLocked();
   1400             readAndDeleteLegacyAccountInfoLocked();
   1401             writeAccountInfoLocked();
   1402             writeStatusLocked();
   1403             writePendingOperationsLocked();
   1404             writeStatisticsLocked();
   1405         }
   1406     }
   1407 
   1408     /**
   1409      * Read all account information back in to the initial engine state.
   1410      */
   1411     private void readAccountInfoLocked() {
   1412         int highestAuthorityId = -1;
   1413         FileInputStream fis = null;
   1414         try {
   1415             fis = mAccountInfoFile.openRead();
   1416             if (DEBUG_FILE) Log.v(TAG, "Reading " + mAccountInfoFile.getBaseFile());
   1417             XmlPullParser parser = Xml.newPullParser();
   1418             parser.setInput(fis, null);
   1419             int eventType = parser.getEventType();
   1420             while (eventType != XmlPullParser.START_TAG) {
   1421                 eventType = parser.next();
   1422             }
   1423             String tagName = parser.getName();
   1424             if ("accounts".equals(tagName)) {
   1425                 String listen = parser.getAttributeValue(
   1426                         null, "listen-for-tickles");
   1427                 String versionString = parser.getAttributeValue(null, "version");
   1428                 int version;
   1429                 try {
   1430                     version = (versionString == null) ? 0 : Integer.parseInt(versionString);
   1431                 } catch (NumberFormatException e) {
   1432                     version = 0;
   1433                 }
   1434                 String nextIdString = parser.getAttributeValue(null, "nextAuthorityId");
   1435                 try {
   1436                     int id = (nextIdString == null) ? 0 : Integer.parseInt(nextIdString);
   1437                     mNextAuthorityId = Math.max(mNextAuthorityId, id);
   1438                 } catch (NumberFormatException e) {
   1439                     // don't care
   1440                 }
   1441                 mMasterSyncAutomatically = listen == null || Boolean.parseBoolean(listen);
   1442                 eventType = parser.next();
   1443                 AuthorityInfo authority = null;
   1444                 Pair<Bundle, Long> periodicSync = null;
   1445                 do {
   1446                     if (eventType == XmlPullParser.START_TAG) {
   1447                         tagName = parser.getName();
   1448                         if (parser.getDepth() == 2) {
   1449                             if ("authority".equals(tagName)) {
   1450                                 authority = parseAuthority(parser, version);
   1451                                 periodicSync = null;
   1452                                 if (authority.ident > highestAuthorityId) {
   1453                                     highestAuthorityId = authority.ident;
   1454                                 }
   1455                             }
   1456                         } else if (parser.getDepth() == 3) {
   1457                             if ("periodicSync".equals(tagName) && authority != null) {
   1458                                 periodicSync = parsePeriodicSync(parser, authority);
   1459                             }
   1460                         } else if (parser.getDepth() == 4 && periodicSync != null) {
   1461                             if ("extra".equals(tagName)) {
   1462                                 parseExtra(parser, periodicSync);
   1463                             }
   1464                         }
   1465                     }
   1466                     eventType = parser.next();
   1467                 } while (eventType != XmlPullParser.END_DOCUMENT);
   1468             }
   1469         } catch (XmlPullParserException e) {
   1470             Log.w(TAG, "Error reading accounts", e);
   1471             return;
   1472         } catch (java.io.IOException e) {
   1473             if (fis == null) Log.i(TAG, "No initial accounts");
   1474             else Log.w(TAG, "Error reading accounts", e);
   1475             return;
   1476         } finally {
   1477             mNextAuthorityId = Math.max(highestAuthorityId + 1, mNextAuthorityId);
   1478             if (fis != null) {
   1479                 try {
   1480                     fis.close();
   1481                 } catch (java.io.IOException e1) {
   1482                 }
   1483             }
   1484         }
   1485 
   1486         maybeMigrateSettingsForRenamedAuthorities();
   1487     }
   1488 
   1489     /**
   1490      * some authority names have changed. copy over their settings and delete the old ones
   1491      * @return true if a change was made
   1492      */
   1493     private boolean maybeMigrateSettingsForRenamedAuthorities() {
   1494         boolean writeNeeded = false;
   1495 
   1496         ArrayList<AuthorityInfo> authoritiesToRemove = new ArrayList<AuthorityInfo>();
   1497         final int N = mAuthorities.size();
   1498         for (int i=0; i<N; i++) {
   1499             AuthorityInfo authority = mAuthorities.valueAt(i);
   1500             // skip this authority if it isn't one of the renamed ones
   1501             final String newAuthorityName = sAuthorityRenames.get(authority.authority);
   1502             if (newAuthorityName == null) {
   1503                 continue;
   1504             }
   1505 
   1506             // remember this authority so we can remove it later. we can't remove it
   1507             // now without messing up this loop iteration
   1508             authoritiesToRemove.add(authority);
   1509 
   1510             // this authority isn't enabled, no need to copy it to the new authority name since
   1511             // the default is "disabled"
   1512             if (!authority.enabled) {
   1513                 continue;
   1514             }
   1515 
   1516             // if we already have a record of this new authority then don't copy over the settings
   1517             if (getAuthorityLocked(authority.account, newAuthorityName, "cleanup") != null) {
   1518                 continue;
   1519             }
   1520 
   1521             AuthorityInfo newAuthority = getOrCreateAuthorityLocked(authority.account,
   1522                     newAuthorityName, -1 /* ident */, false /* doWrite */);
   1523             newAuthority.enabled = true;
   1524             writeNeeded = true;
   1525         }
   1526 
   1527         for (AuthorityInfo authorityInfo : authoritiesToRemove) {
   1528             removeAuthorityLocked(authorityInfo.account, authorityInfo.authority,
   1529                     false /* doWrite */);
   1530             writeNeeded = true;
   1531         }
   1532 
   1533         return writeNeeded;
   1534     }
   1535 
   1536     private AuthorityInfo parseAuthority(XmlPullParser parser, int version) {
   1537         AuthorityInfo authority = null;
   1538         int id = -1;
   1539         try {
   1540             id = Integer.parseInt(parser.getAttributeValue(
   1541                     null, "id"));
   1542         } catch (NumberFormatException e) {
   1543             Log.e(TAG, "error parsing the id of the authority", e);
   1544         } catch (NullPointerException e) {
   1545             Log.e(TAG, "the id of the authority is null", e);
   1546         }
   1547         if (id >= 0) {
   1548             String authorityName = parser.getAttributeValue(null, "authority");
   1549             String enabled = parser.getAttributeValue(null, "enabled");
   1550             String syncable = parser.getAttributeValue(null, "syncable");
   1551             String accountName = parser.getAttributeValue(null, "account");
   1552             String accountType = parser.getAttributeValue(null, "type");
   1553             if (accountType == null) {
   1554                 accountType = "com.google";
   1555                 syncable = "unknown";
   1556             }
   1557             authority = mAuthorities.get(id);
   1558             if (DEBUG_FILE) Log.v(TAG, "Adding authority: account="
   1559                     + accountName + " auth=" + authorityName
   1560                     + " enabled=" + enabled
   1561                     + " syncable=" + syncable);
   1562             if (authority == null) {
   1563                 if (DEBUG_FILE) Log.v(TAG, "Creating entry");
   1564                 authority = getOrCreateAuthorityLocked(
   1565                         new Account(accountName, accountType), authorityName, id, false);
   1566                 // If the version is 0 then we are upgrading from a file format that did not
   1567                 // know about periodic syncs. In that case don't clear the list since we
   1568                 // want the default, which is a daily periodioc sync.
   1569                 // Otherwise clear out this default list since we will populate it later with
   1570                 // the periodic sync descriptions that are read from the configuration file.
   1571                 if (version > 0) {
   1572                     authority.periodicSyncs.clear();
   1573                 }
   1574             }
   1575             if (authority != null) {
   1576                 authority.enabled = enabled == null || Boolean.parseBoolean(enabled);
   1577                 if ("unknown".equals(syncable)) {
   1578                     authority.syncable = -1;
   1579                 } else {
   1580                     authority.syncable =
   1581                             (syncable == null || Boolean.parseBoolean(syncable)) ? 1 : 0;
   1582                 }
   1583             } else {
   1584                 Log.w(TAG, "Failure adding authority: account="
   1585                         + accountName + " auth=" + authorityName
   1586                         + " enabled=" + enabled
   1587                         + " syncable=" + syncable);
   1588             }
   1589         }
   1590 
   1591         return authority;
   1592     }
   1593 
   1594     private Pair<Bundle, Long> parsePeriodicSync(XmlPullParser parser, AuthorityInfo authority) {
   1595         Bundle extras = new Bundle();
   1596         String periodValue = parser.getAttributeValue(null, "period");
   1597         final long period;
   1598         try {
   1599             period = Long.parseLong(periodValue);
   1600         } catch (NumberFormatException e) {
   1601             Log.e(TAG, "error parsing the period of a periodic sync", e);
   1602             return null;
   1603         } catch (NullPointerException e) {
   1604             Log.e(TAG, "the period of a periodic sync is null", e);
   1605             return null;
   1606         }
   1607         final Pair<Bundle, Long> periodicSync = Pair.create(extras, period);
   1608         authority.periodicSyncs.add(periodicSync);
   1609 
   1610         return periodicSync;
   1611     }
   1612 
   1613     private void parseExtra(XmlPullParser parser, Pair<Bundle, Long> periodicSync) {
   1614         final Bundle extras = periodicSync.first;
   1615         String name = parser.getAttributeValue(null, "name");
   1616         String type = parser.getAttributeValue(null, "type");
   1617         String value1 = parser.getAttributeValue(null, "value1");
   1618         String value2 = parser.getAttributeValue(null, "value2");
   1619 
   1620         try {
   1621             if ("long".equals(type)) {
   1622                 extras.putLong(name, Long.parseLong(value1));
   1623             } else if ("integer".equals(type)) {
   1624                 extras.putInt(name, Integer.parseInt(value1));
   1625             } else if ("double".equals(type)) {
   1626                 extras.putDouble(name, Double.parseDouble(value1));
   1627             } else if ("float".equals(type)) {
   1628                 extras.putFloat(name, Float.parseFloat(value1));
   1629             } else if ("boolean".equals(type)) {
   1630                 extras.putBoolean(name, Boolean.parseBoolean(value1));
   1631             } else if ("string".equals(type)) {
   1632                 extras.putString(name, value1);
   1633             } else if ("account".equals(type)) {
   1634                 extras.putParcelable(name, new Account(value1, value2));
   1635             }
   1636         } catch (NumberFormatException e) {
   1637             Log.e(TAG, "error parsing bundle value", e);
   1638         } catch (NullPointerException e) {
   1639             Log.e(TAG, "error parsing bundle value", e);
   1640         }
   1641     }
   1642 
   1643     /**
   1644      * Write all account information to the account file.
   1645      */
   1646     private void writeAccountInfoLocked() {
   1647         if (DEBUG_FILE) Log.v(TAG, "Writing new " + mAccountInfoFile.getBaseFile());
   1648         FileOutputStream fos = null;
   1649 
   1650         try {
   1651             fos = mAccountInfoFile.startWrite();
   1652             XmlSerializer out = new FastXmlSerializer();
   1653             out.setOutput(fos, "utf-8");
   1654             out.startDocument(null, true);
   1655             out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
   1656 
   1657             out.startTag(null, "accounts");
   1658             out.attribute(null, "version", Integer.toString(ACCOUNTS_VERSION));
   1659             out.attribute(null, "nextAuthorityId", Integer.toString(mNextAuthorityId));
   1660             if (!mMasterSyncAutomatically) {
   1661                 out.attribute(null, "listen-for-tickles", "false");
   1662             }
   1663 
   1664             final int N = mAuthorities.size();
   1665             for (int i=0; i<N; i++) {
   1666                 AuthorityInfo authority = mAuthorities.valueAt(i);
   1667                 out.startTag(null, "authority");
   1668                 out.attribute(null, "id", Integer.toString(authority.ident));
   1669                 out.attribute(null, "account", authority.account.name);
   1670                 out.attribute(null, "type", authority.account.type);
   1671                 out.attribute(null, "authority", authority.authority);
   1672                 out.attribute(null, "enabled", Boolean.toString(authority.enabled));
   1673                 if (authority.syncable < 0) {
   1674                     out.attribute(null, "syncable", "unknown");
   1675                 } else {
   1676                     out.attribute(null, "syncable", Boolean.toString(authority.syncable != 0));
   1677                 }
   1678                 for (Pair<Bundle, Long> periodicSync : authority.periodicSyncs) {
   1679                     out.startTag(null, "periodicSync");
   1680                     out.attribute(null, "period", Long.toString(periodicSync.second));
   1681                     final Bundle extras = periodicSync.first;
   1682                     for (String key : extras.keySet()) {
   1683                         out.startTag(null, "extra");
   1684                         out.attribute(null, "name", key);
   1685                         final Object value = extras.get(key);
   1686                         if (value instanceof Long) {
   1687                             out.attribute(null, "type", "long");
   1688                             out.attribute(null, "value1", value.toString());
   1689                         } else if (value instanceof Integer) {
   1690                             out.attribute(null, "type", "integer");
   1691                             out.attribute(null, "value1", value.toString());
   1692                         } else if (value instanceof Boolean) {
   1693                             out.attribute(null, "type", "boolean");
   1694                             out.attribute(null, "value1", value.toString());
   1695                         } else if (value instanceof Float) {
   1696                             out.attribute(null, "type", "float");
   1697                             out.attribute(null, "value1", value.toString());
   1698                         } else if (value instanceof Double) {
   1699                             out.attribute(null, "type", "double");
   1700                             out.attribute(null, "value1", value.toString());
   1701                         } else if (value instanceof String) {
   1702                             out.attribute(null, "type", "string");
   1703                             out.attribute(null, "value1", value.toString());
   1704                         } else if (value instanceof Account) {
   1705                             out.attribute(null, "type", "account");
   1706                             out.attribute(null, "value1", ((Account)value).name);
   1707                             out.attribute(null, "value2", ((Account)value).type);
   1708                         }
   1709                         out.endTag(null, "extra");
   1710                     }
   1711                     out.endTag(null, "periodicSync");
   1712                 }
   1713                 out.endTag(null, "authority");
   1714             }
   1715 
   1716             out.endTag(null, "accounts");
   1717 
   1718             out.endDocument();
   1719 
   1720             mAccountInfoFile.finishWrite(fos);
   1721         } catch (java.io.IOException e1) {
   1722             Log.w(TAG, "Error writing accounts", e1);
   1723             if (fos != null) {
   1724                 mAccountInfoFile.failWrite(fos);
   1725             }
   1726         }
   1727     }
   1728 
   1729     static int getIntColumn(Cursor c, String name) {
   1730         return c.getInt(c.getColumnIndex(name));
   1731     }
   1732 
   1733     static long getLongColumn(Cursor c, String name) {
   1734         return c.getLong(c.getColumnIndex(name));
   1735     }
   1736 
   1737     /**
   1738      * Load sync engine state from the old syncmanager database, and then
   1739      * erase it.  Note that we don't deal with pending operations, active
   1740      * sync, or history.
   1741      */
   1742     private void readAndDeleteLegacyAccountInfoLocked() {
   1743         // Look for old database to initialize from.
   1744         File file = mContext.getDatabasePath("syncmanager.db");
   1745         if (!file.exists()) {
   1746             return;
   1747         }
   1748         String path = file.getPath();
   1749         SQLiteDatabase db = null;
   1750         try {
   1751             db = SQLiteDatabase.openDatabase(path, null,
   1752                     SQLiteDatabase.OPEN_READONLY);
   1753         } catch (SQLiteException e) {
   1754         }
   1755 
   1756         if (db != null) {
   1757             final boolean hasType = db.getVersion() >= 11;
   1758 
   1759             // Copy in all of the status information, as well as accounts.
   1760             if (DEBUG_FILE) Log.v(TAG, "Reading legacy sync accounts db");
   1761             SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
   1762             qb.setTables("stats, status");
   1763             HashMap<String,String> map = new HashMap<String,String>();
   1764             map.put("_id", "status._id as _id");
   1765             map.put("account", "stats.account as account");
   1766             if (hasType) {
   1767                 map.put("account_type", "stats.account_type as account_type");
   1768             }
   1769             map.put("authority", "stats.authority as authority");
   1770             map.put("totalElapsedTime", "totalElapsedTime");
   1771             map.put("numSyncs", "numSyncs");
   1772             map.put("numSourceLocal", "numSourceLocal");
   1773             map.put("numSourcePoll", "numSourcePoll");
   1774             map.put("numSourceServer", "numSourceServer");
   1775             map.put("numSourceUser", "numSourceUser");
   1776             map.put("lastSuccessSource", "lastSuccessSource");
   1777             map.put("lastSuccessTime", "lastSuccessTime");
   1778             map.put("lastFailureSource", "lastFailureSource");
   1779             map.put("lastFailureTime", "lastFailureTime");
   1780             map.put("lastFailureMesg", "lastFailureMesg");
   1781             map.put("pending", "pending");
   1782             qb.setProjectionMap(map);
   1783             qb.appendWhere("stats._id = status.stats_id");
   1784             Cursor c = qb.query(db, null, null, null, null, null, null);
   1785             while (c.moveToNext()) {
   1786                 String accountName = c.getString(c.getColumnIndex("account"));
   1787                 String accountType = hasType
   1788                         ? c.getString(c.getColumnIndex("account_type")) : null;
   1789                 if (accountType == null) {
   1790                     accountType = "com.google";
   1791                 }
   1792                 String authorityName = c.getString(c.getColumnIndex("authority"));
   1793                 AuthorityInfo authority = this.getOrCreateAuthorityLocked(
   1794                         new Account(accountName, accountType),
   1795                         authorityName, -1, false);
   1796                 if (authority != null) {
   1797                     int i = mSyncStatus.size();
   1798                     boolean found = false;
   1799                     SyncStatusInfo st = null;
   1800                     while (i > 0) {
   1801                         i--;
   1802                         st = mSyncStatus.valueAt(i);
   1803                         if (st.authorityId == authority.ident) {
   1804                             found = true;
   1805                             break;
   1806                         }
   1807                     }
   1808                     if (!found) {
   1809                         st = new SyncStatusInfo(authority.ident);
   1810                         mSyncStatus.put(authority.ident, st);
   1811                     }
   1812                     st.totalElapsedTime = getLongColumn(c, "totalElapsedTime");
   1813                     st.numSyncs = getIntColumn(c, "numSyncs");
   1814                     st.numSourceLocal = getIntColumn(c, "numSourceLocal");
   1815                     st.numSourcePoll = getIntColumn(c, "numSourcePoll");
   1816                     st.numSourceServer = getIntColumn(c, "numSourceServer");
   1817                     st.numSourceUser = getIntColumn(c, "numSourceUser");
   1818                     st.numSourcePeriodic = 0;
   1819                     st.lastSuccessSource = getIntColumn(c, "lastSuccessSource");
   1820                     st.lastSuccessTime = getLongColumn(c, "lastSuccessTime");
   1821                     st.lastFailureSource = getIntColumn(c, "lastFailureSource");
   1822                     st.lastFailureTime = getLongColumn(c, "lastFailureTime");
   1823                     st.lastFailureMesg = c.getString(c.getColumnIndex("lastFailureMesg"));
   1824                     st.pending = getIntColumn(c, "pending") != 0;
   1825                 }
   1826             }
   1827 
   1828             c.close();
   1829 
   1830             // Retrieve the settings.
   1831             qb = new SQLiteQueryBuilder();
   1832             qb.setTables("settings");
   1833             c = qb.query(db, null, null, null, null, null, null);
   1834             while (c.moveToNext()) {
   1835                 String name = c.getString(c.getColumnIndex("name"));
   1836                 String value = c.getString(c.getColumnIndex("value"));
   1837                 if (name == null) continue;
   1838                 if (name.equals("listen_for_tickles")) {
   1839                     setMasterSyncAutomatically(value == null || Boolean.parseBoolean(value));
   1840                 } else if (name.startsWith("sync_provider_")) {
   1841                     String provider = name.substring("sync_provider_".length(),
   1842                             name.length());
   1843                     int i = mAuthorities.size();
   1844                     while (i > 0) {
   1845                         i--;
   1846                         AuthorityInfo authority = mAuthorities.valueAt(i);
   1847                         if (authority.authority.equals(provider)) {
   1848                             authority.enabled = value == null || Boolean.parseBoolean(value);
   1849                             authority.syncable = 1;
   1850                         }
   1851                     }
   1852                 }
   1853             }
   1854 
   1855             c.close();
   1856 
   1857             db.close();
   1858 
   1859             (new File(path)).delete();
   1860         }
   1861     }
   1862 
   1863     public static final int STATUS_FILE_END = 0;
   1864     public static final int STATUS_FILE_ITEM = 100;
   1865 
   1866     /**
   1867      * Read all sync status back in to the initial engine state.
   1868      */
   1869     private void readStatusLocked() {
   1870         if (DEBUG_FILE) Log.v(TAG, "Reading " + mStatusFile.getBaseFile());
   1871         try {
   1872             byte[] data = mStatusFile.readFully();
   1873             Parcel in = Parcel.obtain();
   1874             in.unmarshall(data, 0, data.length);
   1875             in.setDataPosition(0);
   1876             int token;
   1877             while ((token=in.readInt()) != STATUS_FILE_END) {
   1878                 if (token == STATUS_FILE_ITEM) {
   1879                     SyncStatusInfo status = new SyncStatusInfo(in);
   1880                     if (mAuthorities.indexOfKey(status.authorityId) >= 0) {
   1881                         status.pending = false;
   1882                         if (DEBUG_FILE) Log.v(TAG, "Adding status for id "
   1883                                 + status.authorityId);
   1884                         mSyncStatus.put(status.authorityId, status);
   1885                     }
   1886                 } else {
   1887                     // Ooops.
   1888                     Log.w(TAG, "Unknown status token: " + token);
   1889                     break;
   1890                 }
   1891             }
   1892         } catch (java.io.IOException e) {
   1893             Log.i(TAG, "No initial status");
   1894         }
   1895     }
   1896 
   1897     /**
   1898      * Write all sync status to the sync status file.
   1899      */
   1900     private void writeStatusLocked() {
   1901         if (DEBUG_FILE) Log.v(TAG, "Writing new " + mStatusFile.getBaseFile());
   1902 
   1903         // The file is being written, so we don't need to have a scheduled
   1904         // write until the next change.
   1905         removeMessages(MSG_WRITE_STATUS);
   1906 
   1907         FileOutputStream fos = null;
   1908         try {
   1909             fos = mStatusFile.startWrite();
   1910             Parcel out = Parcel.obtain();
   1911             final int N = mSyncStatus.size();
   1912             for (int i=0; i<N; i++) {
   1913                 SyncStatusInfo status = mSyncStatus.valueAt(i);
   1914                 out.writeInt(STATUS_FILE_ITEM);
   1915                 status.writeToParcel(out, 0);
   1916             }
   1917             out.writeInt(STATUS_FILE_END);
   1918             fos.write(out.marshall());
   1919             out.recycle();
   1920 
   1921             mStatusFile.finishWrite(fos);
   1922         } catch (java.io.IOException e1) {
   1923             Log.w(TAG, "Error writing status", e1);
   1924             if (fos != null) {
   1925                 mStatusFile.failWrite(fos);
   1926             }
   1927         }
   1928     }
   1929 
   1930     public static final int PENDING_OPERATION_VERSION = 2;
   1931 
   1932     /**
   1933      * Read all pending operations back in to the initial engine state.
   1934      */
   1935     private void readPendingOperationsLocked() {
   1936         if (DEBUG_FILE) Log.v(TAG, "Reading " + mPendingFile.getBaseFile());
   1937         try {
   1938             byte[] data = mPendingFile.readFully();
   1939             Parcel in = Parcel.obtain();
   1940             in.unmarshall(data, 0, data.length);
   1941             in.setDataPosition(0);
   1942             final int SIZE = in.dataSize();
   1943             while (in.dataPosition() < SIZE) {
   1944                 int version = in.readInt();
   1945                 if (version != PENDING_OPERATION_VERSION && version != 1) {
   1946                     Log.w(TAG, "Unknown pending operation version "
   1947                             + version + "; dropping all ops");
   1948                     break;
   1949                 }
   1950                 int authorityId = in.readInt();
   1951                 int syncSource = in.readInt();
   1952                 byte[] flatExtras = in.createByteArray();
   1953                 boolean expedited;
   1954                 if (version == PENDING_OPERATION_VERSION) {
   1955                     expedited = in.readInt() != 0;
   1956                 } else {
   1957                     expedited = false;
   1958                 }
   1959                 AuthorityInfo authority = mAuthorities.get(authorityId);
   1960                 if (authority != null) {
   1961                     Bundle extras = null;
   1962                     if (flatExtras != null) {
   1963                         extras = unflattenBundle(flatExtras);
   1964                     }
   1965                     PendingOperation op = new PendingOperation(
   1966                             authority.account, syncSource,
   1967                             authority.authority, extras, expedited);
   1968                     op.authorityId = authorityId;
   1969                     op.flatExtras = flatExtras;
   1970                     if (DEBUG_FILE) Log.v(TAG, "Adding pending op: account=" + op.account
   1971                             + " auth=" + op.authority
   1972                             + " src=" + op.syncSource
   1973                             + " expedited=" + op.expedited
   1974                             + " extras=" + op.extras);
   1975                     mPendingOperations.add(op);
   1976                 }
   1977             }
   1978         } catch (java.io.IOException e) {
   1979             Log.i(TAG, "No initial pending operations");
   1980         }
   1981     }
   1982 
   1983     private void writePendingOperationLocked(PendingOperation op, Parcel out) {
   1984         out.writeInt(PENDING_OPERATION_VERSION);
   1985         out.writeInt(op.authorityId);
   1986         out.writeInt(op.syncSource);
   1987         if (op.flatExtras == null && op.extras != null) {
   1988             op.flatExtras = flattenBundle(op.extras);
   1989         }
   1990         out.writeByteArray(op.flatExtras);
   1991         out.writeInt(op.expedited ? 1 : 0);
   1992     }
   1993 
   1994     /**
   1995      * Write all currently pending ops to the pending ops file.
   1996      */
   1997     private void writePendingOperationsLocked() {
   1998         final int N = mPendingOperations.size();
   1999         FileOutputStream fos = null;
   2000         try {
   2001             if (N == 0) {
   2002                 if (DEBUG_FILE) Log.v(TAG, "Truncating " + mPendingFile.getBaseFile());
   2003                 mPendingFile.truncate();
   2004                 return;
   2005             }
   2006 
   2007             if (DEBUG_FILE) Log.v(TAG, "Writing new " + mPendingFile.getBaseFile());
   2008             fos = mPendingFile.startWrite();
   2009 
   2010             Parcel out = Parcel.obtain();
   2011             for (int i=0; i<N; i++) {
   2012                 PendingOperation op = mPendingOperations.get(i);
   2013                 writePendingOperationLocked(op, out);
   2014             }
   2015             fos.write(out.marshall());
   2016             out.recycle();
   2017 
   2018             mPendingFile.finishWrite(fos);
   2019         } catch (java.io.IOException e1) {
   2020             Log.w(TAG, "Error writing pending operations", e1);
   2021             if (fos != null) {
   2022                 mPendingFile.failWrite(fos);
   2023             }
   2024         }
   2025     }
   2026 
   2027     /**
   2028      * Append the given operation to the pending ops file; if unable to,
   2029      * write all pending ops.
   2030      */
   2031     private void appendPendingOperationLocked(PendingOperation op) {
   2032         if (DEBUG_FILE) Log.v(TAG, "Appending to " + mPendingFile.getBaseFile());
   2033         FileOutputStream fos = null;
   2034         try {
   2035             fos = mPendingFile.openAppend();
   2036         } catch (java.io.IOException e) {
   2037             if (DEBUG_FILE) Log.v(TAG, "Failed append; writing full file");
   2038             writePendingOperationsLocked();
   2039             return;
   2040         }
   2041 
   2042         try {
   2043             Parcel out = Parcel.obtain();
   2044             writePendingOperationLocked(op, out);
   2045             fos.write(out.marshall());
   2046             out.recycle();
   2047         } catch (java.io.IOException e1) {
   2048             Log.w(TAG, "Error writing pending operations", e1);
   2049         } finally {
   2050             try {
   2051                 fos.close();
   2052             } catch (java.io.IOException e2) {
   2053             }
   2054         }
   2055     }
   2056 
   2057     static private byte[] flattenBundle(Bundle bundle) {
   2058         byte[] flatData = null;
   2059         Parcel parcel = Parcel.obtain();
   2060         try {
   2061             bundle.writeToParcel(parcel, 0);
   2062             flatData = parcel.marshall();
   2063         } finally {
   2064             parcel.recycle();
   2065         }
   2066         return flatData;
   2067     }
   2068 
   2069     static private Bundle unflattenBundle(byte[] flatData) {
   2070         Bundle bundle;
   2071         Parcel parcel = Parcel.obtain();
   2072         try {
   2073             parcel.unmarshall(flatData, 0, flatData.length);
   2074             parcel.setDataPosition(0);
   2075             bundle = parcel.readBundle();
   2076         } catch (RuntimeException e) {
   2077             // A RuntimeException is thrown if we were unable to parse the parcel.
   2078             // Create an empty parcel in this case.
   2079             bundle = new Bundle();
   2080         } finally {
   2081             parcel.recycle();
   2082         }
   2083         return bundle;
   2084     }
   2085 
   2086     public static final int STATISTICS_FILE_END = 0;
   2087     public static final int STATISTICS_FILE_ITEM_OLD = 100;
   2088     public static final int STATISTICS_FILE_ITEM = 101;
   2089 
   2090     /**
   2091      * Read all sync statistics back in to the initial engine state.
   2092      */
   2093     private void readStatisticsLocked() {
   2094         try {
   2095             byte[] data = mStatisticsFile.readFully();
   2096             Parcel in = Parcel.obtain();
   2097             in.unmarshall(data, 0, data.length);
   2098             in.setDataPosition(0);
   2099             int token;
   2100             int index = 0;
   2101             while ((token=in.readInt()) != STATISTICS_FILE_END) {
   2102                 if (token == STATISTICS_FILE_ITEM
   2103                         || token == STATISTICS_FILE_ITEM_OLD) {
   2104                     int day = in.readInt();
   2105                     if (token == STATISTICS_FILE_ITEM_OLD) {
   2106                         day = day - 2009 + 14245;  // Magic!
   2107                     }
   2108                     DayStats ds = new DayStats(day);
   2109                     ds.successCount = in.readInt();
   2110                     ds.successTime = in.readLong();
   2111                     ds.failureCount = in.readInt();
   2112                     ds.failureTime = in.readLong();
   2113                     if (index < mDayStats.length) {
   2114                         mDayStats[index] = ds;
   2115                         index++;
   2116                     }
   2117                 } else {
   2118                     // Ooops.
   2119                     Log.w(TAG, "Unknown stats token: " + token);
   2120                     break;
   2121                 }
   2122             }
   2123         } catch (java.io.IOException e) {
   2124             Log.i(TAG, "No initial statistics");
   2125         }
   2126     }
   2127 
   2128     /**
   2129      * Write all sync statistics to the sync status file.
   2130      */
   2131     private void writeStatisticsLocked() {
   2132         if (DEBUG_FILE) Log.v(TAG, "Writing new " + mStatisticsFile.getBaseFile());
   2133 
   2134         // The file is being written, so we don't need to have a scheduled
   2135         // write until the next change.
   2136         removeMessages(MSG_WRITE_STATISTICS);
   2137 
   2138         FileOutputStream fos = null;
   2139         try {
   2140             fos = mStatisticsFile.startWrite();
   2141             Parcel out = Parcel.obtain();
   2142             final int N = mDayStats.length;
   2143             for (int i=0; i<N; i++) {
   2144                 DayStats ds = mDayStats[i];
   2145                 if (ds == null) {
   2146                     break;
   2147                 }
   2148                 out.writeInt(STATISTICS_FILE_ITEM);
   2149                 out.writeInt(ds.day);
   2150                 out.writeInt(ds.successCount);
   2151                 out.writeLong(ds.successTime);
   2152                 out.writeInt(ds.failureCount);
   2153                 out.writeLong(ds.failureTime);
   2154             }
   2155             out.writeInt(STATISTICS_FILE_END);
   2156             fos.write(out.marshall());
   2157             out.recycle();
   2158 
   2159             mStatisticsFile.finishWrite(fos);
   2160         } catch (java.io.IOException e1) {
   2161             Log.w(TAG, "Error writing stats", e1);
   2162             if (fos != null) {
   2163                 mStatisticsFile.failWrite(fos);
   2164             }
   2165         }
   2166     }
   2167 }
   2168