blob: 3591871f5386c979e865f9c5d575bd4360fcbe48 [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
Makoto Onukib47e8942017-09-18 14:03:03 -0700914 public int getAuthorityCount() {
915 synchronized (mAuthorities) {
916 return mAuthorities.size();
917 }
918 }
919
Dianne Hackborn231cc602009-04-27 17:10:36 -0700920 public AuthorityInfo getAuthority(int authorityId) {
921 synchronized (mAuthorities) {
922 return mAuthorities.get(authorityId);
923 }
924 }
Costin Manolache360e4542009-09-04 13:36:04 -0700925
Dianne Hackborn231cc602009-04-27 17:10:36 -0700926 /**
Matthew Williams56dbf8f2013-07-26 12:56:39 -0700927 * Returns true if there is currently a sync operation being actively processed for the given
Matthew Williams8ef22042013-07-26 12:56:39 -0700928 * target.
Dianne Hackborn231cc602009-04-27 17:10:36 -0700929 */
Matthew Williams56dbf8f2013-07-26 12:56:39 -0700930 public boolean isSyncActive(EndPoint info) {
Dianne Hackborn231cc602009-04-27 17:10:36 -0700931 synchronized (mAuthorities) {
Matthew Williams56dbf8f2013-07-26 12:56:39 -0700932 for (SyncInfo syncInfo : getCurrentSyncs(info.userId)) {
Fred Quintana918339a2010-10-05 14:00:39 -0700933 AuthorityInfo ainfo = getAuthority(syncInfo.authorityId);
Matthew Williams8ef22042013-07-26 12:56:39 -0700934 if (ainfo != null && ainfo.target.matchesSpec(info)) {
Dianne Hackborn231cc602009-04-27 17:10:36 -0700935 return true;
936 }
937 }
938 }
Dianne Hackborn231cc602009-04-27 17:10:36 -0700939 return false;
940 }
Costin Manolache360e4542009-09-04 13:36:04 -0700941
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000942 public void markPending(EndPoint info, boolean pendingValue) {
Dianne Hackborn231cc602009-04-27 17:10:36 -0700943 synchronized (mAuthorities) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000944 AuthorityInfo authority = getOrCreateAuthorityLocked(info,
945 -1 /* desired identifier */,
946 true /* write accounts to storage */);
Dianne Hackborn231cc602009-04-27 17:10:36 -0700947 if (authority == null) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000948 return;
Dianne Hackborn231cc602009-04-27 17:10:36 -0700949 }
Dianne Hackborn231cc602009-04-27 17:10:36 -0700950 SyncStatusInfo status = getOrCreateSyncStatusLocked(authority.ident);
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000951 status.pending = pendingValue;
Dianne Hackborn231cc602009-04-27 17:10:36 -0700952 }
Fred Quintanaac9385e2009-06-22 18:00:59 -0700953 reportChange(ContentResolver.SYNC_OBSERVER_TYPE_PENDING);
Dianne Hackborn231cc602009-04-27 17:10:36 -0700954 }
Costin Manolache360e4542009-09-04 13:36:04 -0700955
Dianne Hackborn231cc602009-04-27 17:10:36 -0700956 /**
957 * Called when the set of account has changed, given the new array of
958 * active accounts.
959 */
Amith Yamasani04e0d262012-02-14 11:50:53 -0800960 public void doDatabaseCleanup(Account[] accounts, int userId) {
Dianne Hackborn231cc602009-04-27 17:10:36 -0700961 synchronized (mAuthorities) {
Matthew Williams8ef22042013-07-26 12:56:39 -0700962 if (Log.isLoggable(TAG, Log.VERBOSE)) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000963 Slog.v(TAG, "Updating for new accounts...");
Matthew Williams8ef22042013-07-26 12:56:39 -0700964 }
Dianne Hackborn231cc602009-04-27 17:10:36 -0700965 SparseArray<AuthorityInfo> removing = new SparseArray<AuthorityInfo>();
966 Iterator<AccountInfo> accIt = mAccounts.values().iterator();
967 while (accIt.hasNext()) {
968 AccountInfo acc = accIt.next();
Amith Yamasani04e0d262012-02-14 11:50:53 -0800969 if (!ArrayUtils.contains(accounts, acc.accountAndUser.account)
970 && acc.accountAndUser.userId == userId) {
Dianne Hackborn231cc602009-04-27 17:10:36 -0700971 // This account no longer exists...
Matthew Williams56dbf8f2013-07-26 12:56:39 -0700972 if (Log.isLoggable(TAG, Log.VERBOSE)) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000973 Slog.v(TAG, "Account removed: " + acc.accountAndUser);
Fred Quintana77c560f2010-03-29 22:20:26 -0700974 }
Dianne Hackborn231cc602009-04-27 17:10:36 -0700975 for (AuthorityInfo auth : acc.authorities.values()) {
976 removing.put(auth.ident, auth);
977 }
978 accIt.remove();
979 }
980 }
Costin Manolache360e4542009-09-04 13:36:04 -0700981
Dianne Hackborn231cc602009-04-27 17:10:36 -0700982 // Clean out all data structures.
983 int i = removing.size();
984 if (i > 0) {
985 while (i > 0) {
986 i--;
987 int ident = removing.keyAt(i);
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000988 AuthorityInfo auth = removing.valueAt(i);
989 if (mAuthorityRemovedListener != null) {
990 mAuthorityRemovedListener.onAuthorityRemoved(auth.target);
991 }
Dianne Hackborn231cc602009-04-27 17:10:36 -0700992 mAuthorities.remove(ident);
993 int j = mSyncStatus.size();
994 while (j > 0) {
995 j--;
996 if (mSyncStatus.keyAt(j) == ident) {
997 mSyncStatus.remove(mSyncStatus.keyAt(j));
998 }
999 }
1000 j = mSyncHistory.size();
1001 while (j > 0) {
1002 j--;
1003 if (mSyncHistory.get(j).authorityId == ident) {
1004 mSyncHistory.remove(j);
1005 }
1006 }
1007 }
1008 writeAccountInfoLocked();
1009 writeStatusLocked();
Dianne Hackborn231cc602009-04-27 17:10:36 -07001010 writeStatisticsLocked();
1011 }
1012 }
1013 }
1014
1015 /**
Fred Quintana918339a2010-10-05 14:00:39 -07001016 * Called when a sync is starting. Supply a valid ActiveSyncContext with information
1017 * about the sync.
Dianne Hackborn231cc602009-04-27 17:10:36 -07001018 */
Fred Quintana918339a2010-10-05 14:00:39 -07001019 public SyncInfo addActiveSync(SyncManager.ActiveSyncContext activeSyncContext) {
1020 final SyncInfo syncInfo;
Dianne Hackborn231cc602009-04-27 17:10:36 -07001021 synchronized (mAuthorities) {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001022 if (Log.isLoggable(TAG, Log.VERBOSE)) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001023 Slog.v(TAG, "setActiveSync: account="
1024 + " auth=" + activeSyncContext.mSyncOperation.target
1025 + " src=" + activeSyncContext.mSyncOperation.syncSource
1026 + " extras=" + activeSyncContext.mSyncOperation.extras);
Dianne Hackborn231cc602009-04-27 17:10:36 -07001027 }
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001028 final EndPoint info = activeSyncContext.mSyncOperation.target;
1029 AuthorityInfo authorityInfo = getOrCreateAuthorityLocked(
1030 info,
Matthew Williams8ef22042013-07-26 12:56:39 -07001031 -1 /* assign a new identifier if creating a new target */,
Fred Quintana918339a2010-10-05 14:00:39 -07001032 true /* write to storage if this results in a change */);
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001033 syncInfo = new SyncInfo(
1034 authorityInfo.ident,
Matthew Williams8ef22042013-07-26 12:56:39 -07001035 authorityInfo.target.account,
1036 authorityInfo.target.provider,
Fred Quintana918339a2010-10-05 14:00:39 -07001037 activeSyncContext.mStartTime);
Matthew Williams8ef22042013-07-26 12:56:39 -07001038 getCurrentSyncs(authorityInfo.target.userId).add(syncInfo);
Dianne Hackborn231cc602009-04-27 17:10:36 -07001039 }
Fred Quintana918339a2010-10-05 14:00:39 -07001040 reportActiveChange();
1041 return syncInfo;
1042 }
1043
1044 /**
1045 * Called to indicate that a previously active sync is no longer active.
1046 */
Amith Yamasani04e0d262012-02-14 11:50:53 -08001047 public void removeActiveSync(SyncInfo syncInfo, int userId) {
Fred Quintana918339a2010-10-05 14:00:39 -07001048 synchronized (mAuthorities) {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001049 if (Log.isLoggable(TAG, Log.VERBOSE)) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001050 Slog.v(TAG, "removeActiveSync: account=" + syncInfo.account
Amith Yamasani04e0d262012-02-14 11:50:53 -08001051 + " user=" + userId
Matthew Williams5a9decd2014-06-04 09:25:11 -07001052 + " auth=" + syncInfo.authority);
Fred Quintana918339a2010-10-05 14:00:39 -07001053 }
Amith Yamasani04e0d262012-02-14 11:50:53 -08001054 getCurrentSyncs(userId).remove(syncInfo);
Fred Quintana918339a2010-10-05 14:00:39 -07001055 }
1056
1057 reportActiveChange();
Dianne Hackborn231cc602009-04-27 17:10:36 -07001058 }
1059
1060 /**
1061 * To allow others to send active change reports, to poke clients.
1062 */
1063 public void reportActiveChange() {
Fred Quintanaac9385e2009-06-22 18:00:59 -07001064 reportChange(ContentResolver.SYNC_OBSERVER_TYPE_ACTIVE);
Dianne Hackborn231cc602009-04-27 17:10:36 -07001065 }
Costin Manolache360e4542009-09-04 13:36:04 -07001066
Dianne Hackborn231cc602009-04-27 17:10:36 -07001067 /**
Matthew Williams8ef22042013-07-26 12:56:39 -07001068 * Note that sync has started for the given operation.
Dianne Hackborn231cc602009-04-27 17:10:36 -07001069 */
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001070 public long insertStartSyncEvent(SyncOperation op, long now) {
Dianne Hackborn231cc602009-04-27 17:10:36 -07001071 long id;
1072 synchronized (mAuthorities) {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001073 if (Log.isLoggable(TAG, Log.VERBOSE)) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001074 Slog.v(TAG, "insertStartSyncEvent: " + op);
Fred Quintana77c560f2010-03-29 22:20:26 -07001075 }
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001076 AuthorityInfo authority = getAuthorityLocked(op.target, "insertStartSyncEvent");
Dianne Hackborn231cc602009-04-27 17:10:36 -07001077 if (authority == null) {
1078 return -1;
1079 }
1080 SyncHistoryItem item = new SyncHistoryItem();
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001081 item.initialization = op.isInitialization();
Dianne Hackborn231cc602009-04-27 17:10:36 -07001082 item.authorityId = authority.ident;
1083 item.historyId = mNextHistoryId++;
1084 if (mNextHistoryId < 0) mNextHistoryId = 0;
1085 item.eventTime = now;
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001086 item.source = op.syncSource;
1087 item.reason = op.reason;
1088 item.extras = op.extras;
Dianne Hackborn231cc602009-04-27 17:10:36 -07001089 item.event = EVENT_START;
1090 mSyncHistory.add(0, item);
1091 while (mSyncHistory.size() > MAX_HISTORY) {
1092 mSyncHistory.remove(mSyncHistory.size()-1);
1093 }
1094 id = item.historyId;
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001095 if (Log.isLoggable(TAG, Log.VERBOSE)) Slog.v(TAG, "returning historyId " + id);
Dianne Hackborn231cc602009-04-27 17:10:36 -07001096 }
Costin Manolache360e4542009-09-04 13:36:04 -07001097
Fred Quintanaac9385e2009-06-22 18:00:59 -07001098 reportChange(ContentResolver.SYNC_OBSERVER_TYPE_STATUS);
Dianne Hackborn231cc602009-04-27 17:10:36 -07001099 return id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001100 }
1101
Fred Quintana77c560f2010-03-29 22:20:26 -07001102 public void stopSyncEvent(long historyId, long elapsedTime, String resultMessage,
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001103 long downstreamActivity, long upstreamActivity) {
Dianne Hackborn231cc602009-04-27 17:10:36 -07001104 synchronized (mAuthorities) {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001105 if (Log.isLoggable(TAG, Log.VERBOSE)) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001106 Slog.v(TAG, "stopSyncEvent: historyId=" + historyId);
Fred Quintana77c560f2010-03-29 22:20:26 -07001107 }
Dianne Hackborn231cc602009-04-27 17:10:36 -07001108 SyncHistoryItem item = null;
1109 int i = mSyncHistory.size();
1110 while (i > 0) {
1111 i--;
1112 item = mSyncHistory.get(i);
1113 if (item.historyId == historyId) {
1114 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001115 }
Dianne Hackborn231cc602009-04-27 17:10:36 -07001116 item = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001117 }
Costin Manolache360e4542009-09-04 13:36:04 -07001118
Dianne Hackborn231cc602009-04-27 17:10:36 -07001119 if (item == null) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001120 Slog.w(TAG, "stopSyncEvent: no history for id " + historyId);
Dianne Hackborn231cc602009-04-27 17:10:36 -07001121 return;
1122 }
Costin Manolache360e4542009-09-04 13:36:04 -07001123
Dianne Hackborn231cc602009-04-27 17:10:36 -07001124 item.elapsedTime = elapsedTime;
1125 item.event = EVENT_STOP;
1126 item.mesg = resultMessage;
1127 item.downstreamActivity = downstreamActivity;
1128 item.upstreamActivity = upstreamActivity;
Costin Manolache360e4542009-09-04 13:36:04 -07001129
Dianne Hackborn231cc602009-04-27 17:10:36 -07001130 SyncStatusInfo status = getOrCreateSyncStatusLocked(item.authorityId);
Costin Manolache360e4542009-09-04 13:36:04 -07001131
Dianne Hackborn231cc602009-04-27 17:10:36 -07001132 status.numSyncs++;
1133 status.totalElapsedTime += elapsedTime;
1134 switch (item.source) {
1135 case SOURCE_LOCAL:
1136 status.numSourceLocal++;
1137 break;
1138 case SOURCE_POLL:
1139 status.numSourcePoll++;
1140 break;
1141 case SOURCE_USER:
1142 status.numSourceUser++;
1143 break;
1144 case SOURCE_SERVER:
1145 status.numSourceServer++;
1146 break;
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001147 case SOURCE_PERIODIC:
1148 status.numSourcePeriodic++;
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001149 break;
Dianne Hackborn231cc602009-04-27 17:10:36 -07001150 }
Costin Manolache360e4542009-09-04 13:36:04 -07001151
Dianne Hackborn231cc602009-04-27 17:10:36 -07001152 boolean writeStatisticsNow = false;
Dianne Hackborn55280a92009-05-07 15:53:46 -07001153 int day = getCurrentDayLocked();
Dianne Hackborn231cc602009-04-27 17:10:36 -07001154 if (mDayStats[0] == null) {
1155 mDayStats[0] = new DayStats(day);
1156 } else if (day != mDayStats[0].day) {
1157 System.arraycopy(mDayStats, 0, mDayStats, 1, mDayStats.length-1);
1158 mDayStats[0] = new DayStats(day);
1159 writeStatisticsNow = true;
1160 } else if (mDayStats[0] == null) {
1161 }
1162 final DayStats ds = mDayStats[0];
Costin Manolache360e4542009-09-04 13:36:04 -07001163
Dianne Hackborn231cc602009-04-27 17:10:36 -07001164 final long lastSyncTime = (item.eventTime + elapsedTime);
1165 boolean writeStatusNow = false;
1166 if (MESG_SUCCESS.equals(resultMessage)) {
1167 // - if successful, update the successful columns
1168 if (status.lastSuccessTime == 0 || status.lastFailureTime != 0) {
1169 writeStatusNow = true;
1170 }
1171 status.lastSuccessTime = lastSyncTime;
1172 status.lastSuccessSource = item.source;
1173 status.lastFailureTime = 0;
1174 status.lastFailureSource = -1;
1175 status.lastFailureMesg = null;
1176 status.initialFailureTime = 0;
1177 ds.successCount++;
1178 ds.successTime += elapsedTime;
1179 } else if (!MESG_CANCELED.equals(resultMessage)) {
1180 if (status.lastFailureTime == 0) {
1181 writeStatusNow = true;
1182 }
1183 status.lastFailureTime = lastSyncTime;
1184 status.lastFailureSource = item.source;
1185 status.lastFailureMesg = resultMessage;
1186 if (status.initialFailureTime == 0) {
1187 status.initialFailureTime = lastSyncTime;
1188 }
1189 ds.failureCount++;
1190 ds.failureTime += elapsedTime;
1191 }
Makoto Onuki15e7a252017-06-08 17:12:05 -07001192 final StringBuilder event = new StringBuilder();
1193 event.append("" + resultMessage + " Source=" + SyncStorageEngine.SOURCES[item.source]
1194 + " Elapsed=");
1195 SyncManager.formatDurationHMS(event, elapsedTime);
1196 event.append(" Reason=");
1197 event.append(SyncOperation.reasonToString(null, item.reason));
1198 event.append(" Extras=");
1199 SyncOperation.extrasToStringBuilder(item.extras, event);
1200
1201 status.addEvent(event.toString());
Costin Manolache360e4542009-09-04 13:36:04 -07001202
Dianne Hackborn231cc602009-04-27 17:10:36 -07001203 if (writeStatusNow) {
1204 writeStatusLocked();
1205 } else if (!hasMessages(MSG_WRITE_STATUS)) {
1206 sendMessageDelayed(obtainMessage(MSG_WRITE_STATUS),
1207 WRITE_STATUS_DELAY);
1208 }
1209 if (writeStatisticsNow) {
1210 writeStatisticsLocked();
1211 } else if (!hasMessages(MSG_WRITE_STATISTICS)) {
1212 sendMessageDelayed(obtainMessage(MSG_WRITE_STATISTICS),
1213 WRITE_STATISTICS_DELAY);
Costin Manolache360e4542009-09-04 13:36:04 -07001214 }
Dianne Hackborn231cc602009-04-27 17:10:36 -07001215 }
Costin Manolache360e4542009-09-04 13:36:04 -07001216
Fred Quintanaac9385e2009-06-22 18:00:59 -07001217 reportChange(ContentResolver.SYNC_OBSERVER_TYPE_STATUS);
Dianne Hackborn231cc602009-04-27 17:10:36 -07001218 }
1219
1220 /**
Matthew Williamsa7456e42013-11-12 14:41:02 -08001221 * Return a list of the currently active syncs. Note that the returned
1222 * items are the real, live active sync objects, so be careful what you do
1223 * with it.
Fred Quintana918339a2010-10-05 14:00:39 -07001224 */
Matthew Williamsa7456e42013-11-12 14:41:02 -08001225 private List<SyncInfo> getCurrentSyncs(int userId) {
Fred Quintana918339a2010-10-05 14:00:39 -07001226 synchronized (mAuthorities) {
Matthew Williamsa7456e42013-11-12 14:41:02 -08001227 return getCurrentSyncsLocked(userId);
Dianne Hackborn231cc602009-04-27 17:10:36 -07001228 }
1229 }
Costin Manolache360e4542009-09-04 13:36:04 -07001230
Dianne Hackborn231cc602009-04-27 17:10:36 -07001231 /**
Matthew Williamsf39549e2016-01-19 23:04:04 +00001232 * @param userId Id of user to return current sync info.
1233 * @param canAccessAccounts Determines whether to redact Account information from the result.
1234 * @return a copy of the current syncs data structure. Will not return null.
Matthew Williamsa7456e42013-11-12 14:41:02 -08001235 */
Matthew Williamsf39549e2016-01-19 23:04:04 +00001236 public List<SyncInfo> getCurrentSyncsCopy(int userId, boolean canAccessAccounts) {
Matthew Williamsa7456e42013-11-12 14:41:02 -08001237 synchronized (mAuthorities) {
1238 final List<SyncInfo> syncs = getCurrentSyncsLocked(userId);
1239 final List<SyncInfo> syncsCopy = new ArrayList<SyncInfo>();
1240 for (SyncInfo sync : syncs) {
Matthew Williamsf39549e2016-01-19 23:04:04 +00001241 SyncInfo copy;
1242 if (!canAccessAccounts) {
1243 copy = SyncInfo.createAccountRedacted(
1244 sync.authorityId, sync.authority, sync.startTime);
1245 } else {
1246 copy = new SyncInfo(sync);
1247 }
1248 syncsCopy.add(copy);
Matthew Williamsa7456e42013-11-12 14:41:02 -08001249 }
1250 return syncsCopy;
1251 }
1252 }
1253
1254 private List<SyncInfo> getCurrentSyncsLocked(int userId) {
1255 ArrayList<SyncInfo> syncs = mCurrentSyncs.get(userId);
1256 if (syncs == null) {
1257 syncs = new ArrayList<SyncInfo>();
1258 mCurrentSyncs.put(userId, syncs);
1259 }
1260 return syncs;
1261 }
1262
1263 /**
Matthew Williams8ef22042013-07-26 12:56:39 -07001264 * Return a copy of the specified target with the corresponding sync status
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001265 */
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001266 public Pair<AuthorityInfo, SyncStatusInfo> getCopyOfAuthorityWithSyncStatus(EndPoint info) {
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001267 synchronized (mAuthorities) {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001268 AuthorityInfo authorityInfo = getOrCreateAuthorityLocked(info,
Matthew Williams8ef22042013-07-26 12:56:39 -07001269 -1 /* assign a new identifier if creating a new target */,
Georgi Nikolovdbe846b2013-06-25 14:09:56 -07001270 true /* write to storage if this results in a change */);
1271 return createCopyPairOfAuthorityWithSyncStatusLocked(authorityInfo);
1272 }
1273 }
1274
1275 /**
Matthew Williams8ef22042013-07-26 12:56:39 -07001276 * Returns the status that matches the target.
Costin Manolacheb7520982009-09-02 18:03:05 -07001277 *
Matthew Williams8ef22042013-07-26 12:56:39 -07001278 * @param info the endpoint target we are querying status info for.
1279 * @return the SyncStatusInfo for the endpoint.
Dianne Hackborn231cc602009-04-27 17:10:36 -07001280 */
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001281 public SyncStatusInfo getStatusByAuthority(EndPoint info) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001282 if (info.account == null || info.provider == null) {
Matthew Williamsd08d6682013-10-14 10:39:41 -07001283 return null;
Costin Manolacheb7520982009-09-02 18:03:05 -07001284 }
Dianne Hackborn231cc602009-04-27 17:10:36 -07001285 synchronized (mAuthorities) {
Dianne Hackborn231cc602009-04-27 17:10:36 -07001286 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);
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001290 if (ainfo != null
Matthew Williams8ef22042013-07-26 12:56:39 -07001291 && ainfo.target.matchesSpec(info)) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001292 return cur;
Dianne Hackborn231cc602009-04-27 17:10:36 -07001293 }
1294 }
Costin Manolacheb7520982009-09-02 18:03:05 -07001295 return null;
Dianne Hackborn231cc602009-04-27 17:10:36 -07001296 }
1297 }
Costin Manolache360e4542009-09-04 13:36:04 -07001298
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001299 /** Return true if the pending status is true of any matching authorities. */
1300 public boolean isSyncPending(EndPoint info) {
Dianne Hackborn231cc602009-04-27 17:10:36 -07001301 synchronized (mAuthorities) {
1302 final int N = mSyncStatus.size();
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001303 for (int i = 0; i < N; i++) {
Costin Manolache360e4542009-09-04 13:36:04 -07001304 SyncStatusInfo cur = mSyncStatus.valueAt(i);
Dianne Hackborn231cc602009-04-27 17:10:36 -07001305 AuthorityInfo ainfo = mAuthorities.get(cur.authorityId);
1306 if (ainfo == null) {
1307 continue;
1308 }
Matthew Williams8ef22042013-07-26 12:56:39 -07001309 if (!ainfo.target.matchesSpec(info)) {
Amith Yamasani04e0d262012-02-14 11:50:53 -08001310 continue;
1311 }
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001312 if (cur.pending) {
Dianne Hackborn231cc602009-04-27 17:10:36 -07001313 return true;
1314 }
1315 }
1316 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001317 }
1318 }
1319
1320 /**
Dianne Hackborn231cc602009-04-27 17:10:36 -07001321 * Return an array of the current sync status for all authorities. 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 ArrayList<SyncHistoryItem> getSyncHistory() {
1326 synchronized (mAuthorities) {
1327 final int N = mSyncHistory.size();
1328 ArrayList<SyncHistoryItem> items = new ArrayList<SyncHistoryItem>(N);
1329 for (int i=0; i<N; i++) {
1330 items.add(mSyncHistory.get(i));
1331 }
1332 return items;
1333 }
1334 }
Costin Manolache360e4542009-09-04 13:36:04 -07001335
Dianne Hackborn231cc602009-04-27 17:10:36 -07001336 /**
1337 * Return an array of the current per-day statistics. Note
1338 * that the objects inside the array are the real, live status objects,
1339 * so be careful what you do with them.
1340 */
1341 public DayStats[] getDayStatistics() {
1342 synchronized (mAuthorities) {
1343 DayStats[] ds = new DayStats[mDayStats.length];
1344 System.arraycopy(mDayStats, 0, ds, 0, ds.length);
1345 return ds;
1346 }
1347 }
Costin Manolache360e4542009-09-04 13:36:04 -07001348
Georgi Nikolovdbe846b2013-06-25 14:09:56 -07001349 private Pair<AuthorityInfo, SyncStatusInfo> createCopyPairOfAuthorityWithSyncStatusLocked(
1350 AuthorityInfo authorityInfo) {
1351 SyncStatusInfo syncStatusInfo = getOrCreateSyncStatusLocked(authorityInfo.ident);
1352 return Pair.create(new AuthorityInfo(authorityInfo), new SyncStatusInfo(syncStatusInfo));
1353 }
1354
Dianne Hackborn55280a92009-05-07 15:53:46 -07001355 private int getCurrentDayLocked() {
Dianne Hackborn231cc602009-04-27 17:10:36 -07001356 mCal.setTimeInMillis(System.currentTimeMillis());
1357 final int dayOfYear = mCal.get(Calendar.DAY_OF_YEAR);
1358 if (mYear != mCal.get(Calendar.YEAR)) {
1359 mYear = mCal.get(Calendar.YEAR);
1360 mCal.clear();
1361 mCal.set(Calendar.YEAR, mYear);
1362 mYearInDays = (int)(mCal.getTimeInMillis()/86400000);
1363 }
1364 return dayOfYear + mYearInDays;
1365 }
Costin Manolache360e4542009-09-04 13:36:04 -07001366
Dianne Hackborn231cc602009-04-27 17:10:36 -07001367 /**
Matthew Williams8ef22042013-07-26 12:56:39 -07001368 * Retrieve a target's full info, returning null if one does not exist.
Costin Manolache360e4542009-09-04 13:36:04 -07001369 *
Matthew Williams8ef22042013-07-26 12:56:39 -07001370 * @param info info of the target to look up.
Dianne Hackborn231cc602009-04-27 17:10:36 -07001371 * @param tag If non-null, this will be used in a log message if the
Matthew Williams8ef22042013-07-26 12:56:39 -07001372 * requested target does not exist.
Dianne Hackborn231cc602009-04-27 17:10:36 -07001373 */
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001374 private AuthorityInfo getAuthorityLocked(EndPoint info, String tag) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001375 AccountAndUser au = new AccountAndUser(info.account, info.userId);
1376 AccountInfo accountInfo = mAccounts.get(au);
1377 if (accountInfo == null) {
1378 if (tag != null) {
1379 if (Log.isLoggable(TAG, Log.VERBOSE)) {
1380 Slog.v(TAG, tag + ": unknown account " + au);
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001381 }
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001382 }
Dianne Hackborn231cc602009-04-27 17:10:36 -07001383 return null;
1384 }
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001385 AuthorityInfo authority = accountInfo.authorities.get(info.provider);
1386 if (authority == null) {
1387 if (tag != null) {
1388 if (Log.isLoggable(TAG, Log.VERBOSE)) {
1389 Slog.v(TAG, tag + ": unknown provider " + info.provider);
1390 }
1391 }
1392 return null;
1393 }
1394 return authority;
Dianne Hackborn231cc602009-04-27 17:10:36 -07001395 }
Costin Manolache360e4542009-09-04 13:36:04 -07001396
Matthew Williamsfa774182013-06-18 15:44:11 -07001397 /**
Matthew Williams8ef22042013-07-26 12:56:39 -07001398 * @param info info identifying target.
1399 * @param ident unique identifier for target. -1 for none.
Matthew Williamsfa774182013-06-18 15:44:11 -07001400 * @param doWrite if true, update the accounts.xml file on the disk.
Matthew Williams8ef22042013-07-26 12:56:39 -07001401 * @return the authority that corresponds to the provided sync target, creating it if none
Matthew Williamsfa774182013-06-18 15:44:11 -07001402 * exists.
1403 */
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001404 private AuthorityInfo getOrCreateAuthorityLocked(EndPoint info, int ident, boolean doWrite) {
1405 AuthorityInfo authority = null;
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001406 AccountAndUser au = new AccountAndUser(info.account, info.userId);
1407 AccountInfo account = mAccounts.get(au);
1408 if (account == null) {
1409 account = new AccountInfo(au);
1410 mAccounts.put(au, account);
1411 }
1412 authority = account.authorities.get(info.provider);
1413 if (authority == null) {
1414 authority = createAuthorityLocked(info, ident, doWrite);
1415 account.authorities.put(info.provider, authority);
Matthew Williamsfa774182013-06-18 15:44:11 -07001416 }
1417 return authority;
1418 }
1419
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001420 private AuthorityInfo createAuthorityLocked(EndPoint info, int ident, boolean doWrite) {
1421 AuthorityInfo authority;
1422 if (ident < 0) {
1423 ident = mNextAuthorityId;
1424 mNextAuthorityId++;
1425 doWrite = true;
Dianne Hackborn231cc602009-04-27 17:10:36 -07001426 }
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001427 if (Log.isLoggable(TAG, Log.VERBOSE)) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001428 Slog.v(TAG, "created a new AuthorityInfo for " + info);
Dianne Hackborn231cc602009-04-27 17:10:36 -07001429 }
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001430 authority = new AuthorityInfo(info, ident);
1431 mAuthorities.put(ident, authority);
1432 if (doWrite) {
1433 writeAccountInfoLocked();
1434 }
Dianne Hackborn231cc602009-04-27 17:10:36 -07001435 return authority;
1436 }
Costin Manolache360e4542009-09-04 13:36:04 -07001437
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001438 public void removeAuthority(EndPoint info) {
1439 synchronized (mAuthorities) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001440 removeAuthorityLocked(info.account, info.userId, info.provider, true /* doWrite */);
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001441 }
1442 }
1443
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001444
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001445 /**
1446 * Remove an authority associated with a provider. Needs to be a standalone function for
1447 * backward compatibility.
1448 */
Amith Yamasani04e0d262012-02-14 11:50:53 -08001449 private void removeAuthorityLocked(Account account, int userId, String authorityName,
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001450 boolean doWrite) {
Amith Yamasani04e0d262012-02-14 11:50:53 -08001451 AccountInfo accountInfo = mAccounts.get(new AccountAndUser(account, userId));
Fred Quintana7620f1a2010-03-16 15:58:44 -07001452 if (accountInfo != null) {
Fred Quintanafb084402010-03-23 17:57:03 -07001453 final AuthorityInfo authorityInfo = accountInfo.authorities.remove(authorityName);
1454 if (authorityInfo != null) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001455 if (mAuthorityRemovedListener != null) {
1456 mAuthorityRemovedListener.onAuthorityRemoved(authorityInfo.target);
1457 }
Fred Quintanafb084402010-03-23 17:57:03 -07001458 mAuthorities.remove(authorityInfo.ident);
Fred Quintana77c560f2010-03-29 22:20:26 -07001459 if (doWrite) {
1460 writeAccountInfoLocked();
1461 }
Fred Quintana7620f1a2010-03-16 15:58:44 -07001462 }
1463 }
1464 }
1465
Dianne Hackborn231cc602009-04-27 17:10:36 -07001466 private SyncStatusInfo getOrCreateSyncStatusLocked(int authorityId) {
1467 SyncStatusInfo status = mSyncStatus.get(authorityId);
1468 if (status == null) {
1469 status = new SyncStatusInfo(authorityId);
1470 mSyncStatus.put(authorityId, status);
1471 }
1472 return status;
1473 }
Costin Manolache360e4542009-09-04 13:36:04 -07001474
Dianne Hackborn55280a92009-05-07 15:53:46 -07001475 public void writeAllState() {
1476 synchronized (mAuthorities) {
1477 // Account info is always written so no need to do it here.
Dianne Hackborn55280a92009-05-07 15:53:46 -07001478 writeStatusLocked();
1479 writeStatisticsLocked();
1480 }
1481 }
Costin Manolache360e4542009-09-04 13:36:04 -07001482
Svet Ganov65712b02016-09-01 10:24:11 -07001483 public boolean shouldGrantSyncAdaptersAccountAccess() {
1484 return mGrantSyncAdaptersAccountAccess;
1485 }
1486
Dianne Hackborn231cc602009-04-27 17:10:36 -07001487 /**
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001488 * public for testing
1489 */
1490 public void clearAndReadState() {
1491 synchronized (mAuthorities) {
1492 mAuthorities.clear();
1493 mAccounts.clear();
Matthew Williamsfa774182013-06-18 15:44:11 -07001494 mServices.clear();
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001495 mSyncStatus.clear();
1496 mSyncHistory.clear();
1497
1498 readAccountInfoLocked();
1499 readStatusLocked();
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001500 readStatisticsLocked();
Fred Quintana77c560f2010-03-29 22:20:26 -07001501 readAndDeleteLegacyAccountInfoLocked();
1502 writeAccountInfoLocked();
1503 writeStatusLocked();
Fred Quintana77c560f2010-03-29 22:20:26 -07001504 writeStatisticsLocked();
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001505 }
1506 }
1507
1508 /**
Dianne Hackborn231cc602009-04-27 17:10:36 -07001509 * Read all account information back in to the initial engine state.
1510 */
1511 private void readAccountInfoLocked() {
Fred Quintana77c560f2010-03-29 22:20:26 -07001512 int highestAuthorityId = -1;
Dianne Hackborn231cc602009-04-27 17:10:36 -07001513 FileInputStream fis = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001514 try {
Dianne Hackborn231cc602009-04-27 17:10:36 -07001515 fis = mAccountInfoFile.openRead();
Matthew Williamsba352712013-08-13 15:53:31 -07001516 if (Log.isLoggable(TAG_FILE, Log.VERBOSE)) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001517 Slog.v(TAG_FILE, "Reading " + mAccountInfoFile.getBaseFile());
Matthew Williamsba352712013-08-13 15:53:31 -07001518 }
Dianne Hackborn231cc602009-04-27 17:10:36 -07001519 XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01001520 parser.setInput(fis, StandardCharsets.UTF_8.name());
Dianne Hackborn231cc602009-04-27 17:10:36 -07001521 int eventType = parser.getEventType();
Narayan Kamath2ac3cb72014-01-06 11:31:35 +00001522 while (eventType != XmlPullParser.START_TAG &&
1523 eventType != XmlPullParser.END_DOCUMENT) {
Dianne Hackborn231cc602009-04-27 17:10:36 -07001524 eventType = parser.next();
1525 }
Narayan Kamath2ac3cb72014-01-06 11:31:35 +00001526 if (eventType == XmlPullParser.END_DOCUMENT) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001527 Slog.i(TAG, "No initial accounts");
Narayan Kamath2ac3cb72014-01-06 11:31:35 +00001528 return;
1529 }
1530
Dianne Hackborn231cc602009-04-27 17:10:36 -07001531 String tagName = parser.getName();
1532 if ("accounts".equals(tagName)) {
Amith Yamasani04e0d262012-02-14 11:50:53 -08001533 String listen = parser.getAttributeValue(null, XML_ATTR_LISTEN_FOR_TICKLES);
Fred Quintanac2e46912010-03-15 16:10:44 -07001534 String versionString = parser.getAttributeValue(null, "version");
1535 int version;
1536 try {
1537 version = (versionString == null) ? 0 : Integer.parseInt(versionString);
1538 } catch (NumberFormatException e) {
1539 version = 0;
1540 }
Svet Ganov65712b02016-09-01 10:24:11 -07001541
1542 if (version < 3) {
1543 mGrantSyncAdaptersAccountAccess = true;
1544 }
1545
Amith Yamasani04e0d262012-02-14 11:50:53 -08001546 String nextIdString = parser.getAttributeValue(null, XML_ATTR_NEXT_AUTHORITY_ID);
Fred Quintana77c560f2010-03-29 22:20:26 -07001547 try {
1548 int id = (nextIdString == null) ? 0 : Integer.parseInt(nextIdString);
1549 mNextAuthorityId = Math.max(mNextAuthorityId, id);
1550 } catch (NumberFormatException e) {
1551 // don't care
Fred Quintanac2e46912010-03-15 16:10:44 -07001552 }
Ashish Sharma69d95de2012-04-11 17:27:24 -07001553 String offsetString = parser.getAttributeValue(null, XML_ATTR_SYNC_RANDOM_OFFSET);
1554 try {
1555 mSyncRandomOffset = (offsetString == null) ? 0 : Integer.parseInt(offsetString);
1556 } catch (NumberFormatException e) {
1557 mSyncRandomOffset = 0;
1558 }
1559 if (mSyncRandomOffset == 0) {
1560 Random random = new Random(System.currentTimeMillis());
1561 mSyncRandomOffset = random.nextInt(86400);
1562 }
Amith Yamasani04e0d262012-02-14 11:50:53 -08001563 mMasterSyncAutomatically.put(0, listen == null || Boolean.parseBoolean(listen));
Dianne Hackborn231cc602009-04-27 17:10:36 -07001564 eventType = parser.next();
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001565 AuthorityInfo authority = null;
Matthew Williamsfa774182013-06-18 15:44:11 -07001566 PeriodicSync periodicSync = null;
Suprabh Shukla042a4782017-05-12 15:26:54 -07001567 AccountAuthorityValidator validator = new AccountAuthorityValidator(mContext);
Dianne Hackborn231cc602009-04-27 17:10:36 -07001568 do {
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001569 if (eventType == XmlPullParser.START_TAG) {
Dianne Hackborn231cc602009-04-27 17:10:36 -07001570 tagName = parser.getName();
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001571 if (parser.getDepth() == 2) {
1572 if ("authority".equals(tagName)) {
Suprabh Shukla042a4782017-05-12 15:26:54 -07001573 authority = parseAuthority(parser, version, validator);
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001574 periodicSync = null;
Shreyas Basarge11cb4e22016-01-13 14:27:16 +00001575 if (authority != null) {
1576 if (authority.ident > highestAuthorityId) {
1577 highestAuthorityId = authority.ident;
1578 }
Shreyas Basargebae9ded2016-02-17 13:54:44 +00001579 } else {
1580 EventLog.writeEvent(0x534e4554, "26513719", -1,
1581 "Malformed authority");
Fred Quintana77c560f2010-03-29 22:20:26 -07001582 }
Amith Yamasani04e0d262012-02-14 11:50:53 -08001583 } else if (XML_TAG_LISTEN_FOR_TICKLES.equals(tagName)) {
1584 parseListenForTickles(parser);
Dianne Hackborn231cc602009-04-27 17:10:36 -07001585 }
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001586 } else if (parser.getDepth() == 3) {
1587 if ("periodicSync".equals(tagName) && authority != null) {
1588 periodicSync = parsePeriodicSync(parser, authority);
1589 }
1590 } else if (parser.getDepth() == 4 && periodicSync != null) {
1591 if ("extra".equals(tagName)) {
Matthew Williamsfa774182013-06-18 15:44:11 -07001592 parseExtra(parser, periodicSync.extras);
Dianne Hackborn231cc602009-04-27 17:10:36 -07001593 }
1594 }
1595 }
1596 eventType = parser.next();
1597 } while (eventType != XmlPullParser.END_DOCUMENT);
1598 }
1599 } catch (XmlPullParserException e) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001600 Slog.w(TAG, "Error reading accounts", e);
Fred Quintanac2e46912010-03-15 16:10:44 -07001601 return;
Dianne Hackborn231cc602009-04-27 17:10:36 -07001602 } catch (java.io.IOException e) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001603 if (fis == null) Slog.i(TAG, "No initial accounts");
1604 else Slog.w(TAG, "Error reading accounts", e);
Fred Quintanac2e46912010-03-15 16:10:44 -07001605 return;
Dianne Hackborn231cc602009-04-27 17:10:36 -07001606 } finally {
Fred Quintana77c560f2010-03-29 22:20:26 -07001607 mNextAuthorityId = Math.max(highestAuthorityId + 1, mNextAuthorityId);
Dianne Hackborn231cc602009-04-27 17:10:36 -07001608 if (fis != null) {
1609 try {
1610 fis.close();
1611 } catch (java.io.IOException e1) {
1612 }
1613 }
1614 }
Fred Quintanac2e46912010-03-15 16:10:44 -07001615
Fred Quintana77c560f2010-03-29 22:20:26 -07001616 maybeMigrateSettingsForRenamedAuthorities();
Dianne Hackborn231cc602009-04-27 17:10:36 -07001617 }
Costin Manolache360e4542009-09-04 13:36:04 -07001618
Fred Quintanafb084402010-03-23 17:57:03 -07001619 /**
Matthew Williamsba352712013-08-13 15:53:31 -07001620 * Ensure the old pending.bin is deleted, as it has been changed to pending.xml.
1621 * pending.xml was used starting in KLP.
1622 * @param syncDir directory where the sync files are located.
1623 */
1624 private void maybeDeleteLegacyPendingInfoLocked(File syncDir) {
1625 File file = new File(syncDir, "pending.bin");
1626 if (!file.exists()) {
1627 return;
1628 } else {
1629 file.delete();
1630 }
1631 }
1632
1633 /**
Fred Quintanafb084402010-03-23 17:57:03 -07001634 * some authority names have changed. copy over their settings and delete the old ones
1635 * @return true if a change was made
1636 */
1637 private boolean maybeMigrateSettingsForRenamedAuthorities() {
1638 boolean writeNeeded = false;
1639
1640 ArrayList<AuthorityInfo> authoritiesToRemove = new ArrayList<AuthorityInfo>();
1641 final int N = mAuthorities.size();
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001642 for (int i = 0; i < N; i++) {
Fred Quintanafb084402010-03-23 17:57:03 -07001643 AuthorityInfo authority = mAuthorities.valueAt(i);
1644 // skip this authority if it isn't one of the renamed ones
Matthew Williams8ef22042013-07-26 12:56:39 -07001645 final String newAuthorityName = sAuthorityRenames.get(authority.target.provider);
Fred Quintanafb084402010-03-23 17:57:03 -07001646 if (newAuthorityName == null) {
1647 continue;
1648 }
1649
1650 // remember this authority so we can remove it later. we can't remove it
1651 // now without messing up this loop iteration
1652 authoritiesToRemove.add(authority);
1653
1654 // this authority isn't enabled, no need to copy it to the new authority name since
1655 // the default is "disabled"
1656 if (!authority.enabled) {
1657 continue;
1658 }
1659
1660 // if we already have a record of this new authority then don't copy over the settings
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001661 EndPoint newInfo =
Matthew Williams8ef22042013-07-26 12:56:39 -07001662 new EndPoint(authority.target.account,
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001663 newAuthorityName,
Matthew Williams8ef22042013-07-26 12:56:39 -07001664 authority.target.userId);
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001665 if (getAuthorityLocked(newInfo, "cleanup") != null) {
Fred Quintanafb084402010-03-23 17:57:03 -07001666 continue;
1667 }
1668
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001669 AuthorityInfo newAuthority =
1670 getOrCreateAuthorityLocked(newInfo, -1 /* ident */, false /* doWrite */);
Fred Quintanafb084402010-03-23 17:57:03 -07001671 newAuthority.enabled = true;
1672 writeNeeded = true;
1673 }
1674
1675 for (AuthorityInfo authorityInfo : authoritiesToRemove) {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001676 removeAuthorityLocked(
Matthew Williams8ef22042013-07-26 12:56:39 -07001677 authorityInfo.target.account,
1678 authorityInfo.target.userId,
1679 authorityInfo.target.provider,
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001680 false /* doWrite */);
Fred Quintanafb084402010-03-23 17:57:03 -07001681 writeNeeded = true;
1682 }
1683
1684 return writeNeeded;
1685 }
1686
Amith Yamasani04e0d262012-02-14 11:50:53 -08001687 private void parseListenForTickles(XmlPullParser parser) {
1688 String user = parser.getAttributeValue(null, XML_ATTR_USER);
1689 int userId = 0;
1690 try {
1691 userId = Integer.parseInt(user);
1692 } catch (NumberFormatException e) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001693 Slog.e(TAG, "error parsing the user for listen-for-tickles", e);
Amith Yamasani04e0d262012-02-14 11:50:53 -08001694 } catch (NullPointerException e) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001695 Slog.e(TAG, "the user in listen-for-tickles is null", e);
Amith Yamasani04e0d262012-02-14 11:50:53 -08001696 }
1697 String enabled = parser.getAttributeValue(null, XML_ATTR_ENABLED);
1698 boolean listen = enabled == null || Boolean.parseBoolean(enabled);
1699 mMasterSyncAutomatically.put(userId, listen);
1700 }
1701
Suprabh Shukla042a4782017-05-12 15:26:54 -07001702 private AuthorityInfo parseAuthority(XmlPullParser parser, int version,
1703 AccountAuthorityValidator validator) {
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001704 AuthorityInfo authority = null;
1705 int id = -1;
1706 try {
Matthew Williamsfa774182013-06-18 15:44:11 -07001707 id = Integer.parseInt(parser.getAttributeValue(null, "id"));
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001708 } catch (NumberFormatException e) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001709 Slog.e(TAG, "error parsing the id of the authority", e);
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001710 } catch (NullPointerException e) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001711 Slog.e(TAG, "the id of the authority is null", e);
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001712 }
1713 if (id >= 0) {
Fred Quintanafb084402010-03-23 17:57:03 -07001714 String authorityName = parser.getAttributeValue(null, "authority");
Amith Yamasani04e0d262012-02-14 11:50:53 -08001715 String enabled = parser.getAttributeValue(null, XML_ATTR_ENABLED);
Fred Quintanafb084402010-03-23 17:57:03 -07001716 String syncable = parser.getAttributeValue(null, "syncable");
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001717 String accountName = parser.getAttributeValue(null, "account");
1718 String accountType = parser.getAttributeValue(null, "type");
Amith Yamasani04e0d262012-02-14 11:50:53 -08001719 String user = parser.getAttributeValue(null, XML_ATTR_USER);
Matthew Williamsfa774182013-06-18 15:44:11 -07001720 String packageName = parser.getAttributeValue(null, "package");
1721 String className = parser.getAttributeValue(null, "class");
Amith Yamasani04e0d262012-02-14 11:50:53 -08001722 int userId = user == null ? 0 : Integer.parseInt(user);
Matthew Williams8ef22042013-07-26 12:56:39 -07001723 if (accountType == null && packageName == null) {
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001724 accountType = "com.google";
Matthew Williams53abfdb2015-06-10 20:06:37 -07001725 syncable = String.valueOf(AuthorityInfo.NOT_INITIALIZED);
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001726 }
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001727 authority = mAuthorities.get(id);
Matthew Williamsba352712013-08-13 15:53:31 -07001728 if (Log.isLoggable(TAG_FILE, Log.VERBOSE)) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001729 Slog.v(TAG_FILE, "Adding authority:"
Matthew Williams8ef22042013-07-26 12:56:39 -07001730 + " account=" + accountName
1731 + " accountType=" + accountType
1732 + " auth=" + authorityName
1733 + " package=" + packageName
1734 + " class=" + className
Matthew Williamsba352712013-08-13 15:53:31 -07001735 + " user=" + userId
1736 + " enabled=" + enabled
1737 + " syncable=" + syncable);
1738 }
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001739 if (authority == null) {
Matthew Williamsba352712013-08-13 15:53:31 -07001740 if (Log.isLoggable(TAG_FILE, Log.VERBOSE)) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001741 Slog.v(TAG_FILE, "Creating authority entry");
Matthew Williamsfa774182013-06-18 15:44:11 -07001742 }
Matthew Williams8ef22042013-07-26 12:56:39 -07001743 if (accountName != null && authorityName != null) {
Suprabh Shukla042a4782017-05-12 15:26:54 -07001744 EndPoint info = new EndPoint(
Matthew Williams8ef22042013-07-26 12:56:39 -07001745 new Account(accountName, accountType),
1746 authorityName, userId);
Suprabh Shukla042a4782017-05-12 15:26:54 -07001747 if (validator.isAccountValid(info.account, userId)
1748 && validator.isAuthorityValid(authorityName, userId)) {
1749 authority = getOrCreateAuthorityLocked(info, id, false);
1750 // If the version is 0 then we are upgrading from a file format that did not
1751 // know about periodic syncs. In that case don't clear the list since we
1752 // want the default, which is a daily periodic sync.
1753 // Otherwise clear out this default list since we will populate it later
1754 // with
1755 // the periodic sync descriptions that are read from the configuration file.
1756 if (version > 0) {
1757 authority.periodicSyncs.clear();
1758 }
1759 } else {
1760 EventLog.writeEvent(0x534e4554, "35028827", -1,
1761 "account:" + info.account + " provider:" + authorityName + " user:"
1762 + userId);
Dianne Hackbornbef28fe2015-10-29 17:57:11 -07001763 }
Fred Quintanac2e46912010-03-15 16:10:44 -07001764 }
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001765 }
1766 if (authority != null) {
1767 authority.enabled = enabled == null || Boolean.parseBoolean(enabled);
Matthew Williams53abfdb2015-06-10 20:06:37 -07001768 try {
1769 authority.syncable = (syncable == null) ?
1770 AuthorityInfo.NOT_INITIALIZED : Integer.parseInt(syncable);
1771 } catch (NumberFormatException e) {
1772 // On L we stored this as {"unknown", "true", "false"} so fall back to this
1773 // format.
1774 if ("unknown".equals(syncable)) {
1775 authority.syncable = AuthorityInfo.NOT_INITIALIZED;
1776 } else {
1777 authority.syncable = Boolean.parseBoolean(syncable) ?
1778 AuthorityInfo.SYNCABLE : AuthorityInfo.NOT_SYNCABLE;
1779 }
1780
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001781 }
1782 } else {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001783 Slog.w(TAG, "Failure adding authority: account="
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001784 + accountName + " auth=" + authorityName
1785 + " enabled=" + enabled
1786 + " syncable=" + syncable);
1787 }
1788 }
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001789 return authority;
1790 }
1791
Matthew Williamsfa774182013-06-18 15:44:11 -07001792 /**
1793 * Parse a periodic sync from accounts.xml. Sets the bundle to be empty.
1794 */
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001795 private PeriodicSync parsePeriodicSync(XmlPullParser parser, AuthorityInfo authorityInfo) {
Matthew Williamsfa774182013-06-18 15:44:11 -07001796 Bundle extras = new Bundle(); // Gets filled in later.
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001797 String periodValue = parser.getAttributeValue(null, "period");
Matthew Williamsfa774182013-06-18 15:44:11 -07001798 String flexValue = parser.getAttributeValue(null, "flex");
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001799 final long period;
Matthew Williamsfa774182013-06-18 15:44:11 -07001800 long flextime;
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001801 try {
1802 period = Long.parseLong(periodValue);
1803 } catch (NumberFormatException e) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001804 Slog.e(TAG, "error parsing the period of a periodic sync", e);
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001805 return null;
1806 } catch (NullPointerException e) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001807 Slog.e(TAG, "the period of a periodic sync is null", e);
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001808 return null;
1809 }
Matthew Williamsfa774182013-06-18 15:44:11 -07001810 try {
1811 flextime = Long.parseLong(flexValue);
1812 } catch (NumberFormatException e) {
Matthew Williamsfa774182013-06-18 15:44:11 -07001813 flextime = calculateDefaultFlexTime(period);
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001814 Slog.e(TAG, "Error formatting value parsed for periodic sync flex: " + flexValue
Matthew Williams8ef22042013-07-26 12:56:39 -07001815 + ", using default: "
1816 + flextime);
Matthew Williamsfa774182013-06-18 15:44:11 -07001817 } catch (NullPointerException expected) {
1818 flextime = calculateDefaultFlexTime(period);
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001819 Slog.d(TAG, "No flex time specified for this sync, using a default. period: "
1820 + period + " flex: " + flextime);
Matthew Williamsfa774182013-06-18 15:44:11 -07001821 }
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001822 PeriodicSync periodicSync;
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001823 periodicSync =
Matthew Williams8ef22042013-07-26 12:56:39 -07001824 new PeriodicSync(authorityInfo.target.account,
1825 authorityInfo.target.provider,
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001826 extras,
Matthew Williamsfa774182013-06-18 15:44:11 -07001827 period, flextime);
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001828 authorityInfo.periodicSyncs.add(periodicSync);
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001829 return periodicSync;
1830 }
1831
Matthew Williamsfa774182013-06-18 15:44:11 -07001832 private void parseExtra(XmlPullParser parser, Bundle extras) {
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001833 String name = parser.getAttributeValue(null, "name");
1834 String type = parser.getAttributeValue(null, "type");
1835 String value1 = parser.getAttributeValue(null, "value1");
1836 String value2 = parser.getAttributeValue(null, "value2");
1837
1838 try {
1839 if ("long".equals(type)) {
1840 extras.putLong(name, Long.parseLong(value1));
1841 } else if ("integer".equals(type)) {
1842 extras.putInt(name, Integer.parseInt(value1));
1843 } else if ("double".equals(type)) {
1844 extras.putDouble(name, Double.parseDouble(value1));
1845 } else if ("float".equals(type)) {
1846 extras.putFloat(name, Float.parseFloat(value1));
1847 } else if ("boolean".equals(type)) {
1848 extras.putBoolean(name, Boolean.parseBoolean(value1));
1849 } else if ("string".equals(type)) {
1850 extras.putString(name, value1);
1851 } else if ("account".equals(type)) {
1852 extras.putParcelable(name, new Account(value1, value2));
1853 }
1854 } catch (NumberFormatException e) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001855 Slog.e(TAG, "error parsing bundle value", e);
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001856 } catch (NullPointerException e) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001857 Slog.e(TAG, "error parsing bundle value", e);
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001858 }
1859 }
1860
Dianne Hackborn231cc602009-04-27 17:10:36 -07001861 /**
1862 * Write all account information to the account file.
1863 */
1864 private void writeAccountInfoLocked() {
Matthew Williamsba352712013-08-13 15:53:31 -07001865 if (Log.isLoggable(TAG_FILE, Log.VERBOSE)) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001866 Slog.v(TAG_FILE, "Writing new " + mAccountInfoFile.getBaseFile());
Matthew Williamsba352712013-08-13 15:53:31 -07001867 }
Dianne Hackborn231cc602009-04-27 17:10:36 -07001868 FileOutputStream fos = null;
Costin Manolache360e4542009-09-04 13:36:04 -07001869
Dianne Hackborn231cc602009-04-27 17:10:36 -07001870 try {
1871 fos = mAccountInfoFile.startWrite();
1872 XmlSerializer out = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01001873 out.setOutput(fos, StandardCharsets.UTF_8.name());
Dianne Hackborn231cc602009-04-27 17:10:36 -07001874 out.startDocument(null, true);
1875 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
Costin Manolache360e4542009-09-04 13:36:04 -07001876
Dianne Hackborn231cc602009-04-27 17:10:36 -07001877 out.startTag(null, "accounts");
Fred Quintanac2e46912010-03-15 16:10:44 -07001878 out.attribute(null, "version", Integer.toString(ACCOUNTS_VERSION));
Amith Yamasani04e0d262012-02-14 11:50:53 -08001879 out.attribute(null, XML_ATTR_NEXT_AUTHORITY_ID, Integer.toString(mNextAuthorityId));
Ashish Sharma69d95de2012-04-11 17:27:24 -07001880 out.attribute(null, XML_ATTR_SYNC_RANDOM_OFFSET, Integer.toString(mSyncRandomOffset));
Amith Yamasani04e0d262012-02-14 11:50:53 -08001881
1882 // Write the Sync Automatically flags for each user
1883 final int M = mMasterSyncAutomatically.size();
1884 for (int m = 0; m < M; m++) {
1885 int userId = mMasterSyncAutomatically.keyAt(m);
1886 Boolean listen = mMasterSyncAutomatically.valueAt(m);
1887 out.startTag(null, XML_TAG_LISTEN_FOR_TICKLES);
1888 out.attribute(null, XML_ATTR_USER, Integer.toString(userId));
1889 out.attribute(null, XML_ATTR_ENABLED, Boolean.toString(listen));
1890 out.endTag(null, XML_TAG_LISTEN_FOR_TICKLES);
Dianne Hackborn231cc602009-04-27 17:10:36 -07001891 }
Costin Manolache360e4542009-09-04 13:36:04 -07001892
Dianne Hackborn231cc602009-04-27 17:10:36 -07001893 final int N = mAuthorities.size();
Matthew Williamsfa774182013-06-18 15:44:11 -07001894 for (int i = 0; i < N; i++) {
Costin Manolache360e4542009-09-04 13:36:04 -07001895 AuthorityInfo authority = mAuthorities.valueAt(i);
Matthew Williams8ef22042013-07-26 12:56:39 -07001896 EndPoint info = authority.target;
Dianne Hackborn231cc602009-04-27 17:10:36 -07001897 out.startTag(null, "authority");
1898 out.attribute(null, "id", Integer.toString(authority.ident));
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001899 out.attribute(null, XML_ATTR_USER, Integer.toString(info.userId));
Amith Yamasani04e0d262012-02-14 11:50:53 -08001900 out.attribute(null, XML_ATTR_ENABLED, Boolean.toString(authority.enabled));
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001901 out.attribute(null, "account", info.account.name);
1902 out.attribute(null, "type", info.account.type);
1903 out.attribute(null, "authority", info.provider);
Matthew Williams53abfdb2015-06-10 20:06:37 -07001904 out.attribute(null, "syncable", Integer.toString(authority.syncable));
Dianne Hackborn231cc602009-04-27 17:10:36 -07001905 out.endTag(null, "authority");
1906 }
Dianne Hackborn231cc602009-04-27 17:10:36 -07001907 out.endTag(null, "accounts");
Dianne Hackborn231cc602009-04-27 17:10:36 -07001908 out.endDocument();
Dianne Hackborn231cc602009-04-27 17:10:36 -07001909 mAccountInfoFile.finishWrite(fos);
1910 } catch (java.io.IOException e1) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001911 Slog.w(TAG, "Error writing accounts", e1);
Dianne Hackborn231cc602009-04-27 17:10:36 -07001912 if (fos != null) {
1913 mAccountInfoFile.failWrite(fos);
1914 }
1915 }
1916 }
Costin Manolache360e4542009-09-04 13:36:04 -07001917
Dianne Hackborn231cc602009-04-27 17:10:36 -07001918 static int getIntColumn(Cursor c, String name) {
1919 return c.getInt(c.getColumnIndex(name));
1920 }
Costin Manolache360e4542009-09-04 13:36:04 -07001921
Dianne Hackborn231cc602009-04-27 17:10:36 -07001922 static long getLongColumn(Cursor c, String name) {
1923 return c.getLong(c.getColumnIndex(name));
1924 }
Costin Manolache360e4542009-09-04 13:36:04 -07001925
Dianne Hackborn231cc602009-04-27 17:10:36 -07001926 /**
1927 * Load sync engine state from the old syncmanager database, and then
1928 * erase it. Note that we don't deal with pending operations, active
1929 * sync, or history.
1930 */
Fred Quintana77c560f2010-03-29 22:20:26 -07001931 private void readAndDeleteLegacyAccountInfoLocked() {
Dianne Hackborn231cc602009-04-27 17:10:36 -07001932 // Look for old database to initialize from.
1933 File file = mContext.getDatabasePath("syncmanager.db");
1934 if (!file.exists()) {
1935 return;
1936 }
1937 String path = file.getPath();
1938 SQLiteDatabase db = null;
1939 try {
1940 db = SQLiteDatabase.openDatabase(path, null,
1941 SQLiteDatabase.OPEN_READONLY);
1942 } catch (SQLiteException e) {
1943 }
Costin Manolache360e4542009-09-04 13:36:04 -07001944
Dianne Hackborn231cc602009-04-27 17:10:36 -07001945 if (db != null) {
Dianne Hackborn2d5ed1f2009-05-06 15:22:38 -07001946 final boolean hasType = db.getVersion() >= 11;
Costin Manolache360e4542009-09-04 13:36:04 -07001947
Dianne Hackborn231cc602009-04-27 17:10:36 -07001948 // Copy in all of the status information, as well as accounts.
Matthew Williamsba352712013-08-13 15:53:31 -07001949 if (Log.isLoggable(TAG_FILE, Log.VERBOSE)) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001950 Slog.v(TAG_FILE, "Reading legacy sync accounts db");
Matthew Williamsba352712013-08-13 15:53:31 -07001951 }
Dianne Hackborn231cc602009-04-27 17:10:36 -07001952 SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
1953 qb.setTables("stats, status");
1954 HashMap<String,String> map = new HashMap<String,String>();
1955 map.put("_id", "status._id as _id");
1956 map.put("account", "stats.account as account");
Dianne Hackborn2d5ed1f2009-05-06 15:22:38 -07001957 if (hasType) {
1958 map.put("account_type", "stats.account_type as account_type");
1959 }
Dianne Hackborn231cc602009-04-27 17:10:36 -07001960 map.put("authority", "stats.authority as authority");
1961 map.put("totalElapsedTime", "totalElapsedTime");
1962 map.put("numSyncs", "numSyncs");
1963 map.put("numSourceLocal", "numSourceLocal");
1964 map.put("numSourcePoll", "numSourcePoll");
1965 map.put("numSourceServer", "numSourceServer");
1966 map.put("numSourceUser", "numSourceUser");
1967 map.put("lastSuccessSource", "lastSuccessSource");
1968 map.put("lastSuccessTime", "lastSuccessTime");
1969 map.put("lastFailureSource", "lastFailureSource");
1970 map.put("lastFailureTime", "lastFailureTime");
1971 map.put("lastFailureMesg", "lastFailureMesg");
1972 map.put("pending", "pending");
1973 qb.setProjectionMap(map);
1974 qb.appendWhere("stats._id = status.stats_id");
1975 Cursor c = qb.query(db, null, null, null, null, null, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001976 while (c.moveToNext()) {
Dianne Hackborn231cc602009-04-27 17:10:36 -07001977 String accountName = c.getString(c.getColumnIndex("account"));
Dianne Hackborn2d5ed1f2009-05-06 15:22:38 -07001978 String accountType = hasType
1979 ? c.getString(c.getColumnIndex("account_type")) : null;
Dianne Hackborn7a135592009-05-06 00:28:37 -07001980 if (accountType == null) {
Costin Manolache3348f142009-09-29 18:58:36 -07001981 accountType = "com.google";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001982 }
Dianne Hackborn231cc602009-04-27 17:10:36 -07001983 String authorityName = c.getString(c.getColumnIndex("authority"));
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001984 AuthorityInfo authority =
1985 this.getOrCreateAuthorityLocked(
1986 new EndPoint(new Account(accountName, accountType),
1987 authorityName,
1988 0 /* legacy is single-user */)
1989 , -1,
1990 false);
Dianne Hackborn231cc602009-04-27 17:10:36 -07001991 if (authority != null) {
1992 int i = mSyncStatus.size();
1993 boolean found = false;
1994 SyncStatusInfo st = null;
1995 while (i > 0) {
1996 i--;
Costin Manolache360e4542009-09-04 13:36:04 -07001997 st = mSyncStatus.valueAt(i);
Dianne Hackborn231cc602009-04-27 17:10:36 -07001998 if (st.authorityId == authority.ident) {
1999 found = true;
2000 break;
2001 }
2002 }
2003 if (!found) {
2004 st = new SyncStatusInfo(authority.ident);
2005 mSyncStatus.put(authority.ident, st);
2006 }
2007 st.totalElapsedTime = getLongColumn(c, "totalElapsedTime");
2008 st.numSyncs = getIntColumn(c, "numSyncs");
2009 st.numSourceLocal = getIntColumn(c, "numSourceLocal");
2010 st.numSourcePoll = getIntColumn(c, "numSourcePoll");
2011 st.numSourceServer = getIntColumn(c, "numSourceServer");
2012 st.numSourceUser = getIntColumn(c, "numSourceUser");
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08002013 st.numSourcePeriodic = 0;
Dianne Hackborn231cc602009-04-27 17:10:36 -07002014 st.lastSuccessSource = getIntColumn(c, "lastSuccessSource");
2015 st.lastSuccessTime = getLongColumn(c, "lastSuccessTime");
2016 st.lastFailureSource = getIntColumn(c, "lastFailureSource");
2017 st.lastFailureTime = getLongColumn(c, "lastFailureTime");
2018 st.lastFailureMesg = c.getString(c.getColumnIndex("lastFailureMesg"));
2019 st.pending = getIntColumn(c, "pending") != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002020 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002021 }
Costin Manolache360e4542009-09-04 13:36:04 -07002022
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002023 c.close();
Costin Manolache360e4542009-09-04 13:36:04 -07002024
Dianne Hackborn231cc602009-04-27 17:10:36 -07002025 // Retrieve the settings.
2026 qb = new SQLiteQueryBuilder();
2027 qb.setTables("settings");
2028 c = qb.query(db, null, null, null, null, null, null);
2029 while (c.moveToNext()) {
2030 String name = c.getString(c.getColumnIndex("name"));
2031 String value = c.getString(c.getColumnIndex("value"));
2032 if (name == null) continue;
2033 if (name.equals("listen_for_tickles")) {
Amith Yamasani04e0d262012-02-14 11:50:53 -08002034 setMasterSyncAutomatically(value == null || Boolean.parseBoolean(value), 0);
Dianne Hackborn231cc602009-04-27 17:10:36 -07002035 } else if (name.startsWith("sync_provider_")) {
2036 String provider = name.substring("sync_provider_".length(),
2037 name.length());
Fred Quintanaac9385e2009-06-22 18:00:59 -07002038 int i = mAuthorities.size();
2039 while (i > 0) {
2040 i--;
Costin Manolache360e4542009-09-04 13:36:04 -07002041 AuthorityInfo authority = mAuthorities.valueAt(i);
Matthew Williams8ef22042013-07-26 12:56:39 -07002042 if (authority.target.provider.equals(provider)) {
Fred Quintanaac9385e2009-06-22 18:00:59 -07002043 authority.enabled = value == null || Boolean.parseBoolean(value);
Fred Quintana5e787c42009-08-16 23:13:53 -07002044 authority.syncable = 1;
Fred Quintanaac9385e2009-06-22 18:00:59 -07002045 }
2046 }
Dianne Hackborn231cc602009-04-27 17:10:36 -07002047 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002048 }
Costin Manolache360e4542009-09-04 13:36:04 -07002049
Dianne Hackborn231cc602009-04-27 17:10:36 -07002050 c.close();
Costin Manolache360e4542009-09-04 13:36:04 -07002051
Dianne Hackborn231cc602009-04-27 17:10:36 -07002052 db.close();
Costin Manolache360e4542009-09-04 13:36:04 -07002053
Dianne Hackborn231cc602009-04-27 17:10:36 -07002054 (new File(path)).delete();
2055 }
2056 }
Costin Manolache360e4542009-09-04 13:36:04 -07002057
Dianne Hackborn231cc602009-04-27 17:10:36 -07002058 public static final int STATUS_FILE_END = 0;
2059 public static final int STATUS_FILE_ITEM = 100;
Costin Manolache360e4542009-09-04 13:36:04 -07002060
Dianne Hackborn231cc602009-04-27 17:10:36 -07002061 /**
2062 * Read all sync status back in to the initial engine state.
2063 */
2064 private void readStatusLocked() {
Matthew Williamsba352712013-08-13 15:53:31 -07002065 if (Log.isLoggable(TAG_FILE, Log.VERBOSE)) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002066 Slog.v(TAG_FILE, "Reading " + mStatusFile.getBaseFile());
Matthew Williamsba352712013-08-13 15:53:31 -07002067 }
Dianne Hackborn231cc602009-04-27 17:10:36 -07002068 try {
2069 byte[] data = mStatusFile.readFully();
2070 Parcel in = Parcel.obtain();
2071 in.unmarshall(data, 0, data.length);
2072 in.setDataPosition(0);
2073 int token;
2074 while ((token=in.readInt()) != STATUS_FILE_END) {
2075 if (token == STATUS_FILE_ITEM) {
2076 SyncStatusInfo status = new SyncStatusInfo(in);
2077 if (mAuthorities.indexOfKey(status.authorityId) >= 0) {
2078 status.pending = false;
Matthew Williamsba352712013-08-13 15:53:31 -07002079 if (Log.isLoggable(TAG_FILE, Log.VERBOSE)) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002080 Slog.v(TAG_FILE, "Adding status for id " + status.authorityId);
Matthew Williamsba352712013-08-13 15:53:31 -07002081 }
Dianne Hackborn231cc602009-04-27 17:10:36 -07002082 mSyncStatus.put(status.authorityId, status);
2083 }
2084 } else {
2085 // Ooops.
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002086 Slog.w(TAG, "Unknown status token: " + token);
Dianne Hackborn231cc602009-04-27 17:10:36 -07002087 break;
2088 }
2089 }
2090 } catch (java.io.IOException e) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002091 Slog.i(TAG, "No initial status");
Dianne Hackborn231cc602009-04-27 17:10:36 -07002092 }
2093 }
Costin Manolache360e4542009-09-04 13:36:04 -07002094
Dianne Hackborn231cc602009-04-27 17:10:36 -07002095 /**
2096 * Write all sync status to the sync status file.
2097 */
2098 private void writeStatusLocked() {
Matthew Williamsba352712013-08-13 15:53:31 -07002099 if (Log.isLoggable(TAG_FILE, Log.VERBOSE)) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002100 Slog.v(TAG_FILE, "Writing new " + mStatusFile.getBaseFile());
Matthew Williamsba352712013-08-13 15:53:31 -07002101 }
Costin Manolache360e4542009-09-04 13:36:04 -07002102
Dianne Hackborn231cc602009-04-27 17:10:36 -07002103 // The file is being written, so we don't need to have a scheduled
2104 // write until the next change.
2105 removeMessages(MSG_WRITE_STATUS);
Costin Manolache360e4542009-09-04 13:36:04 -07002106
Dianne Hackborn231cc602009-04-27 17:10:36 -07002107 FileOutputStream fos = null;
2108 try {
2109 fos = mStatusFile.startWrite();
2110 Parcel out = Parcel.obtain();
2111 final int N = mSyncStatus.size();
2112 for (int i=0; i<N; i++) {
2113 SyncStatusInfo status = mSyncStatus.valueAt(i);
2114 out.writeInt(STATUS_FILE_ITEM);
2115 status.writeToParcel(out, 0);
2116 }
2117 out.writeInt(STATUS_FILE_END);
2118 fos.write(out.marshall());
2119 out.recycle();
Costin Manolache360e4542009-09-04 13:36:04 -07002120
Dianne Hackborn231cc602009-04-27 17:10:36 -07002121 mStatusFile.finishWrite(fos);
2122 } catch (java.io.IOException e1) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002123 Slog.w(TAG, "Error writing status", e1);
Dianne Hackborn231cc602009-04-27 17:10:36 -07002124 if (fos != null) {
2125 mStatusFile.failWrite(fos);
2126 }
2127 }
2128 }
Costin Manolache360e4542009-09-04 13:36:04 -07002129
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002130 private void requestSync(AuthorityInfo authorityInfo, int reason, Bundle extras) {
2131 if (android.os.Process.myUid() == android.os.Process.SYSTEM_UID
2132 && mSyncRequestListener != null) {
Matthew Williams8ef22042013-07-26 12:56:39 -07002133 mSyncRequestListener.onSyncRequest(authorityInfo.target, reason, extras);
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002134 } else {
2135 SyncRequest.Builder req =
2136 new SyncRequest.Builder()
Nick Kralevich69002ae2013-10-19 08:43:08 -07002137 .syncOnce()
Matthew Williams8ef22042013-07-26 12:56:39 -07002138 .setExtras(extras);
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002139 req.setSyncAdapter(authorityInfo.target.account, authorityInfo.target.provider);
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002140 ContentResolver.requestSync(req.build());
2141 }
2142 }
2143
Alon Albert57286f92012-10-09 14:21:38 -07002144 private void requestSync(Account account, int userId, int reason, String authority,
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002145 Bundle extras) {
Amith Yamasani04e0d262012-02-14 11:50:53 -08002146 // If this is happening in the system process, then call the syncrequest listener
2147 // to make a request back to the SyncManager directly.
2148 // If this is probably a test instance, then call back through the ContentResolver
2149 // which will know which userId to apply based on the Binder id.
2150 if (android.os.Process.myUid() == android.os.Process.SYSTEM_UID
2151 && mSyncRequestListener != null) {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002152 mSyncRequestListener.onSyncRequest(
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002153 new EndPoint(account, authority, userId),
2154 reason,
2155 extras);
Amith Yamasani04e0d262012-02-14 11:50:53 -08002156 } else {
2157 ContentResolver.requestSync(account, authority, extras);
2158 }
2159 }
2160
Dianne Hackborn231cc602009-04-27 17:10:36 -07002161 public static final int STATISTICS_FILE_END = 0;
2162 public static final int STATISTICS_FILE_ITEM_OLD = 100;
2163 public static final int STATISTICS_FILE_ITEM = 101;
Costin Manolache360e4542009-09-04 13:36:04 -07002164
Dianne Hackborn231cc602009-04-27 17:10:36 -07002165 /**
2166 * Read all sync statistics back in to the initial engine state.
2167 */
2168 private void readStatisticsLocked() {
2169 try {
2170 byte[] data = mStatisticsFile.readFully();
2171 Parcel in = Parcel.obtain();
2172 in.unmarshall(data, 0, data.length);
2173 in.setDataPosition(0);
2174 int token;
2175 int index = 0;
2176 while ((token=in.readInt()) != STATISTICS_FILE_END) {
2177 if (token == STATISTICS_FILE_ITEM
2178 || token == STATISTICS_FILE_ITEM_OLD) {
2179 int day = in.readInt();
2180 if (token == STATISTICS_FILE_ITEM_OLD) {
2181 day = day - 2009 + 14245; // Magic!
2182 }
2183 DayStats ds = new DayStats(day);
2184 ds.successCount = in.readInt();
2185 ds.successTime = in.readLong();
2186 ds.failureCount = in.readInt();
2187 ds.failureTime = in.readLong();
2188 if (index < mDayStats.length) {
2189 mDayStats[index] = ds;
2190 index++;
2191 }
2192 } else {
2193 // Ooops.
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002194 Slog.w(TAG, "Unknown stats token: " + token);
Dianne Hackborn231cc602009-04-27 17:10:36 -07002195 break;
2196 }
2197 }
2198 } catch (java.io.IOException e) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002199 Slog.i(TAG, "No initial statistics");
Dianne Hackborn231cc602009-04-27 17:10:36 -07002200 }
2201 }
Costin Manolache360e4542009-09-04 13:36:04 -07002202
Dianne Hackborn231cc602009-04-27 17:10:36 -07002203 /**
2204 * Write all sync statistics to the sync status file.
2205 */
2206 private void writeStatisticsLocked() {
Matthew Williamsba352712013-08-13 15:53:31 -07002207 if (Log.isLoggable(TAG_FILE, Log.VERBOSE)) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002208 Slog.v(TAG, "Writing new " + mStatisticsFile.getBaseFile());
Matthew Williamsba352712013-08-13 15:53:31 -07002209 }
Costin Manolache360e4542009-09-04 13:36:04 -07002210
Dianne Hackborn231cc602009-04-27 17:10:36 -07002211 // The file is being written, so we don't need to have a scheduled
2212 // write until the next change.
2213 removeMessages(MSG_WRITE_STATISTICS);
Costin Manolache360e4542009-09-04 13:36:04 -07002214
Dianne Hackborn231cc602009-04-27 17:10:36 -07002215 FileOutputStream fos = null;
2216 try {
2217 fos = mStatisticsFile.startWrite();
2218 Parcel out = Parcel.obtain();
2219 final int N = mDayStats.length;
2220 for (int i=0; i<N; i++) {
2221 DayStats ds = mDayStats[i];
2222 if (ds == null) {
2223 break;
2224 }
2225 out.writeInt(STATISTICS_FILE_ITEM);
2226 out.writeInt(ds.day);
2227 out.writeInt(ds.successCount);
2228 out.writeLong(ds.successTime);
2229 out.writeInt(ds.failureCount);
2230 out.writeLong(ds.failureTime);
2231 }
2232 out.writeInt(STATISTICS_FILE_END);
2233 fos.write(out.marshall());
2234 out.recycle();
Costin Manolache360e4542009-09-04 13:36:04 -07002235
Dianne Hackborn231cc602009-04-27 17:10:36 -07002236 mStatisticsFile.finishWrite(fos);
2237 } catch (java.io.IOException e1) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002238 Slog.w(TAG, "Error writing stats", e1);
Dianne Hackborn231cc602009-04-27 17:10:36 -07002239 if (fos != null) {
2240 mStatisticsFile.failWrite(fos);
2241 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002242 }
2243 }
Matthew Williamsfa774182013-06-18 15:44:11 -07002244
2245 /**
Marvin Paula6533252014-11-24 12:57:48 -08002246 * Let the BackupManager know that account sync settings have changed. This will trigger
2247 * {@link com.android.server.backup.SystemBackupAgent} to run.
2248 */
2249 public void queueBackup() {
2250 BackupManager.dataChanged("android");
2251 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002252}