blob: f804fa1cff3e16bd4da7ab2126658a34be9ca29b [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
Jeff Sharkey7a96c392012-11-15 14:01:46 -080017package com.android.server.content;
Jason parksa3cdaa52011-01-13 14:15:43 -060018
Fred Quintanad9d2f112009-04-23 13:36:27 -070019import android.accounts.Account;
Amith Yamasanif29f2362012-04-05 18:29:52 -070020import android.accounts.AccountAndUser;
Suprabh Shukla042a4782017-05-12 15:26:54 -070021import android.accounts.AccountManager;
Marvin Paula6533252014-11-24 12:57:48 -080022import android.app.backup.BackupManager;
Matthew Williamsfa774182013-06-18 15:44:11 -070023import android.content.ComponentName;
Jeff Sharkey7a96c392012-11-15 14:01:46 -080024import android.content.ContentResolver;
25import android.content.Context;
26import android.content.ISyncStatusObserver;
27import android.content.PeriodicSync;
28import android.content.SyncInfo;
Matthew Williams56dbf8f2013-07-26 12:56:39 -070029import android.content.SyncRequest;
Jeff Sharkey7a96c392012-11-15 14:01:46 -080030import android.content.SyncStatusInfo;
Suprabh Shukla042a4782017-05-12 15:26:54 -070031import android.content.pm.PackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032import android.database.Cursor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import android.database.sqlite.SQLiteDatabase;
Dianne Hackborn231cc602009-04-27 17:10:36 -070034import android.database.sqlite.SQLiteException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import android.database.sqlite.SQLiteQueryBuilder;
Dianne Hackborn231cc602009-04-27 17:10:36 -070036import android.os.Bundle;
37import android.os.Environment;
38import android.os.Handler;
39import android.os.Message;
40import android.os.Parcel;
41import android.os.RemoteCallbackList;
42import android.os.RemoteException;
Matthew Williams56dbf8f2013-07-26 12:56:39 -070043import android.os.UserHandle;
Shreyas Basarge8c834c02016-01-07 13:53:16 +000044import android.util.*;
Jeff Sharkey7a96c392012-11-15 14:01:46 -080045
46import com.android.internal.annotations.VisibleForTesting;
47import com.android.internal.util.ArrayUtils;
48import com.android.internal.util.FastXmlSerializer;
49
50import org.xmlpull.v1.XmlPullParser;
51import org.xmlpull.v1.XmlPullParserException;
52import org.xmlpull.v1.XmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053
Dianne Hackborn231cc602009-04-27 17:10:36 -070054import java.io.File;
55import java.io.FileInputStream;
56import java.io.FileOutputStream;
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +010057import java.nio.charset.StandardCharsets;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058import java.util.ArrayList;
Dianne Hackborn231cc602009-04-27 17:10:36 -070059import java.util.Calendar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060import java.util.HashMap;
Dianne Hackborn231cc602009-04-27 17:10:36 -070061import java.util.Iterator;
Jeff Sharkey7a96c392012-11-15 14:01:46 -080062import java.util.List;
Ashish Sharma69d95de2012-04-11 17:27:24 -070063import java.util.Random;
Jason parks1125d782011-01-12 09:47:26 -060064import java.util.TimeZone;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065
66/**
Dianne Hackborn231cc602009-04-27 17:10:36 -070067 * Singleton that tracks the sync data and overall sync
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068 * history on the device.
Costin Manolache360e4542009-09-04 13:36:04 -070069 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070 * @hide
71 */
Dianne Hackborn231cc602009-04-27 17:10:36 -070072public class SyncStorageEngine extends Handler {
Amith Yamasani04e0d262012-02-14 11:50:53 -080073
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074 private static final String TAG = "SyncManager";
Matthew Williamsba352712013-08-13 15:53:31 -070075 private static final String TAG_FILE = "SyncManagerFile";
Costin Manolache360e4542009-09-04 13:36:04 -070076
Amith Yamasani04e0d262012-02-14 11:50:53 -080077 private static final String XML_ATTR_NEXT_AUTHORITY_ID = "nextAuthorityId";
78 private static final String XML_ATTR_LISTEN_FOR_TICKLES = "listen-for-tickles";
Ashish Sharma69d95de2012-04-11 17:27:24 -070079 private static final String XML_ATTR_SYNC_RANDOM_OFFSET = "offsetInSeconds";
Amith Yamasani04e0d262012-02-14 11:50:53 -080080 private static final String XML_ATTR_ENABLED = "enabled";
81 private static final String XML_ATTR_USER = "user";
82 private static final String XML_TAG_LISTEN_FOR_TICKLES = "listenForTickles";
83
Matthew Williamsfa774182013-06-18 15:44:11 -070084 /** Default time for a periodic sync. */
Shreyas Basargecf939c12016-02-19 15:45:29 +000085 private static final long DEFAULT_POLL_FREQUENCY_SECONDS = 60 * 60 * 24; // One day
Fred Quintanac5d1c6d2010-01-27 12:17:49 -080086
Shreyas Basarge8c834c02016-01-07 13:53:16 +000087 /** Percentage of period that is flex by default, if no flexMillis is set. */
Matthew Williamsfa774182013-06-18 15:44:11 -070088 private static final double DEFAULT_FLEX_PERCENT_SYNC = 0.04;
89
90 /** Lower bound on sync time from which we assign a default flex time. */
91 private static final long DEFAULT_MIN_FLEX_ALLOWED_SECS = 5;
92
Jeff Sharkey8b2c3a142012-11-12 11:45:05 -080093 @VisibleForTesting
Dianne Hackborn231cc602009-04-27 17:10:36 -070094 static final long MILLIS_IN_4WEEKS = 1000L * 60 * 60 * 24 * 7 * 4;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095
Dianne Hackborn231cc602009-04-27 17:10:36 -070096 /** Enum value for a sync start event. */
97 public static final int EVENT_START = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098
Dianne Hackborn231cc602009-04-27 17:10:36 -070099 /** Enum value for a sync stop event. */
100 public static final int EVENT_STOP = 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101
Dianne Hackborn231cc602009-04-27 17:10:36 -0700102 /** Enum value for a server-initiated sync. */
103 public static final int SOURCE_SERVER = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104
Dianne Hackborn231cc602009-04-27 17:10:36 -0700105 /** Enum value for a local-initiated sync. */
106 public static final int SOURCE_LOCAL = 1;
Matthew Williams56dbf8f2013-07-26 12:56:39 -0700107 /** Enum value for a poll-based sync (e.g., upon connection to network) */
Dianne Hackborn231cc602009-04-27 17:10:36 -0700108 public static final int SOURCE_POLL = 2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109
Dianne Hackborn231cc602009-04-27 17:10:36 -0700110 /** Enum value for a user-initiated sync. */
111 public static final int SOURCE_USER = 3;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112
Fred Quintanac5d1c6d2010-01-27 12:17:49 -0800113 /** Enum value for a periodic sync. */
114 public static final int SOURCE_PERIODIC = 4;
115
Fred Quintana307da1a2010-01-21 14:24:20 -0800116 public static final long NOT_IN_BACKOFF_MODE = -1;
117
Dianne Hackborn231cc602009-04-27 17:10:36 -0700118 // TODO: i18n -- grab these out of resources.
119 /** String names for the sync source types. */
120 public static final String[] SOURCES = { "SERVER",
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000121 "LOCAL",
122 "POLL",
123 "USER",
124 "PERIODIC",
125 "SERVICE"};
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126
Dianne Hackborn231cc602009-04-27 17:10:36 -0700127 // The MESG column will contain one of these or one of the Error types.
128 public static final String MESG_SUCCESS = "success";
129 public static final String MESG_CANCELED = "canceled";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130
Dianne Hackborna33e3f72009-09-29 17:28:24 -0700131 public static final int MAX_HISTORY = 100;
Costin Manolache360e4542009-09-04 13:36:04 -0700132
Dianne Hackborn231cc602009-04-27 17:10:36 -0700133 private static final int MSG_WRITE_STATUS = 1;
134 private static final long WRITE_STATUS_DELAY = 1000*60*10; // 10 minutes
Costin Manolache360e4542009-09-04 13:36:04 -0700135
Dianne Hackborn231cc602009-04-27 17:10:36 -0700136 private static final int MSG_WRITE_STATISTICS = 2;
137 private static final long WRITE_STATISTICS_DELAY = 1000*60*30; // 1/2 hour
Joe Onorato8294fad2009-07-15 16:08:44 -0700138
139 private static final boolean SYNC_ENABLED_DEFAULT = false;
Costin Manolache360e4542009-09-04 13:36:04 -0700140
Fred Quintanac2e46912010-03-15 16:10:44 -0700141 // the version of the accounts xml file format
Svet Ganov65712b02016-09-01 10:24:11 -0700142 private static final int ACCOUNTS_VERSION = 3;
Fred Quintanafb084402010-03-23 17:57:03 -0700143
144 private static HashMap<String, String> sAuthorityRenames;
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000145 private static PeriodicSyncAddedListener mPeriodicSyncAddedListener;
Fred Quintanafb084402010-03-23 17:57:03 -0700146
147 static {
148 sAuthorityRenames = new HashMap<String, String>();
149 sAuthorityRenames.put("contacts", "com.android.contacts");
150 sAuthorityRenames.put("calendar", "com.android.calendar");
151 }
Fred Quintanac2e46912010-03-15 16:10:44 -0700152
Dianne Hackborn231cc602009-04-27 17:10:36 -0700153 static class AccountInfo {
Amith Yamasani04e0d262012-02-14 11:50:53 -0800154 final AccountAndUser accountAndUser;
Dianne Hackborn231cc602009-04-27 17:10:36 -0700155 final HashMap<String, AuthorityInfo> authorities =
156 new HashMap<String, AuthorityInfo>();
Costin Manolache360e4542009-09-04 13:36:04 -0700157
Amith Yamasani04e0d262012-02-14 11:50:53 -0800158 AccountInfo(AccountAndUser accountAndUser) {
159 this.accountAndUser = accountAndUser;
Dianne Hackborn231cc602009-04-27 17:10:36 -0700160 }
161 }
Costin Manolache360e4542009-09-04 13:36:04 -0700162
Matthew Williams56dbf8f2013-07-26 12:56:39 -0700163 /** Bare bones representation of a sync target. */
164 public static class EndPoint {
165 public final static EndPoint USER_ALL_PROVIDER_ALL_ACCOUNTS_ALL =
166 new EndPoint(null, null, UserHandle.USER_ALL);
Dianne Hackborn7a135592009-05-06 00:28:37 -0700167 final Account account;
Amith Yamasani04e0d262012-02-14 11:50:53 -0800168 final int userId;
Matthew Williams56dbf8f2013-07-26 12:56:39 -0700169 final String provider;
Matthew Williams56dbf8f2013-07-26 12:56:39 -0700170
171 public EndPoint(Account account, String provider, int userId) {
172 this.account = account;
173 this.provider = provider;
174 this.userId = userId;
Matthew Williams56dbf8f2013-07-26 12:56:39 -0700175 }
176
177 /**
Matthew Williams8ef22042013-07-26 12:56:39 -0700178 * An Endpoint for a sync matches if it targets the same sync adapter for the same user.
179 *
180 * @param spec the Endpoint to match. If the spec has null fields, they indicate a wildcard
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000181 * and match any.
Matthew Williams56dbf8f2013-07-26 12:56:39 -0700182 */
Matthew Williams8ef22042013-07-26 12:56:39 -0700183 public boolean matchesSpec(EndPoint spec) {
184 if (userId != spec.userId
Matthew Williams56dbf8f2013-07-26 12:56:39 -0700185 && userId != UserHandle.USER_ALL
Matthew Williams8ef22042013-07-26 12:56:39 -0700186 && spec.userId != UserHandle.USER_ALL) {
Matthew Williams56dbf8f2013-07-26 12:56:39 -0700187 return false;
188 }
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000189 boolean accountsMatch;
190 if (spec.account == null) {
191 accountsMatch = true;
192 } else {
193 accountsMatch = account.equals(spec.account);
Matthew Williams56dbf8f2013-07-26 12:56:39 -0700194 }
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000195 boolean providersMatch;
196 if (spec.provider == null) {
197 providersMatch = true;
198 } else {
199 providersMatch = provider.equals(spec.provider);
200 }
201 return accountsMatch && providersMatch;
Matthew Williams56dbf8f2013-07-26 12:56:39 -0700202 }
203
204 public String toString() {
205 StringBuilder sb = new StringBuilder();
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000206 sb.append(account == null ? "ALL ACCS" : account.name)
Matthew Williams56dbf8f2013-07-26 12:56:39 -0700207 .append("/")
208 .append(provider == null ? "ALL PDRS" : provider);
Matthew Williams8ef22042013-07-26 12:56:39 -0700209 sb.append(":u" + userId);
Matthew Williams56dbf8f2013-07-26 12:56:39 -0700210 return sb.toString();
211 }
212 }
213
214 public static class AuthorityInfo {
Matthew Williams53abfdb2015-06-10 20:06:37 -0700215 // Legal values of getIsSyncable
Svet Ganovf6d424f12016-09-20 20:18:53 -0700216
217 /**
218 * The syncable state is undefined.
219 */
220 public static final int UNDEFINED = -2;
221
Matthew Williams53abfdb2015-06-10 20:06:37 -0700222 /**
223 * Default state for a newly installed adapter. An uninitialized adapter will receive an
224 * initialization sync which are governed by a different set of rules to that of regular
225 * syncs.
226 */
227 public static final int NOT_INITIALIZED = -1;
228 /**
229 * The adapter will not receive any syncs. This is behaviourally equivalent to
230 * setSyncAutomatically -> false. However setSyncAutomatically is surfaced to the user
231 * while this is generally meant to be controlled by the developer.
232 */
233 public static final int NOT_SYNCABLE = 0;
234 /**
235 * The adapter is initialized and functioning. This is the normal state for an adapter.
236 */
237 public static final int SYNCABLE = 1;
238 /**
239 * The adapter is syncable but still requires an initialization sync. For example an adapter
240 * than has been restored from a previous device will be in this state. Not meant for
241 * external use.
242 */
243 public static final int SYNCABLE_NOT_INITIALIZED = 2;
244
Svetoslav Ganov5cb29732016-07-11 19:32:30 -0700245 /**
246 * The adapter is syncable but does not have access to the synced account and needs a
247 * user access approval.
248 */
249 public static final int SYNCABLE_NO_ACCOUNT_ACCESS = 3;
250
Matthew Williams8ef22042013-07-26 12:56:39 -0700251 final EndPoint target;
Dianne Hackborn231cc602009-04-27 17:10:36 -0700252 final int ident;
253 boolean enabled;
Fred Quintana5e787c42009-08-16 23:13:53 -0700254 int syncable;
Matthew Williams56dbf8f2013-07-26 12:56:39 -0700255 /** Time at which this sync will run, taking into account backoff. */
Fred Quintana307da1a2010-01-21 14:24:20 -0800256 long backoffTime;
Matthew Williams56dbf8f2013-07-26 12:56:39 -0700257 /** Amount of delay due to backoff. */
Fred Quintana307da1a2010-01-21 14:24:20 -0800258 long backoffDelay;
Matthew Williams8ef22042013-07-26 12:56:39 -0700259 /** Time offset to add to any requests coming to this target. */
Fred Quintana307da1a2010-01-21 14:24:20 -0800260 long delayUntil;
Matthew Williams56dbf8f2013-07-26 12:56:39 -0700261
Matthew Williamsfa774182013-06-18 15:44:11 -0700262 final ArrayList<PeriodicSync> periodicSyncs;
Fred Quintanaac9385e2009-06-22 18:00:59 -0700263
Carlos Valdivia3aca7d72012-05-07 19:03:51 -0700264 /**
265 * Copy constructor for making deep-ish copies. Only the bundles stored
266 * in periodic syncs can make unexpected changes.
267 *
268 * @param toCopy AuthorityInfo to be copied.
269 */
270 AuthorityInfo(AuthorityInfo toCopy) {
Matthew Williams8ef22042013-07-26 12:56:39 -0700271 target = toCopy.target;
Carlos Valdivia3aca7d72012-05-07 19:03:51 -0700272 ident = toCopy.ident;
273 enabled = toCopy.enabled;
274 syncable = toCopy.syncable;
275 backoffTime = toCopy.backoffTime;
276 backoffDelay = toCopy.backoffDelay;
277 delayUntil = toCopy.delayUntil;
Matthew Williamsfa774182013-06-18 15:44:11 -0700278 periodicSyncs = new ArrayList<PeriodicSync>();
279 for (PeriodicSync sync : toCopy.periodicSyncs) {
Carlos Valdivia3aca7d72012-05-07 19:03:51 -0700280 // Still not a perfect copy, because we are just copying the mappings.
Matthew Williamsfa774182013-06-18 15:44:11 -0700281 periodicSyncs.add(new PeriodicSync(sync));
Carlos Valdivia3aca7d72012-05-07 19:03:51 -0700282 }
283 }
284
Matthew Williams56dbf8f2013-07-26 12:56:39 -0700285 AuthorityInfo(EndPoint info, int id) {
Matthew Williams8ef22042013-07-26 12:56:39 -0700286 target = info;
Matthew Williams56dbf8f2013-07-26 12:56:39 -0700287 ident = id;
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000288 enabled = SYNC_ENABLED_DEFAULT;
Matthew Williams56dbf8f2013-07-26 12:56:39 -0700289 periodicSyncs = new ArrayList<PeriodicSync>();
290 defaultInitialisation();
291 }
292
293 private void defaultInitialisation() {
Matthew Williams53abfdb2015-06-10 20:06:37 -0700294 syncable = NOT_INITIALIZED; // default to "unknown"
Fred Quintana307da1a2010-01-21 14:24:20 -0800295 backoffTime = -1; // if < 0 then we aren't in backoff mode
296 backoffDelay = -1; // if < 0 then we aren't in backoff mode
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000297
298 if (mPeriodicSyncAddedListener != null) {
299 mPeriodicSyncAddedListener.onPeriodicSyncAdded(target, new Bundle(),
300 DEFAULT_POLL_FREQUENCY_SECONDS,
301 calculateDefaultFlexTime(DEFAULT_POLL_FREQUENCY_SECONDS));
Matthew Williams56dbf8f2013-07-26 12:56:39 -0700302 }
Dianne Hackborn231cc602009-04-27 17:10:36 -0700303 }
Matthew Williams06485a72013-07-26 12:56:39 -0700304
305 @Override
306 public String toString() {
307 return target + ", enabled=" + enabled + ", syncable=" + syncable + ", backoff="
308 + backoffTime + ", delay=" + delayUntil;
309 }
Dianne Hackborn231cc602009-04-27 17:10:36 -0700310 }
Costin Manolache360e4542009-09-04 13:36:04 -0700311
Dianne Hackborn231cc602009-04-27 17:10:36 -0700312 public static class SyncHistoryItem {
313 int authorityId;
314 int historyId;
315 long eventTime;
316 long elapsedTime;
317 int source;
318 int event;
319 long upstreamActivity;
320 long downstreamActivity;
321 String mesg;
Fred Quintanadc475562012-05-04 15:51:54 -0700322 boolean initialization;
Alon Albert57286f92012-10-09 14:21:38 -0700323 Bundle extras;
324 int reason;
Dianne Hackborn231cc602009-04-27 17:10:36 -0700325 }
Costin Manolache360e4542009-09-04 13:36:04 -0700326
Dianne Hackborn231cc602009-04-27 17:10:36 -0700327 public static class DayStats {
328 public final int day;
329 public int successCount;
330 public long successTime;
331 public int failureCount;
332 public long failureTime;
Costin Manolache360e4542009-09-04 13:36:04 -0700333
Dianne Hackborn231cc602009-04-27 17:10:36 -0700334 public DayStats(int day) {
335 this.day = day;
336 }
337 }
Costin Manolache360e4542009-09-04 13:36:04 -0700338
Amith Yamasani04e0d262012-02-14 11:50:53 -0800339 interface OnSyncRequestListener {
Matthew Williams56dbf8f2013-07-26 12:56:39 -0700340
341 /** Called when a sync is needed on an account(s) due to some change in state. */
342 public void onSyncRequest(EndPoint info, int reason, Bundle extras);
Amith Yamasani04e0d262012-02-14 11:50:53 -0800343 }
344
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000345 interface PeriodicSyncAddedListener {
346 /** Called when a periodic sync is added. */
347 void onPeriodicSyncAdded(EndPoint target, Bundle extras, long pollFrequency, long flex);
348 }
349
350 interface OnAuthorityRemovedListener {
351 /** Called when an authority is removed. */
352 void onAuthorityRemoved(EndPoint removedAuthority);
353 }
354
Suprabh Shukla042a4782017-05-12 15:26:54 -0700355 /**
356 * Validator that maintains a lazy cache of accounts and providers to tell if an authority or
357 * account is valid.
358 */
359 private static class AccountAuthorityValidator {
360 final private AccountManager mAccountManager;
361 final private PackageManager mPackageManager;
362 final private SparseArray<Account[]> mAccountsCache;
363 final private SparseArray<ArrayMap<String, Boolean>> mProvidersPerUserCache;
364
365 AccountAuthorityValidator(Context context) {
366 mAccountManager = context.getSystemService(AccountManager.class);
367 mPackageManager = context.getPackageManager();
368 mAccountsCache = new SparseArray<>();
369 mProvidersPerUserCache = new SparseArray<>();
370 }
371
372 // An account is valid if an installed authenticator has previously created that account
373 // on the device
374 boolean isAccountValid(Account account, int userId) {
375 Account[] accountsForUser = mAccountsCache.get(userId);
376 if (accountsForUser == null) {
377 accountsForUser = mAccountManager.getAccountsAsUser(userId);
378 mAccountsCache.put(userId, accountsForUser);
379 }
380 return ArrayUtils.contains(accountsForUser, account);
381 }
382
383 // An authority is only valid if it has a content provider installed on the system
384 boolean isAuthorityValid(String authority, int userId) {
385 ArrayMap<String, Boolean> authorityMap = mProvidersPerUserCache.get(userId);
386 if (authorityMap == null) {
387 authorityMap = new ArrayMap<>();
388 mProvidersPerUserCache.put(userId, authorityMap);
389 }
390 if (!authorityMap.containsKey(authority)) {
391 authorityMap.put(authority, mPackageManager.resolveContentProviderAsUser(authority,
392 PackageManager.MATCH_DIRECT_BOOT_AWARE
393 | PackageManager.MATCH_DIRECT_BOOT_UNAWARE, userId) != null);
394 }
395 return authorityMap.get(authority);
396 }
397 }
398
Dianne Hackborn231cc602009-04-27 17:10:36 -0700399 // Primary list of all syncable authorities. Also our global lock.
400 private final SparseArray<AuthorityInfo> mAuthorities =
401 new SparseArray<AuthorityInfo>();
Costin Manolache360e4542009-09-04 13:36:04 -0700402
Amith Yamasani04e0d262012-02-14 11:50:53 -0800403 private final HashMap<AccountAndUser, AccountInfo> mAccounts
404 = new HashMap<AccountAndUser, AccountInfo>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800405
Amith Yamasani04e0d262012-02-14 11:50:53 -0800406 private final SparseArray<ArrayList<SyncInfo>> mCurrentSyncs
407 = new SparseArray<ArrayList<SyncInfo>>();
Costin Manolache360e4542009-09-04 13:36:04 -0700408
Dianne Hackborn231cc602009-04-27 17:10:36 -0700409 private final SparseArray<SyncStatusInfo> mSyncStatus =
410 new SparseArray<SyncStatusInfo>();
Costin Manolache360e4542009-09-04 13:36:04 -0700411
Dianne Hackborn231cc602009-04-27 17:10:36 -0700412 private final ArrayList<SyncHistoryItem> mSyncHistory =
413 new ArrayList<SyncHistoryItem>();
Costin Manolache360e4542009-09-04 13:36:04 -0700414
Dianne Hackborn231cc602009-04-27 17:10:36 -0700415 private final RemoteCallbackList<ISyncStatusObserver> mChangeListeners
416 = new RemoteCallbackList<ISyncStatusObserver>();
Costin Manolache360e4542009-09-04 13:36:04 -0700417
Matthew Williams8ef22042013-07-26 12:56:39 -0700418 /** Reverse mapping for component name -> <userid -> target id>. */
Matthew Williams56dbf8f2013-07-26 12:56:39 -0700419 private final ArrayMap<ComponentName, SparseArray<AuthorityInfo>> mServices =
420 new ArrayMap<ComponentName, SparseArray<AuthorityInfo>>();
Matthew Williamsfa774182013-06-18 15:44:11 -0700421
Fred Quintana77c560f2010-03-29 22:20:26 -0700422 private int mNextAuthorityId = 0;
423
Dianne Hackborn231cc602009-04-27 17:10:36 -0700424 // We keep 4 weeks of stats.
425 private final DayStats[] mDayStats = new DayStats[7*4];
426 private final Calendar mCal;
427 private int mYear;
428 private int mYearInDays;
Costin Manolache360e4542009-09-04 13:36:04 -0700429
Dianne Hackborn231cc602009-04-27 17:10:36 -0700430 private final Context mContext;
Fred Quintanac5d1c6d2010-01-27 12:17:49 -0800431
Dianne Hackborn231cc602009-04-27 17:10:36 -0700432 private static volatile SyncStorageEngine sSyncStorageEngine = null;
Costin Manolache360e4542009-09-04 13:36:04 -0700433
Ashish Sharma69d95de2012-04-11 17:27:24 -0700434 private int mSyncRandomOffset;
435
Dianne Hackborn231cc602009-04-27 17:10:36 -0700436 /**
437 * This file contains the core engine state: all accounts and the
438 * settings for them. It must never be lost, and should be changed
439 * infrequently, so it is stored as an XML file.
440 */
441 private final AtomicFile mAccountInfoFile;
Costin Manolache360e4542009-09-04 13:36:04 -0700442
Dianne Hackborn231cc602009-04-27 17:10:36 -0700443 /**
444 * This file contains the current sync status. We would like to retain
445 * it across boots, but its loss is not the end of the world, so we store
446 * this information as binary data.
447 */
448 private final AtomicFile mStatusFile;
Costin Manolache360e4542009-09-04 13:36:04 -0700449
Dianne Hackborn231cc602009-04-27 17:10:36 -0700450 /**
451 * This file contains sync statistics. This is purely debugging information
452 * so is written infrequently and can be thrown away at any time.
453 */
454 private final AtomicFile mStatisticsFile;
Costin Manolache360e4542009-09-04 13:36:04 -0700455
Dianne Hackborn231cc602009-04-27 17:10:36 -0700456 private int mNextHistoryId = 0;
Amith Yamasani04e0d262012-02-14 11:50:53 -0800457 private SparseArray<Boolean> mMasterSyncAutomatically = new SparseArray<Boolean>();
Yameng Huang2b5d0ea2011-01-11 14:00:19 +0800458 private boolean mDefaultMasterSyncAutomatically;
Amith Yamasani04e0d262012-02-14 11:50:53 -0800459
460 private OnSyncRequestListener mSyncRequestListener;
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000461 private OnAuthorityRemovedListener mAuthorityRemovedListener;
Costin Manolache360e4542009-09-04 13:36:04 -0700462
Svet Ganov65712b02016-09-01 10:24:11 -0700463 private boolean mGrantSyncAdaptersAccountAccess;
464
Fred Quintanac5d1c6d2010-01-27 12:17:49 -0800465 private SyncStorageEngine(Context context, File dataDir) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800466 mContext = context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800467 sSyncStorageEngine = this;
Costin Manolache360e4542009-09-04 13:36:04 -0700468
Dianne Hackborn231cc602009-04-27 17:10:36 -0700469 mCal = Calendar.getInstance(TimeZone.getTimeZone("GMT+0"));
Costin Manolache360e4542009-09-04 13:36:04 -0700470
Yameng Huang2b5d0ea2011-01-11 14:00:19 +0800471 mDefaultMasterSyncAutomatically = mContext.getResources().getBoolean(
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000472 com.android.internal.R.bool.config_syncstorageengine_masterSyncAutomatically);
Yameng Huang2b5d0ea2011-01-11 14:00:19 +0800473
Dianne Hackborn231cc602009-04-27 17:10:36 -0700474 File systemDir = new File(dataDir, "system");
475 File syncDir = new File(systemDir, "sync");
Fred Quintanac5d1c6d2010-01-27 12:17:49 -0800476 syncDir.mkdirs();
Matthew Williamsba352712013-08-13 15:53:31 -0700477
478 maybeDeleteLegacyPendingInfoLocked(syncDir);
479
Dianne Hackborn231cc602009-04-27 17:10:36 -0700480 mAccountInfoFile = new AtomicFile(new File(syncDir, "accounts.xml"));
481 mStatusFile = new AtomicFile(new File(syncDir, "status.bin"));
Dianne Hackborn231cc602009-04-27 17:10:36 -0700482 mStatisticsFile = new AtomicFile(new File(syncDir, "stats.bin"));
Costin Manolache360e4542009-09-04 13:36:04 -0700483
Dianne Hackborn231cc602009-04-27 17:10:36 -0700484 readAccountInfoLocked();
485 readStatusLocked();
Dianne Hackborn231cc602009-04-27 17:10:36 -0700486 readStatisticsLocked();
Fred Quintana77c560f2010-03-29 22:20:26 -0700487 readAndDeleteLegacyAccountInfoLocked();
488 writeAccountInfoLocked();
489 writeStatusLocked();
Fred Quintana77c560f2010-03-29 22:20:26 -0700490 writeStatisticsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800491 }
492
493 public static SyncStorageEngine newTestInstance(Context context) {
Fred Quintanac5d1c6d2010-01-27 12:17:49 -0800494 return new SyncStorageEngine(context, context.getFilesDir());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800495 }
496
497 public static void init(Context context) {
498 if (sSyncStorageEngine != null) {
Fred Quintana307da1a2010-01-21 14:24:20 -0800499 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800500 }
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700501 File dataDir = Environment.getDataDirectory();
Fred Quintanac5d1c6d2010-01-27 12:17:49 -0800502 sSyncStorageEngine = new SyncStorageEngine(context, dataDir);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800503 }
504
505 public static SyncStorageEngine getSingleton() {
506 if (sSyncStorageEngine == null) {
507 throw new IllegalStateException("not initialized");
508 }
509 return sSyncStorageEngine;
510 }
511
Amith Yamasani04e0d262012-02-14 11:50:53 -0800512 protected void setOnSyncRequestListener(OnSyncRequestListener listener) {
513 if (mSyncRequestListener == null) {
514 mSyncRequestListener = listener;
515 }
516 }
517
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000518 protected void setOnAuthorityRemovedListener(OnAuthorityRemovedListener listener) {
519 if (mAuthorityRemovedListener == null) {
520 mAuthorityRemovedListener = listener;
521 }
522 }
523
524 protected void setPeriodicSyncAddedListener(PeriodicSyncAddedListener listener) {
525 if (mPeriodicSyncAddedListener == null) {
526 mPeriodicSyncAddedListener = listener;
527 }
528 }
529
Dianne Hackborn231cc602009-04-27 17:10:36 -0700530 @Override public void handleMessage(Message msg) {
531 if (msg.what == MSG_WRITE_STATUS) {
Dianne Hackborn4e808202010-04-06 22:00:59 -0700532 synchronized (mAuthorities) {
Dianne Hackborn231cc602009-04-27 17:10:36 -0700533 writeStatusLocked();
Fred Quintanad9d2f112009-04-23 13:36:27 -0700534 }
Dianne Hackborn231cc602009-04-27 17:10:36 -0700535 } else if (msg.what == MSG_WRITE_STATISTICS) {
Dianne Hackborn4e808202010-04-06 22:00:59 -0700536 synchronized (mAuthorities) {
Dianne Hackborn231cc602009-04-27 17:10:36 -0700537 writeStatisticsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800538 }
539 }
540 }
Costin Manolache360e4542009-09-04 13:36:04 -0700541
Ashish Sharma69d95de2012-04-11 17:27:24 -0700542 public int getSyncRandomOffset() {
543 return mSyncRandomOffset;
544 }
545
Dianne Hackborn231cc602009-04-27 17:10:36 -0700546 public void addStatusChangeListener(int mask, ISyncStatusObserver callback) {
547 synchronized (mAuthorities) {
548 mChangeListeners.register(callback, mask);
549 }
550 }
Costin Manolache360e4542009-09-04 13:36:04 -0700551
Dianne Hackborn231cc602009-04-27 17:10:36 -0700552 public void removeStatusChangeListener(ISyncStatusObserver callback) {
553 synchronized (mAuthorities) {
554 mChangeListeners.unregister(callback);
555 }
556 }
Costin Manolache360e4542009-09-04 13:36:04 -0700557
Matthew Williamsfa774182013-06-18 15:44:11 -0700558 /**
559 * Figure out a reasonable flex time for cases where none is provided (old api calls).
560 * @param syncTimeSeconds requested sync time from now.
561 * @return amount of seconds before syncTimeSeconds that the sync can occur.
562 * I.e.
563 * earliest_sync_time = syncTimeSeconds - calculateDefaultFlexTime(syncTimeSeconds)
Matthew Williams56dbf8f2013-07-26 12:56:39 -0700564 * The flex time is capped at a percentage of the {@link #DEFAULT_POLL_FREQUENCY_SECONDS}.
Matthew Williamsfa774182013-06-18 15:44:11 -0700565 */
566 public static long calculateDefaultFlexTime(long syncTimeSeconds) {
567 if (syncTimeSeconds < DEFAULT_MIN_FLEX_ALLOWED_SECS) {
568 // Small enough sync request time that we don't add flex time - developer probably
569 // wants to wait for an operation to occur before syncing so we honour the
570 // request time.
571 return 0L;
572 } else if (syncTimeSeconds < DEFAULT_POLL_FREQUENCY_SECONDS) {
573 return (long) (syncTimeSeconds * DEFAULT_FLEX_PERCENT_SYNC);
574 } else {
575 // Large enough sync request time that we cap the flex time.
576 return (long) (DEFAULT_POLL_FREQUENCY_SECONDS * DEFAULT_FLEX_PERCENT_SYNC);
577 }
578 }
579
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000580 void reportChange(int which) {
Dianne Hackborn231cc602009-04-27 17:10:36 -0700581 ArrayList<ISyncStatusObserver> reports = null;
582 synchronized (mAuthorities) {
583 int i = mChangeListeners.beginBroadcast();
584 while (i > 0) {
585 i--;
586 Integer mask = (Integer)mChangeListeners.getBroadcastCookie(i);
587 if ((which & mask.intValue()) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800588 continue;
589 }
Dianne Hackborn231cc602009-04-27 17:10:36 -0700590 if (reports == null) {
591 reports = new ArrayList<ISyncStatusObserver>(i);
592 }
593 reports.add(mChangeListeners.getBroadcastItem(i));
594 }
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700595 mChangeListeners.finishBroadcast();
Dianne Hackborn231cc602009-04-27 17:10:36 -0700596 }
Costin Manolache360e4542009-09-04 13:36:04 -0700597
Matthew Williams56dbf8f2013-07-26 12:56:39 -0700598 if (Log.isLoggable(TAG, Log.VERBOSE)) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000599 Slog.v(TAG, "reportChange " + which + " to: " + reports);
Fred Quintana77c560f2010-03-29 22:20:26 -0700600 }
Costin Manolache360e4542009-09-04 13:36:04 -0700601
Dianne Hackborn231cc602009-04-27 17:10:36 -0700602 if (reports != null) {
603 int i = reports.size();
604 while (i > 0) {
605 i--;
606 try {
607 reports.get(i).onStatusChanged(which);
608 } catch (RemoteException e) {
609 // The remote callback list will take care of this for us.
610 }
611 }
612 }
613 }
Amith Yamasani70c874b2009-07-06 14:53:25 -0700614
Amith Yamasani04e0d262012-02-14 11:50:53 -0800615 public boolean getSyncAutomatically(Account account, int userId, String providerName) {
Dianne Hackborn231cc602009-04-27 17:10:36 -0700616 synchronized (mAuthorities) {
617 if (account != null) {
Matthew Williams56dbf8f2013-07-26 12:56:39 -0700618 AuthorityInfo authority = getAuthorityLocked(
619 new EndPoint(account, providerName, userId),
Fred Quintanaac9385e2009-06-22 18:00:59 -0700620 "getSyncAutomatically");
621 return authority != null && authority.enabled;
Dianne Hackborn231cc602009-04-27 17:10:36 -0700622 }
Fred Quintanaac9385e2009-06-22 18:00:59 -0700623
Dianne Hackborn231cc602009-04-27 17:10:36 -0700624 int i = mAuthorities.size();
625 while (i > 0) {
626 i--;
Matthew Williams56dbf8f2013-07-26 12:56:39 -0700627 AuthorityInfo authorityInfo = mAuthorities.valueAt(i);
Matthew Williams8ef22042013-07-26 12:56:39 -0700628 if (authorityInfo.target.matchesSpec(new EndPoint(account, providerName, userId))
Matthew Williams56dbf8f2013-07-26 12:56:39 -0700629 && authorityInfo.enabled) {
Dianne Hackborn231cc602009-04-27 17:10:36 -0700630 return true;
631 }
632 }
633 return false;
634 }
635 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800636
Amith Yamasani04e0d262012-02-14 11:50:53 -0800637 public void setSyncAutomatically(Account account, int userId, String providerName,
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000638 boolean sync) {
Matthew Williams56dbf8f2013-07-26 12:56:39 -0700639 if (Log.isLoggable(TAG, Log.VERBOSE)) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000640 Slog.d(TAG, "setSyncAutomatically: " + /* account + */" provider " + providerName
Dianne Hackborn40e9f292012-11-27 19:12:23 -0800641 + ", user " + userId + " -> " + sync);
642 }
Dianne Hackborn231cc602009-04-27 17:10:36 -0700643 synchronized (mAuthorities) {
Matthew Williams56dbf8f2013-07-26 12:56:39 -0700644 AuthorityInfo authority =
645 getOrCreateAuthorityLocked(
646 new EndPoint(account, providerName, userId),
647 -1 /* ident */,
648 false);
Fred Quintana77c560f2010-03-29 22:20:26 -0700649 if (authority.enabled == sync) {
Matthew Williams56dbf8f2013-07-26 12:56:39 -0700650 if (Log.isLoggable(TAG, Log.VERBOSE)) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000651 Slog.d(TAG, "setSyncAutomatically: already set to " + sync + ", doing nothing");
Dianne Hackborn40e9f292012-11-27 19:12:23 -0800652 }
Fred Quintana77c560f2010-03-29 22:20:26 -0700653 return;
654 }
Matthew Williams53abfdb2015-06-10 20:06:37 -0700655 // If the adapter was syncable but missing its initialization sync, set it to
656 // uninitialized now. This is to give it a chance to run any one-time initialization
657 // logic.
658 if (sync && authority.syncable == AuthorityInfo.SYNCABLE_NOT_INITIALIZED) {
659 authority.syncable = AuthorityInfo.NOT_INITIALIZED;
660 }
Joe Onorato8294fad2009-07-15 16:08:44 -0700661 authority.enabled = sync;
Dianne Hackborn231cc602009-04-27 17:10:36 -0700662 writeAccountInfoLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800663 }
Joe Onorato8294fad2009-07-15 16:08:44 -0700664
Fred Quintana77c560f2010-03-29 22:20:26 -0700665 if (sync) {
Alon Albert57286f92012-10-09 14:21:38 -0700666 requestSync(account, userId, SyncOperation.REASON_SYNC_AUTO, providerName,
667 new Bundle());
Joe Onorato8294fad2009-07-15 16:08:44 -0700668 }
Fred Quintanaac9385e2009-06-22 18:00:59 -0700669 reportChange(ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS);
Marvin Paula6533252014-11-24 12:57:48 -0800670 queueBackup();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800671 }
672
Amith Yamasani04e0d262012-02-14 11:50:53 -0800673 public int getIsSyncable(Account account, int userId, String providerName) {
Fred Quintana5e787c42009-08-16 23:13:53 -0700674 synchronized (mAuthorities) {
675 if (account != null) {
Matthew Williams56dbf8f2013-07-26 12:56:39 -0700676 AuthorityInfo authority = getAuthorityLocked(
677 new EndPoint(account, providerName, userId),
678 "get authority syncable");
Fred Quintana5e787c42009-08-16 23:13:53 -0700679 if (authority == null) {
Matthew Williams53abfdb2015-06-10 20:06:37 -0700680 return AuthorityInfo.NOT_INITIALIZED;
Fred Quintana5e787c42009-08-16 23:13:53 -0700681 }
682 return authority.syncable;
683 }
684
685 int i = mAuthorities.size();
686 while (i > 0) {
687 i--;
Matthew Williams56dbf8f2013-07-26 12:56:39 -0700688 AuthorityInfo authorityInfo = mAuthorities.valueAt(i);
Matthew Williams8ef22042013-07-26 12:56:39 -0700689 if (authorityInfo.target != null
690 && authorityInfo.target.provider.equals(providerName)) {
Matthew Williams56dbf8f2013-07-26 12:56:39 -0700691 return authorityInfo.syncable;
Fred Quintana5e787c42009-08-16 23:13:53 -0700692 }
693 }
Matthew Williams53abfdb2015-06-10 20:06:37 -0700694 return AuthorityInfo.NOT_INITIALIZED;
Fred Quintana5e787c42009-08-16 23:13:53 -0700695 }
696 }
697
Amith Yamasani04e0d262012-02-14 11:50:53 -0800698 public void setIsSyncable(Account account, int userId, String providerName, int syncable) {
Matthew Williams8ef22042013-07-26 12:56:39 -0700699 setSyncableStateForEndPoint(new EndPoint(account, providerName, userId), syncable);
Matthew Williams56dbf8f2013-07-26 12:56:39 -0700700 }
701
Matthew Williams56dbf8f2013-07-26 12:56:39 -0700702 /**
703 * An enabled sync service and a syncable provider's adapter both get resolved to the same
704 * persisted variable - namely the "syncable" attribute for an AuthorityInfo in accounts.xml.
Matthew Williams7a2ab3a2013-09-11 14:25:51 -0700705 * @param target target to set value for.
706 * @param syncable 0 indicates unsyncable, <0 unknown, >0 is active/syncable.
Matthew Williams56dbf8f2013-07-26 12:56:39 -0700707 */
Matthew Williams8ef22042013-07-26 12:56:39 -0700708 private void setSyncableStateForEndPoint(EndPoint target, int syncable) {
Matthew Williams7a2ab3a2013-09-11 14:25:51 -0700709 AuthorityInfo aInfo;
710 synchronized (mAuthorities) {
711 aInfo = getOrCreateAuthorityLocked(target, -1, false);
Matthew Williams53abfdb2015-06-10 20:06:37 -0700712 if (syncable < AuthorityInfo.NOT_INITIALIZED) {
713 syncable = AuthorityInfo.NOT_INITIALIZED;
Matthew Williams56dbf8f2013-07-26 12:56:39 -0700714 }
Matthew Williams7a2ab3a2013-09-11 14:25:51 -0700715 if (Log.isLoggable(TAG, Log.VERBOSE)) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000716 Slog.d(TAG, "setIsSyncable: " + aInfo.toString() + " -> " + syncable);
Matthew Williams7a2ab3a2013-09-11 14:25:51 -0700717 }
718 if (aInfo.syncable == syncable) {
719 if (Log.isLoggable(TAG, Log.VERBOSE)) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000720 Slog.d(TAG, "setIsSyncable: already set to " + syncable + ", doing nothing");
Matthew Williams7a2ab3a2013-09-11 14:25:51 -0700721 }
722 return;
723 }
724 aInfo.syncable = syncable;
725 writeAccountInfoLocked();
Matthew Williams56dbf8f2013-07-26 12:56:39 -0700726 }
Matthew Williams53abfdb2015-06-10 20:06:37 -0700727 if (syncable == AuthorityInfo.SYNCABLE) {
Matthew Williams56dbf8f2013-07-26 12:56:39 -0700728 requestSync(aInfo, SyncOperation.REASON_IS_SYNCABLE, new Bundle());
Fred Quintana5e787c42009-08-16 23:13:53 -0700729 }
730 reportChange(ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS);
731 }
732
Matthew Williams56dbf8f2013-07-26 12:56:39 -0700733 public Pair<Long, Long> getBackoff(EndPoint info) {
Fred Quintana307da1a2010-01-21 14:24:20 -0800734 synchronized (mAuthorities) {
Matthew Williams56dbf8f2013-07-26 12:56:39 -0700735 AuthorityInfo authority = getAuthorityLocked(info, "getBackoff");
736 if (authority != null) {
737 return Pair.create(authority.backoffTime, authority.backoffDelay);
Fred Quintana307da1a2010-01-21 14:24:20 -0800738 }
Matthew Williams56dbf8f2013-07-26 12:56:39 -0700739 return null;
Fred Quintana307da1a2010-01-21 14:24:20 -0800740 }
741 }
742
Matthew Williams56dbf8f2013-07-26 12:56:39 -0700743 /**
744 * Update the backoff for the given endpoint. The endpoint may be for a provider/account and
745 * the account or provider info be null, which signifies all accounts or providers.
746 */
747 public void setBackoff(EndPoint info, long nextSyncTime, long nextDelay) {
748 if (Log.isLoggable(TAG, Log.VERBOSE)) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000749 Slog.v(TAG, "setBackoff: " + info
Fred Quintana307da1a2010-01-21 14:24:20 -0800750 + " -> nextSyncTime " + nextSyncTime + ", nextDelay " + nextDelay);
751 }
Matthew Williams56dbf8f2013-07-26 12:56:39 -0700752 boolean changed;
Fred Quintana307da1a2010-01-21 14:24:20 -0800753 synchronized (mAuthorities) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000754 if (info.account == null || info.provider == null) {
Matthew Williams56dbf8f2013-07-26 12:56:39 -0700755 // Do more work for a provider sync if the provided info has specified all
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000756 // accounts/providers.
Matthew Williams56dbf8f2013-07-26 12:56:39 -0700757 changed = setBackoffLocked(
758 info.account /* may be null */,
759 info.userId,
760 info.provider /* may be null */,
761 nextSyncTime, nextDelay);
Fred Quintana307da1a2010-01-21 14:24:20 -0800762 } else {
Matthew Williams56dbf8f2013-07-26 12:56:39 -0700763 AuthorityInfo authorityInfo =
764 getOrCreateAuthorityLocked(info, -1 /* ident */, true);
765 if (authorityInfo.backoffTime == nextSyncTime
766 && authorityInfo.backoffDelay == nextDelay) {
767 changed = false;
768 } else {
769 authorityInfo.backoffTime = nextSyncTime;
770 authorityInfo.backoffDelay = nextDelay;
771 changed = true;
Fred Quintana307da1a2010-01-21 14:24:20 -0800772 }
Fred Quintana307da1a2010-01-21 14:24:20 -0800773 }
Fred Quintana307da1a2010-01-21 14:24:20 -0800774 }
Fred Quintana307da1a2010-01-21 14:24:20 -0800775 if (changed) {
776 reportChange(ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS);
777 }
778 }
779
Matthew Williams56dbf8f2013-07-26 12:56:39 -0700780 /**
781 * Either set backoff for a specific authority, or set backoff for all the
782 * accounts on a specific adapter/all adapters.
783 *
784 * @param account account for which to set backoff. Null to specify all accounts.
785 * @param userId id of the user making this request.
786 * @param providerName provider for which to set backoff. Null to specify all providers.
Matthew Williams7a2ab3a2013-09-11 14:25:51 -0700787 * @return true if a change occured.
Matthew Williams56dbf8f2013-07-26 12:56:39 -0700788 */
789 private boolean setBackoffLocked(Account account, int userId, String providerName,
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000790 long nextSyncTime, long nextDelay) {
Matthew Williams56dbf8f2013-07-26 12:56:39 -0700791 boolean changed = false;
792 for (AccountInfo accountInfo : mAccounts.values()) {
793 if (account != null && !account.equals(accountInfo.accountAndUser.account)
794 && userId != accountInfo.accountAndUser.userId) {
795 continue;
796 }
797 for (AuthorityInfo authorityInfo : accountInfo.authorities.values()) {
798 if (providerName != null
Matthew Williams8ef22042013-07-26 12:56:39 -0700799 && !providerName.equals(authorityInfo.target.provider)) {
Matthew Williams56dbf8f2013-07-26 12:56:39 -0700800 continue;
801 }
802 if (authorityInfo.backoffTime != nextSyncTime
803 || authorityInfo.backoffDelay != nextDelay) {
804 authorityInfo.backoffTime = nextSyncTime;
805 authorityInfo.backoffDelay = nextDelay;
806 changed = true;
807 }
808 }
809 }
810 return changed;
811 }
812
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000813 public void clearAllBackoffsLocked() {
Alon Albert744e310f2010-12-14 11:37:20 -0800814 boolean changed = false;
815 synchronized (mAuthorities) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000816 // Clear backoff for all sync adapters.
817 for (AccountInfo accountInfo : mAccounts.values()) {
818 for (AuthorityInfo authorityInfo : accountInfo.authorities.values()) {
819 if (authorityInfo.backoffTime != NOT_IN_BACKOFF_MODE
820 || authorityInfo.backoffDelay != NOT_IN_BACKOFF_MODE) {
821 if (Log.isLoggable(TAG, Log.VERBOSE)) {
822 Slog.v(TAG, "clearAllBackoffsLocked:"
823 + " authority:" + authorityInfo.target
824 + " account:" + accountInfo.accountAndUser.account.name
825 + " user:" + accountInfo.accountAndUser.userId
826 + " backoffTime was: " + authorityInfo.backoffTime
827 + " backoffDelay was: " + authorityInfo.backoffDelay);
Alon Albert744e310f2010-12-14 11:37:20 -0800828 }
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000829 authorityInfo.backoffTime = NOT_IN_BACKOFF_MODE;
830 authorityInfo.backoffDelay = NOT_IN_BACKOFF_MODE;
831 changed = true;
Alon Albert744e310f2010-12-14 11:37:20 -0800832 }
833 }
834 }
835 }
836
837 if (changed) {
838 reportChange(ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS);
839 }
840 }
841
Matthew Williams56dbf8f2013-07-26 12:56:39 -0700842 public long getDelayUntilTime(EndPoint info) {
Fred Quintana307da1a2010-01-21 14:24:20 -0800843 synchronized (mAuthorities) {
Matthew Williams56dbf8f2013-07-26 12:56:39 -0700844 AuthorityInfo authority = getAuthorityLocked(info, "getDelayUntil");
Fred Quintana307da1a2010-01-21 14:24:20 -0800845 if (authority == null) {
846 return 0;
847 }
848 return authority.delayUntil;
849 }
850 }
851
Matthew Williams56dbf8f2013-07-26 12:56:39 -0700852 public void setDelayUntilTime(EndPoint info, long delayUntil) {
853 if (Log.isLoggable(TAG, Log.VERBOSE)) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000854 Slog.v(TAG, "setDelayUntil: " + info
Matthew Williams56dbf8f2013-07-26 12:56:39 -0700855 + " -> delayUntil " + delayUntil);
Fred Quintanac5d1c6d2010-01-27 12:17:49 -0800856 }
857 synchronized (mAuthorities) {
Matthew Williams56dbf8f2013-07-26 12:56:39 -0700858 AuthorityInfo authority = getOrCreateAuthorityLocked(info, -1, true);
859 if (authority.delayUntil == delayUntil) {
860 return;
Matthew Williamsfa774182013-06-18 15:44:11 -0700861 }
Matthew Williams56dbf8f2013-07-26 12:56:39 -0700862 authority.delayUntil = delayUntil;
863 }
864 reportChange(ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS);
865 }
866
Matthew Williams56dbf8f2013-07-26 12:56:39 -0700867 /**
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000868 * Restore all periodic syncs read from persisted files. Used to restore periodic syncs
869 * after an OS update.
Matthew Williams56dbf8f2013-07-26 12:56:39 -0700870 */
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000871 boolean restoreAllPeriodicSyncs() {
872 if (mPeriodicSyncAddedListener == null) {
873 return false;
Fred Quintanac5d1c6d2010-01-27 12:17:49 -0800874 }
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000875 synchronized (mAuthorities) {
876 for (int i=0; i<mAuthorities.size(); i++) {
877 AuthorityInfo authority = mAuthorities.valueAt(i);
878 for (PeriodicSync periodicSync: authority.periodicSyncs) {
879 mPeriodicSyncAddedListener.onPeriodicSyncAdded(authority.target,
880 periodicSync.extras, periodicSync.period, periodicSync.flexTime);
881 }
882 authority.periodicSyncs.clear();
883 }
884 writeAccountInfoLocked();
885 }
886 return true;
Fred Quintanac5d1c6d2010-01-27 12:17:49 -0800887 }
888
Amith Yamasani04e0d262012-02-14 11:50:53 -0800889 public void setMasterSyncAutomatically(boolean flag, int userId) {
Dianne Hackborn231cc602009-04-27 17:10:36 -0700890 synchronized (mAuthorities) {
Amith Yamasani04e0d262012-02-14 11:50:53 -0800891 Boolean auto = mMasterSyncAutomatically.get(userId);
Matthew Williams8ef22042013-07-26 12:56:39 -0700892 if (auto != null && auto.equals(flag)) {
Fred Quintana77c560f2010-03-29 22:20:26 -0700893 return;
894 }
Amith Yamasani04e0d262012-02-14 11:50:53 -0800895 mMasterSyncAutomatically.put(userId, flag);
Dianne Hackborn231cc602009-04-27 17:10:36 -0700896 writeAccountInfoLocked();
897 }
Fred Quintana77c560f2010-03-29 22:20:26 -0700898 if (flag) {
Alon Albert57286f92012-10-09 14:21:38 -0700899 requestSync(null, userId, SyncOperation.REASON_MASTER_SYNC_AUTO, null,
900 new Bundle());
Joe Onorato8294fad2009-07-15 16:08:44 -0700901 }
Fred Quintanaac9385e2009-06-22 18:00:59 -0700902 reportChange(ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS);
Jeff Sharkey7a96c392012-11-15 14:01:46 -0800903 mContext.sendBroadcast(ContentResolver.ACTION_SYNC_CONN_STATUS_CHANGED);
Marvin Paula6533252014-11-24 12:57:48 -0800904 queueBackup();
Dianne Hackborn231cc602009-04-27 17:10:36 -0700905 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800906
Amith Yamasani04e0d262012-02-14 11:50:53 -0800907 public boolean getMasterSyncAutomatically(int userId) {
Dianne Hackborn231cc602009-04-27 17:10:36 -0700908 synchronized (mAuthorities) {
Amith Yamasani04e0d262012-02-14 11:50:53 -0800909 Boolean auto = mMasterSyncAutomatically.get(userId);
Yameng Huang2b5d0ea2011-01-11 14:00:19 +0800910 return auto == null ? mDefaultMasterSyncAutomatically : auto;
Dianne Hackborn231cc602009-04-27 17:10:36 -0700911 }
912 }
Costin Manolache360e4542009-09-04 13:36:04 -0700913
Dianne Hackborn231cc602009-04-27 17:10:36 -0700914 public AuthorityInfo getAuthority(int authorityId) {
915 synchronized (mAuthorities) {
916 return mAuthorities.get(authorityId);
917 }
918 }
Costin Manolache360e4542009-09-04 13:36:04 -0700919
Dianne Hackborn231cc602009-04-27 17:10:36 -0700920 /**
Matthew Williams56dbf8f2013-07-26 12:56:39 -0700921 * Returns true if there is currently a sync operation being actively processed for the given
Matthew Williams8ef22042013-07-26 12:56:39 -0700922 * target.
Dianne Hackborn231cc602009-04-27 17:10:36 -0700923 */
Matthew Williams56dbf8f2013-07-26 12:56:39 -0700924 public boolean isSyncActive(EndPoint info) {
Dianne Hackborn231cc602009-04-27 17:10:36 -0700925 synchronized (mAuthorities) {
Matthew Williams56dbf8f2013-07-26 12:56:39 -0700926 for (SyncInfo syncInfo : getCurrentSyncs(info.userId)) {
Fred Quintana918339a2010-10-05 14:00:39 -0700927 AuthorityInfo ainfo = getAuthority(syncInfo.authorityId);
Matthew Williams8ef22042013-07-26 12:56:39 -0700928 if (ainfo != null && ainfo.target.matchesSpec(info)) {
Dianne Hackborn231cc602009-04-27 17:10:36 -0700929 return true;
930 }
931 }
932 }
Dianne Hackborn231cc602009-04-27 17:10:36 -0700933 return false;
934 }
Costin Manolache360e4542009-09-04 13:36:04 -0700935
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000936 public void markPending(EndPoint info, boolean pendingValue) {
Dianne Hackborn231cc602009-04-27 17:10:36 -0700937 synchronized (mAuthorities) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000938 AuthorityInfo authority = getOrCreateAuthorityLocked(info,
939 -1 /* desired identifier */,
940 true /* write accounts to storage */);
Dianne Hackborn231cc602009-04-27 17:10:36 -0700941 if (authority == null) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000942 return;
Dianne Hackborn231cc602009-04-27 17:10:36 -0700943 }
Dianne Hackborn231cc602009-04-27 17:10:36 -0700944 SyncStatusInfo status = getOrCreateSyncStatusLocked(authority.ident);
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000945 status.pending = pendingValue;
Dianne Hackborn231cc602009-04-27 17:10:36 -0700946 }
Fred Quintanaac9385e2009-06-22 18:00:59 -0700947 reportChange(ContentResolver.SYNC_OBSERVER_TYPE_PENDING);
Dianne Hackborn231cc602009-04-27 17:10:36 -0700948 }
Costin Manolache360e4542009-09-04 13:36:04 -0700949
Dianne Hackborn231cc602009-04-27 17:10:36 -0700950 /**
951 * Called when the set of account has changed, given the new array of
952 * active accounts.
953 */
Amith Yamasani04e0d262012-02-14 11:50:53 -0800954 public void doDatabaseCleanup(Account[] accounts, int userId) {
Dianne Hackborn231cc602009-04-27 17:10:36 -0700955 synchronized (mAuthorities) {
Matthew Williams8ef22042013-07-26 12:56:39 -0700956 if (Log.isLoggable(TAG, Log.VERBOSE)) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000957 Slog.v(TAG, "Updating for new accounts...");
Matthew Williams8ef22042013-07-26 12:56:39 -0700958 }
Dianne Hackborn231cc602009-04-27 17:10:36 -0700959 SparseArray<AuthorityInfo> removing = new SparseArray<AuthorityInfo>();
960 Iterator<AccountInfo> accIt = mAccounts.values().iterator();
961 while (accIt.hasNext()) {
962 AccountInfo acc = accIt.next();
Amith Yamasani04e0d262012-02-14 11:50:53 -0800963 if (!ArrayUtils.contains(accounts, acc.accountAndUser.account)
964 && acc.accountAndUser.userId == userId) {
Dianne Hackborn231cc602009-04-27 17:10:36 -0700965 // This account no longer exists...
Matthew Williams56dbf8f2013-07-26 12:56:39 -0700966 if (Log.isLoggable(TAG, Log.VERBOSE)) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000967 Slog.v(TAG, "Account removed: " + acc.accountAndUser);
Fred Quintana77c560f2010-03-29 22:20:26 -0700968 }
Dianne Hackborn231cc602009-04-27 17:10:36 -0700969 for (AuthorityInfo auth : acc.authorities.values()) {
970 removing.put(auth.ident, auth);
971 }
972 accIt.remove();
973 }
974 }
Costin Manolache360e4542009-09-04 13:36:04 -0700975
Dianne Hackborn231cc602009-04-27 17:10:36 -0700976 // Clean out all data structures.
977 int i = removing.size();
978 if (i > 0) {
979 while (i > 0) {
980 i--;
981 int ident = removing.keyAt(i);
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000982 AuthorityInfo auth = removing.valueAt(i);
983 if (mAuthorityRemovedListener != null) {
984 mAuthorityRemovedListener.onAuthorityRemoved(auth.target);
985 }
Dianne Hackborn231cc602009-04-27 17:10:36 -0700986 mAuthorities.remove(ident);
987 int j = mSyncStatus.size();
988 while (j > 0) {
989 j--;
990 if (mSyncStatus.keyAt(j) == ident) {
991 mSyncStatus.remove(mSyncStatus.keyAt(j));
992 }
993 }
994 j = mSyncHistory.size();
995 while (j > 0) {
996 j--;
997 if (mSyncHistory.get(j).authorityId == ident) {
998 mSyncHistory.remove(j);
999 }
1000 }
1001 }
1002 writeAccountInfoLocked();
1003 writeStatusLocked();
Dianne Hackborn231cc602009-04-27 17:10:36 -07001004 writeStatisticsLocked();
1005 }
1006 }
1007 }
1008
1009 /**
Fred Quintana918339a2010-10-05 14:00:39 -07001010 * Called when a sync is starting. Supply a valid ActiveSyncContext with information
1011 * about the sync.
Dianne Hackborn231cc602009-04-27 17:10:36 -07001012 */
Fred Quintana918339a2010-10-05 14:00:39 -07001013 public SyncInfo addActiveSync(SyncManager.ActiveSyncContext activeSyncContext) {
1014 final SyncInfo syncInfo;
Dianne Hackborn231cc602009-04-27 17:10:36 -07001015 synchronized (mAuthorities) {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001016 if (Log.isLoggable(TAG, Log.VERBOSE)) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001017 Slog.v(TAG, "setActiveSync: account="
1018 + " auth=" + activeSyncContext.mSyncOperation.target
1019 + " src=" + activeSyncContext.mSyncOperation.syncSource
1020 + " extras=" + activeSyncContext.mSyncOperation.extras);
Dianne Hackborn231cc602009-04-27 17:10:36 -07001021 }
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001022 final EndPoint info = activeSyncContext.mSyncOperation.target;
1023 AuthorityInfo authorityInfo = getOrCreateAuthorityLocked(
1024 info,
Matthew Williams8ef22042013-07-26 12:56:39 -07001025 -1 /* assign a new identifier if creating a new target */,
Fred Quintana918339a2010-10-05 14:00:39 -07001026 true /* write to storage if this results in a change */);
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001027 syncInfo = new SyncInfo(
1028 authorityInfo.ident,
Matthew Williams8ef22042013-07-26 12:56:39 -07001029 authorityInfo.target.account,
1030 authorityInfo.target.provider,
Fred Quintana918339a2010-10-05 14:00:39 -07001031 activeSyncContext.mStartTime);
Matthew Williams8ef22042013-07-26 12:56:39 -07001032 getCurrentSyncs(authorityInfo.target.userId).add(syncInfo);
Dianne Hackborn231cc602009-04-27 17:10:36 -07001033 }
Fred Quintana918339a2010-10-05 14:00:39 -07001034 reportActiveChange();
1035 return syncInfo;
1036 }
1037
1038 /**
1039 * Called to indicate that a previously active sync is no longer active.
1040 */
Amith Yamasani04e0d262012-02-14 11:50:53 -08001041 public void removeActiveSync(SyncInfo syncInfo, int userId) {
Fred Quintana918339a2010-10-05 14:00:39 -07001042 synchronized (mAuthorities) {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001043 if (Log.isLoggable(TAG, Log.VERBOSE)) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001044 Slog.v(TAG, "removeActiveSync: account=" + syncInfo.account
Amith Yamasani04e0d262012-02-14 11:50:53 -08001045 + " user=" + userId
Matthew Williams5a9decd2014-06-04 09:25:11 -07001046 + " auth=" + syncInfo.authority);
Fred Quintana918339a2010-10-05 14:00:39 -07001047 }
Amith Yamasani04e0d262012-02-14 11:50:53 -08001048 getCurrentSyncs(userId).remove(syncInfo);
Fred Quintana918339a2010-10-05 14:00:39 -07001049 }
1050
1051 reportActiveChange();
Dianne Hackborn231cc602009-04-27 17:10:36 -07001052 }
1053
1054 /**
1055 * To allow others to send active change reports, to poke clients.
1056 */
1057 public void reportActiveChange() {
Fred Quintanaac9385e2009-06-22 18:00:59 -07001058 reportChange(ContentResolver.SYNC_OBSERVER_TYPE_ACTIVE);
Dianne Hackborn231cc602009-04-27 17:10:36 -07001059 }
Costin Manolache360e4542009-09-04 13:36:04 -07001060
Dianne Hackborn231cc602009-04-27 17:10:36 -07001061 /**
Matthew Williams8ef22042013-07-26 12:56:39 -07001062 * Note that sync has started for the given operation.
Dianne Hackborn231cc602009-04-27 17:10:36 -07001063 */
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001064 public long insertStartSyncEvent(SyncOperation op, long now) {
Dianne Hackborn231cc602009-04-27 17:10:36 -07001065 long id;
1066 synchronized (mAuthorities) {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001067 if (Log.isLoggable(TAG, Log.VERBOSE)) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001068 Slog.v(TAG, "insertStartSyncEvent: " + op);
Fred Quintana77c560f2010-03-29 22:20:26 -07001069 }
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001070 AuthorityInfo authority = getAuthorityLocked(op.target, "insertStartSyncEvent");
Dianne Hackborn231cc602009-04-27 17:10:36 -07001071 if (authority == null) {
1072 return -1;
1073 }
1074 SyncHistoryItem item = new SyncHistoryItem();
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001075 item.initialization = op.isInitialization();
Dianne Hackborn231cc602009-04-27 17:10:36 -07001076 item.authorityId = authority.ident;
1077 item.historyId = mNextHistoryId++;
1078 if (mNextHistoryId < 0) mNextHistoryId = 0;
1079 item.eventTime = now;
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001080 item.source = op.syncSource;
1081 item.reason = op.reason;
1082 item.extras = op.extras;
Dianne Hackborn231cc602009-04-27 17:10:36 -07001083 item.event = EVENT_START;
1084 mSyncHistory.add(0, item);
1085 while (mSyncHistory.size() > MAX_HISTORY) {
1086 mSyncHistory.remove(mSyncHistory.size()-1);
1087 }
1088 id = item.historyId;
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001089 if (Log.isLoggable(TAG, Log.VERBOSE)) Slog.v(TAG, "returning historyId " + id);
Dianne Hackborn231cc602009-04-27 17:10:36 -07001090 }
Costin Manolache360e4542009-09-04 13:36:04 -07001091
Fred Quintanaac9385e2009-06-22 18:00:59 -07001092 reportChange(ContentResolver.SYNC_OBSERVER_TYPE_STATUS);
Dianne Hackborn231cc602009-04-27 17:10:36 -07001093 return id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001094 }
1095
Fred Quintana77c560f2010-03-29 22:20:26 -07001096 public void stopSyncEvent(long historyId, long elapsedTime, String resultMessage,
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001097 long downstreamActivity, long upstreamActivity) {
Dianne Hackborn231cc602009-04-27 17:10:36 -07001098 synchronized (mAuthorities) {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001099 if (Log.isLoggable(TAG, Log.VERBOSE)) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001100 Slog.v(TAG, "stopSyncEvent: historyId=" + historyId);
Fred Quintana77c560f2010-03-29 22:20:26 -07001101 }
Dianne Hackborn231cc602009-04-27 17:10:36 -07001102 SyncHistoryItem item = null;
1103 int i = mSyncHistory.size();
1104 while (i > 0) {
1105 i--;
1106 item = mSyncHistory.get(i);
1107 if (item.historyId == historyId) {
1108 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001109 }
Dianne Hackborn231cc602009-04-27 17:10:36 -07001110 item = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001111 }
Costin Manolache360e4542009-09-04 13:36:04 -07001112
Dianne Hackborn231cc602009-04-27 17:10:36 -07001113 if (item == null) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001114 Slog.w(TAG, "stopSyncEvent: no history for id " + historyId);
Dianne Hackborn231cc602009-04-27 17:10:36 -07001115 return;
1116 }
Costin Manolache360e4542009-09-04 13:36:04 -07001117
Dianne Hackborn231cc602009-04-27 17:10:36 -07001118 item.elapsedTime = elapsedTime;
1119 item.event = EVENT_STOP;
1120 item.mesg = resultMessage;
1121 item.downstreamActivity = downstreamActivity;
1122 item.upstreamActivity = upstreamActivity;
Costin Manolache360e4542009-09-04 13:36:04 -07001123
Dianne Hackborn231cc602009-04-27 17:10:36 -07001124 SyncStatusInfo status = getOrCreateSyncStatusLocked(item.authorityId);
Costin Manolache360e4542009-09-04 13:36:04 -07001125
Dianne Hackborn231cc602009-04-27 17:10:36 -07001126 status.numSyncs++;
1127 status.totalElapsedTime += elapsedTime;
1128 switch (item.source) {
1129 case SOURCE_LOCAL:
1130 status.numSourceLocal++;
1131 break;
1132 case SOURCE_POLL:
1133 status.numSourcePoll++;
1134 break;
1135 case SOURCE_USER:
1136 status.numSourceUser++;
1137 break;
1138 case SOURCE_SERVER:
1139 status.numSourceServer++;
1140 break;
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001141 case SOURCE_PERIODIC:
1142 status.numSourcePeriodic++;
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001143 break;
Dianne Hackborn231cc602009-04-27 17:10:36 -07001144 }
Costin Manolache360e4542009-09-04 13:36:04 -07001145
Dianne Hackborn231cc602009-04-27 17:10:36 -07001146 boolean writeStatisticsNow = false;
Dianne Hackborn55280a92009-05-07 15:53:46 -07001147 int day = getCurrentDayLocked();
Dianne Hackborn231cc602009-04-27 17:10:36 -07001148 if (mDayStats[0] == null) {
1149 mDayStats[0] = new DayStats(day);
1150 } else if (day != mDayStats[0].day) {
1151 System.arraycopy(mDayStats, 0, mDayStats, 1, mDayStats.length-1);
1152 mDayStats[0] = new DayStats(day);
1153 writeStatisticsNow = true;
1154 } else if (mDayStats[0] == null) {
1155 }
1156 final DayStats ds = mDayStats[0];
Costin Manolache360e4542009-09-04 13:36:04 -07001157
Dianne Hackborn231cc602009-04-27 17:10:36 -07001158 final long lastSyncTime = (item.eventTime + elapsedTime);
1159 boolean writeStatusNow = false;
1160 if (MESG_SUCCESS.equals(resultMessage)) {
1161 // - if successful, update the successful columns
1162 if (status.lastSuccessTime == 0 || status.lastFailureTime != 0) {
1163 writeStatusNow = true;
1164 }
1165 status.lastSuccessTime = lastSyncTime;
1166 status.lastSuccessSource = item.source;
1167 status.lastFailureTime = 0;
1168 status.lastFailureSource = -1;
1169 status.lastFailureMesg = null;
1170 status.initialFailureTime = 0;
1171 ds.successCount++;
1172 ds.successTime += elapsedTime;
1173 } else if (!MESG_CANCELED.equals(resultMessage)) {
1174 if (status.lastFailureTime == 0) {
1175 writeStatusNow = true;
1176 }
1177 status.lastFailureTime = lastSyncTime;
1178 status.lastFailureSource = item.source;
1179 status.lastFailureMesg = resultMessage;
1180 if (status.initialFailureTime == 0) {
1181 status.initialFailureTime = lastSyncTime;
1182 }
1183 ds.failureCount++;
1184 ds.failureTime += elapsedTime;
1185 }
Costin Manolache360e4542009-09-04 13:36:04 -07001186
Dianne Hackborn231cc602009-04-27 17:10:36 -07001187 if (writeStatusNow) {
1188 writeStatusLocked();
1189 } else if (!hasMessages(MSG_WRITE_STATUS)) {
1190 sendMessageDelayed(obtainMessage(MSG_WRITE_STATUS),
1191 WRITE_STATUS_DELAY);
1192 }
1193 if (writeStatisticsNow) {
1194 writeStatisticsLocked();
1195 } else if (!hasMessages(MSG_WRITE_STATISTICS)) {
1196 sendMessageDelayed(obtainMessage(MSG_WRITE_STATISTICS),
1197 WRITE_STATISTICS_DELAY);
Costin Manolache360e4542009-09-04 13:36:04 -07001198 }
Dianne Hackborn231cc602009-04-27 17:10:36 -07001199 }
Costin Manolache360e4542009-09-04 13:36:04 -07001200
Fred Quintanaac9385e2009-06-22 18:00:59 -07001201 reportChange(ContentResolver.SYNC_OBSERVER_TYPE_STATUS);
Dianne Hackborn231cc602009-04-27 17:10:36 -07001202 }
1203
1204 /**
Matthew Williamsa7456e42013-11-12 14:41:02 -08001205 * Return a list of the currently active syncs. Note that the returned
1206 * items are the real, live active sync objects, so be careful what you do
1207 * with it.
Fred Quintana918339a2010-10-05 14:00:39 -07001208 */
Matthew Williamsa7456e42013-11-12 14:41:02 -08001209 private List<SyncInfo> getCurrentSyncs(int userId) {
Fred Quintana918339a2010-10-05 14:00:39 -07001210 synchronized (mAuthorities) {
Matthew Williamsa7456e42013-11-12 14:41:02 -08001211 return getCurrentSyncsLocked(userId);
Dianne Hackborn231cc602009-04-27 17:10:36 -07001212 }
1213 }
Costin Manolache360e4542009-09-04 13:36:04 -07001214
Dianne Hackborn231cc602009-04-27 17:10:36 -07001215 /**
Matthew Williamsf39549e2016-01-19 23:04:04 +00001216 * @param userId Id of user to return current sync info.
1217 * @param canAccessAccounts Determines whether to redact Account information from the result.
1218 * @return a copy of the current syncs data structure. Will not return null.
Matthew Williamsa7456e42013-11-12 14:41:02 -08001219 */
Matthew Williamsf39549e2016-01-19 23:04:04 +00001220 public List<SyncInfo> getCurrentSyncsCopy(int userId, boolean canAccessAccounts) {
Matthew Williamsa7456e42013-11-12 14:41:02 -08001221 synchronized (mAuthorities) {
1222 final List<SyncInfo> syncs = getCurrentSyncsLocked(userId);
1223 final List<SyncInfo> syncsCopy = new ArrayList<SyncInfo>();
1224 for (SyncInfo sync : syncs) {
Matthew Williamsf39549e2016-01-19 23:04:04 +00001225 SyncInfo copy;
1226 if (!canAccessAccounts) {
1227 copy = SyncInfo.createAccountRedacted(
1228 sync.authorityId, sync.authority, sync.startTime);
1229 } else {
1230 copy = new SyncInfo(sync);
1231 }
1232 syncsCopy.add(copy);
Matthew Williamsa7456e42013-11-12 14:41:02 -08001233 }
1234 return syncsCopy;
1235 }
1236 }
1237
1238 private List<SyncInfo> getCurrentSyncsLocked(int userId) {
1239 ArrayList<SyncInfo> syncs = mCurrentSyncs.get(userId);
1240 if (syncs == null) {
1241 syncs = new ArrayList<SyncInfo>();
1242 mCurrentSyncs.put(userId, syncs);
1243 }
1244 return syncs;
1245 }
1246
1247 /**
Matthew Williams8ef22042013-07-26 12:56:39 -07001248 * Return a copy of the specified target with the corresponding sync status
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001249 */
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001250 public Pair<AuthorityInfo, SyncStatusInfo> getCopyOfAuthorityWithSyncStatus(EndPoint info) {
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001251 synchronized (mAuthorities) {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001252 AuthorityInfo authorityInfo = getOrCreateAuthorityLocked(info,
Matthew Williams8ef22042013-07-26 12:56:39 -07001253 -1 /* assign a new identifier if creating a new target */,
Georgi Nikolovdbe846b2013-06-25 14:09:56 -07001254 true /* write to storage if this results in a change */);
1255 return createCopyPairOfAuthorityWithSyncStatusLocked(authorityInfo);
1256 }
1257 }
1258
1259 /**
Matthew Williams8ef22042013-07-26 12:56:39 -07001260 * Returns the status that matches the target.
Costin Manolacheb7520982009-09-02 18:03:05 -07001261 *
Matthew Williams8ef22042013-07-26 12:56:39 -07001262 * @param info the endpoint target we are querying status info for.
1263 * @return the SyncStatusInfo for the endpoint.
Dianne Hackborn231cc602009-04-27 17:10:36 -07001264 */
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001265 public SyncStatusInfo getStatusByAuthority(EndPoint info) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001266 if (info.account == null || info.provider == null) {
Matthew Williamsd08d6682013-10-14 10:39:41 -07001267 return null;
Costin Manolacheb7520982009-09-02 18:03:05 -07001268 }
Dianne Hackborn231cc602009-04-27 17:10:36 -07001269 synchronized (mAuthorities) {
Dianne Hackborn231cc602009-04-27 17:10:36 -07001270 final int N = mSyncStatus.size();
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001271 for (int i = 0; i < N; i++) {
Costin Manolache360e4542009-09-04 13:36:04 -07001272 SyncStatusInfo cur = mSyncStatus.valueAt(i);
Dianne Hackborn231cc602009-04-27 17:10:36 -07001273 AuthorityInfo ainfo = mAuthorities.get(cur.authorityId);
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001274 if (ainfo != null
Matthew Williams8ef22042013-07-26 12:56:39 -07001275 && ainfo.target.matchesSpec(info)) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001276 return cur;
Dianne Hackborn231cc602009-04-27 17:10:36 -07001277 }
1278 }
Costin Manolacheb7520982009-09-02 18:03:05 -07001279 return null;
Dianne Hackborn231cc602009-04-27 17:10:36 -07001280 }
1281 }
Costin Manolache360e4542009-09-04 13:36:04 -07001282
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001283 /** Return true if the pending status is true of any matching authorities. */
1284 public boolean isSyncPending(EndPoint info) {
Dianne Hackborn231cc602009-04-27 17:10:36 -07001285 synchronized (mAuthorities) {
1286 final int N = mSyncStatus.size();
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001287 for (int i = 0; i < N; i++) {
Costin Manolache360e4542009-09-04 13:36:04 -07001288 SyncStatusInfo cur = mSyncStatus.valueAt(i);
Dianne Hackborn231cc602009-04-27 17:10:36 -07001289 AuthorityInfo ainfo = mAuthorities.get(cur.authorityId);
1290 if (ainfo == null) {
1291 continue;
1292 }
Matthew Williams8ef22042013-07-26 12:56:39 -07001293 if (!ainfo.target.matchesSpec(info)) {
Amith Yamasani04e0d262012-02-14 11:50:53 -08001294 continue;
1295 }
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001296 if (cur.pending) {
Dianne Hackborn231cc602009-04-27 17:10:36 -07001297 return true;
1298 }
1299 }
1300 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001301 }
1302 }
1303
1304 /**
Dianne Hackborn231cc602009-04-27 17:10:36 -07001305 * Return an array of the current sync status for all authorities. Note
1306 * that the objects inside the array are the real, live status objects,
1307 * so be careful what you do with them.
1308 */
1309 public ArrayList<SyncHistoryItem> getSyncHistory() {
1310 synchronized (mAuthorities) {
1311 final int N = mSyncHistory.size();
1312 ArrayList<SyncHistoryItem> items = new ArrayList<SyncHistoryItem>(N);
1313 for (int i=0; i<N; i++) {
1314 items.add(mSyncHistory.get(i));
1315 }
1316 return items;
1317 }
1318 }
Costin Manolache360e4542009-09-04 13:36:04 -07001319
Dianne Hackborn231cc602009-04-27 17:10:36 -07001320 /**
1321 * Return an array of the current per-day statistics. Note
1322 * that the objects inside the array are the real, live status objects,
1323 * so be careful what you do with them.
1324 */
1325 public DayStats[] getDayStatistics() {
1326 synchronized (mAuthorities) {
1327 DayStats[] ds = new DayStats[mDayStats.length];
1328 System.arraycopy(mDayStats, 0, ds, 0, ds.length);
1329 return ds;
1330 }
1331 }
Costin Manolache360e4542009-09-04 13:36:04 -07001332
Georgi Nikolovdbe846b2013-06-25 14:09:56 -07001333 private Pair<AuthorityInfo, SyncStatusInfo> createCopyPairOfAuthorityWithSyncStatusLocked(
1334 AuthorityInfo authorityInfo) {
1335 SyncStatusInfo syncStatusInfo = getOrCreateSyncStatusLocked(authorityInfo.ident);
1336 return Pair.create(new AuthorityInfo(authorityInfo), new SyncStatusInfo(syncStatusInfo));
1337 }
1338
Dianne Hackborn55280a92009-05-07 15:53:46 -07001339 private int getCurrentDayLocked() {
Dianne Hackborn231cc602009-04-27 17:10:36 -07001340 mCal.setTimeInMillis(System.currentTimeMillis());
1341 final int dayOfYear = mCal.get(Calendar.DAY_OF_YEAR);
1342 if (mYear != mCal.get(Calendar.YEAR)) {
1343 mYear = mCal.get(Calendar.YEAR);
1344 mCal.clear();
1345 mCal.set(Calendar.YEAR, mYear);
1346 mYearInDays = (int)(mCal.getTimeInMillis()/86400000);
1347 }
1348 return dayOfYear + mYearInDays;
1349 }
Costin Manolache360e4542009-09-04 13:36:04 -07001350
Dianne Hackborn231cc602009-04-27 17:10:36 -07001351 /**
Matthew Williams8ef22042013-07-26 12:56:39 -07001352 * Retrieve a target's full info, returning null if one does not exist.
Costin Manolache360e4542009-09-04 13:36:04 -07001353 *
Matthew Williams8ef22042013-07-26 12:56:39 -07001354 * @param info info of the target to look up.
Dianne Hackborn231cc602009-04-27 17:10:36 -07001355 * @param tag If non-null, this will be used in a log message if the
Matthew Williams8ef22042013-07-26 12:56:39 -07001356 * requested target does not exist.
Dianne Hackborn231cc602009-04-27 17:10:36 -07001357 */
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001358 private AuthorityInfo getAuthorityLocked(EndPoint info, String tag) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001359 AccountAndUser au = new AccountAndUser(info.account, info.userId);
1360 AccountInfo accountInfo = mAccounts.get(au);
1361 if (accountInfo == null) {
1362 if (tag != null) {
1363 if (Log.isLoggable(TAG, Log.VERBOSE)) {
1364 Slog.v(TAG, tag + ": unknown account " + au);
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001365 }
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001366 }
Dianne Hackborn231cc602009-04-27 17:10:36 -07001367 return null;
1368 }
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001369 AuthorityInfo authority = accountInfo.authorities.get(info.provider);
1370 if (authority == null) {
1371 if (tag != null) {
1372 if (Log.isLoggable(TAG, Log.VERBOSE)) {
1373 Slog.v(TAG, tag + ": unknown provider " + info.provider);
1374 }
1375 }
1376 return null;
1377 }
1378 return authority;
Dianne Hackborn231cc602009-04-27 17:10:36 -07001379 }
Costin Manolache360e4542009-09-04 13:36:04 -07001380
Matthew Williamsfa774182013-06-18 15:44:11 -07001381 /**
Matthew Williams8ef22042013-07-26 12:56:39 -07001382 * @param info info identifying target.
1383 * @param ident unique identifier for target. -1 for none.
Matthew Williamsfa774182013-06-18 15:44:11 -07001384 * @param doWrite if true, update the accounts.xml file on the disk.
Matthew Williams8ef22042013-07-26 12:56:39 -07001385 * @return the authority that corresponds to the provided sync target, creating it if none
Matthew Williamsfa774182013-06-18 15:44:11 -07001386 * exists.
1387 */
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001388 private AuthorityInfo getOrCreateAuthorityLocked(EndPoint info, int ident, boolean doWrite) {
1389 AuthorityInfo authority = null;
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001390 AccountAndUser au = new AccountAndUser(info.account, info.userId);
1391 AccountInfo account = mAccounts.get(au);
1392 if (account == null) {
1393 account = new AccountInfo(au);
1394 mAccounts.put(au, account);
1395 }
1396 authority = account.authorities.get(info.provider);
1397 if (authority == null) {
1398 authority = createAuthorityLocked(info, ident, doWrite);
1399 account.authorities.put(info.provider, authority);
Matthew Williamsfa774182013-06-18 15:44:11 -07001400 }
1401 return authority;
1402 }
1403
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001404 private AuthorityInfo createAuthorityLocked(EndPoint info, int ident, boolean doWrite) {
1405 AuthorityInfo authority;
1406 if (ident < 0) {
1407 ident = mNextAuthorityId;
1408 mNextAuthorityId++;
1409 doWrite = true;
Dianne Hackborn231cc602009-04-27 17:10:36 -07001410 }
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001411 if (Log.isLoggable(TAG, Log.VERBOSE)) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001412 Slog.v(TAG, "created a new AuthorityInfo for " + info);
Dianne Hackborn231cc602009-04-27 17:10:36 -07001413 }
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001414 authority = new AuthorityInfo(info, ident);
1415 mAuthorities.put(ident, authority);
1416 if (doWrite) {
1417 writeAccountInfoLocked();
1418 }
Dianne Hackborn231cc602009-04-27 17:10:36 -07001419 return authority;
1420 }
Costin Manolache360e4542009-09-04 13:36:04 -07001421
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001422 public void removeAuthority(EndPoint info) {
1423 synchronized (mAuthorities) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001424 removeAuthorityLocked(info.account, info.userId, info.provider, true /* doWrite */);
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001425 }
1426 }
1427
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001428
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001429 /**
1430 * Remove an authority associated with a provider. Needs to be a standalone function for
1431 * backward compatibility.
1432 */
Amith Yamasani04e0d262012-02-14 11:50:53 -08001433 private void removeAuthorityLocked(Account account, int userId, String authorityName,
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001434 boolean doWrite) {
Amith Yamasani04e0d262012-02-14 11:50:53 -08001435 AccountInfo accountInfo = mAccounts.get(new AccountAndUser(account, userId));
Fred Quintana7620f1a2010-03-16 15:58:44 -07001436 if (accountInfo != null) {
Fred Quintanafb084402010-03-23 17:57:03 -07001437 final AuthorityInfo authorityInfo = accountInfo.authorities.remove(authorityName);
1438 if (authorityInfo != null) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001439 if (mAuthorityRemovedListener != null) {
1440 mAuthorityRemovedListener.onAuthorityRemoved(authorityInfo.target);
1441 }
Fred Quintanafb084402010-03-23 17:57:03 -07001442 mAuthorities.remove(authorityInfo.ident);
Fred Quintana77c560f2010-03-29 22:20:26 -07001443 if (doWrite) {
1444 writeAccountInfoLocked();
1445 }
Fred Quintana7620f1a2010-03-16 15:58:44 -07001446 }
1447 }
1448 }
1449
Dianne Hackborn231cc602009-04-27 17:10:36 -07001450 private SyncStatusInfo getOrCreateSyncStatusLocked(int authorityId) {
1451 SyncStatusInfo status = mSyncStatus.get(authorityId);
1452 if (status == null) {
1453 status = new SyncStatusInfo(authorityId);
1454 mSyncStatus.put(authorityId, status);
1455 }
1456 return status;
1457 }
Costin Manolache360e4542009-09-04 13:36:04 -07001458
Dianne Hackborn55280a92009-05-07 15:53:46 -07001459 public void writeAllState() {
1460 synchronized (mAuthorities) {
1461 // Account info is always written so no need to do it here.
Dianne Hackborn55280a92009-05-07 15:53:46 -07001462 writeStatusLocked();
1463 writeStatisticsLocked();
1464 }
1465 }
Costin Manolache360e4542009-09-04 13:36:04 -07001466
Svet Ganov65712b02016-09-01 10:24:11 -07001467 public boolean shouldGrantSyncAdaptersAccountAccess() {
1468 return mGrantSyncAdaptersAccountAccess;
1469 }
1470
Dianne Hackborn231cc602009-04-27 17:10:36 -07001471 /**
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001472 * public for testing
1473 */
1474 public void clearAndReadState() {
1475 synchronized (mAuthorities) {
1476 mAuthorities.clear();
1477 mAccounts.clear();
Matthew Williamsfa774182013-06-18 15:44:11 -07001478 mServices.clear();
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001479 mSyncStatus.clear();
1480 mSyncHistory.clear();
1481
1482 readAccountInfoLocked();
1483 readStatusLocked();
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001484 readStatisticsLocked();
Fred Quintana77c560f2010-03-29 22:20:26 -07001485 readAndDeleteLegacyAccountInfoLocked();
1486 writeAccountInfoLocked();
1487 writeStatusLocked();
Fred Quintana77c560f2010-03-29 22:20:26 -07001488 writeStatisticsLocked();
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001489 }
1490 }
1491
1492 /**
Dianne Hackborn231cc602009-04-27 17:10:36 -07001493 * Read all account information back in to the initial engine state.
1494 */
1495 private void readAccountInfoLocked() {
Fred Quintana77c560f2010-03-29 22:20:26 -07001496 int highestAuthorityId = -1;
Dianne Hackborn231cc602009-04-27 17:10:36 -07001497 FileInputStream fis = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001498 try {
Dianne Hackborn231cc602009-04-27 17:10:36 -07001499 fis = mAccountInfoFile.openRead();
Matthew Williamsba352712013-08-13 15:53:31 -07001500 if (Log.isLoggable(TAG_FILE, Log.VERBOSE)) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001501 Slog.v(TAG_FILE, "Reading " + mAccountInfoFile.getBaseFile());
Matthew Williamsba352712013-08-13 15:53:31 -07001502 }
Dianne Hackborn231cc602009-04-27 17:10:36 -07001503 XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01001504 parser.setInput(fis, StandardCharsets.UTF_8.name());
Dianne Hackborn231cc602009-04-27 17:10:36 -07001505 int eventType = parser.getEventType();
Narayan Kamath2ac3cb72014-01-06 11:31:35 +00001506 while (eventType != XmlPullParser.START_TAG &&
1507 eventType != XmlPullParser.END_DOCUMENT) {
Dianne Hackborn231cc602009-04-27 17:10:36 -07001508 eventType = parser.next();
1509 }
Narayan Kamath2ac3cb72014-01-06 11:31:35 +00001510 if (eventType == XmlPullParser.END_DOCUMENT) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001511 Slog.i(TAG, "No initial accounts");
Narayan Kamath2ac3cb72014-01-06 11:31:35 +00001512 return;
1513 }
1514
Dianne Hackborn231cc602009-04-27 17:10:36 -07001515 String tagName = parser.getName();
1516 if ("accounts".equals(tagName)) {
Amith Yamasani04e0d262012-02-14 11:50:53 -08001517 String listen = parser.getAttributeValue(null, XML_ATTR_LISTEN_FOR_TICKLES);
Fred Quintanac2e46912010-03-15 16:10:44 -07001518 String versionString = parser.getAttributeValue(null, "version");
1519 int version;
1520 try {
1521 version = (versionString == null) ? 0 : Integer.parseInt(versionString);
1522 } catch (NumberFormatException e) {
1523 version = 0;
1524 }
Svet Ganov65712b02016-09-01 10:24:11 -07001525
1526 if (version < 3) {
1527 mGrantSyncAdaptersAccountAccess = true;
1528 }
1529
Amith Yamasani04e0d262012-02-14 11:50:53 -08001530 String nextIdString = parser.getAttributeValue(null, XML_ATTR_NEXT_AUTHORITY_ID);
Fred Quintana77c560f2010-03-29 22:20:26 -07001531 try {
1532 int id = (nextIdString == null) ? 0 : Integer.parseInt(nextIdString);
1533 mNextAuthorityId = Math.max(mNextAuthorityId, id);
1534 } catch (NumberFormatException e) {
1535 // don't care
Fred Quintanac2e46912010-03-15 16:10:44 -07001536 }
Ashish Sharma69d95de2012-04-11 17:27:24 -07001537 String offsetString = parser.getAttributeValue(null, XML_ATTR_SYNC_RANDOM_OFFSET);
1538 try {
1539 mSyncRandomOffset = (offsetString == null) ? 0 : Integer.parseInt(offsetString);
1540 } catch (NumberFormatException e) {
1541 mSyncRandomOffset = 0;
1542 }
1543 if (mSyncRandomOffset == 0) {
1544 Random random = new Random(System.currentTimeMillis());
1545 mSyncRandomOffset = random.nextInt(86400);
1546 }
Amith Yamasani04e0d262012-02-14 11:50:53 -08001547 mMasterSyncAutomatically.put(0, listen == null || Boolean.parseBoolean(listen));
Dianne Hackborn231cc602009-04-27 17:10:36 -07001548 eventType = parser.next();
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001549 AuthorityInfo authority = null;
Matthew Williamsfa774182013-06-18 15:44:11 -07001550 PeriodicSync periodicSync = null;
Suprabh Shukla042a4782017-05-12 15:26:54 -07001551 AccountAuthorityValidator validator = new AccountAuthorityValidator(mContext);
Dianne Hackborn231cc602009-04-27 17:10:36 -07001552 do {
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001553 if (eventType == XmlPullParser.START_TAG) {
Dianne Hackborn231cc602009-04-27 17:10:36 -07001554 tagName = parser.getName();
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001555 if (parser.getDepth() == 2) {
1556 if ("authority".equals(tagName)) {
Suprabh Shukla042a4782017-05-12 15:26:54 -07001557 authority = parseAuthority(parser, version, validator);
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001558 periodicSync = null;
Shreyas Basarge11cb4e22016-01-13 14:27:16 +00001559 if (authority != null) {
1560 if (authority.ident > highestAuthorityId) {
1561 highestAuthorityId = authority.ident;
1562 }
Shreyas Basargebae9ded2016-02-17 13:54:44 +00001563 } else {
1564 EventLog.writeEvent(0x534e4554, "26513719", -1,
1565 "Malformed authority");
Fred Quintana77c560f2010-03-29 22:20:26 -07001566 }
Amith Yamasani04e0d262012-02-14 11:50:53 -08001567 } else if (XML_TAG_LISTEN_FOR_TICKLES.equals(tagName)) {
1568 parseListenForTickles(parser);
Dianne Hackborn231cc602009-04-27 17:10:36 -07001569 }
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001570 } else if (parser.getDepth() == 3) {
1571 if ("periodicSync".equals(tagName) && authority != null) {
1572 periodicSync = parsePeriodicSync(parser, authority);
1573 }
1574 } else if (parser.getDepth() == 4 && periodicSync != null) {
1575 if ("extra".equals(tagName)) {
Matthew Williamsfa774182013-06-18 15:44:11 -07001576 parseExtra(parser, periodicSync.extras);
Dianne Hackborn231cc602009-04-27 17:10:36 -07001577 }
1578 }
1579 }
1580 eventType = parser.next();
1581 } while (eventType != XmlPullParser.END_DOCUMENT);
1582 }
1583 } catch (XmlPullParserException e) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001584 Slog.w(TAG, "Error reading accounts", e);
Fred Quintanac2e46912010-03-15 16:10:44 -07001585 return;
Dianne Hackborn231cc602009-04-27 17:10:36 -07001586 } catch (java.io.IOException e) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001587 if (fis == null) Slog.i(TAG, "No initial accounts");
1588 else Slog.w(TAG, "Error reading accounts", e);
Fred Quintanac2e46912010-03-15 16:10:44 -07001589 return;
Dianne Hackborn231cc602009-04-27 17:10:36 -07001590 } finally {
Fred Quintana77c560f2010-03-29 22:20:26 -07001591 mNextAuthorityId = Math.max(highestAuthorityId + 1, mNextAuthorityId);
Dianne Hackborn231cc602009-04-27 17:10:36 -07001592 if (fis != null) {
1593 try {
1594 fis.close();
1595 } catch (java.io.IOException e1) {
1596 }
1597 }
1598 }
Fred Quintanac2e46912010-03-15 16:10:44 -07001599
Fred Quintana77c560f2010-03-29 22:20:26 -07001600 maybeMigrateSettingsForRenamedAuthorities();
Dianne Hackborn231cc602009-04-27 17:10:36 -07001601 }
Costin Manolache360e4542009-09-04 13:36:04 -07001602
Fred Quintanafb084402010-03-23 17:57:03 -07001603 /**
Matthew Williamsba352712013-08-13 15:53:31 -07001604 * Ensure the old pending.bin is deleted, as it has been changed to pending.xml.
1605 * pending.xml was used starting in KLP.
1606 * @param syncDir directory where the sync files are located.
1607 */
1608 private void maybeDeleteLegacyPendingInfoLocked(File syncDir) {
1609 File file = new File(syncDir, "pending.bin");
1610 if (!file.exists()) {
1611 return;
1612 } else {
1613 file.delete();
1614 }
1615 }
1616
1617 /**
Fred Quintanafb084402010-03-23 17:57:03 -07001618 * some authority names have changed. copy over their settings and delete the old ones
1619 * @return true if a change was made
1620 */
1621 private boolean maybeMigrateSettingsForRenamedAuthorities() {
1622 boolean writeNeeded = false;
1623
1624 ArrayList<AuthorityInfo> authoritiesToRemove = new ArrayList<AuthorityInfo>();
1625 final int N = mAuthorities.size();
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001626 for (int i = 0; i < N; i++) {
Fred Quintanafb084402010-03-23 17:57:03 -07001627 AuthorityInfo authority = mAuthorities.valueAt(i);
1628 // skip this authority if it isn't one of the renamed ones
Matthew Williams8ef22042013-07-26 12:56:39 -07001629 final String newAuthorityName = sAuthorityRenames.get(authority.target.provider);
Fred Quintanafb084402010-03-23 17:57:03 -07001630 if (newAuthorityName == null) {
1631 continue;
1632 }
1633
1634 // remember this authority so we can remove it later. we can't remove it
1635 // now without messing up this loop iteration
1636 authoritiesToRemove.add(authority);
1637
1638 // this authority isn't enabled, no need to copy it to the new authority name since
1639 // the default is "disabled"
1640 if (!authority.enabled) {
1641 continue;
1642 }
1643
1644 // if we already have a record of this new authority then don't copy over the settings
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001645 EndPoint newInfo =
Matthew Williams8ef22042013-07-26 12:56:39 -07001646 new EndPoint(authority.target.account,
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001647 newAuthorityName,
Matthew Williams8ef22042013-07-26 12:56:39 -07001648 authority.target.userId);
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001649 if (getAuthorityLocked(newInfo, "cleanup") != null) {
Fred Quintanafb084402010-03-23 17:57:03 -07001650 continue;
1651 }
1652
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001653 AuthorityInfo newAuthority =
1654 getOrCreateAuthorityLocked(newInfo, -1 /* ident */, false /* doWrite */);
Fred Quintanafb084402010-03-23 17:57:03 -07001655 newAuthority.enabled = true;
1656 writeNeeded = true;
1657 }
1658
1659 for (AuthorityInfo authorityInfo : authoritiesToRemove) {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001660 removeAuthorityLocked(
Matthew Williams8ef22042013-07-26 12:56:39 -07001661 authorityInfo.target.account,
1662 authorityInfo.target.userId,
1663 authorityInfo.target.provider,
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001664 false /* doWrite */);
Fred Quintanafb084402010-03-23 17:57:03 -07001665 writeNeeded = true;
1666 }
1667
1668 return writeNeeded;
1669 }
1670
Amith Yamasani04e0d262012-02-14 11:50:53 -08001671 private void parseListenForTickles(XmlPullParser parser) {
1672 String user = parser.getAttributeValue(null, XML_ATTR_USER);
1673 int userId = 0;
1674 try {
1675 userId = Integer.parseInt(user);
1676 } catch (NumberFormatException e) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001677 Slog.e(TAG, "error parsing the user for listen-for-tickles", e);
Amith Yamasani04e0d262012-02-14 11:50:53 -08001678 } catch (NullPointerException e) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001679 Slog.e(TAG, "the user in listen-for-tickles is null", e);
Amith Yamasani04e0d262012-02-14 11:50:53 -08001680 }
1681 String enabled = parser.getAttributeValue(null, XML_ATTR_ENABLED);
1682 boolean listen = enabled == null || Boolean.parseBoolean(enabled);
1683 mMasterSyncAutomatically.put(userId, listen);
1684 }
1685
Suprabh Shukla042a4782017-05-12 15:26:54 -07001686 private AuthorityInfo parseAuthority(XmlPullParser parser, int version,
1687 AccountAuthorityValidator validator) {
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001688 AuthorityInfo authority = null;
1689 int id = -1;
1690 try {
Matthew Williamsfa774182013-06-18 15:44:11 -07001691 id = Integer.parseInt(parser.getAttributeValue(null, "id"));
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001692 } catch (NumberFormatException e) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001693 Slog.e(TAG, "error parsing the id of the authority", e);
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001694 } catch (NullPointerException e) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001695 Slog.e(TAG, "the id of the authority is null", e);
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001696 }
1697 if (id >= 0) {
Fred Quintanafb084402010-03-23 17:57:03 -07001698 String authorityName = parser.getAttributeValue(null, "authority");
Amith Yamasani04e0d262012-02-14 11:50:53 -08001699 String enabled = parser.getAttributeValue(null, XML_ATTR_ENABLED);
Fred Quintanafb084402010-03-23 17:57:03 -07001700 String syncable = parser.getAttributeValue(null, "syncable");
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001701 String accountName = parser.getAttributeValue(null, "account");
1702 String accountType = parser.getAttributeValue(null, "type");
Amith Yamasani04e0d262012-02-14 11:50:53 -08001703 String user = parser.getAttributeValue(null, XML_ATTR_USER);
Matthew Williamsfa774182013-06-18 15:44:11 -07001704 String packageName = parser.getAttributeValue(null, "package");
1705 String className = parser.getAttributeValue(null, "class");
Amith Yamasani04e0d262012-02-14 11:50:53 -08001706 int userId = user == null ? 0 : Integer.parseInt(user);
Matthew Williams8ef22042013-07-26 12:56:39 -07001707 if (accountType == null && packageName == null) {
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001708 accountType = "com.google";
Matthew Williams53abfdb2015-06-10 20:06:37 -07001709 syncable = String.valueOf(AuthorityInfo.NOT_INITIALIZED);
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001710 }
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001711 authority = mAuthorities.get(id);
Matthew Williamsba352712013-08-13 15:53:31 -07001712 if (Log.isLoggable(TAG_FILE, Log.VERBOSE)) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001713 Slog.v(TAG_FILE, "Adding authority:"
Matthew Williams8ef22042013-07-26 12:56:39 -07001714 + " account=" + accountName
1715 + " accountType=" + accountType
1716 + " auth=" + authorityName
1717 + " package=" + packageName
1718 + " class=" + className
Matthew Williamsba352712013-08-13 15:53:31 -07001719 + " user=" + userId
1720 + " enabled=" + enabled
1721 + " syncable=" + syncable);
1722 }
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001723 if (authority == null) {
Matthew Williamsba352712013-08-13 15:53:31 -07001724 if (Log.isLoggable(TAG_FILE, Log.VERBOSE)) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001725 Slog.v(TAG_FILE, "Creating authority entry");
Matthew Williamsfa774182013-06-18 15:44:11 -07001726 }
Matthew Williams8ef22042013-07-26 12:56:39 -07001727 if (accountName != null && authorityName != null) {
Suprabh Shukla042a4782017-05-12 15:26:54 -07001728 EndPoint info = new EndPoint(
Matthew Williams8ef22042013-07-26 12:56:39 -07001729 new Account(accountName, accountType),
1730 authorityName, userId);
Suprabh Shukla042a4782017-05-12 15:26:54 -07001731 if (validator.isAccountValid(info.account, userId)
1732 && validator.isAuthorityValid(authorityName, userId)) {
1733 authority = getOrCreateAuthorityLocked(info, id, false);
1734 // If the version is 0 then we are upgrading from a file format that did not
1735 // know about periodic syncs. In that case don't clear the list since we
1736 // want the default, which is a daily periodic sync.
1737 // Otherwise clear out this default list since we will populate it later
1738 // with
1739 // the periodic sync descriptions that are read from the configuration file.
1740 if (version > 0) {
1741 authority.periodicSyncs.clear();
1742 }
1743 } else {
1744 EventLog.writeEvent(0x534e4554, "35028827", -1,
1745 "account:" + info.account + " provider:" + authorityName + " user:"
1746 + userId);
Dianne Hackbornbef28fe2015-10-29 17:57:11 -07001747 }
Fred Quintanac2e46912010-03-15 16:10:44 -07001748 }
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001749 }
1750 if (authority != null) {
1751 authority.enabled = enabled == null || Boolean.parseBoolean(enabled);
Matthew Williams53abfdb2015-06-10 20:06:37 -07001752 try {
1753 authority.syncable = (syncable == null) ?
1754 AuthorityInfo.NOT_INITIALIZED : Integer.parseInt(syncable);
1755 } catch (NumberFormatException e) {
1756 // On L we stored this as {"unknown", "true", "false"} so fall back to this
1757 // format.
1758 if ("unknown".equals(syncable)) {
1759 authority.syncable = AuthorityInfo.NOT_INITIALIZED;
1760 } else {
1761 authority.syncable = Boolean.parseBoolean(syncable) ?
1762 AuthorityInfo.SYNCABLE : AuthorityInfo.NOT_SYNCABLE;
1763 }
1764
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001765 }
1766 } else {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001767 Slog.w(TAG, "Failure adding authority: account="
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001768 + accountName + " auth=" + authorityName
1769 + " enabled=" + enabled
1770 + " syncable=" + syncable);
1771 }
1772 }
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001773 return authority;
1774 }
1775
Matthew Williamsfa774182013-06-18 15:44:11 -07001776 /**
1777 * Parse a periodic sync from accounts.xml. Sets the bundle to be empty.
1778 */
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001779 private PeriodicSync parsePeriodicSync(XmlPullParser parser, AuthorityInfo authorityInfo) {
Matthew Williamsfa774182013-06-18 15:44:11 -07001780 Bundle extras = new Bundle(); // Gets filled in later.
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001781 String periodValue = parser.getAttributeValue(null, "period");
Matthew Williamsfa774182013-06-18 15:44:11 -07001782 String flexValue = parser.getAttributeValue(null, "flex");
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001783 final long period;
Matthew Williamsfa774182013-06-18 15:44:11 -07001784 long flextime;
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001785 try {
1786 period = Long.parseLong(periodValue);
1787 } catch (NumberFormatException e) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001788 Slog.e(TAG, "error parsing the period of a periodic sync", e);
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001789 return null;
1790 } catch (NullPointerException e) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001791 Slog.e(TAG, "the period of a periodic sync is null", e);
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001792 return null;
1793 }
Matthew Williamsfa774182013-06-18 15:44:11 -07001794 try {
1795 flextime = Long.parseLong(flexValue);
1796 } catch (NumberFormatException e) {
Matthew Williamsfa774182013-06-18 15:44:11 -07001797 flextime = calculateDefaultFlexTime(period);
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001798 Slog.e(TAG, "Error formatting value parsed for periodic sync flex: " + flexValue
Matthew Williams8ef22042013-07-26 12:56:39 -07001799 + ", using default: "
1800 + flextime);
Matthew Williamsfa774182013-06-18 15:44:11 -07001801 } catch (NullPointerException expected) {
1802 flextime = calculateDefaultFlexTime(period);
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001803 Slog.d(TAG, "No flex time specified for this sync, using a default. period: "
1804 + period + " flex: " + flextime);
Matthew Williamsfa774182013-06-18 15:44:11 -07001805 }
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001806 PeriodicSync periodicSync;
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001807 periodicSync =
Matthew Williams8ef22042013-07-26 12:56:39 -07001808 new PeriodicSync(authorityInfo.target.account,
1809 authorityInfo.target.provider,
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001810 extras,
Matthew Williamsfa774182013-06-18 15:44:11 -07001811 period, flextime);
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001812 authorityInfo.periodicSyncs.add(periodicSync);
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001813 return periodicSync;
1814 }
1815
Matthew Williamsfa774182013-06-18 15:44:11 -07001816 private void parseExtra(XmlPullParser parser, Bundle extras) {
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001817 String name = parser.getAttributeValue(null, "name");
1818 String type = parser.getAttributeValue(null, "type");
1819 String value1 = parser.getAttributeValue(null, "value1");
1820 String value2 = parser.getAttributeValue(null, "value2");
1821
1822 try {
1823 if ("long".equals(type)) {
1824 extras.putLong(name, Long.parseLong(value1));
1825 } else if ("integer".equals(type)) {
1826 extras.putInt(name, Integer.parseInt(value1));
1827 } else if ("double".equals(type)) {
1828 extras.putDouble(name, Double.parseDouble(value1));
1829 } else if ("float".equals(type)) {
1830 extras.putFloat(name, Float.parseFloat(value1));
1831 } else if ("boolean".equals(type)) {
1832 extras.putBoolean(name, Boolean.parseBoolean(value1));
1833 } else if ("string".equals(type)) {
1834 extras.putString(name, value1);
1835 } else if ("account".equals(type)) {
1836 extras.putParcelable(name, new Account(value1, value2));
1837 }
1838 } catch (NumberFormatException e) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001839 Slog.e(TAG, "error parsing bundle value", e);
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001840 } catch (NullPointerException e) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001841 Slog.e(TAG, "error parsing bundle value", e);
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001842 }
1843 }
1844
Dianne Hackborn231cc602009-04-27 17:10:36 -07001845 /**
1846 * Write all account information to the account file.
1847 */
1848 private void writeAccountInfoLocked() {
Matthew Williamsba352712013-08-13 15:53:31 -07001849 if (Log.isLoggable(TAG_FILE, Log.VERBOSE)) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001850 Slog.v(TAG_FILE, "Writing new " + mAccountInfoFile.getBaseFile());
Matthew Williamsba352712013-08-13 15:53:31 -07001851 }
Dianne Hackborn231cc602009-04-27 17:10:36 -07001852 FileOutputStream fos = null;
Costin Manolache360e4542009-09-04 13:36:04 -07001853
Dianne Hackborn231cc602009-04-27 17:10:36 -07001854 try {
1855 fos = mAccountInfoFile.startWrite();
1856 XmlSerializer out = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01001857 out.setOutput(fos, StandardCharsets.UTF_8.name());
Dianne Hackborn231cc602009-04-27 17:10:36 -07001858 out.startDocument(null, true);
1859 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
Costin Manolache360e4542009-09-04 13:36:04 -07001860
Dianne Hackborn231cc602009-04-27 17:10:36 -07001861 out.startTag(null, "accounts");
Fred Quintanac2e46912010-03-15 16:10:44 -07001862 out.attribute(null, "version", Integer.toString(ACCOUNTS_VERSION));
Amith Yamasani04e0d262012-02-14 11:50:53 -08001863 out.attribute(null, XML_ATTR_NEXT_AUTHORITY_ID, Integer.toString(mNextAuthorityId));
Ashish Sharma69d95de2012-04-11 17:27:24 -07001864 out.attribute(null, XML_ATTR_SYNC_RANDOM_OFFSET, Integer.toString(mSyncRandomOffset));
Amith Yamasani04e0d262012-02-14 11:50:53 -08001865
1866 // Write the Sync Automatically flags for each user
1867 final int M = mMasterSyncAutomatically.size();
1868 for (int m = 0; m < M; m++) {
1869 int userId = mMasterSyncAutomatically.keyAt(m);
1870 Boolean listen = mMasterSyncAutomatically.valueAt(m);
1871 out.startTag(null, XML_TAG_LISTEN_FOR_TICKLES);
1872 out.attribute(null, XML_ATTR_USER, Integer.toString(userId));
1873 out.attribute(null, XML_ATTR_ENABLED, Boolean.toString(listen));
1874 out.endTag(null, XML_TAG_LISTEN_FOR_TICKLES);
Dianne Hackborn231cc602009-04-27 17:10:36 -07001875 }
Costin Manolache360e4542009-09-04 13:36:04 -07001876
Dianne Hackborn231cc602009-04-27 17:10:36 -07001877 final int N = mAuthorities.size();
Matthew Williamsfa774182013-06-18 15:44:11 -07001878 for (int i = 0; i < N; i++) {
Costin Manolache360e4542009-09-04 13:36:04 -07001879 AuthorityInfo authority = mAuthorities.valueAt(i);
Matthew Williams8ef22042013-07-26 12:56:39 -07001880 EndPoint info = authority.target;
Dianne Hackborn231cc602009-04-27 17:10:36 -07001881 out.startTag(null, "authority");
1882 out.attribute(null, "id", Integer.toString(authority.ident));
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001883 out.attribute(null, XML_ATTR_USER, Integer.toString(info.userId));
Amith Yamasani04e0d262012-02-14 11:50:53 -08001884 out.attribute(null, XML_ATTR_ENABLED, Boolean.toString(authority.enabled));
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001885 out.attribute(null, "account", info.account.name);
1886 out.attribute(null, "type", info.account.type);
1887 out.attribute(null, "authority", info.provider);
Matthew Williams53abfdb2015-06-10 20:06:37 -07001888 out.attribute(null, "syncable", Integer.toString(authority.syncable));
Dianne Hackborn231cc602009-04-27 17:10:36 -07001889 out.endTag(null, "authority");
1890 }
Dianne Hackborn231cc602009-04-27 17:10:36 -07001891 out.endTag(null, "accounts");
Dianne Hackborn231cc602009-04-27 17:10:36 -07001892 out.endDocument();
Dianne Hackborn231cc602009-04-27 17:10:36 -07001893 mAccountInfoFile.finishWrite(fos);
1894 } catch (java.io.IOException e1) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001895 Slog.w(TAG, "Error writing accounts", e1);
Dianne Hackborn231cc602009-04-27 17:10:36 -07001896 if (fos != null) {
1897 mAccountInfoFile.failWrite(fos);
1898 }
1899 }
1900 }
Costin Manolache360e4542009-09-04 13:36:04 -07001901
Dianne Hackborn231cc602009-04-27 17:10:36 -07001902 static int getIntColumn(Cursor c, String name) {
1903 return c.getInt(c.getColumnIndex(name));
1904 }
Costin Manolache360e4542009-09-04 13:36:04 -07001905
Dianne Hackborn231cc602009-04-27 17:10:36 -07001906 static long getLongColumn(Cursor c, String name) {
1907 return c.getLong(c.getColumnIndex(name));
1908 }
Costin Manolache360e4542009-09-04 13:36:04 -07001909
Dianne Hackborn231cc602009-04-27 17:10:36 -07001910 /**
1911 * Load sync engine state from the old syncmanager database, and then
1912 * erase it. Note that we don't deal with pending operations, active
1913 * sync, or history.
1914 */
Fred Quintana77c560f2010-03-29 22:20:26 -07001915 private void readAndDeleteLegacyAccountInfoLocked() {
Dianne Hackborn231cc602009-04-27 17:10:36 -07001916 // Look for old database to initialize from.
1917 File file = mContext.getDatabasePath("syncmanager.db");
1918 if (!file.exists()) {
1919 return;
1920 }
1921 String path = file.getPath();
1922 SQLiteDatabase db = null;
1923 try {
1924 db = SQLiteDatabase.openDatabase(path, null,
1925 SQLiteDatabase.OPEN_READONLY);
1926 } catch (SQLiteException e) {
1927 }
Costin Manolache360e4542009-09-04 13:36:04 -07001928
Dianne Hackborn231cc602009-04-27 17:10:36 -07001929 if (db != null) {
Dianne Hackborn2d5ed1f2009-05-06 15:22:38 -07001930 final boolean hasType = db.getVersion() >= 11;
Costin Manolache360e4542009-09-04 13:36:04 -07001931
Dianne Hackborn231cc602009-04-27 17:10:36 -07001932 // Copy in all of the status information, as well as accounts.
Matthew Williamsba352712013-08-13 15:53:31 -07001933 if (Log.isLoggable(TAG_FILE, Log.VERBOSE)) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001934 Slog.v(TAG_FILE, "Reading legacy sync accounts db");
Matthew Williamsba352712013-08-13 15:53:31 -07001935 }
Dianne Hackborn231cc602009-04-27 17:10:36 -07001936 SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
1937 qb.setTables("stats, status");
1938 HashMap<String,String> map = new HashMap<String,String>();
1939 map.put("_id", "status._id as _id");
1940 map.put("account", "stats.account as account");
Dianne Hackborn2d5ed1f2009-05-06 15:22:38 -07001941 if (hasType) {
1942 map.put("account_type", "stats.account_type as account_type");
1943 }
Dianne Hackborn231cc602009-04-27 17:10:36 -07001944 map.put("authority", "stats.authority as authority");
1945 map.put("totalElapsedTime", "totalElapsedTime");
1946 map.put("numSyncs", "numSyncs");
1947 map.put("numSourceLocal", "numSourceLocal");
1948 map.put("numSourcePoll", "numSourcePoll");
1949 map.put("numSourceServer", "numSourceServer");
1950 map.put("numSourceUser", "numSourceUser");
1951 map.put("lastSuccessSource", "lastSuccessSource");
1952 map.put("lastSuccessTime", "lastSuccessTime");
1953 map.put("lastFailureSource", "lastFailureSource");
1954 map.put("lastFailureTime", "lastFailureTime");
1955 map.put("lastFailureMesg", "lastFailureMesg");
1956 map.put("pending", "pending");
1957 qb.setProjectionMap(map);
1958 qb.appendWhere("stats._id = status.stats_id");
1959 Cursor c = qb.query(db, null, null, null, null, null, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001960 while (c.moveToNext()) {
Dianne Hackborn231cc602009-04-27 17:10:36 -07001961 String accountName = c.getString(c.getColumnIndex("account"));
Dianne Hackborn2d5ed1f2009-05-06 15:22:38 -07001962 String accountType = hasType
1963 ? c.getString(c.getColumnIndex("account_type")) : null;
Dianne Hackborn7a135592009-05-06 00:28:37 -07001964 if (accountType == null) {
Costin Manolache3348f142009-09-29 18:58:36 -07001965 accountType = "com.google";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001966 }
Dianne Hackborn231cc602009-04-27 17:10:36 -07001967 String authorityName = c.getString(c.getColumnIndex("authority"));
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001968 AuthorityInfo authority =
1969 this.getOrCreateAuthorityLocked(
1970 new EndPoint(new Account(accountName, accountType),
1971 authorityName,
1972 0 /* legacy is single-user */)
1973 , -1,
1974 false);
Dianne Hackborn231cc602009-04-27 17:10:36 -07001975 if (authority != null) {
1976 int i = mSyncStatus.size();
1977 boolean found = false;
1978 SyncStatusInfo st = null;
1979 while (i > 0) {
1980 i--;
Costin Manolache360e4542009-09-04 13:36:04 -07001981 st = mSyncStatus.valueAt(i);
Dianne Hackborn231cc602009-04-27 17:10:36 -07001982 if (st.authorityId == authority.ident) {
1983 found = true;
1984 break;
1985 }
1986 }
1987 if (!found) {
1988 st = new SyncStatusInfo(authority.ident);
1989 mSyncStatus.put(authority.ident, st);
1990 }
1991 st.totalElapsedTime = getLongColumn(c, "totalElapsedTime");
1992 st.numSyncs = getIntColumn(c, "numSyncs");
1993 st.numSourceLocal = getIntColumn(c, "numSourceLocal");
1994 st.numSourcePoll = getIntColumn(c, "numSourcePoll");
1995 st.numSourceServer = getIntColumn(c, "numSourceServer");
1996 st.numSourceUser = getIntColumn(c, "numSourceUser");
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001997 st.numSourcePeriodic = 0;
Dianne Hackborn231cc602009-04-27 17:10:36 -07001998 st.lastSuccessSource = getIntColumn(c, "lastSuccessSource");
1999 st.lastSuccessTime = getLongColumn(c, "lastSuccessTime");
2000 st.lastFailureSource = getIntColumn(c, "lastFailureSource");
2001 st.lastFailureTime = getLongColumn(c, "lastFailureTime");
2002 st.lastFailureMesg = c.getString(c.getColumnIndex("lastFailureMesg"));
2003 st.pending = getIntColumn(c, "pending") != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002004 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002005 }
Costin Manolache360e4542009-09-04 13:36:04 -07002006
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002007 c.close();
Costin Manolache360e4542009-09-04 13:36:04 -07002008
Dianne Hackborn231cc602009-04-27 17:10:36 -07002009 // Retrieve the settings.
2010 qb = new SQLiteQueryBuilder();
2011 qb.setTables("settings");
2012 c = qb.query(db, null, null, null, null, null, null);
2013 while (c.moveToNext()) {
2014 String name = c.getString(c.getColumnIndex("name"));
2015 String value = c.getString(c.getColumnIndex("value"));
2016 if (name == null) continue;
2017 if (name.equals("listen_for_tickles")) {
Amith Yamasani04e0d262012-02-14 11:50:53 -08002018 setMasterSyncAutomatically(value == null || Boolean.parseBoolean(value), 0);
Dianne Hackborn231cc602009-04-27 17:10:36 -07002019 } else if (name.startsWith("sync_provider_")) {
2020 String provider = name.substring("sync_provider_".length(),
2021 name.length());
Fred Quintanaac9385e2009-06-22 18:00:59 -07002022 int i = mAuthorities.size();
2023 while (i > 0) {
2024 i--;
Costin Manolache360e4542009-09-04 13:36:04 -07002025 AuthorityInfo authority = mAuthorities.valueAt(i);
Matthew Williams8ef22042013-07-26 12:56:39 -07002026 if (authority.target.provider.equals(provider)) {
Fred Quintanaac9385e2009-06-22 18:00:59 -07002027 authority.enabled = value == null || Boolean.parseBoolean(value);
Fred Quintana5e787c42009-08-16 23:13:53 -07002028 authority.syncable = 1;
Fred Quintanaac9385e2009-06-22 18:00:59 -07002029 }
2030 }
Dianne Hackborn231cc602009-04-27 17:10:36 -07002031 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002032 }
Costin Manolache360e4542009-09-04 13:36:04 -07002033
Dianne Hackborn231cc602009-04-27 17:10:36 -07002034 c.close();
Costin Manolache360e4542009-09-04 13:36:04 -07002035
Dianne Hackborn231cc602009-04-27 17:10:36 -07002036 db.close();
Costin Manolache360e4542009-09-04 13:36:04 -07002037
Dianne Hackborn231cc602009-04-27 17:10:36 -07002038 (new File(path)).delete();
2039 }
2040 }
Costin Manolache360e4542009-09-04 13:36:04 -07002041
Dianne Hackborn231cc602009-04-27 17:10:36 -07002042 public static final int STATUS_FILE_END = 0;
2043 public static final int STATUS_FILE_ITEM = 100;
Costin Manolache360e4542009-09-04 13:36:04 -07002044
Dianne Hackborn231cc602009-04-27 17:10:36 -07002045 /**
2046 * Read all sync status back in to the initial engine state.
2047 */
2048 private void readStatusLocked() {
Matthew Williamsba352712013-08-13 15:53:31 -07002049 if (Log.isLoggable(TAG_FILE, Log.VERBOSE)) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002050 Slog.v(TAG_FILE, "Reading " + mStatusFile.getBaseFile());
Matthew Williamsba352712013-08-13 15:53:31 -07002051 }
Dianne Hackborn231cc602009-04-27 17:10:36 -07002052 try {
2053 byte[] data = mStatusFile.readFully();
2054 Parcel in = Parcel.obtain();
2055 in.unmarshall(data, 0, data.length);
2056 in.setDataPosition(0);
2057 int token;
2058 while ((token=in.readInt()) != STATUS_FILE_END) {
2059 if (token == STATUS_FILE_ITEM) {
2060 SyncStatusInfo status = new SyncStatusInfo(in);
2061 if (mAuthorities.indexOfKey(status.authorityId) >= 0) {
2062 status.pending = false;
Matthew Williamsba352712013-08-13 15:53:31 -07002063 if (Log.isLoggable(TAG_FILE, Log.VERBOSE)) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002064 Slog.v(TAG_FILE, "Adding status for id " + status.authorityId);
Matthew Williamsba352712013-08-13 15:53:31 -07002065 }
Dianne Hackborn231cc602009-04-27 17:10:36 -07002066 mSyncStatus.put(status.authorityId, status);
2067 }
2068 } else {
2069 // Ooops.
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002070 Slog.w(TAG, "Unknown status token: " + token);
Dianne Hackborn231cc602009-04-27 17:10:36 -07002071 break;
2072 }
2073 }
2074 } catch (java.io.IOException e) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002075 Slog.i(TAG, "No initial status");
Dianne Hackborn231cc602009-04-27 17:10:36 -07002076 }
2077 }
Costin Manolache360e4542009-09-04 13:36:04 -07002078
Dianne Hackborn231cc602009-04-27 17:10:36 -07002079 /**
2080 * Write all sync status to the sync status file.
2081 */
2082 private void writeStatusLocked() {
Matthew Williamsba352712013-08-13 15:53:31 -07002083 if (Log.isLoggable(TAG_FILE, Log.VERBOSE)) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002084 Slog.v(TAG_FILE, "Writing new " + mStatusFile.getBaseFile());
Matthew Williamsba352712013-08-13 15:53:31 -07002085 }
Costin Manolache360e4542009-09-04 13:36:04 -07002086
Dianne Hackborn231cc602009-04-27 17:10:36 -07002087 // The file is being written, so we don't need to have a scheduled
2088 // write until the next change.
2089 removeMessages(MSG_WRITE_STATUS);
Costin Manolache360e4542009-09-04 13:36:04 -07002090
Dianne Hackborn231cc602009-04-27 17:10:36 -07002091 FileOutputStream fos = null;
2092 try {
2093 fos = mStatusFile.startWrite();
2094 Parcel out = Parcel.obtain();
2095 final int N = mSyncStatus.size();
2096 for (int i=0; i<N; i++) {
2097 SyncStatusInfo status = mSyncStatus.valueAt(i);
2098 out.writeInt(STATUS_FILE_ITEM);
2099 status.writeToParcel(out, 0);
2100 }
2101 out.writeInt(STATUS_FILE_END);
2102 fos.write(out.marshall());
2103 out.recycle();
Costin Manolache360e4542009-09-04 13:36:04 -07002104
Dianne Hackborn231cc602009-04-27 17:10:36 -07002105 mStatusFile.finishWrite(fos);
2106 } catch (java.io.IOException e1) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002107 Slog.w(TAG, "Error writing status", e1);
Dianne Hackborn231cc602009-04-27 17:10:36 -07002108 if (fos != null) {
2109 mStatusFile.failWrite(fos);
2110 }
2111 }
2112 }
Costin Manolache360e4542009-09-04 13:36:04 -07002113
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002114 private void requestSync(AuthorityInfo authorityInfo, int reason, Bundle extras) {
2115 if (android.os.Process.myUid() == android.os.Process.SYSTEM_UID
2116 && mSyncRequestListener != null) {
Matthew Williams8ef22042013-07-26 12:56:39 -07002117 mSyncRequestListener.onSyncRequest(authorityInfo.target, reason, extras);
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002118 } else {
2119 SyncRequest.Builder req =
2120 new SyncRequest.Builder()
Nick Kralevich69002ae2013-10-19 08:43:08 -07002121 .syncOnce()
Matthew Williams8ef22042013-07-26 12:56:39 -07002122 .setExtras(extras);
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002123 req.setSyncAdapter(authorityInfo.target.account, authorityInfo.target.provider);
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002124 ContentResolver.requestSync(req.build());
2125 }
2126 }
2127
Alon Albert57286f92012-10-09 14:21:38 -07002128 private void requestSync(Account account, int userId, int reason, String authority,
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002129 Bundle extras) {
Amith Yamasani04e0d262012-02-14 11:50:53 -08002130 // If this is happening in the system process, then call the syncrequest listener
2131 // to make a request back to the SyncManager directly.
2132 // If this is probably a test instance, then call back through the ContentResolver
2133 // which will know which userId to apply based on the Binder id.
2134 if (android.os.Process.myUid() == android.os.Process.SYSTEM_UID
2135 && mSyncRequestListener != null) {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002136 mSyncRequestListener.onSyncRequest(
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002137 new EndPoint(account, authority, userId),
2138 reason,
2139 extras);
Amith Yamasani04e0d262012-02-14 11:50:53 -08002140 } else {
2141 ContentResolver.requestSync(account, authority, extras);
2142 }
2143 }
2144
Dianne Hackborn231cc602009-04-27 17:10:36 -07002145 public static final int STATISTICS_FILE_END = 0;
2146 public static final int STATISTICS_FILE_ITEM_OLD = 100;
2147 public static final int STATISTICS_FILE_ITEM = 101;
Costin Manolache360e4542009-09-04 13:36:04 -07002148
Dianne Hackborn231cc602009-04-27 17:10:36 -07002149 /**
2150 * Read all sync statistics back in to the initial engine state.
2151 */
2152 private void readStatisticsLocked() {
2153 try {
2154 byte[] data = mStatisticsFile.readFully();
2155 Parcel in = Parcel.obtain();
2156 in.unmarshall(data, 0, data.length);
2157 in.setDataPosition(0);
2158 int token;
2159 int index = 0;
2160 while ((token=in.readInt()) != STATISTICS_FILE_END) {
2161 if (token == STATISTICS_FILE_ITEM
2162 || token == STATISTICS_FILE_ITEM_OLD) {
2163 int day = in.readInt();
2164 if (token == STATISTICS_FILE_ITEM_OLD) {
2165 day = day - 2009 + 14245; // Magic!
2166 }
2167 DayStats ds = new DayStats(day);
2168 ds.successCount = in.readInt();
2169 ds.successTime = in.readLong();
2170 ds.failureCount = in.readInt();
2171 ds.failureTime = in.readLong();
2172 if (index < mDayStats.length) {
2173 mDayStats[index] = ds;
2174 index++;
2175 }
2176 } else {
2177 // Ooops.
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002178 Slog.w(TAG, "Unknown stats token: " + token);
Dianne Hackborn231cc602009-04-27 17:10:36 -07002179 break;
2180 }
2181 }
2182 } catch (java.io.IOException e) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002183 Slog.i(TAG, "No initial statistics");
Dianne Hackborn231cc602009-04-27 17:10:36 -07002184 }
2185 }
Costin Manolache360e4542009-09-04 13:36:04 -07002186
Dianne Hackborn231cc602009-04-27 17:10:36 -07002187 /**
2188 * Write all sync statistics to the sync status file.
2189 */
2190 private void writeStatisticsLocked() {
Matthew Williamsba352712013-08-13 15:53:31 -07002191 if (Log.isLoggable(TAG_FILE, Log.VERBOSE)) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002192 Slog.v(TAG, "Writing new " + mStatisticsFile.getBaseFile());
Matthew Williamsba352712013-08-13 15:53:31 -07002193 }
Costin Manolache360e4542009-09-04 13:36:04 -07002194
Dianne Hackborn231cc602009-04-27 17:10:36 -07002195 // The file is being written, so we don't need to have a scheduled
2196 // write until the next change.
2197 removeMessages(MSG_WRITE_STATISTICS);
Costin Manolache360e4542009-09-04 13:36:04 -07002198
Dianne Hackborn231cc602009-04-27 17:10:36 -07002199 FileOutputStream fos = null;
2200 try {
2201 fos = mStatisticsFile.startWrite();
2202 Parcel out = Parcel.obtain();
2203 final int N = mDayStats.length;
2204 for (int i=0; i<N; i++) {
2205 DayStats ds = mDayStats[i];
2206 if (ds == null) {
2207 break;
2208 }
2209 out.writeInt(STATISTICS_FILE_ITEM);
2210 out.writeInt(ds.day);
2211 out.writeInt(ds.successCount);
2212 out.writeLong(ds.successTime);
2213 out.writeInt(ds.failureCount);
2214 out.writeLong(ds.failureTime);
2215 }
2216 out.writeInt(STATISTICS_FILE_END);
2217 fos.write(out.marshall());
2218 out.recycle();
Costin Manolache360e4542009-09-04 13:36:04 -07002219
Dianne Hackborn231cc602009-04-27 17:10:36 -07002220 mStatisticsFile.finishWrite(fos);
2221 } catch (java.io.IOException e1) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002222 Slog.w(TAG, "Error writing stats", e1);
Dianne Hackborn231cc602009-04-27 17:10:36 -07002223 if (fos != null) {
2224 mStatisticsFile.failWrite(fos);
2225 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002226 }
2227 }
Matthew Williamsfa774182013-06-18 15:44:11 -07002228
2229 /**
Marvin Paula6533252014-11-24 12:57:48 -08002230 * Let the BackupManager know that account sync settings have changed. This will trigger
2231 * {@link com.android.server.backup.SystemBackupAgent} to run.
2232 */
2233 public void queueBackup() {
2234 BackupManager.dataChanged("android");
2235 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002236}