blob: 1ecab097594c5e19db93802bef472284e2caa616 [file] [log] [blame]
Dianne Hackborn231cc602009-04-27 17:10:36 -07001/*
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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017package android.content;
18
Jeff Sharkey8b2c3a142012-11-12 11:45:05 -080019import com.android.internal.annotations.VisibleForTesting;
Dianne Hackborn231cc602009-04-27 17:10:36 -070020import com.android.internal.util.ArrayUtils;
Dianne Hackborn2269d1572010-02-24 19:54:22 -080021import com.android.internal.util.FastXmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022
Jason parksa3cdaa52011-01-13 14:15:43 -060023import org.xmlpull.v1.XmlPullParser;
24import org.xmlpull.v1.XmlPullParserException;
25import org.xmlpull.v1.XmlSerializer;
26
Fred Quintanad9d2f112009-04-23 13:36:27 -070027import android.accounts.Account;
Amith Yamasanif29f2362012-04-05 18:29:52 -070028import android.accounts.AccountAndUser;
Yameng Huang2b5d0ea2011-01-11 14:00:19 +080029import android.content.res.Resources;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import android.database.Cursor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031import android.database.sqlite.SQLiteDatabase;
Dianne Hackborn231cc602009-04-27 17:10:36 -070032import android.database.sqlite.SQLiteException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import android.database.sqlite.SQLiteQueryBuilder;
Dianne Hackborn231cc602009-04-27 17:10:36 -070034import android.os.Bundle;
35import android.os.Environment;
36import android.os.Handler;
37import android.os.Message;
38import android.os.Parcel;
39import android.os.RemoteCallbackList;
40import android.os.RemoteException;
Dianne Hackborn39606a02012-07-31 17:54:35 -070041import android.util.AtomicFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042import android.util.Log;
Dianne Hackborn231cc602009-04-27 17:10:36 -070043import android.util.SparseArray;
44import android.util.Xml;
Jason parksa3cdaa52011-01-13 14:15:43 -060045import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046
Dianne Hackborn231cc602009-04-27 17:10:36 -070047import java.io.File;
48import java.io.FileInputStream;
49import java.io.FileOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050import java.util.ArrayList;
Dianne Hackborn231cc602009-04-27 17:10:36 -070051import java.util.Calendar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import java.util.HashMap;
Dianne Hackborn231cc602009-04-27 17:10:36 -070053import java.util.Iterator;
Ashish Sharma69d95de2012-04-11 17:27:24 -070054import java.util.Random;
Jason parks1125d782011-01-12 09:47:26 -060055import java.util.TimeZone;
Jason parksa3cdaa52011-01-13 14:15:43 -060056import java.util.List;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057
58/**
Dianne Hackborn231cc602009-04-27 17:10:36 -070059 * Singleton that tracks the sync data and overall sync
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060 * history on the device.
Costin Manolache360e4542009-09-04 13:36:04 -070061 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062 * @hide
63 */
Dianne Hackborn231cc602009-04-27 17:10:36 -070064public class SyncStorageEngine extends Handler {
Amith Yamasani04e0d262012-02-14 11:50:53 -080065
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066 private static final String TAG = "SyncManager";
Dianne Hackborn40e9f292012-11-27 19:12:23 -080067 private static final boolean DEBUG = false;
Dianne Hackborn231cc602009-04-27 17:10:36 -070068 private static final boolean DEBUG_FILE = false;
Costin Manolache360e4542009-09-04 13:36:04 -070069
Amith Yamasani04e0d262012-02-14 11:50:53 -080070 private static final String XML_ATTR_NEXT_AUTHORITY_ID = "nextAuthorityId";
71 private static final String XML_ATTR_LISTEN_FOR_TICKLES = "listen-for-tickles";
Ashish Sharma69d95de2012-04-11 17:27:24 -070072 private static final String XML_ATTR_SYNC_RANDOM_OFFSET = "offsetInSeconds";
Amith Yamasani04e0d262012-02-14 11:50:53 -080073 private static final String XML_ATTR_ENABLED = "enabled";
74 private static final String XML_ATTR_USER = "user";
75 private static final String XML_TAG_LISTEN_FOR_TICKLES = "listenForTickles";
76
Fred Quintanac5d1c6d2010-01-27 12:17:49 -080077 private static final long DEFAULT_POLL_FREQUENCY_SECONDS = 60 * 60 * 24; // One day
78
Jeff Sharkey8b2c3a142012-11-12 11:45:05 -080079 @VisibleForTesting
Dianne Hackborn231cc602009-04-27 17:10:36 -070080 static final long MILLIS_IN_4WEEKS = 1000L * 60 * 60 * 24 * 7 * 4;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081
Dianne Hackborn231cc602009-04-27 17:10:36 -070082 /** Enum value for a sync start event. */
83 public static final int EVENT_START = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084
Dianne Hackborn231cc602009-04-27 17:10:36 -070085 /** Enum value for a sync stop event. */
86 public static final int EVENT_STOP = 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087
Dianne Hackborn231cc602009-04-27 17:10:36 -070088 // TODO: i18n -- grab these out of resources.
89 /** String names for the sync event types. */
90 public static final String[] EVENTS = { "START", "STOP" };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091
Dianne Hackborn231cc602009-04-27 17:10:36 -070092 /** Enum value for a server-initiated sync. */
93 public static final int SOURCE_SERVER = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094
Dianne Hackborn231cc602009-04-27 17:10:36 -070095 /** Enum value for a local-initiated sync. */
96 public static final int SOURCE_LOCAL = 1;
97 /**
98 * Enum value for a poll-based sync (e.g., upon connection to
99 * network)
100 */
101 public static final int SOURCE_POLL = 2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102
Dianne Hackborn231cc602009-04-27 17:10:36 -0700103 /** Enum value for a user-initiated sync. */
104 public static final int SOURCE_USER = 3;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105
Fred Quintanac5d1c6d2010-01-27 12:17:49 -0800106 /** Enum value for a periodic sync. */
107 public static final int SOURCE_PERIODIC = 4;
108
Fred Quintana307da1a2010-01-21 14:24:20 -0800109 public static final long NOT_IN_BACKOFF_MODE = -1;
110
Dianne Hackborn043fcd92010-10-06 14:27:34 -0700111 public static final Intent SYNC_CONNECTION_SETTING_CHANGED_INTENT =
Fred Quintanaac9385e2009-06-22 18:00:59 -0700112 new Intent("com.android.sync.SYNC_CONN_STATUS_CHANGED");
113
Dianne Hackborn231cc602009-04-27 17:10:36 -0700114 // TODO: i18n -- grab these out of resources.
115 /** String names for the sync source types. */
116 public static final String[] SOURCES = { "SERVER",
117 "LOCAL",
118 "POLL",
Fred Quintanac5d1c6d2010-01-27 12:17:49 -0800119 "USER",
120 "PERIODIC" };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121
Dianne Hackborn231cc602009-04-27 17:10:36 -0700122 // The MESG column will contain one of these or one of the Error types.
123 public static final String MESG_SUCCESS = "success";
124 public static final String MESG_CANCELED = "canceled";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125
Dianne Hackborna33e3f72009-09-29 17:28:24 -0700126 public static final int MAX_HISTORY = 100;
Costin Manolache360e4542009-09-04 13:36:04 -0700127
Dianne Hackborn231cc602009-04-27 17:10:36 -0700128 private static final int MSG_WRITE_STATUS = 1;
129 private static final long WRITE_STATUS_DELAY = 1000*60*10; // 10 minutes
Costin Manolache360e4542009-09-04 13:36:04 -0700130
Dianne Hackborn231cc602009-04-27 17:10:36 -0700131 private static final int MSG_WRITE_STATISTICS = 2;
132 private static final long WRITE_STATISTICS_DELAY = 1000*60*30; // 1/2 hour
Joe Onorato8294fad2009-07-15 16:08:44 -0700133
134 private static final boolean SYNC_ENABLED_DEFAULT = false;
Costin Manolache360e4542009-09-04 13:36:04 -0700135
Fred Quintanac2e46912010-03-15 16:10:44 -0700136 // the version of the accounts xml file format
Fred Quintanafb084402010-03-23 17:57:03 -0700137 private static final int ACCOUNTS_VERSION = 2;
138
139 private static HashMap<String, String> sAuthorityRenames;
140
141 static {
142 sAuthorityRenames = new HashMap<String, String>();
143 sAuthorityRenames.put("contacts", "com.android.contacts");
144 sAuthorityRenames.put("calendar", "com.android.calendar");
145 }
Fred Quintanac2e46912010-03-15 16:10:44 -0700146
Dianne Hackborn231cc602009-04-27 17:10:36 -0700147 public static class PendingOperation {
Dianne Hackborn7a135592009-05-06 00:28:37 -0700148 final Account account;
Amith Yamasani04e0d262012-02-14 11:50:53 -0800149 final int userId;
Dianne Hackborn231cc602009-04-27 17:10:36 -0700150 final int syncSource;
151 final String authority;
152 final Bundle extras; // note: read-only.
Fred Quintana307da1a2010-01-21 14:24:20 -0800153 final boolean expedited;
Costin Manolache360e4542009-09-04 13:36:04 -0700154
Dianne Hackborn231cc602009-04-27 17:10:36 -0700155 int authorityId;
156 byte[] flatExtras;
Costin Manolache360e4542009-09-04 13:36:04 -0700157
Amith Yamasani04e0d262012-02-14 11:50:53 -0800158 PendingOperation(Account account, int userId, int source,
Fred Quintana307da1a2010-01-21 14:24:20 -0800159 String authority, Bundle extras, boolean expedited) {
Dianne Hackborn231cc602009-04-27 17:10:36 -0700160 this.account = account;
Amith Yamasani04e0d262012-02-14 11:50:53 -0800161 this.userId = userId;
Dianne Hackborn231cc602009-04-27 17:10:36 -0700162 this.syncSource = source;
163 this.authority = authority;
164 this.extras = extras != null ? new Bundle(extras) : extras;
Fred Quintana307da1a2010-01-21 14:24:20 -0800165 this.expedited = expedited;
Dianne Hackborn231cc602009-04-27 17:10:36 -0700166 this.authorityId = -1;
167 }
168
169 PendingOperation(PendingOperation other) {
170 this.account = other.account;
Amith Yamasani04e0d262012-02-14 11:50:53 -0800171 this.userId = other.userId;
Dianne Hackborn231cc602009-04-27 17:10:36 -0700172 this.syncSource = other.syncSource;
173 this.authority = other.authority;
174 this.extras = other.extras;
175 this.authorityId = other.authorityId;
Fred Quintana307da1a2010-01-21 14:24:20 -0800176 this.expedited = other.expedited;
Dianne Hackborn231cc602009-04-27 17:10:36 -0700177 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178 }
Costin Manolache360e4542009-09-04 13:36:04 -0700179
Dianne Hackborn231cc602009-04-27 17:10:36 -0700180 static class AccountInfo {
Amith Yamasani04e0d262012-02-14 11:50:53 -0800181 final AccountAndUser accountAndUser;
Dianne Hackborn231cc602009-04-27 17:10:36 -0700182 final HashMap<String, AuthorityInfo> authorities =
183 new HashMap<String, AuthorityInfo>();
Costin Manolache360e4542009-09-04 13:36:04 -0700184
Amith Yamasani04e0d262012-02-14 11:50:53 -0800185 AccountInfo(AccountAndUser accountAndUser) {
186 this.accountAndUser = accountAndUser;
Dianne Hackborn231cc602009-04-27 17:10:36 -0700187 }
188 }
Costin Manolache360e4542009-09-04 13:36:04 -0700189
Dianne Hackborn231cc602009-04-27 17:10:36 -0700190 public static class AuthorityInfo {
Dianne Hackborn7a135592009-05-06 00:28:37 -0700191 final Account account;
Amith Yamasani04e0d262012-02-14 11:50:53 -0800192 final int userId;
Dianne Hackborn231cc602009-04-27 17:10:36 -0700193 final String authority;
194 final int ident;
195 boolean enabled;
Fred Quintana5e787c42009-08-16 23:13:53 -0700196 int syncable;
Fred Quintana307da1a2010-01-21 14:24:20 -0800197 long backoffTime;
198 long backoffDelay;
199 long delayUntil;
Fred Quintanac5d1c6d2010-01-27 12:17:49 -0800200 final ArrayList<Pair<Bundle, Long>> periodicSyncs;
Fred Quintanaac9385e2009-06-22 18:00:59 -0700201
Carlos Valdivia3aca7d72012-05-07 19:03:51 -0700202 /**
203 * Copy constructor for making deep-ish copies. Only the bundles stored
204 * in periodic syncs can make unexpected changes.
205 *
206 * @param toCopy AuthorityInfo to be copied.
207 */
208 AuthorityInfo(AuthorityInfo toCopy) {
209 account = toCopy.account;
210 userId = toCopy.userId;
211 authority = toCopy.authority;
212 ident = toCopy.ident;
213 enabled = toCopy.enabled;
214 syncable = toCopy.syncable;
215 backoffTime = toCopy.backoffTime;
216 backoffDelay = toCopy.backoffDelay;
217 delayUntil = toCopy.delayUntil;
218 periodicSyncs = new ArrayList<Pair<Bundle, Long>>();
219 for (Pair<Bundle, Long> sync : toCopy.periodicSyncs) {
220 // Still not a perfect copy, because we are just copying the mappings.
221 periodicSyncs.add(Pair.create(new Bundle(sync.first), sync.second));
222 }
223 }
224
Amith Yamasani04e0d262012-02-14 11:50:53 -0800225 AuthorityInfo(Account account, int userId, String authority, int ident) {
Dianne Hackborn231cc602009-04-27 17:10:36 -0700226 this.account = account;
Amith Yamasani04e0d262012-02-14 11:50:53 -0800227 this.userId = userId;
Dianne Hackborn231cc602009-04-27 17:10:36 -0700228 this.authority = authority;
229 this.ident = ident;
Joe Onorato8294fad2009-07-15 16:08:44 -0700230 enabled = SYNC_ENABLED_DEFAULT;
Fred Quintana4a6679b2009-08-17 13:05:39 -0700231 syncable = -1; // default to "unknown"
Fred Quintana307da1a2010-01-21 14:24:20 -0800232 backoffTime = -1; // if < 0 then we aren't in backoff mode
233 backoffDelay = -1; // if < 0 then we aren't in backoff mode
Fred Quintanac5d1c6d2010-01-27 12:17:49 -0800234 periodicSyncs = new ArrayList<Pair<Bundle, Long>>();
235 periodicSyncs.add(Pair.create(new Bundle(), DEFAULT_POLL_FREQUENCY_SECONDS));
Dianne Hackborn231cc602009-04-27 17:10:36 -0700236 }
237 }
Costin Manolache360e4542009-09-04 13:36:04 -0700238
Dianne Hackborn231cc602009-04-27 17:10:36 -0700239 public static class SyncHistoryItem {
240 int authorityId;
241 int historyId;
242 long eventTime;
243 long elapsedTime;
244 int source;
245 int event;
246 long upstreamActivity;
247 long downstreamActivity;
248 String mesg;
Fred Quintanadc475562012-05-04 15:51:54 -0700249 boolean initialization;
Dianne Hackborn231cc602009-04-27 17:10:36 -0700250 }
Costin Manolache360e4542009-09-04 13:36:04 -0700251
Dianne Hackborn231cc602009-04-27 17:10:36 -0700252 public static class DayStats {
253 public final int day;
254 public int successCount;
255 public long successTime;
256 public int failureCount;
257 public long failureTime;
Costin Manolache360e4542009-09-04 13:36:04 -0700258
Dianne Hackborn231cc602009-04-27 17:10:36 -0700259 public DayStats(int day) {
260 this.day = day;
261 }
262 }
Costin Manolache360e4542009-09-04 13:36:04 -0700263
Amith Yamasani04e0d262012-02-14 11:50:53 -0800264 interface OnSyncRequestListener {
265 /**
266 * Called when a sync is needed on an account(s) due to some change in state.
267 * @param account
268 * @param userId
269 * @param authority
270 * @param extras
271 */
272 public void onSyncRequest(Account account, int userId, String authority, Bundle extras);
273 }
274
Dianne Hackborn231cc602009-04-27 17:10:36 -0700275 // Primary list of all syncable authorities. Also our global lock.
276 private final SparseArray<AuthorityInfo> mAuthorities =
277 new SparseArray<AuthorityInfo>();
Costin Manolache360e4542009-09-04 13:36:04 -0700278
Amith Yamasani04e0d262012-02-14 11:50:53 -0800279 private final HashMap<AccountAndUser, AccountInfo> mAccounts
280 = new HashMap<AccountAndUser, AccountInfo>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281
Dianne Hackborn231cc602009-04-27 17:10:36 -0700282 private final ArrayList<PendingOperation> mPendingOperations =
283 new ArrayList<PendingOperation>();
Costin Manolache360e4542009-09-04 13:36:04 -0700284
Amith Yamasani04e0d262012-02-14 11:50:53 -0800285 private final SparseArray<ArrayList<SyncInfo>> mCurrentSyncs
286 = new SparseArray<ArrayList<SyncInfo>>();
Costin Manolache360e4542009-09-04 13:36:04 -0700287
Dianne Hackborn231cc602009-04-27 17:10:36 -0700288 private final SparseArray<SyncStatusInfo> mSyncStatus =
289 new SparseArray<SyncStatusInfo>();
Costin Manolache360e4542009-09-04 13:36:04 -0700290
Dianne Hackborn231cc602009-04-27 17:10:36 -0700291 private final ArrayList<SyncHistoryItem> mSyncHistory =
292 new ArrayList<SyncHistoryItem>();
Costin Manolache360e4542009-09-04 13:36:04 -0700293
Dianne Hackborn231cc602009-04-27 17:10:36 -0700294 private final RemoteCallbackList<ISyncStatusObserver> mChangeListeners
295 = new RemoteCallbackList<ISyncStatusObserver>();
Costin Manolache360e4542009-09-04 13:36:04 -0700296
Fred Quintana77c560f2010-03-29 22:20:26 -0700297 private int mNextAuthorityId = 0;
298
Dianne Hackborn231cc602009-04-27 17:10:36 -0700299 // We keep 4 weeks of stats.
300 private final DayStats[] mDayStats = new DayStats[7*4];
301 private final Calendar mCal;
302 private int mYear;
303 private int mYearInDays;
Costin Manolache360e4542009-09-04 13:36:04 -0700304
Dianne Hackborn231cc602009-04-27 17:10:36 -0700305 private final Context mContext;
Fred Quintanac5d1c6d2010-01-27 12:17:49 -0800306
Dianne Hackborn231cc602009-04-27 17:10:36 -0700307 private static volatile SyncStorageEngine sSyncStorageEngine = null;
Costin Manolache360e4542009-09-04 13:36:04 -0700308
Ashish Sharma69d95de2012-04-11 17:27:24 -0700309 private int mSyncRandomOffset;
310
Dianne Hackborn231cc602009-04-27 17:10:36 -0700311 /**
312 * This file contains the core engine state: all accounts and the
313 * settings for them. It must never be lost, and should be changed
314 * infrequently, so it is stored as an XML file.
315 */
316 private final AtomicFile mAccountInfoFile;
Costin Manolache360e4542009-09-04 13:36:04 -0700317
Dianne Hackborn231cc602009-04-27 17:10:36 -0700318 /**
319 * This file contains the current sync status. We would like to retain
320 * it across boots, but its loss is not the end of the world, so we store
321 * this information as binary data.
322 */
323 private final AtomicFile mStatusFile;
Costin Manolache360e4542009-09-04 13:36:04 -0700324
Dianne Hackborn231cc602009-04-27 17:10:36 -0700325 /**
326 * This file contains sync statistics. This is purely debugging information
327 * so is written infrequently and can be thrown away at any time.
328 */
329 private final AtomicFile mStatisticsFile;
Costin Manolache360e4542009-09-04 13:36:04 -0700330
Dianne Hackborn231cc602009-04-27 17:10:36 -0700331 /**
332 * This file contains the pending sync operations. It is a binary file,
333 * which must be updated every time an operation is added or removed,
334 * so we have special handling of it.
335 */
336 private final AtomicFile mPendingFile;
337 private static final int PENDING_FINISH_TO_WRITE = 4;
338 private int mNumPendingFinished = 0;
Costin Manolache360e4542009-09-04 13:36:04 -0700339
Dianne Hackborn231cc602009-04-27 17:10:36 -0700340 private int mNextHistoryId = 0;
Amith Yamasani04e0d262012-02-14 11:50:53 -0800341 private SparseArray<Boolean> mMasterSyncAutomatically = new SparseArray<Boolean>();
Yameng Huang2b5d0ea2011-01-11 14:00:19 +0800342 private boolean mDefaultMasterSyncAutomatically;
Amith Yamasani04e0d262012-02-14 11:50:53 -0800343
344 private OnSyncRequestListener mSyncRequestListener;
Costin Manolache360e4542009-09-04 13:36:04 -0700345
Fred Quintanac5d1c6d2010-01-27 12:17:49 -0800346 private SyncStorageEngine(Context context, File dataDir) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800347 mContext = context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348 sSyncStorageEngine = this;
Costin Manolache360e4542009-09-04 13:36:04 -0700349
Dianne Hackborn231cc602009-04-27 17:10:36 -0700350 mCal = Calendar.getInstance(TimeZone.getTimeZone("GMT+0"));
Costin Manolache360e4542009-09-04 13:36:04 -0700351
Yameng Huang2b5d0ea2011-01-11 14:00:19 +0800352 mDefaultMasterSyncAutomatically = mContext.getResources().getBoolean(
353 com.android.internal.R.bool.config_syncstorageengine_masterSyncAutomatically);
354
Dianne Hackborn231cc602009-04-27 17:10:36 -0700355 File systemDir = new File(dataDir, "system");
356 File syncDir = new File(systemDir, "sync");
Fred Quintanac5d1c6d2010-01-27 12:17:49 -0800357 syncDir.mkdirs();
Dianne Hackborn231cc602009-04-27 17:10:36 -0700358 mAccountInfoFile = new AtomicFile(new File(syncDir, "accounts.xml"));
359 mStatusFile = new AtomicFile(new File(syncDir, "status.bin"));
360 mPendingFile = new AtomicFile(new File(syncDir, "pending.bin"));
361 mStatisticsFile = new AtomicFile(new File(syncDir, "stats.bin"));
Costin Manolache360e4542009-09-04 13:36:04 -0700362
Dianne Hackborn231cc602009-04-27 17:10:36 -0700363 readAccountInfoLocked();
364 readStatusLocked();
365 readPendingOperationsLocked();
366 readStatisticsLocked();
Fred Quintana77c560f2010-03-29 22:20:26 -0700367 readAndDeleteLegacyAccountInfoLocked();
368 writeAccountInfoLocked();
369 writeStatusLocked();
370 writePendingOperationsLocked();
371 writeStatisticsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800372 }
373
374 public static SyncStorageEngine newTestInstance(Context context) {
Fred Quintanac5d1c6d2010-01-27 12:17:49 -0800375 return new SyncStorageEngine(context, context.getFilesDir());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800376 }
377
378 public static void init(Context context) {
379 if (sSyncStorageEngine != null) {
Fred Quintana307da1a2010-01-21 14:24:20 -0800380 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800381 }
Fred Quintanac5d1c6d2010-01-27 12:17:49 -0800382 // This call will return the correct directory whether Encrypted File Systems is
383 // enabled or not.
Jason parksa3cdaa52011-01-13 14:15:43 -0600384 File dataDir = Environment.getSecureDataDirectory();
Fred Quintanac5d1c6d2010-01-27 12:17:49 -0800385 sSyncStorageEngine = new SyncStorageEngine(context, dataDir);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800386 }
387
388 public static SyncStorageEngine getSingleton() {
389 if (sSyncStorageEngine == null) {
390 throw new IllegalStateException("not initialized");
391 }
392 return sSyncStorageEngine;
393 }
394
Amith Yamasani04e0d262012-02-14 11:50:53 -0800395 protected void setOnSyncRequestListener(OnSyncRequestListener listener) {
396 if (mSyncRequestListener == null) {
397 mSyncRequestListener = listener;
398 }
399 }
400
Dianne Hackborn231cc602009-04-27 17:10:36 -0700401 @Override public void handleMessage(Message msg) {
402 if (msg.what == MSG_WRITE_STATUS) {
Dianne Hackborn4e808202010-04-06 22:00:59 -0700403 synchronized (mAuthorities) {
Dianne Hackborn231cc602009-04-27 17:10:36 -0700404 writeStatusLocked();
Fred Quintanad9d2f112009-04-23 13:36:27 -0700405 }
Dianne Hackborn231cc602009-04-27 17:10:36 -0700406 } else if (msg.what == MSG_WRITE_STATISTICS) {
Dianne Hackborn4e808202010-04-06 22:00:59 -0700407 synchronized (mAuthorities) {
Dianne Hackborn231cc602009-04-27 17:10:36 -0700408 writeStatisticsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800409 }
410 }
411 }
Costin Manolache360e4542009-09-04 13:36:04 -0700412
Ashish Sharma69d95de2012-04-11 17:27:24 -0700413 public int getSyncRandomOffset() {
414 return mSyncRandomOffset;
415 }
416
Dianne Hackborn231cc602009-04-27 17:10:36 -0700417 public void addStatusChangeListener(int mask, ISyncStatusObserver callback) {
418 synchronized (mAuthorities) {
419 mChangeListeners.register(callback, mask);
420 }
421 }
Costin Manolache360e4542009-09-04 13:36:04 -0700422
Dianne Hackborn231cc602009-04-27 17:10:36 -0700423 public void removeStatusChangeListener(ISyncStatusObserver callback) {
424 synchronized (mAuthorities) {
425 mChangeListeners.unregister(callback);
426 }
427 }
Costin Manolache360e4542009-09-04 13:36:04 -0700428
Dianne Hackborn231cc602009-04-27 17:10:36 -0700429 private void reportChange(int which) {
430 ArrayList<ISyncStatusObserver> reports = null;
431 synchronized (mAuthorities) {
432 int i = mChangeListeners.beginBroadcast();
433 while (i > 0) {
434 i--;
435 Integer mask = (Integer)mChangeListeners.getBroadcastCookie(i);
436 if ((which & mask.intValue()) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800437 continue;
438 }
Dianne Hackborn231cc602009-04-27 17:10:36 -0700439 if (reports == null) {
440 reports = new ArrayList<ISyncStatusObserver>(i);
441 }
442 reports.add(mChangeListeners.getBroadcastItem(i));
443 }
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700444 mChangeListeners.finishBroadcast();
Dianne Hackborn231cc602009-04-27 17:10:36 -0700445 }
Costin Manolache360e4542009-09-04 13:36:04 -0700446
Dianne Hackborn40e9f292012-11-27 19:12:23 -0800447 if (DEBUG) {
Fred Quintana77c560f2010-03-29 22:20:26 -0700448 Log.v(TAG, "reportChange " + which + " to: " + reports);
449 }
Costin Manolache360e4542009-09-04 13:36:04 -0700450
Dianne Hackborn231cc602009-04-27 17:10:36 -0700451 if (reports != null) {
452 int i = reports.size();
453 while (i > 0) {
454 i--;
455 try {
456 reports.get(i).onStatusChanged(which);
457 } catch (RemoteException e) {
458 // The remote callback list will take care of this for us.
459 }
460 }
461 }
462 }
Amith Yamasani70c874b2009-07-06 14:53:25 -0700463
Amith Yamasani04e0d262012-02-14 11:50:53 -0800464 public boolean getSyncAutomatically(Account account, int userId, String providerName) {
Dianne Hackborn231cc602009-04-27 17:10:36 -0700465 synchronized (mAuthorities) {
466 if (account != null) {
Amith Yamasani04e0d262012-02-14 11:50:53 -0800467 AuthorityInfo authority = getAuthorityLocked(account, userId, providerName,
Fred Quintanaac9385e2009-06-22 18:00:59 -0700468 "getSyncAutomatically");
469 return authority != null && authority.enabled;
Dianne Hackborn231cc602009-04-27 17:10:36 -0700470 }
Fred Quintanaac9385e2009-06-22 18:00:59 -0700471
Dianne Hackborn231cc602009-04-27 17:10:36 -0700472 int i = mAuthorities.size();
473 while (i > 0) {
474 i--;
Costin Manolache360e4542009-09-04 13:36:04 -0700475 AuthorityInfo authority = mAuthorities.valueAt(i);
Dianne Hackborn231cc602009-04-27 17:10:36 -0700476 if (authority.authority.equals(providerName)
Amith Yamasani04e0d262012-02-14 11:50:53 -0800477 && authority.userId == userId
Dianne Hackborn231cc602009-04-27 17:10:36 -0700478 && authority.enabled) {
479 return true;
480 }
481 }
482 return false;
483 }
484 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800485
Amith Yamasani04e0d262012-02-14 11:50:53 -0800486 public void setSyncAutomatically(Account account, int userId, String providerName,
487 boolean sync) {
Dianne Hackborn40e9f292012-11-27 19:12:23 -0800488 if (DEBUG) {
489 Log.d(TAG, "setSyncAutomatically: " + /* account + */" provider " + providerName
490 + ", user " + userId + " -> " + sync);
491 }
Dianne Hackborn231cc602009-04-27 17:10:36 -0700492 synchronized (mAuthorities) {
Amith Yamasani04e0d262012-02-14 11:50:53 -0800493 AuthorityInfo authority = getOrCreateAuthorityLocked(account, userId, providerName, -1,
494 false);
Fred Quintana77c560f2010-03-29 22:20:26 -0700495 if (authority.enabled == sync) {
Dianne Hackborn40e9f292012-11-27 19:12:23 -0800496 if (DEBUG) {
497 Log.d(TAG, "setSyncAutomatically: already set to " + sync + ", doing nothing");
498 }
Fred Quintana77c560f2010-03-29 22:20:26 -0700499 return;
500 }
Joe Onorato8294fad2009-07-15 16:08:44 -0700501 authority.enabled = sync;
Dianne Hackborn231cc602009-04-27 17:10:36 -0700502 writeAccountInfoLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800503 }
Joe Onorato8294fad2009-07-15 16:08:44 -0700504
Fred Quintana77c560f2010-03-29 22:20:26 -0700505 if (sync) {
Amith Yamasani04e0d262012-02-14 11:50:53 -0800506 requestSync(account, userId, providerName, new Bundle());
Joe Onorato8294fad2009-07-15 16:08:44 -0700507 }
Fred Quintanaac9385e2009-06-22 18:00:59 -0700508 reportChange(ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800509 }
510
Amith Yamasani04e0d262012-02-14 11:50:53 -0800511 public int getIsSyncable(Account account, int userId, String providerName) {
Fred Quintana5e787c42009-08-16 23:13:53 -0700512 synchronized (mAuthorities) {
513 if (account != null) {
Amith Yamasani04e0d262012-02-14 11:50:53 -0800514 AuthorityInfo authority = getAuthorityLocked(account, userId, providerName,
Fred Quintana5e787c42009-08-16 23:13:53 -0700515 "getIsSyncable");
516 if (authority == null) {
517 return -1;
518 }
519 return authority.syncable;
520 }
521
522 int i = mAuthorities.size();
523 while (i > 0) {
524 i--;
Costin Manolache360e4542009-09-04 13:36:04 -0700525 AuthorityInfo authority = mAuthorities.valueAt(i);
Fred Quintana5e787c42009-08-16 23:13:53 -0700526 if (authority.authority.equals(providerName)) {
527 return authority.syncable;
528 }
529 }
530 return -1;
531 }
532 }
533
Amith Yamasani04e0d262012-02-14 11:50:53 -0800534 public void setIsSyncable(Account account, int userId, String providerName, int syncable) {
Fred Quintanab763ab22009-08-18 18:07:30 -0700535 if (syncable > 1) {
536 syncable = 1;
537 } else if (syncable < -1) {
538 syncable = -1;
539 }
Dianne Hackborn40e9f292012-11-27 19:12:23 -0800540 if (DEBUG) {
541 Log.d(TAG, "setIsSyncable: " + account + ", provider " + providerName
542 + ", user " + userId + " -> " + syncable);
543 }
Fred Quintana5e787c42009-08-16 23:13:53 -0700544 synchronized (mAuthorities) {
Amith Yamasani04e0d262012-02-14 11:50:53 -0800545 AuthorityInfo authority = getOrCreateAuthorityLocked(account, userId, providerName, -1,
546 false);
Fred Quintana77c560f2010-03-29 22:20:26 -0700547 if (authority.syncable == syncable) {
Dianne Hackborn40e9f292012-11-27 19:12:23 -0800548 if (DEBUG) {
549 Log.d(TAG, "setIsSyncable: already set to " + syncable + ", doing nothing");
550 }
Fred Quintana77c560f2010-03-29 22:20:26 -0700551 return;
552 }
Fred Quintana5e787c42009-08-16 23:13:53 -0700553 authority.syncable = syncable;
554 writeAccountInfoLocked();
555 }
556
Fred Quintana77c560f2010-03-29 22:20:26 -0700557 if (syncable > 0) {
Amith Yamasani04e0d262012-02-14 11:50:53 -0800558 requestSync(account, userId, providerName, new Bundle());
Fred Quintana5e787c42009-08-16 23:13:53 -0700559 }
560 reportChange(ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS);
561 }
562
Amith Yamasani04e0d262012-02-14 11:50:53 -0800563 public Pair<Long, Long> getBackoff(Account account, int userId, String providerName) {
Fred Quintana307da1a2010-01-21 14:24:20 -0800564 synchronized (mAuthorities) {
Amith Yamasani04e0d262012-02-14 11:50:53 -0800565 AuthorityInfo authority = getAuthorityLocked(account, userId, providerName,
566 "getBackoff");
Fred Quintana307da1a2010-01-21 14:24:20 -0800567 if (authority == null || authority.backoffTime < 0) {
568 return null;
569 }
570 return Pair.create(authority.backoffTime, authority.backoffDelay);
571 }
572 }
573
Amith Yamasani04e0d262012-02-14 11:50:53 -0800574 public void setBackoff(Account account, int userId, String providerName,
Fred Quintana307da1a2010-01-21 14:24:20 -0800575 long nextSyncTime, long nextDelay) {
Dianne Hackborn40e9f292012-11-27 19:12:23 -0800576 if (DEBUG) {
Fred Quintana307da1a2010-01-21 14:24:20 -0800577 Log.v(TAG, "setBackoff: " + account + ", provider " + providerName
Amith Yamasani04e0d262012-02-14 11:50:53 -0800578 + ", user " + userId
Fred Quintana307da1a2010-01-21 14:24:20 -0800579 + " -> nextSyncTime " + nextSyncTime + ", nextDelay " + nextDelay);
580 }
581 boolean changed = false;
582 synchronized (mAuthorities) {
583 if (account == null || providerName == null) {
584 for (AccountInfo accountInfo : mAccounts.values()) {
Amith Yamasani04e0d262012-02-14 11:50:53 -0800585 if (account != null && !account.equals(accountInfo.accountAndUser.account)
586 && userId != accountInfo.accountAndUser.userId) {
587 continue;
588 }
Fred Quintana307da1a2010-01-21 14:24:20 -0800589 for (AuthorityInfo authorityInfo : accountInfo.authorities.values()) {
590 if (providerName != null && !providerName.equals(authorityInfo.authority)) {
591 continue;
592 }
593 if (authorityInfo.backoffTime != nextSyncTime
594 || authorityInfo.backoffDelay != nextDelay) {
595 authorityInfo.backoffTime = nextSyncTime;
596 authorityInfo.backoffDelay = nextDelay;
597 changed = true;
598 }
599 }
600 }
601 } else {
602 AuthorityInfo authority =
Amith Yamasani04e0d262012-02-14 11:50:53 -0800603 getOrCreateAuthorityLocked(account, userId, providerName, -1 /* ident */,
604 true);
Fred Quintana307da1a2010-01-21 14:24:20 -0800605 if (authority.backoffTime == nextSyncTime && authority.backoffDelay == nextDelay) {
606 return;
607 }
608 authority.backoffTime = nextSyncTime;
609 authority.backoffDelay = nextDelay;
610 changed = true;
611 }
Fred Quintana307da1a2010-01-21 14:24:20 -0800612 }
613
614 if (changed) {
615 reportChange(ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS);
616 }
617 }
618
Alon Alberted1d2532011-02-15 14:02:14 -0800619 public void clearAllBackoffs(SyncQueue syncQueue) {
Alon Albert744e310f2010-12-14 11:37:20 -0800620 boolean changed = false;
621 synchronized (mAuthorities) {
Jeff Sharkeya706e2f2012-10-16 12:02:42 -0700622 synchronized (syncQueue) {
623 for (AccountInfo accountInfo : mAccounts.values()) {
624 for (AuthorityInfo authorityInfo : accountInfo.authorities.values()) {
625 if (authorityInfo.backoffTime != NOT_IN_BACKOFF_MODE
626 || authorityInfo.backoffDelay != NOT_IN_BACKOFF_MODE) {
Dianne Hackborn40e9f292012-11-27 19:12:23 -0800627 if (DEBUG) {
Jeff Sharkeya706e2f2012-10-16 12:02:42 -0700628 Log.v(TAG, "clearAllBackoffs:"
629 + " authority:" + authorityInfo.authority
630 + " account:" + accountInfo.accountAndUser.account.name
631 + " user:" + accountInfo.accountAndUser.userId
632 + " backoffTime was: " + authorityInfo.backoffTime
633 + " backoffDelay was: " + authorityInfo.backoffDelay);
634 }
635 authorityInfo.backoffTime = NOT_IN_BACKOFF_MODE;
636 authorityInfo.backoffDelay = NOT_IN_BACKOFF_MODE;
637 syncQueue.onBackoffChanged(accountInfo.accountAndUser.account,
638 accountInfo.accountAndUser.userId, authorityInfo.authority, 0);
639 changed = true;
Alon Albert744e310f2010-12-14 11:37:20 -0800640 }
Alon Albert744e310f2010-12-14 11:37:20 -0800641 }
642 }
643 }
644 }
645
646 if (changed) {
647 reportChange(ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS);
648 }
649 }
650
Amith Yamasani04e0d262012-02-14 11:50:53 -0800651 public void setDelayUntilTime(Account account, int userId, String providerName,
652 long delayUntil) {
Dianne Hackborn40e9f292012-11-27 19:12:23 -0800653 if (DEBUG) {
Fred Quintana307da1a2010-01-21 14:24:20 -0800654 Log.v(TAG, "setDelayUntil: " + account + ", provider " + providerName
Amith Yamasani04e0d262012-02-14 11:50:53 -0800655 + ", user " + userId + " -> delayUntil " + delayUntil);
Fred Quintana307da1a2010-01-21 14:24:20 -0800656 }
657 synchronized (mAuthorities) {
Fred Quintanac5d1c6d2010-01-27 12:17:49 -0800658 AuthorityInfo authority = getOrCreateAuthorityLocked(
Amith Yamasani04e0d262012-02-14 11:50:53 -0800659 account, userId, providerName, -1 /* ident */, true);
Fred Quintana307da1a2010-01-21 14:24:20 -0800660 if (authority.delayUntil == delayUntil) {
661 return;
662 }
663 authority.delayUntil = delayUntil;
Fred Quintana307da1a2010-01-21 14:24:20 -0800664 }
665
666 reportChange(ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS);
667 }
668
Amith Yamasani04e0d262012-02-14 11:50:53 -0800669 public long getDelayUntilTime(Account account, int userId, String providerName) {
Fred Quintana307da1a2010-01-21 14:24:20 -0800670 synchronized (mAuthorities) {
Amith Yamasani04e0d262012-02-14 11:50:53 -0800671 AuthorityInfo authority = getAuthorityLocked(account, userId, providerName,
672 "getDelayUntil");
Fred Quintana307da1a2010-01-21 14:24:20 -0800673 if (authority == null) {
674 return 0;
675 }
676 return authority.delayUntil;
677 }
678 }
679
Amith Yamasani04e0d262012-02-14 11:50:53 -0800680 private void updateOrRemovePeriodicSync(Account account, int userId, String providerName,
681 Bundle extras,
Fred Quintanac5d1c6d2010-01-27 12:17:49 -0800682 long period, boolean add) {
683 if (period <= 0) {
684 period = 0;
685 }
686 if (extras == null) {
687 extras = new Bundle();
688 }
Dianne Hackborn40e9f292012-11-27 19:12:23 -0800689 if (DEBUG) {
Amith Yamasani04e0d262012-02-14 11:50:53 -0800690 Log.v(TAG, "addOrRemovePeriodicSync: " + account + ", user " + userId
691 + ", provider " + providerName
Fred Quintanac5d1c6d2010-01-27 12:17:49 -0800692 + " -> period " + period + ", extras " + extras);
693 }
694 synchronized (mAuthorities) {
Fred Quintana77c560f2010-03-29 22:20:26 -0700695 try {
696 AuthorityInfo authority =
Amith Yamasani04e0d262012-02-14 11:50:53 -0800697 getOrCreateAuthorityLocked(account, userId, providerName, -1, false);
Fred Quintana77c560f2010-03-29 22:20:26 -0700698 if (add) {
699 // add this periodic sync if one with the same extras doesn't already
700 // exist in the periodicSyncs array
701 boolean alreadyPresent = false;
702 for (int i = 0, N = authority.periodicSyncs.size(); i < N; i++) {
703 Pair<Bundle, Long> syncInfo = authority.periodicSyncs.get(i);
704 final Bundle existingExtras = syncInfo.first;
705 if (equals(existingExtras, extras)) {
706 if (syncInfo.second == period) {
707 return;
708 }
709 authority.periodicSyncs.set(i, Pair.create(extras, period));
710 alreadyPresent = true;
711 break;
Fred Quintanac5d1c6d2010-01-27 12:17:49 -0800712 }
Fred Quintana77c560f2010-03-29 22:20:26 -0700713 }
714 // if we added an entry to the periodicSyncs array also add an entry to
715 // the periodic syncs status to correspond to it
716 if (!alreadyPresent) {
717 authority.periodicSyncs.add(Pair.create(extras, period));
718 SyncStatusInfo status = getOrCreateSyncStatusLocked(authority.ident);
719 status.setPeriodicSyncTime(authority.periodicSyncs.size() - 1, 0);
720 }
721 } else {
722 // remove any periodic syncs that match the authority and extras
723 SyncStatusInfo status = mSyncStatus.get(authority.ident);
724 boolean changed = false;
725 Iterator<Pair<Bundle, Long>> iterator = authority.periodicSyncs.iterator();
726 int i = 0;
727 while (iterator.hasNext()) {
728 Pair<Bundle, Long> syncInfo = iterator.next();
729 if (equals(syncInfo.first, extras)) {
730 iterator.remove();
731 changed = true;
732 // if we removed an entry from the periodicSyncs array also
733 // remove the corresponding entry from the status
734 if (status != null) {
735 status.removePeriodicSyncTime(i);
736 }
737 } else {
738 i++;
739 }
740 }
741 if (!changed) {
742 return;
Fred Quintanac5d1c6d2010-01-27 12:17:49 -0800743 }
744 }
Fred Quintana77c560f2010-03-29 22:20:26 -0700745 } finally {
746 writeAccountInfoLocked();
747 writeStatusLocked();
Fred Quintanac5d1c6d2010-01-27 12:17:49 -0800748 }
Fred Quintanac5d1c6d2010-01-27 12:17:49 -0800749 }
750
751 reportChange(ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS);
752 }
753
Amith Yamasani04e0d262012-02-14 11:50:53 -0800754 public void addPeriodicSync(Account account, int userId, String providerName, Bundle extras,
Fred Quintanac5d1c6d2010-01-27 12:17:49 -0800755 long pollFrequency) {
Amith Yamasani04e0d262012-02-14 11:50:53 -0800756 updateOrRemovePeriodicSync(account, userId, providerName, extras, pollFrequency,
757 true /* add */);
Fred Quintanac5d1c6d2010-01-27 12:17:49 -0800758 }
759
Amith Yamasani04e0d262012-02-14 11:50:53 -0800760 public void removePeriodicSync(Account account, int userId, String providerName,
761 Bundle extras) {
762 updateOrRemovePeriodicSync(account, userId, providerName, extras, 0 /* period, ignored */,
Fred Quintanac5d1c6d2010-01-27 12:17:49 -0800763 false /* remove */);
764 }
765
Amith Yamasani04e0d262012-02-14 11:50:53 -0800766 public List<PeriodicSync> getPeriodicSyncs(Account account, int userId, String providerName) {
Fred Quintanac5d1c6d2010-01-27 12:17:49 -0800767 ArrayList<PeriodicSync> syncs = new ArrayList<PeriodicSync>();
768 synchronized (mAuthorities) {
Amith Yamasani04e0d262012-02-14 11:50:53 -0800769 AuthorityInfo authority = getAuthorityLocked(account, userId, providerName,
770 "getPeriodicSyncs");
Fred Quintanac5d1c6d2010-01-27 12:17:49 -0800771 if (authority != null) {
772 for (Pair<Bundle, Long> item : authority.periodicSyncs) {
Amith Yamasani04e0d262012-02-14 11:50:53 -0800773 syncs.add(new PeriodicSync(account, providerName, item.first,
774 item.second));
Fred Quintanac5d1c6d2010-01-27 12:17:49 -0800775 }
776 }
777 }
778 return syncs;
779 }
780
Amith Yamasani04e0d262012-02-14 11:50:53 -0800781 public void setMasterSyncAutomatically(boolean flag, int userId) {
Dianne Hackborn231cc602009-04-27 17:10:36 -0700782 synchronized (mAuthorities) {
Amith Yamasani04e0d262012-02-14 11:50:53 -0800783 Boolean auto = mMasterSyncAutomatically.get(userId);
784 if (auto != null && (boolean) auto == flag) {
Fred Quintana77c560f2010-03-29 22:20:26 -0700785 return;
786 }
Amith Yamasani04e0d262012-02-14 11:50:53 -0800787 mMasterSyncAutomatically.put(userId, flag);
Dianne Hackborn231cc602009-04-27 17:10:36 -0700788 writeAccountInfoLocked();
789 }
Fred Quintana77c560f2010-03-29 22:20:26 -0700790 if (flag) {
Amith Yamasani04e0d262012-02-14 11:50:53 -0800791 requestSync(null, userId, null, new Bundle());
Joe Onorato8294fad2009-07-15 16:08:44 -0700792 }
Fred Quintanaac9385e2009-06-22 18:00:59 -0700793 reportChange(ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS);
794 mContext.sendBroadcast(SYNC_CONNECTION_SETTING_CHANGED_INTENT);
Dianne Hackborn231cc602009-04-27 17:10:36 -0700795 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800796
Amith Yamasani04e0d262012-02-14 11:50:53 -0800797 public boolean getMasterSyncAutomatically(int userId) {
Dianne Hackborn231cc602009-04-27 17:10:36 -0700798 synchronized (mAuthorities) {
Amith Yamasani04e0d262012-02-14 11:50:53 -0800799 Boolean auto = mMasterSyncAutomatically.get(userId);
Yameng Huang2b5d0ea2011-01-11 14:00:19 +0800800 return auto == null ? mDefaultMasterSyncAutomatically : auto;
Dianne Hackborn231cc602009-04-27 17:10:36 -0700801 }
802 }
Costin Manolache360e4542009-09-04 13:36:04 -0700803
Amith Yamasani04e0d262012-02-14 11:50:53 -0800804 public AuthorityInfo getOrCreateAuthority(Account account, int userId, String authority) {
Dianne Hackborn231cc602009-04-27 17:10:36 -0700805 synchronized (mAuthorities) {
Amith Yamasani04e0d262012-02-14 11:50:53 -0800806 return getOrCreateAuthorityLocked(account, userId, authority,
Fred Quintana1bbcd102010-02-10 10:04:33 -0800807 -1 /* assign a new identifier if creating a new authority */,
808 true /* write to storage if this results in a change */);
Dianne Hackborn231cc602009-04-27 17:10:36 -0700809 }
810 }
Costin Manolache360e4542009-09-04 13:36:04 -0700811
Amith Yamasani04e0d262012-02-14 11:50:53 -0800812 public void removeAuthority(Account account, int userId, String authority) {
Fred Quintana7620f1a2010-03-16 15:58:44 -0700813 synchronized (mAuthorities) {
Amith Yamasani04e0d262012-02-14 11:50:53 -0800814 removeAuthorityLocked(account, userId, authority, true /* doWrite */);
Fred Quintana7620f1a2010-03-16 15:58:44 -0700815 }
816 }
817
Dianne Hackborn231cc602009-04-27 17:10:36 -0700818 public AuthorityInfo getAuthority(int authorityId) {
819 synchronized (mAuthorities) {
820 return mAuthorities.get(authorityId);
821 }
822 }
Costin Manolache360e4542009-09-04 13:36:04 -0700823
Dianne Hackborn231cc602009-04-27 17:10:36 -0700824 /**
825 * Returns true if there is currently a sync operation for the given
Fred Quintana918339a2010-10-05 14:00:39 -0700826 * account or authority actively being processed.
Dianne Hackborn231cc602009-04-27 17:10:36 -0700827 */
Amith Yamasani04e0d262012-02-14 11:50:53 -0800828 public boolean isSyncActive(Account account, int userId, String authority) {
Dianne Hackborn231cc602009-04-27 17:10:36 -0700829 synchronized (mAuthorities) {
Amith Yamasani04e0d262012-02-14 11:50:53 -0800830 for (SyncInfo syncInfo : getCurrentSyncs(userId)) {
Fred Quintana918339a2010-10-05 14:00:39 -0700831 AuthorityInfo ainfo = getAuthority(syncInfo.authorityId);
Dianne Hackborn231cc602009-04-27 17:10:36 -0700832 if (ainfo != null && ainfo.account.equals(account)
Amith Yamasani04e0d262012-02-14 11:50:53 -0800833 && ainfo.authority.equals(authority)
834 && ainfo.userId == userId) {
Dianne Hackborn231cc602009-04-27 17:10:36 -0700835 return true;
836 }
837 }
838 }
Costin Manolache360e4542009-09-04 13:36:04 -0700839
Dianne Hackborn231cc602009-04-27 17:10:36 -0700840 return false;
841 }
Costin Manolache360e4542009-09-04 13:36:04 -0700842
Dianne Hackborn231cc602009-04-27 17:10:36 -0700843 public PendingOperation insertIntoPending(PendingOperation op) {
844 synchronized (mAuthorities) {
Dianne Hackborn40e9f292012-11-27 19:12:23 -0800845 if (DEBUG) {
Fred Quintana77c560f2010-03-29 22:20:26 -0700846 Log.v(TAG, "insertIntoPending: account=" + op.account
Amith Yamasani04e0d262012-02-14 11:50:53 -0800847 + " user=" + op.userId
848 + " auth=" + op.authority
849 + " src=" + op.syncSource
850 + " extras=" + op.extras);
Fred Quintana77c560f2010-03-29 22:20:26 -0700851 }
Costin Manolache360e4542009-09-04 13:36:04 -0700852
Amith Yamasani04e0d262012-02-14 11:50:53 -0800853 AuthorityInfo authority = getOrCreateAuthorityLocked(op.account, op.userId,
Dianne Hackborn231cc602009-04-27 17:10:36 -0700854 op.authority,
855 -1 /* desired identifier */,
856 true /* write accounts to storage */);
857 if (authority == null) {
858 return null;
859 }
Costin Manolache360e4542009-09-04 13:36:04 -0700860
Dianne Hackborn231cc602009-04-27 17:10:36 -0700861 op = new PendingOperation(op);
862 op.authorityId = authority.ident;
863 mPendingOperations.add(op);
864 appendPendingOperationLocked(op);
Costin Manolache360e4542009-09-04 13:36:04 -0700865
Dianne Hackborn231cc602009-04-27 17:10:36 -0700866 SyncStatusInfo status = getOrCreateSyncStatusLocked(authority.ident);
867 status.pending = true;
868 }
Costin Manolache360e4542009-09-04 13:36:04 -0700869
Fred Quintanaac9385e2009-06-22 18:00:59 -0700870 reportChange(ContentResolver.SYNC_OBSERVER_TYPE_PENDING);
Dianne Hackborn231cc602009-04-27 17:10:36 -0700871 return op;
872 }
873
874 public boolean deleteFromPending(PendingOperation op) {
875 boolean res = false;
876 synchronized (mAuthorities) {
Dianne Hackborn40e9f292012-11-27 19:12:23 -0800877 if (DEBUG) {
Fred Quintana77c560f2010-03-29 22:20:26 -0700878 Log.v(TAG, "deleteFromPending: account=" + op.account
Amith Yamasani04e0d262012-02-14 11:50:53 -0800879 + " user=" + op.userId
Dianne Hackborn231cc602009-04-27 17:10:36 -0700880 + " auth=" + op.authority
881 + " src=" + op.syncSource
882 + " extras=" + op.extras);
Fred Quintana77c560f2010-03-29 22:20:26 -0700883 }
Dianne Hackborn231cc602009-04-27 17:10:36 -0700884 if (mPendingOperations.remove(op)) {
885 if (mPendingOperations.size() == 0
886 || mNumPendingFinished >= PENDING_FINISH_TO_WRITE) {
887 writePendingOperationsLocked();
888 mNumPendingFinished = 0;
889 } else {
890 mNumPendingFinished++;
891 }
Costin Manolache360e4542009-09-04 13:36:04 -0700892
Amith Yamasani04e0d262012-02-14 11:50:53 -0800893 AuthorityInfo authority = getAuthorityLocked(op.account, op.userId, op.authority,
Dianne Hackborn231cc602009-04-27 17:10:36 -0700894 "deleteFromPending");
895 if (authority != null) {
Dianne Hackborn40e9f292012-11-27 19:12:23 -0800896 if (DEBUG) Log.v(TAG, "removing - " + authority);
Dianne Hackborn231cc602009-04-27 17:10:36 -0700897 final int N = mPendingOperations.size();
898 boolean morePending = false;
899 for (int i=0; i<N; i++) {
900 PendingOperation cur = mPendingOperations.get(i);
901 if (cur.account.equals(op.account)
Amith Yamasani04e0d262012-02-14 11:50:53 -0800902 && cur.authority.equals(op.authority)
903 && cur.userId == op.userId) {
Dianne Hackborn231cc602009-04-27 17:10:36 -0700904 morePending = true;
905 break;
906 }
907 }
Costin Manolache360e4542009-09-04 13:36:04 -0700908
Dianne Hackborn231cc602009-04-27 17:10:36 -0700909 if (!morePending) {
Dianne Hackborn40e9f292012-11-27 19:12:23 -0800910 if (DEBUG) Log.v(TAG, "no more pending!");
Dianne Hackborn231cc602009-04-27 17:10:36 -0700911 SyncStatusInfo status = getOrCreateSyncStatusLocked(authority.ident);
912 status.pending = false;
913 }
914 }
Costin Manolache360e4542009-09-04 13:36:04 -0700915
Dianne Hackborn231cc602009-04-27 17:10:36 -0700916 res = true;
917 }
918 }
Costin Manolache360e4542009-09-04 13:36:04 -0700919
Fred Quintanaac9385e2009-06-22 18:00:59 -0700920 reportChange(ContentResolver.SYNC_OBSERVER_TYPE_PENDING);
Dianne Hackborn231cc602009-04-27 17:10:36 -0700921 return res;
922 }
923
Dianne Hackborn231cc602009-04-27 17:10:36 -0700924 /**
925 * Return a copy of the current array of pending operations. The
926 * PendingOperation objects are the real objects stored inside, so that
927 * they can be used with deleteFromPending().
928 */
929 public ArrayList<PendingOperation> getPendingOperations() {
930 synchronized (mAuthorities) {
931 return new ArrayList<PendingOperation>(mPendingOperations);
932 }
933 }
Costin Manolache360e4542009-09-04 13:36:04 -0700934
Dianne Hackborn231cc602009-04-27 17:10:36 -0700935 /**
936 * Return the number of currently pending operations.
937 */
938 public int getPendingOperationCount() {
939 synchronized (mAuthorities) {
940 return mPendingOperations.size();
941 }
942 }
Costin Manolache360e4542009-09-04 13:36:04 -0700943
Dianne Hackborn231cc602009-04-27 17:10:36 -0700944 /**
945 * Called when the set of account has changed, given the new array of
946 * active accounts.
947 */
Amith Yamasani04e0d262012-02-14 11:50:53 -0800948 public void doDatabaseCleanup(Account[] accounts, int userId) {
Dianne Hackborn231cc602009-04-27 17:10:36 -0700949 synchronized (mAuthorities) {
Dianne Hackborn40e9f292012-11-27 19:12:23 -0800950 if (DEBUG) Log.v(TAG, "Updating for new accounts...");
Dianne Hackborn231cc602009-04-27 17:10:36 -0700951 SparseArray<AuthorityInfo> removing = new SparseArray<AuthorityInfo>();
952 Iterator<AccountInfo> accIt = mAccounts.values().iterator();
953 while (accIt.hasNext()) {
954 AccountInfo acc = accIt.next();
Amith Yamasani04e0d262012-02-14 11:50:53 -0800955 if (!ArrayUtils.contains(accounts, acc.accountAndUser.account)
956 && acc.accountAndUser.userId == userId) {
Dianne Hackborn231cc602009-04-27 17:10:36 -0700957 // This account no longer exists...
Dianne Hackborn40e9f292012-11-27 19:12:23 -0800958 if (DEBUG) {
959 Log.v(TAG, "Account removed: " + acc.accountAndUser);
Fred Quintana77c560f2010-03-29 22:20:26 -0700960 }
Dianne Hackborn231cc602009-04-27 17:10:36 -0700961 for (AuthorityInfo auth : acc.authorities.values()) {
962 removing.put(auth.ident, auth);
963 }
964 accIt.remove();
965 }
966 }
Costin Manolache360e4542009-09-04 13:36:04 -0700967
Dianne Hackborn231cc602009-04-27 17:10:36 -0700968 // Clean out all data structures.
969 int i = removing.size();
970 if (i > 0) {
971 while (i > 0) {
972 i--;
973 int ident = removing.keyAt(i);
974 mAuthorities.remove(ident);
975 int j = mSyncStatus.size();
976 while (j > 0) {
977 j--;
978 if (mSyncStatus.keyAt(j) == ident) {
979 mSyncStatus.remove(mSyncStatus.keyAt(j));
980 }
981 }
982 j = mSyncHistory.size();
983 while (j > 0) {
984 j--;
985 if (mSyncHistory.get(j).authorityId == ident) {
986 mSyncHistory.remove(j);
987 }
988 }
989 }
990 writeAccountInfoLocked();
991 writeStatusLocked();
992 writePendingOperationsLocked();
993 writeStatisticsLocked();
994 }
995 }
996 }
997
998 /**
Fred Quintana918339a2010-10-05 14:00:39 -0700999 * Called when a sync is starting. Supply a valid ActiveSyncContext with information
1000 * about the sync.
Dianne Hackborn231cc602009-04-27 17:10:36 -07001001 */
Fred Quintana918339a2010-10-05 14:00:39 -07001002 public SyncInfo addActiveSync(SyncManager.ActiveSyncContext activeSyncContext) {
1003 final SyncInfo syncInfo;
Dianne Hackborn231cc602009-04-27 17:10:36 -07001004 synchronized (mAuthorities) {
Dianne Hackborn40e9f292012-11-27 19:12:23 -08001005 if (DEBUG) {
Fred Quintana918339a2010-10-05 14:00:39 -07001006 Log.v(TAG, "setActiveSync: account="
1007 + activeSyncContext.mSyncOperation.account
1008 + " auth=" + activeSyncContext.mSyncOperation.authority
1009 + " src=" + activeSyncContext.mSyncOperation.syncSource
1010 + " extras=" + activeSyncContext.mSyncOperation.extras);
Dianne Hackborn231cc602009-04-27 17:10:36 -07001011 }
Fred Quintana918339a2010-10-05 14:00:39 -07001012 AuthorityInfo authority = getOrCreateAuthorityLocked(
1013 activeSyncContext.mSyncOperation.account,
Amith Yamasani04e0d262012-02-14 11:50:53 -08001014 activeSyncContext.mSyncOperation.userId,
Fred Quintana918339a2010-10-05 14:00:39 -07001015 activeSyncContext.mSyncOperation.authority,
1016 -1 /* assign a new identifier if creating a new authority */,
1017 true /* write to storage if this results in a change */);
1018 syncInfo = new SyncInfo(authority.ident,
1019 authority.account, authority.authority,
1020 activeSyncContext.mStartTime);
Amith Yamasani04e0d262012-02-14 11:50:53 -08001021 getCurrentSyncs(authority.userId).add(syncInfo);
Dianne Hackborn231cc602009-04-27 17:10:36 -07001022 }
Costin Manolache360e4542009-09-04 13:36:04 -07001023
Fred Quintana918339a2010-10-05 14:00:39 -07001024 reportActiveChange();
1025 return syncInfo;
1026 }
1027
1028 /**
1029 * Called to indicate that a previously active sync is no longer active.
1030 */
Amith Yamasani04e0d262012-02-14 11:50:53 -08001031 public void removeActiveSync(SyncInfo syncInfo, int userId) {
Fred Quintana918339a2010-10-05 14:00:39 -07001032 synchronized (mAuthorities) {
Dianne Hackborn40e9f292012-11-27 19:12:23 -08001033 if (DEBUG) {
Amith Yamasani04e0d262012-02-14 11:50:53 -08001034 Log.v(TAG, "removeActiveSync: account=" + syncInfo.account
1035 + " user=" + userId
1036 + " auth=" + syncInfo.authority);
Fred Quintana918339a2010-10-05 14:00:39 -07001037 }
Amith Yamasani04e0d262012-02-14 11:50:53 -08001038 getCurrentSyncs(userId).remove(syncInfo);
Fred Quintana918339a2010-10-05 14:00:39 -07001039 }
1040
1041 reportActiveChange();
Dianne Hackborn231cc602009-04-27 17:10:36 -07001042 }
1043
1044 /**
1045 * To allow others to send active change reports, to poke clients.
1046 */
1047 public void reportActiveChange() {
Fred Quintanaac9385e2009-06-22 18:00:59 -07001048 reportChange(ContentResolver.SYNC_OBSERVER_TYPE_ACTIVE);
Dianne Hackborn231cc602009-04-27 17:10:36 -07001049 }
Costin Manolache360e4542009-09-04 13:36:04 -07001050
Dianne Hackborn231cc602009-04-27 17:10:36 -07001051 /**
1052 * Note that sync has started for the given account and authority.
1053 */
Amith Yamasani04e0d262012-02-14 11:50:53 -08001054 public long insertStartSyncEvent(Account accountName, int userId, String authorityName,
Fred Quintanadc475562012-05-04 15:51:54 -07001055 long now, int source, boolean initialization) {
Dianne Hackborn231cc602009-04-27 17:10:36 -07001056 long id;
1057 synchronized (mAuthorities) {
Dianne Hackborn40e9f292012-11-27 19:12:23 -08001058 if (DEBUG) {
Amith Yamasani04e0d262012-02-14 11:50:53 -08001059 Log.v(TAG, "insertStartSyncEvent: account=" + accountName + "user=" + userId
Dianne Hackborn231cc602009-04-27 17:10:36 -07001060 + " auth=" + authorityName + " source=" + source);
Fred Quintana77c560f2010-03-29 22:20:26 -07001061 }
Amith Yamasani04e0d262012-02-14 11:50:53 -08001062 AuthorityInfo authority = getAuthorityLocked(accountName, userId, authorityName,
Dianne Hackborn231cc602009-04-27 17:10:36 -07001063 "insertStartSyncEvent");
1064 if (authority == null) {
1065 return -1;
1066 }
1067 SyncHistoryItem item = new SyncHistoryItem();
Fred Quintanadc475562012-05-04 15:51:54 -07001068 item.initialization = initialization;
Dianne Hackborn231cc602009-04-27 17:10:36 -07001069 item.authorityId = authority.ident;
1070 item.historyId = mNextHistoryId++;
1071 if (mNextHistoryId < 0) mNextHistoryId = 0;
1072 item.eventTime = now;
1073 item.source = source;
1074 item.event = EVENT_START;
1075 mSyncHistory.add(0, item);
1076 while (mSyncHistory.size() > MAX_HISTORY) {
1077 mSyncHistory.remove(mSyncHistory.size()-1);
1078 }
1079 id = item.historyId;
Dianne Hackborn40e9f292012-11-27 19:12:23 -08001080 if (DEBUG) Log.v(TAG, "returning historyId " + id);
Dianne Hackborn231cc602009-04-27 17:10:36 -07001081 }
Costin Manolache360e4542009-09-04 13:36:04 -07001082
Fred Quintanaac9385e2009-06-22 18:00:59 -07001083 reportChange(ContentResolver.SYNC_OBSERVER_TYPE_STATUS);
Dianne Hackborn231cc602009-04-27 17:10:36 -07001084 return id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001085 }
1086
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001087 public static boolean equals(Bundle b1, Bundle b2) {
1088 if (b1.size() != b2.size()) {
1089 return false;
1090 }
1091 if (b1.isEmpty()) {
1092 return true;
1093 }
1094 for (String key : b1.keySet()) {
1095 if (!b2.containsKey(key)) {
1096 return false;
1097 }
1098 if (!b1.get(key).equals(b2.get(key))) {
1099 return false;
1100 }
1101 }
1102 return true;
1103 }
1104
Fred Quintana77c560f2010-03-29 22:20:26 -07001105 public void stopSyncEvent(long historyId, long elapsedTime, String resultMessage,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001106 long downstreamActivity, long upstreamActivity) {
Dianne Hackborn231cc602009-04-27 17:10:36 -07001107 synchronized (mAuthorities) {
Dianne Hackborn40e9f292012-11-27 19:12:23 -08001108 if (DEBUG) {
Fred Quintana77c560f2010-03-29 22:20:26 -07001109 Log.v(TAG, "stopSyncEvent: historyId=" + historyId);
1110 }
Dianne Hackborn231cc602009-04-27 17:10:36 -07001111 SyncHistoryItem item = null;
1112 int i = mSyncHistory.size();
1113 while (i > 0) {
1114 i--;
1115 item = mSyncHistory.get(i);
1116 if (item.historyId == historyId) {
1117 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001118 }
Dianne Hackborn231cc602009-04-27 17:10:36 -07001119 item = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001120 }
Costin Manolache360e4542009-09-04 13:36:04 -07001121
Dianne Hackborn231cc602009-04-27 17:10:36 -07001122 if (item == null) {
1123 Log.w(TAG, "stopSyncEvent: no history for id " + historyId);
1124 return;
1125 }
Costin Manolache360e4542009-09-04 13:36:04 -07001126
Dianne Hackborn231cc602009-04-27 17:10:36 -07001127 item.elapsedTime = elapsedTime;
1128 item.event = EVENT_STOP;
1129 item.mesg = resultMessage;
1130 item.downstreamActivity = downstreamActivity;
1131 item.upstreamActivity = upstreamActivity;
Costin Manolache360e4542009-09-04 13:36:04 -07001132
Dianne Hackborn231cc602009-04-27 17:10:36 -07001133 SyncStatusInfo status = getOrCreateSyncStatusLocked(item.authorityId);
Costin Manolache360e4542009-09-04 13:36:04 -07001134
Dianne Hackborn231cc602009-04-27 17:10:36 -07001135 status.numSyncs++;
1136 status.totalElapsedTime += elapsedTime;
1137 switch (item.source) {
1138 case SOURCE_LOCAL:
1139 status.numSourceLocal++;
1140 break;
1141 case SOURCE_POLL:
1142 status.numSourcePoll++;
1143 break;
1144 case SOURCE_USER:
1145 status.numSourceUser++;
1146 break;
1147 case SOURCE_SERVER:
1148 status.numSourceServer++;
1149 break;
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001150 case SOURCE_PERIODIC:
1151 status.numSourcePeriodic++;
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001152 break;
Dianne Hackborn231cc602009-04-27 17:10:36 -07001153 }
Costin Manolache360e4542009-09-04 13:36:04 -07001154
Dianne Hackborn231cc602009-04-27 17:10:36 -07001155 boolean writeStatisticsNow = false;
Dianne Hackborn55280a92009-05-07 15:53:46 -07001156 int day = getCurrentDayLocked();
Dianne Hackborn231cc602009-04-27 17:10:36 -07001157 if (mDayStats[0] == null) {
1158 mDayStats[0] = new DayStats(day);
1159 } else if (day != mDayStats[0].day) {
1160 System.arraycopy(mDayStats, 0, mDayStats, 1, mDayStats.length-1);
1161 mDayStats[0] = new DayStats(day);
1162 writeStatisticsNow = true;
1163 } else if (mDayStats[0] == null) {
1164 }
1165 final DayStats ds = mDayStats[0];
Costin Manolache360e4542009-09-04 13:36:04 -07001166
Dianne Hackborn231cc602009-04-27 17:10:36 -07001167 final long lastSyncTime = (item.eventTime + elapsedTime);
1168 boolean writeStatusNow = false;
1169 if (MESG_SUCCESS.equals(resultMessage)) {
1170 // - if successful, update the successful columns
1171 if (status.lastSuccessTime == 0 || status.lastFailureTime != 0) {
1172 writeStatusNow = true;
1173 }
1174 status.lastSuccessTime = lastSyncTime;
1175 status.lastSuccessSource = item.source;
1176 status.lastFailureTime = 0;
1177 status.lastFailureSource = -1;
1178 status.lastFailureMesg = null;
1179 status.initialFailureTime = 0;
1180 ds.successCount++;
1181 ds.successTime += elapsedTime;
1182 } else if (!MESG_CANCELED.equals(resultMessage)) {
1183 if (status.lastFailureTime == 0) {
1184 writeStatusNow = true;
1185 }
1186 status.lastFailureTime = lastSyncTime;
1187 status.lastFailureSource = item.source;
1188 status.lastFailureMesg = resultMessage;
1189 if (status.initialFailureTime == 0) {
1190 status.initialFailureTime = lastSyncTime;
1191 }
1192 ds.failureCount++;
1193 ds.failureTime += elapsedTime;
1194 }
Costin Manolache360e4542009-09-04 13:36:04 -07001195
Dianne Hackborn231cc602009-04-27 17:10:36 -07001196 if (writeStatusNow) {
1197 writeStatusLocked();
1198 } else if (!hasMessages(MSG_WRITE_STATUS)) {
1199 sendMessageDelayed(obtainMessage(MSG_WRITE_STATUS),
1200 WRITE_STATUS_DELAY);
1201 }
1202 if (writeStatisticsNow) {
1203 writeStatisticsLocked();
1204 } else if (!hasMessages(MSG_WRITE_STATISTICS)) {
1205 sendMessageDelayed(obtainMessage(MSG_WRITE_STATISTICS),
1206 WRITE_STATISTICS_DELAY);
Costin Manolache360e4542009-09-04 13:36:04 -07001207 }
Dianne Hackborn231cc602009-04-27 17:10:36 -07001208 }
Costin Manolache360e4542009-09-04 13:36:04 -07001209
Fred Quintanaac9385e2009-06-22 18:00:59 -07001210 reportChange(ContentResolver.SYNC_OBSERVER_TYPE_STATUS);
Dianne Hackborn231cc602009-04-27 17:10:36 -07001211 }
1212
1213 /**
Fred Quintana918339a2010-10-05 14:00:39 -07001214 * Return a list of the currently active syncs. Note that the returned items are the
1215 * real, live active sync objects, so be careful what you do with it.
1216 */
Amith Yamasani04e0d262012-02-14 11:50:53 -08001217 public List<SyncInfo> getCurrentSyncs(int userId) {
Fred Quintana918339a2010-10-05 14:00:39 -07001218 synchronized (mAuthorities) {
Amith Yamasani04e0d262012-02-14 11:50:53 -08001219 ArrayList<SyncInfo> syncs = mCurrentSyncs.get(userId);
1220 if (syncs == null) {
1221 syncs = new ArrayList<SyncInfo>();
1222 mCurrentSyncs.put(userId, syncs);
1223 }
Amith Yamasani1b6ae002012-03-14 14:53:36 -07001224 return syncs;
Dianne Hackborn231cc602009-04-27 17:10:36 -07001225 }
1226 }
Costin Manolache360e4542009-09-04 13:36:04 -07001227
Dianne Hackborn231cc602009-04-27 17:10:36 -07001228 /**
1229 * Return an array of the current sync status for all authorities. 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 ArrayList<SyncStatusInfo> getSyncStatus() {
1234 synchronized (mAuthorities) {
1235 final int N = mSyncStatus.size();
1236 ArrayList<SyncStatusInfo> ops = new ArrayList<SyncStatusInfo>(N);
1237 for (int i=0; i<N; i++) {
1238 ops.add(mSyncStatus.valueAt(i));
1239 }
1240 return ops;
1241 }
1242 }
Costin Manolache360e4542009-09-04 13:36:04 -07001243
Dianne Hackborn231cc602009-04-27 17:10:36 -07001244 /**
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001245 * Return an array of the current authorities. Note
1246 * that the objects inside the array are the real, live objects,
1247 * so be careful what you do with them.
1248 */
1249 public ArrayList<AuthorityInfo> getAuthorities() {
1250 synchronized (mAuthorities) {
1251 final int N = mAuthorities.size();
1252 ArrayList<AuthorityInfo> infos = new ArrayList<AuthorityInfo>(N);
1253 for (int i=0; i<N; i++) {
Carlos Valdivia3aca7d72012-05-07 19:03:51 -07001254 // Make deep copy because AuthorityInfo syncs are liable to change.
1255 infos.add(new AuthorityInfo(mAuthorities.valueAt(i)));
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001256 }
1257 return infos;
1258 }
1259 }
1260
1261 /**
Costin Manolacheb7520982009-09-02 18:03:05 -07001262 * Returns the status that matches the authority and account.
1263 *
1264 * @param account the account we want to check
Dianne Hackborn231cc602009-04-27 17:10:36 -07001265 * @param authority the authority whose row should be selected
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001266 * @return the SyncStatusInfo for the authority
Dianne Hackborn231cc602009-04-27 17:10:36 -07001267 */
Amith Yamasani04e0d262012-02-14 11:50:53 -08001268 public SyncStatusInfo getStatusByAccountAndAuthority(Account account, int userId,
1269 String authority) {
Costin Manolacheb7520982009-09-02 18:03:05 -07001270 if (account == null || authority == null) {
1271 throw new IllegalArgumentException();
1272 }
Dianne Hackborn231cc602009-04-27 17:10:36 -07001273 synchronized (mAuthorities) {
Dianne Hackborn231cc602009-04-27 17:10:36 -07001274 final int N = mSyncStatus.size();
1275 for (int i=0; i<N; i++) {
Costin Manolache360e4542009-09-04 13:36:04 -07001276 SyncStatusInfo cur = mSyncStatus.valueAt(i);
Dianne Hackborn231cc602009-04-27 17:10:36 -07001277 AuthorityInfo ainfo = mAuthorities.get(cur.authorityId);
Costin Manolacheb7520982009-09-02 18:03:05 -07001278
Amith Yamasani04e0d262012-02-14 11:50:53 -08001279 if (ainfo != null && ainfo.authority.equals(authority)
1280 && ainfo.userId == userId
1281 && account.equals(ainfo.account)) {
Costin Manolacheb7520982009-09-02 18:03:05 -07001282 return cur;
Dianne Hackborn231cc602009-04-27 17:10:36 -07001283 }
1284 }
Costin Manolacheb7520982009-09-02 18:03:05 -07001285 return null;
Dianne Hackborn231cc602009-04-27 17:10:36 -07001286 }
1287 }
Costin Manolache360e4542009-09-04 13:36:04 -07001288
Dianne Hackborn231cc602009-04-27 17:10:36 -07001289 /**
1290 * Return true if the pending status is true of any matching authorities.
1291 */
Amith Yamasani04e0d262012-02-14 11:50:53 -08001292 public boolean isSyncPending(Account account, int userId, String authority) {
Dianne Hackborn231cc602009-04-27 17:10:36 -07001293 synchronized (mAuthorities) {
1294 final int N = mSyncStatus.size();
1295 for (int i=0; i<N; i++) {
Costin Manolache360e4542009-09-04 13:36:04 -07001296 SyncStatusInfo cur = mSyncStatus.valueAt(i);
Dianne Hackborn231cc602009-04-27 17:10:36 -07001297 AuthorityInfo ainfo = mAuthorities.get(cur.authorityId);
1298 if (ainfo == null) {
1299 continue;
1300 }
Amith Yamasani04e0d262012-02-14 11:50:53 -08001301 if (userId != ainfo.userId) {
1302 continue;
1303 }
Dianne Hackborn231cc602009-04-27 17:10:36 -07001304 if (account != null && !ainfo.account.equals(account)) {
1305 continue;
1306 }
1307 if (ainfo.authority.equals(authority) && cur.pending) {
1308 return true;
1309 }
1310 }
1311 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001312 }
1313 }
1314
1315 /**
Dianne Hackborn231cc602009-04-27 17:10:36 -07001316 * Return an array of the current sync status for all authorities. Note
1317 * that the objects inside the array are the real, live status objects,
1318 * so be careful what you do with them.
1319 */
1320 public ArrayList<SyncHistoryItem> getSyncHistory() {
1321 synchronized (mAuthorities) {
1322 final int N = mSyncHistory.size();
1323 ArrayList<SyncHistoryItem> items = new ArrayList<SyncHistoryItem>(N);
1324 for (int i=0; i<N; i++) {
1325 items.add(mSyncHistory.get(i));
1326 }
1327 return items;
1328 }
1329 }
Costin Manolache360e4542009-09-04 13:36:04 -07001330
Dianne Hackborn231cc602009-04-27 17:10:36 -07001331 /**
1332 * Return an array of the current per-day statistics. Note
1333 * that the objects inside the array are the real, live status objects,
1334 * so be careful what you do with them.
1335 */
1336 public DayStats[] getDayStatistics() {
1337 synchronized (mAuthorities) {
1338 DayStats[] ds = new DayStats[mDayStats.length];
1339 System.arraycopy(mDayStats, 0, ds, 0, ds.length);
1340 return ds;
1341 }
1342 }
Costin Manolache360e4542009-09-04 13:36:04 -07001343
Dianne Hackborn55280a92009-05-07 15:53:46 -07001344 private int getCurrentDayLocked() {
Dianne Hackborn231cc602009-04-27 17:10:36 -07001345 mCal.setTimeInMillis(System.currentTimeMillis());
1346 final int dayOfYear = mCal.get(Calendar.DAY_OF_YEAR);
1347 if (mYear != mCal.get(Calendar.YEAR)) {
1348 mYear = mCal.get(Calendar.YEAR);
1349 mCal.clear();
1350 mCal.set(Calendar.YEAR, mYear);
1351 mYearInDays = (int)(mCal.getTimeInMillis()/86400000);
1352 }
1353 return dayOfYear + mYearInDays;
1354 }
Costin Manolache360e4542009-09-04 13:36:04 -07001355
Dianne Hackborn231cc602009-04-27 17:10:36 -07001356 /**
1357 * Retrieve an authority, returning null if one does not exist.
Costin Manolache360e4542009-09-04 13:36:04 -07001358 *
Dianne Hackborn231cc602009-04-27 17:10:36 -07001359 * @param accountName The name of the account for the authority.
1360 * @param authorityName The name of the authority itself.
1361 * @param tag If non-null, this will be used in a log message if the
1362 * requested authority does not exist.
1363 */
Amith Yamasani04e0d262012-02-14 11:50:53 -08001364 private AuthorityInfo getAuthorityLocked(Account accountName, int userId, String authorityName,
Dianne Hackborn231cc602009-04-27 17:10:36 -07001365 String tag) {
Amith Yamasani04e0d262012-02-14 11:50:53 -08001366 AccountAndUser au = new AccountAndUser(accountName, userId);
1367 AccountInfo accountInfo = mAccounts.get(au);
1368 if (accountInfo == null) {
Dianne Hackborn231cc602009-04-27 17:10:36 -07001369 if (tag != null) {
Dianne Hackborn40e9f292012-11-27 19:12:23 -08001370 if (DEBUG) {
Amith Yamasani04e0d262012-02-14 11:50:53 -08001371 Log.v(TAG, tag + ": unknown account " + au);
Fred Quintanab763ab22009-08-18 18:07:30 -07001372 }
Dianne Hackborn231cc602009-04-27 17:10:36 -07001373 }
1374 return null;
1375 }
Amith Yamasani04e0d262012-02-14 11:50:53 -08001376 AuthorityInfo authority = accountInfo.authorities.get(authorityName);
Dianne Hackborn231cc602009-04-27 17:10:36 -07001377 if (authority == null) {
1378 if (tag != null) {
Dianne Hackborn40e9f292012-11-27 19:12:23 -08001379 if (DEBUG) {
Fred Quintanab763ab22009-08-18 18:07:30 -07001380 Log.v(TAG, tag + ": unknown authority " + authorityName);
1381 }
Dianne Hackborn231cc602009-04-27 17:10:36 -07001382 }
1383 return null;
1384 }
Costin Manolache360e4542009-09-04 13:36:04 -07001385
Dianne Hackborn231cc602009-04-27 17:10:36 -07001386 return authority;
1387 }
Costin Manolache360e4542009-09-04 13:36:04 -07001388
Amith Yamasani04e0d262012-02-14 11:50:53 -08001389 private AuthorityInfo getOrCreateAuthorityLocked(Account accountName, int userId,
Dianne Hackborn231cc602009-04-27 17:10:36 -07001390 String authorityName, int ident, boolean doWrite) {
Amith Yamasani04e0d262012-02-14 11:50:53 -08001391 AccountAndUser au = new AccountAndUser(accountName, userId);
1392 AccountInfo account = mAccounts.get(au);
Dianne Hackborn231cc602009-04-27 17:10:36 -07001393 if (account == null) {
Amith Yamasani04e0d262012-02-14 11:50:53 -08001394 account = new AccountInfo(au);
1395 mAccounts.put(au, account);
Dianne Hackborn231cc602009-04-27 17:10:36 -07001396 }
1397 AuthorityInfo authority = account.authorities.get(authorityName);
1398 if (authority == null) {
1399 if (ident < 0) {
Fred Quintana77c560f2010-03-29 22:20:26 -07001400 ident = mNextAuthorityId;
1401 mNextAuthorityId++;
1402 doWrite = true;
Dianne Hackborn231cc602009-04-27 17:10:36 -07001403 }
Dianne Hackborn40e9f292012-11-27 19:12:23 -08001404 if (DEBUG) {
Fred Quintana77c560f2010-03-29 22:20:26 -07001405 Log.v(TAG, "created a new AuthorityInfo for " + accountName
Amith Yamasani04e0d262012-02-14 11:50:53 -08001406 + ", user " + userId
1407 + ", provider " + authorityName);
Fred Quintana77c560f2010-03-29 22:20:26 -07001408 }
Amith Yamasani04e0d262012-02-14 11:50:53 -08001409 authority = new AuthorityInfo(accountName, userId, authorityName, ident);
Dianne Hackborn231cc602009-04-27 17:10:36 -07001410 account.authorities.put(authorityName, authority);
1411 mAuthorities.put(ident, authority);
1412 if (doWrite) {
1413 writeAccountInfoLocked();
1414 }
1415 }
Costin Manolache360e4542009-09-04 13:36:04 -07001416
Dianne Hackborn231cc602009-04-27 17:10:36 -07001417 return authority;
1418 }
Costin Manolache360e4542009-09-04 13:36:04 -07001419
Amith Yamasani04e0d262012-02-14 11:50:53 -08001420 private void removeAuthorityLocked(Account account, int userId, String authorityName,
1421 boolean doWrite) {
1422 AccountInfo accountInfo = mAccounts.get(new AccountAndUser(account, userId));
Fred Quintana7620f1a2010-03-16 15:58:44 -07001423 if (accountInfo != null) {
Fred Quintanafb084402010-03-23 17:57:03 -07001424 final AuthorityInfo authorityInfo = accountInfo.authorities.remove(authorityName);
1425 if (authorityInfo != null) {
1426 mAuthorities.remove(authorityInfo.ident);
Fred Quintana77c560f2010-03-29 22:20:26 -07001427 if (doWrite) {
1428 writeAccountInfoLocked();
1429 }
Fred Quintana7620f1a2010-03-16 15:58:44 -07001430 }
1431 }
1432 }
1433
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001434 public SyncStatusInfo getOrCreateSyncStatus(AuthorityInfo authority) {
1435 synchronized (mAuthorities) {
1436 return getOrCreateSyncStatusLocked(authority.ident);
1437 }
1438 }
1439
Dianne Hackborn231cc602009-04-27 17:10:36 -07001440 private SyncStatusInfo getOrCreateSyncStatusLocked(int authorityId) {
1441 SyncStatusInfo status = mSyncStatus.get(authorityId);
1442 if (status == null) {
1443 status = new SyncStatusInfo(authorityId);
1444 mSyncStatus.put(authorityId, status);
1445 }
1446 return status;
1447 }
Costin Manolache360e4542009-09-04 13:36:04 -07001448
Dianne Hackborn55280a92009-05-07 15:53:46 -07001449 public void writeAllState() {
1450 synchronized (mAuthorities) {
1451 // Account info is always written so no need to do it here.
Costin Manolache360e4542009-09-04 13:36:04 -07001452
Dianne Hackborn55280a92009-05-07 15:53:46 -07001453 if (mNumPendingFinished > 0) {
1454 // Only write these if they are out of date.
1455 writePendingOperationsLocked();
1456 }
Costin Manolache360e4542009-09-04 13:36:04 -07001457
Dianne Hackborn55280a92009-05-07 15:53:46 -07001458 // Just always write these... they are likely out of date.
1459 writeStatusLocked();
1460 writeStatisticsLocked();
1461 }
1462 }
Costin Manolache360e4542009-09-04 13:36:04 -07001463
Dianne Hackborn231cc602009-04-27 17:10:36 -07001464 /**
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001465 * public for testing
1466 */
1467 public void clearAndReadState() {
1468 synchronized (mAuthorities) {
1469 mAuthorities.clear();
1470 mAccounts.clear();
1471 mPendingOperations.clear();
1472 mSyncStatus.clear();
1473 mSyncHistory.clear();
1474
1475 readAccountInfoLocked();
1476 readStatusLocked();
1477 readPendingOperationsLocked();
1478 readStatisticsLocked();
Fred Quintana77c560f2010-03-29 22:20:26 -07001479 readAndDeleteLegacyAccountInfoLocked();
1480 writeAccountInfoLocked();
1481 writeStatusLocked();
1482 writePendingOperationsLocked();
1483 writeStatisticsLocked();
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001484 }
1485 }
1486
1487 /**
Dianne Hackborn231cc602009-04-27 17:10:36 -07001488 * Read all account information back in to the initial engine state.
1489 */
1490 private void readAccountInfoLocked() {
Fred Quintana77c560f2010-03-29 22:20:26 -07001491 int highestAuthorityId = -1;
Dianne Hackborn231cc602009-04-27 17:10:36 -07001492 FileInputStream fis = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001493 try {
Dianne Hackborn231cc602009-04-27 17:10:36 -07001494 fis = mAccountInfoFile.openRead();
1495 if (DEBUG_FILE) Log.v(TAG, "Reading " + mAccountInfoFile.getBaseFile());
1496 XmlPullParser parser = Xml.newPullParser();
1497 parser.setInput(fis, null);
1498 int eventType = parser.getEventType();
1499 while (eventType != XmlPullParser.START_TAG) {
1500 eventType = parser.next();
1501 }
1502 String tagName = parser.getName();
1503 if ("accounts".equals(tagName)) {
Amith Yamasani04e0d262012-02-14 11:50:53 -08001504 String listen = parser.getAttributeValue(null, XML_ATTR_LISTEN_FOR_TICKLES);
Fred Quintanac2e46912010-03-15 16:10:44 -07001505 String versionString = parser.getAttributeValue(null, "version");
1506 int version;
1507 try {
1508 version = (versionString == null) ? 0 : Integer.parseInt(versionString);
1509 } catch (NumberFormatException e) {
1510 version = 0;
1511 }
Amith Yamasani04e0d262012-02-14 11:50:53 -08001512 String nextIdString = parser.getAttributeValue(null, XML_ATTR_NEXT_AUTHORITY_ID);
Fred Quintana77c560f2010-03-29 22:20:26 -07001513 try {
1514 int id = (nextIdString == null) ? 0 : Integer.parseInt(nextIdString);
1515 mNextAuthorityId = Math.max(mNextAuthorityId, id);
1516 } catch (NumberFormatException e) {
1517 // don't care
Fred Quintanac2e46912010-03-15 16:10:44 -07001518 }
Ashish Sharma69d95de2012-04-11 17:27:24 -07001519 String offsetString = parser.getAttributeValue(null, XML_ATTR_SYNC_RANDOM_OFFSET);
1520 try {
1521 mSyncRandomOffset = (offsetString == null) ? 0 : Integer.parseInt(offsetString);
1522 } catch (NumberFormatException e) {
1523 mSyncRandomOffset = 0;
1524 }
1525 if (mSyncRandomOffset == 0) {
1526 Random random = new Random(System.currentTimeMillis());
1527 mSyncRandomOffset = random.nextInt(86400);
1528 }
Amith Yamasani04e0d262012-02-14 11:50:53 -08001529 mMasterSyncAutomatically.put(0, listen == null || Boolean.parseBoolean(listen));
Dianne Hackborn231cc602009-04-27 17:10:36 -07001530 eventType = parser.next();
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001531 AuthorityInfo authority = null;
1532 Pair<Bundle, Long> periodicSync = null;
Dianne Hackborn231cc602009-04-27 17:10:36 -07001533 do {
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001534 if (eventType == XmlPullParser.START_TAG) {
Dianne Hackborn231cc602009-04-27 17:10:36 -07001535 tagName = parser.getName();
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001536 if (parser.getDepth() == 2) {
1537 if ("authority".equals(tagName)) {
Fred Quintanac2e46912010-03-15 16:10:44 -07001538 authority = parseAuthority(parser, version);
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001539 periodicSync = null;
Fred Quintana77c560f2010-03-29 22:20:26 -07001540 if (authority.ident > highestAuthorityId) {
1541 highestAuthorityId = authority.ident;
1542 }
Amith Yamasani04e0d262012-02-14 11:50:53 -08001543 } else if (XML_TAG_LISTEN_FOR_TICKLES.equals(tagName)) {
1544 parseListenForTickles(parser);
Dianne Hackborn231cc602009-04-27 17:10:36 -07001545 }
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001546 } else if (parser.getDepth() == 3) {
1547 if ("periodicSync".equals(tagName) && authority != null) {
1548 periodicSync = parsePeriodicSync(parser, authority);
1549 }
1550 } else if (parser.getDepth() == 4 && periodicSync != null) {
1551 if ("extra".equals(tagName)) {
1552 parseExtra(parser, periodicSync);
Dianne Hackborn231cc602009-04-27 17:10:36 -07001553 }
1554 }
1555 }
1556 eventType = parser.next();
1557 } while (eventType != XmlPullParser.END_DOCUMENT);
1558 }
1559 } catch (XmlPullParserException e) {
1560 Log.w(TAG, "Error reading accounts", e);
Fred Quintanac2e46912010-03-15 16:10:44 -07001561 return;
Dianne Hackborn231cc602009-04-27 17:10:36 -07001562 } catch (java.io.IOException e) {
1563 if (fis == null) Log.i(TAG, "No initial accounts");
1564 else Log.w(TAG, "Error reading accounts", e);
Fred Quintanac2e46912010-03-15 16:10:44 -07001565 return;
Dianne Hackborn231cc602009-04-27 17:10:36 -07001566 } finally {
Fred Quintana77c560f2010-03-29 22:20:26 -07001567 mNextAuthorityId = Math.max(highestAuthorityId + 1, mNextAuthorityId);
Dianne Hackborn231cc602009-04-27 17:10:36 -07001568 if (fis != null) {
1569 try {
1570 fis.close();
1571 } catch (java.io.IOException e1) {
1572 }
1573 }
1574 }
Fred Quintanac2e46912010-03-15 16:10:44 -07001575
Fred Quintana77c560f2010-03-29 22:20:26 -07001576 maybeMigrateSettingsForRenamedAuthorities();
Dianne Hackborn231cc602009-04-27 17:10:36 -07001577 }
Costin Manolache360e4542009-09-04 13:36:04 -07001578
Fred Quintanafb084402010-03-23 17:57:03 -07001579 /**
1580 * some authority names have changed. copy over their settings and delete the old ones
1581 * @return true if a change was made
1582 */
1583 private boolean maybeMigrateSettingsForRenamedAuthorities() {
1584 boolean writeNeeded = false;
1585
1586 ArrayList<AuthorityInfo> authoritiesToRemove = new ArrayList<AuthorityInfo>();
1587 final int N = mAuthorities.size();
1588 for (int i=0; i<N; i++) {
1589 AuthorityInfo authority = mAuthorities.valueAt(i);
1590 // skip this authority if it isn't one of the renamed ones
1591 final String newAuthorityName = sAuthorityRenames.get(authority.authority);
1592 if (newAuthorityName == null) {
1593 continue;
1594 }
1595
1596 // remember this authority so we can remove it later. we can't remove it
1597 // now without messing up this loop iteration
1598 authoritiesToRemove.add(authority);
1599
1600 // this authority isn't enabled, no need to copy it to the new authority name since
1601 // the default is "disabled"
1602 if (!authority.enabled) {
1603 continue;
1604 }
1605
1606 // if we already have a record of this new authority then don't copy over the settings
Amith Yamasani04e0d262012-02-14 11:50:53 -08001607 if (getAuthorityLocked(authority.account, authority.userId, newAuthorityName, "cleanup")
1608 != null) {
Fred Quintanafb084402010-03-23 17:57:03 -07001609 continue;
1610 }
1611
1612 AuthorityInfo newAuthority = getOrCreateAuthorityLocked(authority.account,
Amith Yamasani04e0d262012-02-14 11:50:53 -08001613 authority.userId, newAuthorityName, -1 /* ident */, false /* doWrite */);
Fred Quintanafb084402010-03-23 17:57:03 -07001614 newAuthority.enabled = true;
1615 writeNeeded = true;
1616 }
1617
1618 for (AuthorityInfo authorityInfo : authoritiesToRemove) {
Amith Yamasani04e0d262012-02-14 11:50:53 -08001619 removeAuthorityLocked(authorityInfo.account, authorityInfo.userId,
1620 authorityInfo.authority, false /* doWrite */);
Fred Quintanafb084402010-03-23 17:57:03 -07001621 writeNeeded = true;
1622 }
1623
1624 return writeNeeded;
1625 }
1626
Amith Yamasani04e0d262012-02-14 11:50:53 -08001627 private void parseListenForTickles(XmlPullParser parser) {
1628 String user = parser.getAttributeValue(null, XML_ATTR_USER);
1629 int userId = 0;
1630 try {
1631 userId = Integer.parseInt(user);
1632 } catch (NumberFormatException e) {
1633 Log.e(TAG, "error parsing the user for listen-for-tickles", e);
1634 } catch (NullPointerException e) {
1635 Log.e(TAG, "the user in listen-for-tickles is null", e);
1636 }
1637 String enabled = parser.getAttributeValue(null, XML_ATTR_ENABLED);
1638 boolean listen = enabled == null || Boolean.parseBoolean(enabled);
1639 mMasterSyncAutomatically.put(userId, listen);
1640 }
1641
Fred Quintanac2e46912010-03-15 16:10:44 -07001642 private AuthorityInfo parseAuthority(XmlPullParser parser, int version) {
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001643 AuthorityInfo authority = null;
1644 int id = -1;
1645 try {
1646 id = Integer.parseInt(parser.getAttributeValue(
1647 null, "id"));
1648 } catch (NumberFormatException e) {
1649 Log.e(TAG, "error parsing the id of the authority", e);
1650 } catch (NullPointerException e) {
1651 Log.e(TAG, "the id of the authority is null", e);
1652 }
1653 if (id >= 0) {
Fred Quintanafb084402010-03-23 17:57:03 -07001654 String authorityName = parser.getAttributeValue(null, "authority");
Amith Yamasani04e0d262012-02-14 11:50:53 -08001655 String enabled = parser.getAttributeValue(null, XML_ATTR_ENABLED);
Fred Quintanafb084402010-03-23 17:57:03 -07001656 String syncable = parser.getAttributeValue(null, "syncable");
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001657 String accountName = parser.getAttributeValue(null, "account");
1658 String accountType = parser.getAttributeValue(null, "type");
Amith Yamasani04e0d262012-02-14 11:50:53 -08001659 String user = parser.getAttributeValue(null, XML_ATTR_USER);
1660 int userId = user == null ? 0 : Integer.parseInt(user);
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001661 if (accountType == null) {
1662 accountType = "com.google";
Fred Quintanafb084402010-03-23 17:57:03 -07001663 syncable = "unknown";
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001664 }
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001665 authority = mAuthorities.get(id);
1666 if (DEBUG_FILE) Log.v(TAG, "Adding authority: account="
1667 + accountName + " auth=" + authorityName
Amith Yamasani04e0d262012-02-14 11:50:53 -08001668 + " user=" + userId
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001669 + " enabled=" + enabled
1670 + " syncable=" + syncable);
1671 if (authority == null) {
1672 if (DEBUG_FILE) Log.v(TAG, "Creating entry");
1673 authority = getOrCreateAuthorityLocked(
Amith Yamasani04e0d262012-02-14 11:50:53 -08001674 new Account(accountName, accountType), userId, authorityName, id, false);
Fred Quintanac2e46912010-03-15 16:10:44 -07001675 // If the version is 0 then we are upgrading from a file format that did not
1676 // know about periodic syncs. In that case don't clear the list since we
1677 // want the default, which is a daily periodioc sync.
1678 // Otherwise clear out this default list since we will populate it later with
1679 // the periodic sync descriptions that are read from the configuration file.
1680 if (version > 0) {
1681 authority.periodicSyncs.clear();
1682 }
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001683 }
1684 if (authority != null) {
1685 authority.enabled = enabled == null || Boolean.parseBoolean(enabled);
1686 if ("unknown".equals(syncable)) {
1687 authority.syncable = -1;
1688 } else {
1689 authority.syncable =
Fred Quintanafb084402010-03-23 17:57:03 -07001690 (syncable == null || Boolean.parseBoolean(syncable)) ? 1 : 0;
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001691 }
1692 } else {
1693 Log.w(TAG, "Failure adding authority: account="
1694 + accountName + " auth=" + authorityName
1695 + " enabled=" + enabled
1696 + " syncable=" + syncable);
1697 }
1698 }
1699
1700 return authority;
1701 }
1702
1703 private Pair<Bundle, Long> parsePeriodicSync(XmlPullParser parser, AuthorityInfo authority) {
1704 Bundle extras = new Bundle();
1705 String periodValue = parser.getAttributeValue(null, "period");
1706 final long period;
1707 try {
1708 period = Long.parseLong(periodValue);
1709 } catch (NumberFormatException e) {
1710 Log.e(TAG, "error parsing the period of a periodic sync", e);
1711 return null;
1712 } catch (NullPointerException e) {
1713 Log.e(TAG, "the period of a periodic sync is null", e);
1714 return null;
1715 }
1716 final Pair<Bundle, Long> periodicSync = Pair.create(extras, period);
1717 authority.periodicSyncs.add(periodicSync);
Fred Quintanac2e46912010-03-15 16:10:44 -07001718
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001719 return periodicSync;
1720 }
1721
1722 private void parseExtra(XmlPullParser parser, Pair<Bundle, Long> periodicSync) {
1723 final Bundle extras = periodicSync.first;
1724 String name = parser.getAttributeValue(null, "name");
1725 String type = parser.getAttributeValue(null, "type");
1726 String value1 = parser.getAttributeValue(null, "value1");
1727 String value2 = parser.getAttributeValue(null, "value2");
1728
1729 try {
1730 if ("long".equals(type)) {
1731 extras.putLong(name, Long.parseLong(value1));
1732 } else if ("integer".equals(type)) {
1733 extras.putInt(name, Integer.parseInt(value1));
1734 } else if ("double".equals(type)) {
1735 extras.putDouble(name, Double.parseDouble(value1));
1736 } else if ("float".equals(type)) {
1737 extras.putFloat(name, Float.parseFloat(value1));
1738 } else if ("boolean".equals(type)) {
1739 extras.putBoolean(name, Boolean.parseBoolean(value1));
1740 } else if ("string".equals(type)) {
1741 extras.putString(name, value1);
1742 } else if ("account".equals(type)) {
1743 extras.putParcelable(name, new Account(value1, value2));
1744 }
1745 } catch (NumberFormatException e) {
1746 Log.e(TAG, "error parsing bundle value", e);
1747 } catch (NullPointerException e) {
1748 Log.e(TAG, "error parsing bundle value", e);
1749 }
1750 }
1751
Dianne Hackborn231cc602009-04-27 17:10:36 -07001752 /**
1753 * Write all account information to the account file.
1754 */
1755 private void writeAccountInfoLocked() {
1756 if (DEBUG_FILE) Log.v(TAG, "Writing new " + mAccountInfoFile.getBaseFile());
1757 FileOutputStream fos = null;
Costin Manolache360e4542009-09-04 13:36:04 -07001758
Dianne Hackborn231cc602009-04-27 17:10:36 -07001759 try {
1760 fos = mAccountInfoFile.startWrite();
1761 XmlSerializer out = new FastXmlSerializer();
1762 out.setOutput(fos, "utf-8");
1763 out.startDocument(null, true);
1764 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
Costin Manolache360e4542009-09-04 13:36:04 -07001765
Dianne Hackborn231cc602009-04-27 17:10:36 -07001766 out.startTag(null, "accounts");
Fred Quintanac2e46912010-03-15 16:10:44 -07001767 out.attribute(null, "version", Integer.toString(ACCOUNTS_VERSION));
Amith Yamasani04e0d262012-02-14 11:50:53 -08001768 out.attribute(null, XML_ATTR_NEXT_AUTHORITY_ID, Integer.toString(mNextAuthorityId));
Ashish Sharma69d95de2012-04-11 17:27:24 -07001769 out.attribute(null, XML_ATTR_SYNC_RANDOM_OFFSET, Integer.toString(mSyncRandomOffset));
Amith Yamasani04e0d262012-02-14 11:50:53 -08001770
1771 // Write the Sync Automatically flags for each user
1772 final int M = mMasterSyncAutomatically.size();
1773 for (int m = 0; m < M; m++) {
1774 int userId = mMasterSyncAutomatically.keyAt(m);
1775 Boolean listen = mMasterSyncAutomatically.valueAt(m);
1776 out.startTag(null, XML_TAG_LISTEN_FOR_TICKLES);
1777 out.attribute(null, XML_ATTR_USER, Integer.toString(userId));
1778 out.attribute(null, XML_ATTR_ENABLED, Boolean.toString(listen));
1779 out.endTag(null, XML_TAG_LISTEN_FOR_TICKLES);
Dianne Hackborn231cc602009-04-27 17:10:36 -07001780 }
Costin Manolache360e4542009-09-04 13:36:04 -07001781
Dianne Hackborn231cc602009-04-27 17:10:36 -07001782 final int N = mAuthorities.size();
1783 for (int i=0; i<N; i++) {
Costin Manolache360e4542009-09-04 13:36:04 -07001784 AuthorityInfo authority = mAuthorities.valueAt(i);
Dianne Hackborn231cc602009-04-27 17:10:36 -07001785 out.startTag(null, "authority");
1786 out.attribute(null, "id", Integer.toString(authority.ident));
Fred Quintanaffd0cb042009-08-15 21:45:26 -07001787 out.attribute(null, "account", authority.account.name);
Amith Yamasani04e0d262012-02-14 11:50:53 -08001788 out.attribute(null, XML_ATTR_USER, Integer.toString(authority.userId));
Fred Quintanaffd0cb042009-08-15 21:45:26 -07001789 out.attribute(null, "type", authority.account.type);
Dianne Hackborn231cc602009-04-27 17:10:36 -07001790 out.attribute(null, "authority", authority.authority);
Amith Yamasani04e0d262012-02-14 11:50:53 -08001791 out.attribute(null, XML_ATTR_ENABLED, Boolean.toString(authority.enabled));
Fred Quintana5e787c42009-08-16 23:13:53 -07001792 if (authority.syncable < 0) {
1793 out.attribute(null, "syncable", "unknown");
Fred Quintanafb084402010-03-23 17:57:03 -07001794 } else {
1795 out.attribute(null, "syncable", Boolean.toString(authority.syncable != 0));
Fred Quintana5e787c42009-08-16 23:13:53 -07001796 }
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001797 for (Pair<Bundle, Long> periodicSync : authority.periodicSyncs) {
1798 out.startTag(null, "periodicSync");
1799 out.attribute(null, "period", Long.toString(periodicSync.second));
1800 final Bundle extras = periodicSync.first;
1801 for (String key : extras.keySet()) {
1802 out.startTag(null, "extra");
1803 out.attribute(null, "name", key);
1804 final Object value = extras.get(key);
1805 if (value instanceof Long) {
1806 out.attribute(null, "type", "long");
1807 out.attribute(null, "value1", value.toString());
1808 } else if (value instanceof Integer) {
1809 out.attribute(null, "type", "integer");
1810 out.attribute(null, "value1", value.toString());
1811 } else if (value instanceof Boolean) {
1812 out.attribute(null, "type", "boolean");
1813 out.attribute(null, "value1", value.toString());
1814 } else if (value instanceof Float) {
1815 out.attribute(null, "type", "float");
1816 out.attribute(null, "value1", value.toString());
1817 } else if (value instanceof Double) {
1818 out.attribute(null, "type", "double");
1819 out.attribute(null, "value1", value.toString());
1820 } else if (value instanceof String) {
1821 out.attribute(null, "type", "string");
1822 out.attribute(null, "value1", value.toString());
1823 } else if (value instanceof Account) {
1824 out.attribute(null, "type", "account");
1825 out.attribute(null, "value1", ((Account)value).name);
1826 out.attribute(null, "value2", ((Account)value).type);
1827 }
1828 out.endTag(null, "extra");
1829 }
1830 out.endTag(null, "periodicSync");
1831 }
Dianne Hackborn231cc602009-04-27 17:10:36 -07001832 out.endTag(null, "authority");
1833 }
Costin Manolache360e4542009-09-04 13:36:04 -07001834
Dianne Hackborn231cc602009-04-27 17:10:36 -07001835 out.endTag(null, "accounts");
Costin Manolache360e4542009-09-04 13:36:04 -07001836
Dianne Hackborn231cc602009-04-27 17:10:36 -07001837 out.endDocument();
Costin Manolache360e4542009-09-04 13:36:04 -07001838
Dianne Hackborn231cc602009-04-27 17:10:36 -07001839 mAccountInfoFile.finishWrite(fos);
1840 } catch (java.io.IOException e1) {
1841 Log.w(TAG, "Error writing accounts", e1);
1842 if (fos != null) {
1843 mAccountInfoFile.failWrite(fos);
1844 }
1845 }
1846 }
Costin Manolache360e4542009-09-04 13:36:04 -07001847
Dianne Hackborn231cc602009-04-27 17:10:36 -07001848 static int getIntColumn(Cursor c, String name) {
1849 return c.getInt(c.getColumnIndex(name));
1850 }
Costin Manolache360e4542009-09-04 13:36:04 -07001851
Dianne Hackborn231cc602009-04-27 17:10:36 -07001852 static long getLongColumn(Cursor c, String name) {
1853 return c.getLong(c.getColumnIndex(name));
1854 }
Costin Manolache360e4542009-09-04 13:36:04 -07001855
Dianne Hackborn231cc602009-04-27 17:10:36 -07001856 /**
1857 * Load sync engine state from the old syncmanager database, and then
1858 * erase it. Note that we don't deal with pending operations, active
1859 * sync, or history.
1860 */
Fred Quintana77c560f2010-03-29 22:20:26 -07001861 private void readAndDeleteLegacyAccountInfoLocked() {
Dianne Hackborn231cc602009-04-27 17:10:36 -07001862 // Look for old database to initialize from.
1863 File file = mContext.getDatabasePath("syncmanager.db");
1864 if (!file.exists()) {
1865 return;
1866 }
1867 String path = file.getPath();
1868 SQLiteDatabase db = null;
1869 try {
1870 db = SQLiteDatabase.openDatabase(path, null,
1871 SQLiteDatabase.OPEN_READONLY);
1872 } catch (SQLiteException e) {
1873 }
Costin Manolache360e4542009-09-04 13:36:04 -07001874
Dianne Hackborn231cc602009-04-27 17:10:36 -07001875 if (db != null) {
Dianne Hackborn2d5ed1f2009-05-06 15:22:38 -07001876 final boolean hasType = db.getVersion() >= 11;
Costin Manolache360e4542009-09-04 13:36:04 -07001877
Dianne Hackborn231cc602009-04-27 17:10:36 -07001878 // Copy in all of the status information, as well as accounts.
1879 if (DEBUG_FILE) Log.v(TAG, "Reading legacy sync accounts db");
1880 SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
1881 qb.setTables("stats, status");
1882 HashMap<String,String> map = new HashMap<String,String>();
1883 map.put("_id", "status._id as _id");
1884 map.put("account", "stats.account as account");
Dianne Hackborn2d5ed1f2009-05-06 15:22:38 -07001885 if (hasType) {
1886 map.put("account_type", "stats.account_type as account_type");
1887 }
Dianne Hackborn231cc602009-04-27 17:10:36 -07001888 map.put("authority", "stats.authority as authority");
1889 map.put("totalElapsedTime", "totalElapsedTime");
1890 map.put("numSyncs", "numSyncs");
1891 map.put("numSourceLocal", "numSourceLocal");
1892 map.put("numSourcePoll", "numSourcePoll");
1893 map.put("numSourceServer", "numSourceServer");
1894 map.put("numSourceUser", "numSourceUser");
1895 map.put("lastSuccessSource", "lastSuccessSource");
1896 map.put("lastSuccessTime", "lastSuccessTime");
1897 map.put("lastFailureSource", "lastFailureSource");
1898 map.put("lastFailureTime", "lastFailureTime");
1899 map.put("lastFailureMesg", "lastFailureMesg");
1900 map.put("pending", "pending");
1901 qb.setProjectionMap(map);
1902 qb.appendWhere("stats._id = status.stats_id");
1903 Cursor c = qb.query(db, null, null, null, null, null, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001904 while (c.moveToNext()) {
Dianne Hackborn231cc602009-04-27 17:10:36 -07001905 String accountName = c.getString(c.getColumnIndex("account"));
Dianne Hackborn2d5ed1f2009-05-06 15:22:38 -07001906 String accountType = hasType
1907 ? c.getString(c.getColumnIndex("account_type")) : null;
Dianne Hackborn7a135592009-05-06 00:28:37 -07001908 if (accountType == null) {
Costin Manolache3348f142009-09-29 18:58:36 -07001909 accountType = "com.google";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001910 }
Dianne Hackborn231cc602009-04-27 17:10:36 -07001911 String authorityName = c.getString(c.getColumnIndex("authority"));
1912 AuthorityInfo authority = this.getOrCreateAuthorityLocked(
Amith Yamasani04e0d262012-02-14 11:50:53 -08001913 new Account(accountName, accountType), 0 /* legacy is single-user */,
Dianne Hackborn7a135592009-05-06 00:28:37 -07001914 authorityName, -1, false);
Dianne Hackborn231cc602009-04-27 17:10:36 -07001915 if (authority != null) {
1916 int i = mSyncStatus.size();
1917 boolean found = false;
1918 SyncStatusInfo st = null;
1919 while (i > 0) {
1920 i--;
Costin Manolache360e4542009-09-04 13:36:04 -07001921 st = mSyncStatus.valueAt(i);
Dianne Hackborn231cc602009-04-27 17:10:36 -07001922 if (st.authorityId == authority.ident) {
1923 found = true;
1924 break;
1925 }
1926 }
1927 if (!found) {
1928 st = new SyncStatusInfo(authority.ident);
1929 mSyncStatus.put(authority.ident, st);
1930 }
1931 st.totalElapsedTime = getLongColumn(c, "totalElapsedTime");
1932 st.numSyncs = getIntColumn(c, "numSyncs");
1933 st.numSourceLocal = getIntColumn(c, "numSourceLocal");
1934 st.numSourcePoll = getIntColumn(c, "numSourcePoll");
1935 st.numSourceServer = getIntColumn(c, "numSourceServer");
1936 st.numSourceUser = getIntColumn(c, "numSourceUser");
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001937 st.numSourcePeriodic = 0;
Dianne Hackborn231cc602009-04-27 17:10:36 -07001938 st.lastSuccessSource = getIntColumn(c, "lastSuccessSource");
1939 st.lastSuccessTime = getLongColumn(c, "lastSuccessTime");
1940 st.lastFailureSource = getIntColumn(c, "lastFailureSource");
1941 st.lastFailureTime = getLongColumn(c, "lastFailureTime");
1942 st.lastFailureMesg = c.getString(c.getColumnIndex("lastFailureMesg"));
1943 st.pending = getIntColumn(c, "pending") != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001944 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001945 }
Costin Manolache360e4542009-09-04 13:36:04 -07001946
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001947 c.close();
Costin Manolache360e4542009-09-04 13:36:04 -07001948
Dianne Hackborn231cc602009-04-27 17:10:36 -07001949 // Retrieve the settings.
1950 qb = new SQLiteQueryBuilder();
1951 qb.setTables("settings");
1952 c = qb.query(db, null, null, null, null, null, null);
1953 while (c.moveToNext()) {
1954 String name = c.getString(c.getColumnIndex("name"));
1955 String value = c.getString(c.getColumnIndex("value"));
1956 if (name == null) continue;
1957 if (name.equals("listen_for_tickles")) {
Amith Yamasani04e0d262012-02-14 11:50:53 -08001958 setMasterSyncAutomatically(value == null || Boolean.parseBoolean(value), 0);
Dianne Hackborn231cc602009-04-27 17:10:36 -07001959 } else if (name.startsWith("sync_provider_")) {
1960 String provider = name.substring("sync_provider_".length(),
1961 name.length());
Fred Quintanaac9385e2009-06-22 18:00:59 -07001962 int i = mAuthorities.size();
1963 while (i > 0) {
1964 i--;
Costin Manolache360e4542009-09-04 13:36:04 -07001965 AuthorityInfo authority = mAuthorities.valueAt(i);
Fred Quintanaac9385e2009-06-22 18:00:59 -07001966 if (authority.authority.equals(provider)) {
1967 authority.enabled = value == null || Boolean.parseBoolean(value);
Fred Quintana5e787c42009-08-16 23:13:53 -07001968 authority.syncable = 1;
Fred Quintanaac9385e2009-06-22 18:00:59 -07001969 }
1970 }
Dianne Hackborn231cc602009-04-27 17:10:36 -07001971 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001972 }
Costin Manolache360e4542009-09-04 13:36:04 -07001973
Dianne Hackborn231cc602009-04-27 17:10:36 -07001974 c.close();
Costin Manolache360e4542009-09-04 13:36:04 -07001975
Dianne Hackborn231cc602009-04-27 17:10:36 -07001976 db.close();
Costin Manolache360e4542009-09-04 13:36:04 -07001977
Dianne Hackborn231cc602009-04-27 17:10:36 -07001978 (new File(path)).delete();
1979 }
1980 }
Costin Manolache360e4542009-09-04 13:36:04 -07001981
Dianne Hackborn231cc602009-04-27 17:10:36 -07001982 public static final int STATUS_FILE_END = 0;
1983 public static final int STATUS_FILE_ITEM = 100;
Costin Manolache360e4542009-09-04 13:36:04 -07001984
Dianne Hackborn231cc602009-04-27 17:10:36 -07001985 /**
1986 * Read all sync status back in to the initial engine state.
1987 */
1988 private void readStatusLocked() {
1989 if (DEBUG_FILE) Log.v(TAG, "Reading " + mStatusFile.getBaseFile());
1990 try {
1991 byte[] data = mStatusFile.readFully();
1992 Parcel in = Parcel.obtain();
1993 in.unmarshall(data, 0, data.length);
1994 in.setDataPosition(0);
1995 int token;
1996 while ((token=in.readInt()) != STATUS_FILE_END) {
1997 if (token == STATUS_FILE_ITEM) {
1998 SyncStatusInfo status = new SyncStatusInfo(in);
1999 if (mAuthorities.indexOfKey(status.authorityId) >= 0) {
2000 status.pending = false;
2001 if (DEBUG_FILE) Log.v(TAG, "Adding status for id "
2002 + status.authorityId);
2003 mSyncStatus.put(status.authorityId, status);
2004 }
2005 } else {
2006 // Ooops.
2007 Log.w(TAG, "Unknown status token: " + token);
2008 break;
2009 }
2010 }
2011 } catch (java.io.IOException e) {
2012 Log.i(TAG, "No initial status");
2013 }
2014 }
Costin Manolache360e4542009-09-04 13:36:04 -07002015
Dianne Hackborn231cc602009-04-27 17:10:36 -07002016 /**
2017 * Write all sync status to the sync status file.
2018 */
2019 private void writeStatusLocked() {
2020 if (DEBUG_FILE) Log.v(TAG, "Writing new " + mStatusFile.getBaseFile());
Costin Manolache360e4542009-09-04 13:36:04 -07002021
Dianne Hackborn231cc602009-04-27 17:10:36 -07002022 // The file is being written, so we don't need to have a scheduled
2023 // write until the next change.
2024 removeMessages(MSG_WRITE_STATUS);
Costin Manolache360e4542009-09-04 13:36:04 -07002025
Dianne Hackborn231cc602009-04-27 17:10:36 -07002026 FileOutputStream fos = null;
2027 try {
2028 fos = mStatusFile.startWrite();
2029 Parcel out = Parcel.obtain();
2030 final int N = mSyncStatus.size();
2031 for (int i=0; i<N; i++) {
2032 SyncStatusInfo status = mSyncStatus.valueAt(i);
2033 out.writeInt(STATUS_FILE_ITEM);
2034 status.writeToParcel(out, 0);
2035 }
2036 out.writeInt(STATUS_FILE_END);
2037 fos.write(out.marshall());
2038 out.recycle();
Costin Manolache360e4542009-09-04 13:36:04 -07002039
Dianne Hackborn231cc602009-04-27 17:10:36 -07002040 mStatusFile.finishWrite(fos);
2041 } catch (java.io.IOException e1) {
2042 Log.w(TAG, "Error writing status", e1);
2043 if (fos != null) {
2044 mStatusFile.failWrite(fos);
2045 }
2046 }
2047 }
Costin Manolache360e4542009-09-04 13:36:04 -07002048
Fred Quintana307da1a2010-01-21 14:24:20 -08002049 public static final int PENDING_OPERATION_VERSION = 2;
Costin Manolache360e4542009-09-04 13:36:04 -07002050
Dianne Hackborn231cc602009-04-27 17:10:36 -07002051 /**
2052 * Read all pending operations back in to the initial engine state.
2053 */
2054 private void readPendingOperationsLocked() {
2055 if (DEBUG_FILE) Log.v(TAG, "Reading " + mPendingFile.getBaseFile());
2056 try {
2057 byte[] data = mPendingFile.readFully();
2058 Parcel in = Parcel.obtain();
2059 in.unmarshall(data, 0, data.length);
2060 in.setDataPosition(0);
2061 final int SIZE = in.dataSize();
2062 while (in.dataPosition() < SIZE) {
2063 int version = in.readInt();
Fred Quintana307da1a2010-01-21 14:24:20 -08002064 if (version != PENDING_OPERATION_VERSION && version != 1) {
Dianne Hackborn231cc602009-04-27 17:10:36 -07002065 Log.w(TAG, "Unknown pending operation version "
2066 + version + "; dropping all ops");
2067 break;
2068 }
2069 int authorityId = in.readInt();
2070 int syncSource = in.readInt();
2071 byte[] flatExtras = in.createByteArray();
Fred Quintana307da1a2010-01-21 14:24:20 -08002072 boolean expedited;
2073 if (version == PENDING_OPERATION_VERSION) {
2074 expedited = in.readInt() != 0;
2075 } else {
2076 expedited = false;
2077 }
Dianne Hackborn231cc602009-04-27 17:10:36 -07002078 AuthorityInfo authority = mAuthorities.get(authorityId);
2079 if (authority != null) {
Fred Quintana5695c7b2010-12-06 15:07:52 -08002080 Bundle extras;
Dianne Hackborn231cc602009-04-27 17:10:36 -07002081 if (flatExtras != null) {
2082 extras = unflattenBundle(flatExtras);
Fred Quintana5695c7b2010-12-06 15:07:52 -08002083 } else {
2084 // if we are unable to parse the extras for whatever reason convert this
2085 // to a regular sync by creating an empty extras
2086 extras = new Bundle();
Dianne Hackborn231cc602009-04-27 17:10:36 -07002087 }
2088 PendingOperation op = new PendingOperation(
Amith Yamasani04e0d262012-02-14 11:50:53 -08002089 authority.account, authority.userId, syncSource,
Fred Quintana307da1a2010-01-21 14:24:20 -08002090 authority.authority, extras, expedited);
Dianne Hackborn231cc602009-04-27 17:10:36 -07002091 op.authorityId = authorityId;
2092 op.flatExtras = flatExtras;
2093 if (DEBUG_FILE) Log.v(TAG, "Adding pending op: account=" + op.account
2094 + " auth=" + op.authority
2095 + " src=" + op.syncSource
Fred Quintana307da1a2010-01-21 14:24:20 -08002096 + " expedited=" + op.expedited
Dianne Hackborn231cc602009-04-27 17:10:36 -07002097 + " extras=" + op.extras);
2098 mPendingOperations.add(op);
2099 }
2100 }
2101 } catch (java.io.IOException e) {
2102 Log.i(TAG, "No initial pending operations");
2103 }
2104 }
Costin Manolache360e4542009-09-04 13:36:04 -07002105
Dianne Hackborn231cc602009-04-27 17:10:36 -07002106 private void writePendingOperationLocked(PendingOperation op, Parcel out) {
2107 out.writeInt(PENDING_OPERATION_VERSION);
2108 out.writeInt(op.authorityId);
2109 out.writeInt(op.syncSource);
2110 if (op.flatExtras == null && op.extras != null) {
2111 op.flatExtras = flattenBundle(op.extras);
2112 }
2113 out.writeByteArray(op.flatExtras);
Fred Quintana307da1a2010-01-21 14:24:20 -08002114 out.writeInt(op.expedited ? 1 : 0);
Dianne Hackborn231cc602009-04-27 17:10:36 -07002115 }
Costin Manolache360e4542009-09-04 13:36:04 -07002116
Dianne Hackborn231cc602009-04-27 17:10:36 -07002117 /**
2118 * Write all currently pending ops to the pending ops file.
2119 */
2120 private void writePendingOperationsLocked() {
2121 final int N = mPendingOperations.size();
2122 FileOutputStream fos = null;
2123 try {
2124 if (N == 0) {
2125 if (DEBUG_FILE) Log.v(TAG, "Truncating " + mPendingFile.getBaseFile());
2126 mPendingFile.truncate();
2127 return;
2128 }
Costin Manolache360e4542009-09-04 13:36:04 -07002129
Dianne Hackborn231cc602009-04-27 17:10:36 -07002130 if (DEBUG_FILE) Log.v(TAG, "Writing new " + mPendingFile.getBaseFile());
2131 fos = mPendingFile.startWrite();
Costin Manolache360e4542009-09-04 13:36:04 -07002132
Dianne Hackborn231cc602009-04-27 17:10:36 -07002133 Parcel out = Parcel.obtain();
2134 for (int i=0; i<N; i++) {
2135 PendingOperation op = mPendingOperations.get(i);
2136 writePendingOperationLocked(op, out);
2137 }
2138 fos.write(out.marshall());
2139 out.recycle();
Costin Manolache360e4542009-09-04 13:36:04 -07002140
Dianne Hackborn231cc602009-04-27 17:10:36 -07002141 mPendingFile.finishWrite(fos);
2142 } catch (java.io.IOException e1) {
2143 Log.w(TAG, "Error writing pending operations", e1);
2144 if (fos != null) {
2145 mPendingFile.failWrite(fos);
2146 }
2147 }
2148 }
Costin Manolache360e4542009-09-04 13:36:04 -07002149
Dianne Hackborn231cc602009-04-27 17:10:36 -07002150 /**
2151 * Append the given operation to the pending ops file; if unable to,
2152 * write all pending ops.
2153 */
2154 private void appendPendingOperationLocked(PendingOperation op) {
2155 if (DEBUG_FILE) Log.v(TAG, "Appending to " + mPendingFile.getBaseFile());
2156 FileOutputStream fos = null;
2157 try {
2158 fos = mPendingFile.openAppend();
2159 } catch (java.io.IOException e) {
2160 if (DEBUG_FILE) Log.v(TAG, "Failed append; writing full file");
2161 writePendingOperationsLocked();
2162 return;
2163 }
Costin Manolache360e4542009-09-04 13:36:04 -07002164
Dianne Hackborn231cc602009-04-27 17:10:36 -07002165 try {
2166 Parcel out = Parcel.obtain();
2167 writePendingOperationLocked(op, out);
2168 fos.write(out.marshall());
2169 out.recycle();
2170 } catch (java.io.IOException e1) {
2171 Log.w(TAG, "Error writing pending operations", e1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002172 } finally {
Dianne Hackborn231cc602009-04-27 17:10:36 -07002173 try {
2174 fos.close();
2175 } catch (java.io.IOException e2) {
2176 }
2177 }
2178 }
Costin Manolache360e4542009-09-04 13:36:04 -07002179
Dianne Hackborn231cc602009-04-27 17:10:36 -07002180 static private byte[] flattenBundle(Bundle bundle) {
2181 byte[] flatData = null;
2182 Parcel parcel = Parcel.obtain();
2183 try {
2184 bundle.writeToParcel(parcel, 0);
2185 flatData = parcel.marshall();
2186 } finally {
2187 parcel.recycle();
2188 }
2189 return flatData;
2190 }
Costin Manolache360e4542009-09-04 13:36:04 -07002191
Dianne Hackborn231cc602009-04-27 17:10:36 -07002192 static private Bundle unflattenBundle(byte[] flatData) {
2193 Bundle bundle;
2194 Parcel parcel = Parcel.obtain();
2195 try {
2196 parcel.unmarshall(flatData, 0, flatData.length);
2197 parcel.setDataPosition(0);
2198 bundle = parcel.readBundle();
2199 } catch (RuntimeException e) {
2200 // A RuntimeException is thrown if we were unable to parse the parcel.
2201 // Create an empty parcel in this case.
2202 bundle = new Bundle();
2203 } finally {
2204 parcel.recycle();
2205 }
2206 return bundle;
2207 }
Costin Manolache360e4542009-09-04 13:36:04 -07002208
Amith Yamasani04e0d262012-02-14 11:50:53 -08002209 private void requestSync(Account account, int userId, String authority, Bundle extras) {
2210 // If this is happening in the system process, then call the syncrequest listener
2211 // to make a request back to the SyncManager directly.
2212 // If this is probably a test instance, then call back through the ContentResolver
2213 // which will know which userId to apply based on the Binder id.
2214 if (android.os.Process.myUid() == android.os.Process.SYSTEM_UID
2215 && mSyncRequestListener != null) {
2216 mSyncRequestListener.onSyncRequest(account, userId, authority, extras);
2217 } else {
2218 ContentResolver.requestSync(account, authority, extras);
2219 }
2220 }
2221
Dianne Hackborn231cc602009-04-27 17:10:36 -07002222 public static final int STATISTICS_FILE_END = 0;
2223 public static final int STATISTICS_FILE_ITEM_OLD = 100;
2224 public static final int STATISTICS_FILE_ITEM = 101;
Costin Manolache360e4542009-09-04 13:36:04 -07002225
Dianne Hackborn231cc602009-04-27 17:10:36 -07002226 /**
2227 * Read all sync statistics back in to the initial engine state.
2228 */
2229 private void readStatisticsLocked() {
2230 try {
2231 byte[] data = mStatisticsFile.readFully();
2232 Parcel in = Parcel.obtain();
2233 in.unmarshall(data, 0, data.length);
2234 in.setDataPosition(0);
2235 int token;
2236 int index = 0;
2237 while ((token=in.readInt()) != STATISTICS_FILE_END) {
2238 if (token == STATISTICS_FILE_ITEM
2239 || token == STATISTICS_FILE_ITEM_OLD) {
2240 int day = in.readInt();
2241 if (token == STATISTICS_FILE_ITEM_OLD) {
2242 day = day - 2009 + 14245; // Magic!
2243 }
2244 DayStats ds = new DayStats(day);
2245 ds.successCount = in.readInt();
2246 ds.successTime = in.readLong();
2247 ds.failureCount = in.readInt();
2248 ds.failureTime = in.readLong();
2249 if (index < mDayStats.length) {
2250 mDayStats[index] = ds;
2251 index++;
2252 }
2253 } else {
2254 // Ooops.
2255 Log.w(TAG, "Unknown stats token: " + token);
2256 break;
2257 }
2258 }
2259 } catch (java.io.IOException e) {
2260 Log.i(TAG, "No initial statistics");
2261 }
2262 }
Costin Manolache360e4542009-09-04 13:36:04 -07002263
Dianne Hackborn231cc602009-04-27 17:10:36 -07002264 /**
2265 * Write all sync statistics to the sync status file.
2266 */
2267 private void writeStatisticsLocked() {
2268 if (DEBUG_FILE) Log.v(TAG, "Writing new " + mStatisticsFile.getBaseFile());
Costin Manolache360e4542009-09-04 13:36:04 -07002269
Dianne Hackborn231cc602009-04-27 17:10:36 -07002270 // The file is being written, so we don't need to have a scheduled
2271 // write until the next change.
2272 removeMessages(MSG_WRITE_STATISTICS);
Costin Manolache360e4542009-09-04 13:36:04 -07002273
Dianne Hackborn231cc602009-04-27 17:10:36 -07002274 FileOutputStream fos = null;
2275 try {
2276 fos = mStatisticsFile.startWrite();
2277 Parcel out = Parcel.obtain();
2278 final int N = mDayStats.length;
2279 for (int i=0; i<N; i++) {
2280 DayStats ds = mDayStats[i];
2281 if (ds == null) {
2282 break;
2283 }
2284 out.writeInt(STATISTICS_FILE_ITEM);
2285 out.writeInt(ds.day);
2286 out.writeInt(ds.successCount);
2287 out.writeLong(ds.successTime);
2288 out.writeInt(ds.failureCount);
2289 out.writeLong(ds.failureTime);
2290 }
2291 out.writeInt(STATISTICS_FILE_END);
2292 fos.write(out.marshall());
2293 out.recycle();
Costin Manolache360e4542009-09-04 13:36:04 -07002294
Dianne Hackborn231cc602009-04-27 17:10:36 -07002295 mStatisticsFile.finishWrite(fos);
2296 } catch (java.io.IOException e1) {
2297 Log.w(TAG, "Error writing stats", e1);
2298 if (fos != null) {
2299 mStatisticsFile.failWrite(fos);
2300 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002301 }
2302 }
2303}