Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 17 | package com.android.server.content; |
Jason parks | a3cdaa5 | 2011-01-13 14:15:43 -0600 | [diff] [blame] | 18 | |
Fred Quintana | d9d2f11 | 2009-04-23 13:36:27 -0700 | [diff] [blame] | 19 | import android.accounts.Account; |
Amith Yamasani | f29f236 | 2012-04-05 18:29:52 -0700 | [diff] [blame] | 20 | import android.accounts.AccountAndUser; |
Suprabh Shukla | 042a478 | 2017-05-12 15:26:54 -0700 | [diff] [blame] | 21 | import android.accounts.AccountManager; |
Marvin Paul | a653325 | 2014-11-24 12:57:48 -0800 | [diff] [blame] | 22 | import android.app.backup.BackupManager; |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 23 | import android.content.ComponentName; |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 24 | import android.content.ContentResolver; |
| 25 | import android.content.Context; |
| 26 | import android.content.ISyncStatusObserver; |
| 27 | import android.content.PeriodicSync; |
| 28 | import android.content.SyncInfo; |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 29 | import android.content.SyncRequest; |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 30 | import android.content.SyncStatusInfo; |
Suprabh Shukla | 042a478 | 2017-05-12 15:26:54 -0700 | [diff] [blame] | 31 | import android.content.pm.PackageManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | import android.database.Cursor; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | import android.database.sqlite.SQLiteDatabase; |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 34 | import android.database.sqlite.SQLiteException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | import android.database.sqlite.SQLiteQueryBuilder; |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 36 | import android.os.Bundle; |
| 37 | import android.os.Environment; |
| 38 | import android.os.Handler; |
Makoto Onuki | 6963bea7 | 2017-12-12 10:42:39 -0800 | [diff] [blame^] | 39 | import android.os.Looper; |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 40 | import android.os.Message; |
| 41 | import android.os.Parcel; |
| 42 | import android.os.RemoteCallbackList; |
| 43 | import android.os.RemoteException; |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 44 | import android.os.UserHandle; |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 45 | import android.util.*; |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 46 | |
| 47 | import com.android.internal.annotations.VisibleForTesting; |
| 48 | import com.android.internal.util.ArrayUtils; |
| 49 | import com.android.internal.util.FastXmlSerializer; |
| 50 | |
| 51 | import org.xmlpull.v1.XmlPullParser; |
| 52 | import org.xmlpull.v1.XmlPullParserException; |
| 53 | import org.xmlpull.v1.XmlSerializer; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 54 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 55 | import java.io.File; |
| 56 | import java.io.FileInputStream; |
| 57 | import java.io.FileOutputStream; |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 58 | import java.nio.charset.StandardCharsets; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 59 | import java.util.ArrayList; |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 60 | import java.util.Calendar; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 61 | import java.util.HashMap; |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 62 | import java.util.Iterator; |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 63 | import java.util.List; |
Ashish Sharma | 69d95de | 2012-04-11 17:27:24 -0700 | [diff] [blame] | 64 | import java.util.Random; |
Jason parks | 1125d78 | 2011-01-12 09:47:26 -0600 | [diff] [blame] | 65 | import java.util.TimeZone; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 66 | |
| 67 | /** |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 68 | * Singleton that tracks the sync data and overall sync |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 69 | * history on the device. |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 70 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 71 | * @hide |
| 72 | */ |
Makoto Onuki | 6963bea7 | 2017-12-12 10:42:39 -0800 | [diff] [blame^] | 73 | public class SyncStorageEngine { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 74 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 75 | private static final String TAG = "SyncManager"; |
Matthew Williams | ba35271 | 2013-08-13 15:53:31 -0700 | [diff] [blame] | 76 | private static final String TAG_FILE = "SyncManagerFile"; |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 77 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 78 | private static final String XML_ATTR_NEXT_AUTHORITY_ID = "nextAuthorityId"; |
| 79 | private static final String XML_ATTR_LISTEN_FOR_TICKLES = "listen-for-tickles"; |
Ashish Sharma | 69d95de | 2012-04-11 17:27:24 -0700 | [diff] [blame] | 80 | private static final String XML_ATTR_SYNC_RANDOM_OFFSET = "offsetInSeconds"; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 81 | private static final String XML_ATTR_ENABLED = "enabled"; |
| 82 | private static final String XML_ATTR_USER = "user"; |
| 83 | private static final String XML_TAG_LISTEN_FOR_TICKLES = "listenForTickles"; |
| 84 | |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 85 | /** Default time for a periodic sync. */ |
Shreyas Basarge | cf939c1 | 2016-02-19 15:45:29 +0000 | [diff] [blame] | 86 | private static final long DEFAULT_POLL_FREQUENCY_SECONDS = 60 * 60 * 24; // One day |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 87 | |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 88 | /** Percentage of period that is flex by default, if no flexMillis is set. */ |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 89 | private static final double DEFAULT_FLEX_PERCENT_SYNC = 0.04; |
| 90 | |
| 91 | /** Lower bound on sync time from which we assign a default flex time. */ |
| 92 | private static final long DEFAULT_MIN_FLEX_ALLOWED_SECS = 5; |
| 93 | |
Jeff Sharkey | 8b2c3a14 | 2012-11-12 11:45:05 -0800 | [diff] [blame] | 94 | @VisibleForTesting |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 95 | static final long MILLIS_IN_4WEEKS = 1000L * 60 * 60 * 24 * 7 * 4; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 96 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 97 | /** Enum value for a sync start event. */ |
| 98 | public static final int EVENT_START = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 99 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 100 | /** Enum value for a sync stop event. */ |
| 101 | public static final int EVENT_STOP = 1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 102 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 103 | /** Enum value for a server-initiated sync. */ |
| 104 | public static final int SOURCE_SERVER = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 105 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 106 | /** Enum value for a local-initiated sync. */ |
| 107 | public static final int SOURCE_LOCAL = 1; |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 108 | /** Enum value for a poll-based sync (e.g., upon connection to network) */ |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 109 | public static final int SOURCE_POLL = 2; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 110 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 111 | /** Enum value for a user-initiated sync. */ |
| 112 | public static final int SOURCE_USER = 3; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 113 | |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 114 | /** Enum value for a periodic sync. */ |
| 115 | public static final int SOURCE_PERIODIC = 4; |
| 116 | |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 117 | public static final long NOT_IN_BACKOFF_MODE = -1; |
| 118 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 119 | // TODO: i18n -- grab these out of resources. |
| 120 | /** String names for the sync source types. */ |
| 121 | public static final String[] SOURCES = { "SERVER", |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 122 | "LOCAL", |
| 123 | "POLL", |
| 124 | "USER", |
| 125 | "PERIODIC", |
| 126 | "SERVICE"}; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 127 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 128 | // The MESG column will contain one of these or one of the Error types. |
| 129 | public static final String MESG_SUCCESS = "success"; |
| 130 | public static final String MESG_CANCELED = "canceled"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 131 | |
Dianne Hackborn | a33e3f7 | 2009-09-29 17:28:24 -0700 | [diff] [blame] | 132 | public static final int MAX_HISTORY = 100; |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 133 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 134 | private static final int MSG_WRITE_STATUS = 1; |
| 135 | private static final long WRITE_STATUS_DELAY = 1000*60*10; // 10 minutes |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 136 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 137 | private static final int MSG_WRITE_STATISTICS = 2; |
| 138 | private static final long WRITE_STATISTICS_DELAY = 1000*60*30; // 1/2 hour |
Joe Onorato | 8294fad | 2009-07-15 16:08:44 -0700 | [diff] [blame] | 139 | |
| 140 | private static final boolean SYNC_ENABLED_DEFAULT = false; |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 141 | |
Fred Quintana | c2e4691 | 2010-03-15 16:10:44 -0700 | [diff] [blame] | 142 | // the version of the accounts xml file format |
Svet Ganov | 65712b0 | 2016-09-01 10:24:11 -0700 | [diff] [blame] | 143 | private static final int ACCOUNTS_VERSION = 3; |
Fred Quintana | fb08440 | 2010-03-23 17:57:03 -0700 | [diff] [blame] | 144 | |
| 145 | private static HashMap<String, String> sAuthorityRenames; |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 146 | private static PeriodicSyncAddedListener mPeriodicSyncAddedListener; |
Fred Quintana | fb08440 | 2010-03-23 17:57:03 -0700 | [diff] [blame] | 147 | |
| 148 | static { |
| 149 | sAuthorityRenames = new HashMap<String, String>(); |
| 150 | sAuthorityRenames.put("contacts", "com.android.contacts"); |
| 151 | sAuthorityRenames.put("calendar", "com.android.calendar"); |
| 152 | } |
Fred Quintana | c2e4691 | 2010-03-15 16:10:44 -0700 | [diff] [blame] | 153 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 154 | static class AccountInfo { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 155 | final AccountAndUser accountAndUser; |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 156 | final HashMap<String, AuthorityInfo> authorities = |
| 157 | new HashMap<String, AuthorityInfo>(); |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 158 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 159 | AccountInfo(AccountAndUser accountAndUser) { |
| 160 | this.accountAndUser = accountAndUser; |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 161 | } |
| 162 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 163 | |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 164 | /** Bare bones representation of a sync target. */ |
| 165 | public static class EndPoint { |
| 166 | public final static EndPoint USER_ALL_PROVIDER_ALL_ACCOUNTS_ALL = |
| 167 | new EndPoint(null, null, UserHandle.USER_ALL); |
Dianne Hackborn | 7a13559 | 2009-05-06 00:28:37 -0700 | [diff] [blame] | 168 | final Account account; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 169 | final int userId; |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 170 | final String provider; |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 171 | |
| 172 | public EndPoint(Account account, String provider, int userId) { |
| 173 | this.account = account; |
| 174 | this.provider = provider; |
| 175 | this.userId = userId; |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | /** |
Matthew Williams | 8ef2204 | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 179 | * An Endpoint for a sync matches if it targets the same sync adapter for the same user. |
| 180 | * |
| 181 | * @param spec the Endpoint to match. If the spec has null fields, they indicate a wildcard |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 182 | * and match any. |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 183 | */ |
Matthew Williams | 8ef2204 | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 184 | public boolean matchesSpec(EndPoint spec) { |
| 185 | if (userId != spec.userId |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 186 | && userId != UserHandle.USER_ALL |
Matthew Williams | 8ef2204 | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 187 | && spec.userId != UserHandle.USER_ALL) { |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 188 | return false; |
| 189 | } |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 190 | boolean accountsMatch; |
| 191 | if (spec.account == null) { |
| 192 | accountsMatch = true; |
| 193 | } else { |
| 194 | accountsMatch = account.equals(spec.account); |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 195 | } |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 196 | boolean providersMatch; |
| 197 | if (spec.provider == null) { |
| 198 | providersMatch = true; |
| 199 | } else { |
| 200 | providersMatch = provider.equals(spec.provider); |
| 201 | } |
| 202 | return accountsMatch && providersMatch; |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | public String toString() { |
| 206 | StringBuilder sb = new StringBuilder(); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 207 | sb.append(account == null ? "ALL ACCS" : account.name) |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 208 | .append("/") |
| 209 | .append(provider == null ? "ALL PDRS" : provider); |
Matthew Williams | 8ef2204 | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 210 | sb.append(":u" + userId); |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 211 | return sb.toString(); |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | public static class AuthorityInfo { |
Matthew Williams | 53abfdb | 2015-06-10 20:06:37 -0700 | [diff] [blame] | 216 | // Legal values of getIsSyncable |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 217 | |
| 218 | /** |
| 219 | * The syncable state is undefined. |
| 220 | */ |
| 221 | public static final int UNDEFINED = -2; |
| 222 | |
Matthew Williams | 53abfdb | 2015-06-10 20:06:37 -0700 | [diff] [blame] | 223 | /** |
| 224 | * Default state for a newly installed adapter. An uninitialized adapter will receive an |
| 225 | * initialization sync which are governed by a different set of rules to that of regular |
| 226 | * syncs. |
| 227 | */ |
| 228 | public static final int NOT_INITIALIZED = -1; |
| 229 | /** |
| 230 | * The adapter will not receive any syncs. This is behaviourally equivalent to |
| 231 | * setSyncAutomatically -> false. However setSyncAutomatically is surfaced to the user |
| 232 | * while this is generally meant to be controlled by the developer. |
| 233 | */ |
| 234 | public static final int NOT_SYNCABLE = 0; |
| 235 | /** |
| 236 | * The adapter is initialized and functioning. This is the normal state for an adapter. |
| 237 | */ |
| 238 | public static final int SYNCABLE = 1; |
| 239 | /** |
| 240 | * The adapter is syncable but still requires an initialization sync. For example an adapter |
| 241 | * than has been restored from a previous device will be in this state. Not meant for |
| 242 | * external use. |
| 243 | */ |
| 244 | public static final int SYNCABLE_NOT_INITIALIZED = 2; |
| 245 | |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 246 | /** |
| 247 | * The adapter is syncable but does not have access to the synced account and needs a |
| 248 | * user access approval. |
| 249 | */ |
| 250 | public static final int SYNCABLE_NO_ACCOUNT_ACCESS = 3; |
| 251 | |
Matthew Williams | 8ef2204 | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 252 | final EndPoint target; |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 253 | final int ident; |
| 254 | boolean enabled; |
Fred Quintana | 5e787c4 | 2009-08-16 23:13:53 -0700 | [diff] [blame] | 255 | int syncable; |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 256 | /** Time at which this sync will run, taking into account backoff. */ |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 257 | long backoffTime; |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 258 | /** Amount of delay due to backoff. */ |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 259 | long backoffDelay; |
Matthew Williams | 8ef2204 | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 260 | /** Time offset to add to any requests coming to this target. */ |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 261 | long delayUntil; |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 262 | |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 263 | final ArrayList<PeriodicSync> periodicSyncs; |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 264 | |
Carlos Valdivia | 3aca7d7 | 2012-05-07 19:03:51 -0700 | [diff] [blame] | 265 | /** |
| 266 | * Copy constructor for making deep-ish copies. Only the bundles stored |
| 267 | * in periodic syncs can make unexpected changes. |
| 268 | * |
| 269 | * @param toCopy AuthorityInfo to be copied. |
| 270 | */ |
| 271 | AuthorityInfo(AuthorityInfo toCopy) { |
Matthew Williams | 8ef2204 | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 272 | target = toCopy.target; |
Carlos Valdivia | 3aca7d7 | 2012-05-07 19:03:51 -0700 | [diff] [blame] | 273 | ident = toCopy.ident; |
| 274 | enabled = toCopy.enabled; |
| 275 | syncable = toCopy.syncable; |
| 276 | backoffTime = toCopy.backoffTime; |
| 277 | backoffDelay = toCopy.backoffDelay; |
| 278 | delayUntil = toCopy.delayUntil; |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 279 | periodicSyncs = new ArrayList<PeriodicSync>(); |
| 280 | for (PeriodicSync sync : toCopy.periodicSyncs) { |
Carlos Valdivia | 3aca7d7 | 2012-05-07 19:03:51 -0700 | [diff] [blame] | 281 | // Still not a perfect copy, because we are just copying the mappings. |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 282 | periodicSyncs.add(new PeriodicSync(sync)); |
Carlos Valdivia | 3aca7d7 | 2012-05-07 19:03:51 -0700 | [diff] [blame] | 283 | } |
| 284 | } |
| 285 | |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 286 | AuthorityInfo(EndPoint info, int id) { |
Matthew Williams | 8ef2204 | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 287 | target = info; |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 288 | ident = id; |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 289 | enabled = SYNC_ENABLED_DEFAULT; |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 290 | periodicSyncs = new ArrayList<PeriodicSync>(); |
| 291 | defaultInitialisation(); |
| 292 | } |
| 293 | |
| 294 | private void defaultInitialisation() { |
Matthew Williams | 53abfdb | 2015-06-10 20:06:37 -0700 | [diff] [blame] | 295 | syncable = NOT_INITIALIZED; // default to "unknown" |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 296 | backoffTime = -1; // if < 0 then we aren't in backoff mode |
| 297 | backoffDelay = -1; // if < 0 then we aren't in backoff mode |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 298 | |
| 299 | if (mPeriodicSyncAddedListener != null) { |
| 300 | mPeriodicSyncAddedListener.onPeriodicSyncAdded(target, new Bundle(), |
| 301 | DEFAULT_POLL_FREQUENCY_SECONDS, |
| 302 | calculateDefaultFlexTime(DEFAULT_POLL_FREQUENCY_SECONDS)); |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 303 | } |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 304 | } |
Matthew Williams | 06485a7 | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 305 | |
| 306 | @Override |
| 307 | public String toString() { |
| 308 | return target + ", enabled=" + enabled + ", syncable=" + syncable + ", backoff=" |
| 309 | + backoffTime + ", delay=" + delayUntil; |
| 310 | } |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 311 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 312 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 313 | public static class SyncHistoryItem { |
| 314 | int authorityId; |
| 315 | int historyId; |
| 316 | long eventTime; |
| 317 | long elapsedTime; |
| 318 | int source; |
| 319 | int event; |
| 320 | long upstreamActivity; |
| 321 | long downstreamActivity; |
| 322 | String mesg; |
Fred Quintana | dc47556 | 2012-05-04 15:51:54 -0700 | [diff] [blame] | 323 | boolean initialization; |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 324 | Bundle extras; |
| 325 | int reason; |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 326 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 327 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 328 | public static class DayStats { |
| 329 | public final int day; |
| 330 | public int successCount; |
| 331 | public long successTime; |
| 332 | public int failureCount; |
| 333 | public long failureTime; |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 334 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 335 | public DayStats(int day) { |
| 336 | this.day = day; |
| 337 | } |
| 338 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 339 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 340 | interface OnSyncRequestListener { |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 341 | |
| 342 | /** Called when a sync is needed on an account(s) due to some change in state. */ |
| 343 | public void onSyncRequest(EndPoint info, int reason, Bundle extras); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 344 | } |
| 345 | |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 346 | interface PeriodicSyncAddedListener { |
| 347 | /** Called when a periodic sync is added. */ |
| 348 | void onPeriodicSyncAdded(EndPoint target, Bundle extras, long pollFrequency, long flex); |
| 349 | } |
| 350 | |
| 351 | interface OnAuthorityRemovedListener { |
| 352 | /** Called when an authority is removed. */ |
| 353 | void onAuthorityRemoved(EndPoint removedAuthority); |
| 354 | } |
| 355 | |
Suprabh Shukla | 042a478 | 2017-05-12 15:26:54 -0700 | [diff] [blame] | 356 | /** |
| 357 | * Validator that maintains a lazy cache of accounts and providers to tell if an authority or |
| 358 | * account is valid. |
| 359 | */ |
| 360 | private static class AccountAuthorityValidator { |
| 361 | final private AccountManager mAccountManager; |
| 362 | final private PackageManager mPackageManager; |
| 363 | final private SparseArray<Account[]> mAccountsCache; |
| 364 | final private SparseArray<ArrayMap<String, Boolean>> mProvidersPerUserCache; |
| 365 | |
| 366 | AccountAuthorityValidator(Context context) { |
| 367 | mAccountManager = context.getSystemService(AccountManager.class); |
| 368 | mPackageManager = context.getPackageManager(); |
| 369 | mAccountsCache = new SparseArray<>(); |
| 370 | mProvidersPerUserCache = new SparseArray<>(); |
| 371 | } |
| 372 | |
| 373 | // An account is valid if an installed authenticator has previously created that account |
| 374 | // on the device |
| 375 | boolean isAccountValid(Account account, int userId) { |
| 376 | Account[] accountsForUser = mAccountsCache.get(userId); |
| 377 | if (accountsForUser == null) { |
| 378 | accountsForUser = mAccountManager.getAccountsAsUser(userId); |
| 379 | mAccountsCache.put(userId, accountsForUser); |
| 380 | } |
| 381 | return ArrayUtils.contains(accountsForUser, account); |
| 382 | } |
| 383 | |
| 384 | // An authority is only valid if it has a content provider installed on the system |
| 385 | boolean isAuthorityValid(String authority, int userId) { |
| 386 | ArrayMap<String, Boolean> authorityMap = mProvidersPerUserCache.get(userId); |
| 387 | if (authorityMap == null) { |
| 388 | authorityMap = new ArrayMap<>(); |
| 389 | mProvidersPerUserCache.put(userId, authorityMap); |
| 390 | } |
| 391 | if (!authorityMap.containsKey(authority)) { |
| 392 | authorityMap.put(authority, mPackageManager.resolveContentProviderAsUser(authority, |
| 393 | PackageManager.MATCH_DIRECT_BOOT_AWARE |
| 394 | | PackageManager.MATCH_DIRECT_BOOT_UNAWARE, userId) != null); |
| 395 | } |
| 396 | return authorityMap.get(authority); |
| 397 | } |
| 398 | } |
| 399 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 400 | // Primary list of all syncable authorities. Also our global lock. |
| 401 | private final SparseArray<AuthorityInfo> mAuthorities = |
| 402 | new SparseArray<AuthorityInfo>(); |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 403 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 404 | private final HashMap<AccountAndUser, AccountInfo> mAccounts |
| 405 | = new HashMap<AccountAndUser, AccountInfo>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 406 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 407 | private final SparseArray<ArrayList<SyncInfo>> mCurrentSyncs |
| 408 | = new SparseArray<ArrayList<SyncInfo>>(); |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 409 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 410 | private final SparseArray<SyncStatusInfo> mSyncStatus = |
| 411 | new SparseArray<SyncStatusInfo>(); |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 412 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 413 | private final ArrayList<SyncHistoryItem> mSyncHistory = |
| 414 | new ArrayList<SyncHistoryItem>(); |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 415 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 416 | private final RemoteCallbackList<ISyncStatusObserver> mChangeListeners |
| 417 | = new RemoteCallbackList<ISyncStatusObserver>(); |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 418 | |
Matthew Williams | 8ef2204 | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 419 | /** Reverse mapping for component name -> <userid -> target id>. */ |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 420 | private final ArrayMap<ComponentName, SparseArray<AuthorityInfo>> mServices = |
| 421 | new ArrayMap<ComponentName, SparseArray<AuthorityInfo>>(); |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 422 | |
Fred Quintana | 77c560f | 2010-03-29 22:20:26 -0700 | [diff] [blame] | 423 | private int mNextAuthorityId = 0; |
| 424 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 425 | // We keep 4 weeks of stats. |
| 426 | private final DayStats[] mDayStats = new DayStats[7*4]; |
| 427 | private final Calendar mCal; |
| 428 | private int mYear; |
| 429 | private int mYearInDays; |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 430 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 431 | private final Context mContext; |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 432 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 433 | private static volatile SyncStorageEngine sSyncStorageEngine = null; |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 434 | |
Ashish Sharma | 69d95de | 2012-04-11 17:27:24 -0700 | [diff] [blame] | 435 | private int mSyncRandomOffset; |
| 436 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 437 | /** |
| 438 | * This file contains the core engine state: all accounts and the |
| 439 | * settings for them. It must never be lost, and should be changed |
| 440 | * infrequently, so it is stored as an XML file. |
| 441 | */ |
| 442 | private final AtomicFile mAccountInfoFile; |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 443 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 444 | /** |
| 445 | * This file contains the current sync status. We would like to retain |
| 446 | * it across boots, but its loss is not the end of the world, so we store |
| 447 | * this information as binary data. |
| 448 | */ |
| 449 | private final AtomicFile mStatusFile; |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 450 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 451 | /** |
| 452 | * This file contains sync statistics. This is purely debugging information |
| 453 | * so is written infrequently and can be thrown away at any time. |
| 454 | */ |
| 455 | private final AtomicFile mStatisticsFile; |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 456 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 457 | private int mNextHistoryId = 0; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 458 | private SparseArray<Boolean> mMasterSyncAutomatically = new SparseArray<Boolean>(); |
Yameng Huang | 2b5d0ea | 2011-01-11 14:00:19 +0800 | [diff] [blame] | 459 | private boolean mDefaultMasterSyncAutomatically; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 460 | |
| 461 | private OnSyncRequestListener mSyncRequestListener; |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 462 | private OnAuthorityRemovedListener mAuthorityRemovedListener; |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 463 | |
Svet Ganov | 65712b0 | 2016-09-01 10:24:11 -0700 | [diff] [blame] | 464 | private boolean mGrantSyncAdaptersAccountAccess; |
| 465 | |
Makoto Onuki | 6963bea7 | 2017-12-12 10:42:39 -0800 | [diff] [blame^] | 466 | private final MyHandler mHandler; |
| 467 | |
| 468 | private SyncStorageEngine(Context context, File dataDir, Looper looper) { |
| 469 | mHandler = new MyHandler(looper); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 470 | mContext = context; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 471 | sSyncStorageEngine = this; |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 472 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 473 | mCal = Calendar.getInstance(TimeZone.getTimeZone("GMT+0")); |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 474 | |
Yameng Huang | 2b5d0ea | 2011-01-11 14:00:19 +0800 | [diff] [blame] | 475 | mDefaultMasterSyncAutomatically = mContext.getResources().getBoolean( |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 476 | com.android.internal.R.bool.config_syncstorageengine_masterSyncAutomatically); |
Yameng Huang | 2b5d0ea | 2011-01-11 14:00:19 +0800 | [diff] [blame] | 477 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 478 | File systemDir = new File(dataDir, "system"); |
| 479 | File syncDir = new File(systemDir, "sync"); |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 480 | syncDir.mkdirs(); |
Matthew Williams | ba35271 | 2013-08-13 15:53:31 -0700 | [diff] [blame] | 481 | |
| 482 | maybeDeleteLegacyPendingInfoLocked(syncDir); |
| 483 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 484 | mAccountInfoFile = new AtomicFile(new File(syncDir, "accounts.xml")); |
| 485 | mStatusFile = new AtomicFile(new File(syncDir, "status.bin")); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 486 | mStatisticsFile = new AtomicFile(new File(syncDir, "stats.bin")); |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 487 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 488 | readAccountInfoLocked(); |
| 489 | readStatusLocked(); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 490 | readStatisticsLocked(); |
Fred Quintana | 77c560f | 2010-03-29 22:20:26 -0700 | [diff] [blame] | 491 | readAndDeleteLegacyAccountInfoLocked(); |
| 492 | writeAccountInfoLocked(); |
| 493 | writeStatusLocked(); |
Fred Quintana | 77c560f | 2010-03-29 22:20:26 -0700 | [diff] [blame] | 494 | writeStatisticsLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | public static SyncStorageEngine newTestInstance(Context context) { |
Makoto Onuki | 6963bea7 | 2017-12-12 10:42:39 -0800 | [diff] [blame^] | 498 | return new SyncStorageEngine(context, context.getFilesDir(), Looper.getMainLooper()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 499 | } |
| 500 | |
Makoto Onuki | 6963bea7 | 2017-12-12 10:42:39 -0800 | [diff] [blame^] | 501 | public static void init(Context context, Looper looper) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 502 | if (sSyncStorageEngine != null) { |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 503 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 504 | } |
Jeff Sharkey | 8212ae0 | 2016-02-10 14:46:43 -0700 | [diff] [blame] | 505 | File dataDir = Environment.getDataDirectory(); |
Makoto Onuki | 6963bea7 | 2017-12-12 10:42:39 -0800 | [diff] [blame^] | 506 | sSyncStorageEngine = new SyncStorageEngine(context, dataDir, looper); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | public static SyncStorageEngine getSingleton() { |
| 510 | if (sSyncStorageEngine == null) { |
| 511 | throw new IllegalStateException("not initialized"); |
| 512 | } |
| 513 | return sSyncStorageEngine; |
| 514 | } |
| 515 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 516 | protected void setOnSyncRequestListener(OnSyncRequestListener listener) { |
| 517 | if (mSyncRequestListener == null) { |
| 518 | mSyncRequestListener = listener; |
| 519 | } |
| 520 | } |
| 521 | |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 522 | protected void setOnAuthorityRemovedListener(OnAuthorityRemovedListener listener) { |
| 523 | if (mAuthorityRemovedListener == null) { |
| 524 | mAuthorityRemovedListener = listener; |
| 525 | } |
| 526 | } |
| 527 | |
| 528 | protected void setPeriodicSyncAddedListener(PeriodicSyncAddedListener listener) { |
| 529 | if (mPeriodicSyncAddedListener == null) { |
| 530 | mPeriodicSyncAddedListener = listener; |
| 531 | } |
| 532 | } |
| 533 | |
Makoto Onuki | 6963bea7 | 2017-12-12 10:42:39 -0800 | [diff] [blame^] | 534 | private class MyHandler extends Handler { |
| 535 | public MyHandler(Looper looper) { |
| 536 | super(looper); |
| 537 | } |
| 538 | |
| 539 | @Override |
| 540 | public void handleMessage(Message msg) { |
| 541 | if (msg.what == MSG_WRITE_STATUS) { |
| 542 | synchronized (mAuthorities) { |
| 543 | writeStatusLocked(); |
| 544 | } |
| 545 | } else if (msg.what == MSG_WRITE_STATISTICS) { |
| 546 | synchronized (mAuthorities) { |
| 547 | writeStatisticsLocked(); |
| 548 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 549 | } |
| 550 | } |
| 551 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 552 | |
Ashish Sharma | 69d95de | 2012-04-11 17:27:24 -0700 | [diff] [blame] | 553 | public int getSyncRandomOffset() { |
| 554 | return mSyncRandomOffset; |
| 555 | } |
| 556 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 557 | public void addStatusChangeListener(int mask, ISyncStatusObserver callback) { |
| 558 | synchronized (mAuthorities) { |
| 559 | mChangeListeners.register(callback, mask); |
| 560 | } |
| 561 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 562 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 563 | public void removeStatusChangeListener(ISyncStatusObserver callback) { |
| 564 | synchronized (mAuthorities) { |
| 565 | mChangeListeners.unregister(callback); |
| 566 | } |
| 567 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 568 | |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 569 | /** |
| 570 | * Figure out a reasonable flex time for cases where none is provided (old api calls). |
| 571 | * @param syncTimeSeconds requested sync time from now. |
| 572 | * @return amount of seconds before syncTimeSeconds that the sync can occur. |
| 573 | * I.e. |
| 574 | * earliest_sync_time = syncTimeSeconds - calculateDefaultFlexTime(syncTimeSeconds) |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 575 | * The flex time is capped at a percentage of the {@link #DEFAULT_POLL_FREQUENCY_SECONDS}. |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 576 | */ |
| 577 | public static long calculateDefaultFlexTime(long syncTimeSeconds) { |
| 578 | if (syncTimeSeconds < DEFAULT_MIN_FLEX_ALLOWED_SECS) { |
| 579 | // Small enough sync request time that we don't add flex time - developer probably |
| 580 | // wants to wait for an operation to occur before syncing so we honour the |
| 581 | // request time. |
| 582 | return 0L; |
| 583 | } else if (syncTimeSeconds < DEFAULT_POLL_FREQUENCY_SECONDS) { |
| 584 | return (long) (syncTimeSeconds * DEFAULT_FLEX_PERCENT_SYNC); |
| 585 | } else { |
| 586 | // Large enough sync request time that we cap the flex time. |
| 587 | return (long) (DEFAULT_POLL_FREQUENCY_SECONDS * DEFAULT_FLEX_PERCENT_SYNC); |
| 588 | } |
| 589 | } |
| 590 | |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 591 | void reportChange(int which) { |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 592 | ArrayList<ISyncStatusObserver> reports = null; |
| 593 | synchronized (mAuthorities) { |
| 594 | int i = mChangeListeners.beginBroadcast(); |
| 595 | while (i > 0) { |
| 596 | i--; |
| 597 | Integer mask = (Integer)mChangeListeners.getBroadcastCookie(i); |
| 598 | if ((which & mask.intValue()) == 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 599 | continue; |
| 600 | } |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 601 | if (reports == null) { |
| 602 | reports = new ArrayList<ISyncStatusObserver>(i); |
| 603 | } |
| 604 | reports.add(mChangeListeners.getBroadcastItem(i)); |
| 605 | } |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 606 | mChangeListeners.finishBroadcast(); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 607 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 608 | |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 609 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 610 | Slog.v(TAG, "reportChange " + which + " to: " + reports); |
Fred Quintana | 77c560f | 2010-03-29 22:20:26 -0700 | [diff] [blame] | 611 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 612 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 613 | if (reports != null) { |
| 614 | int i = reports.size(); |
| 615 | while (i > 0) { |
| 616 | i--; |
| 617 | try { |
| 618 | reports.get(i).onStatusChanged(which); |
| 619 | } catch (RemoteException e) { |
| 620 | // The remote callback list will take care of this for us. |
| 621 | } |
| 622 | } |
| 623 | } |
| 624 | } |
Amith Yamasani | 70c874b | 2009-07-06 14:53:25 -0700 | [diff] [blame] | 625 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 626 | public boolean getSyncAutomatically(Account account, int userId, String providerName) { |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 627 | synchronized (mAuthorities) { |
| 628 | if (account != null) { |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 629 | AuthorityInfo authority = getAuthorityLocked( |
| 630 | new EndPoint(account, providerName, userId), |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 631 | "getSyncAutomatically"); |
| 632 | return authority != null && authority.enabled; |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 633 | } |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 634 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 635 | int i = mAuthorities.size(); |
| 636 | while (i > 0) { |
| 637 | i--; |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 638 | AuthorityInfo authorityInfo = mAuthorities.valueAt(i); |
Matthew Williams | 8ef2204 | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 639 | if (authorityInfo.target.matchesSpec(new EndPoint(account, providerName, userId)) |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 640 | && authorityInfo.enabled) { |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 641 | return true; |
| 642 | } |
| 643 | } |
| 644 | return false; |
| 645 | } |
| 646 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 647 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 648 | public void setSyncAutomatically(Account account, int userId, String providerName, |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 649 | boolean sync) { |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 650 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 651 | Slog.d(TAG, "setSyncAutomatically: " + /* account + */" provider " + providerName |
Dianne Hackborn | 40e9f29 | 2012-11-27 19:12:23 -0800 | [diff] [blame] | 652 | + ", user " + userId + " -> " + sync); |
| 653 | } |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 654 | synchronized (mAuthorities) { |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 655 | AuthorityInfo authority = |
| 656 | getOrCreateAuthorityLocked( |
| 657 | new EndPoint(account, providerName, userId), |
| 658 | -1 /* ident */, |
| 659 | false); |
Fred Quintana | 77c560f | 2010-03-29 22:20:26 -0700 | [diff] [blame] | 660 | if (authority.enabled == sync) { |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 661 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 662 | Slog.d(TAG, "setSyncAutomatically: already set to " + sync + ", doing nothing"); |
Dianne Hackborn | 40e9f29 | 2012-11-27 19:12:23 -0800 | [diff] [blame] | 663 | } |
Fred Quintana | 77c560f | 2010-03-29 22:20:26 -0700 | [diff] [blame] | 664 | return; |
| 665 | } |
Matthew Williams | 53abfdb | 2015-06-10 20:06:37 -0700 | [diff] [blame] | 666 | // If the adapter was syncable but missing its initialization sync, set it to |
| 667 | // uninitialized now. This is to give it a chance to run any one-time initialization |
| 668 | // logic. |
| 669 | if (sync && authority.syncable == AuthorityInfo.SYNCABLE_NOT_INITIALIZED) { |
| 670 | authority.syncable = AuthorityInfo.NOT_INITIALIZED; |
| 671 | } |
Joe Onorato | 8294fad | 2009-07-15 16:08:44 -0700 | [diff] [blame] | 672 | authority.enabled = sync; |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 673 | writeAccountInfoLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 674 | } |
Joe Onorato | 8294fad | 2009-07-15 16:08:44 -0700 | [diff] [blame] | 675 | |
Fred Quintana | 77c560f | 2010-03-29 22:20:26 -0700 | [diff] [blame] | 676 | if (sync) { |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 677 | requestSync(account, userId, SyncOperation.REASON_SYNC_AUTO, providerName, |
| 678 | new Bundle()); |
Joe Onorato | 8294fad | 2009-07-15 16:08:44 -0700 | [diff] [blame] | 679 | } |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 680 | reportChange(ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS); |
Marvin Paul | a653325 | 2014-11-24 12:57:48 -0800 | [diff] [blame] | 681 | queueBackup(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 682 | } |
| 683 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 684 | public int getIsSyncable(Account account, int userId, String providerName) { |
Fred Quintana | 5e787c4 | 2009-08-16 23:13:53 -0700 | [diff] [blame] | 685 | synchronized (mAuthorities) { |
| 686 | if (account != null) { |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 687 | AuthorityInfo authority = getAuthorityLocked( |
| 688 | new EndPoint(account, providerName, userId), |
| 689 | "get authority syncable"); |
Fred Quintana | 5e787c4 | 2009-08-16 23:13:53 -0700 | [diff] [blame] | 690 | if (authority == null) { |
Matthew Williams | 53abfdb | 2015-06-10 20:06:37 -0700 | [diff] [blame] | 691 | return AuthorityInfo.NOT_INITIALIZED; |
Fred Quintana | 5e787c4 | 2009-08-16 23:13:53 -0700 | [diff] [blame] | 692 | } |
| 693 | return authority.syncable; |
| 694 | } |
| 695 | |
| 696 | int i = mAuthorities.size(); |
| 697 | while (i > 0) { |
| 698 | i--; |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 699 | AuthorityInfo authorityInfo = mAuthorities.valueAt(i); |
Matthew Williams | 8ef2204 | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 700 | if (authorityInfo.target != null |
| 701 | && authorityInfo.target.provider.equals(providerName)) { |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 702 | return authorityInfo.syncable; |
Fred Quintana | 5e787c4 | 2009-08-16 23:13:53 -0700 | [diff] [blame] | 703 | } |
| 704 | } |
Matthew Williams | 53abfdb | 2015-06-10 20:06:37 -0700 | [diff] [blame] | 705 | return AuthorityInfo.NOT_INITIALIZED; |
Fred Quintana | 5e787c4 | 2009-08-16 23:13:53 -0700 | [diff] [blame] | 706 | } |
| 707 | } |
| 708 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 709 | public void setIsSyncable(Account account, int userId, String providerName, int syncable) { |
Matthew Williams | 8ef2204 | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 710 | setSyncableStateForEndPoint(new EndPoint(account, providerName, userId), syncable); |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 711 | } |
| 712 | |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 713 | /** |
| 714 | * An enabled sync service and a syncable provider's adapter both get resolved to the same |
| 715 | * persisted variable - namely the "syncable" attribute for an AuthorityInfo in accounts.xml. |
Matthew Williams | 7a2ab3a | 2013-09-11 14:25:51 -0700 | [diff] [blame] | 716 | * @param target target to set value for. |
| 717 | * @param syncable 0 indicates unsyncable, <0 unknown, >0 is active/syncable. |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 718 | */ |
Matthew Williams | 8ef2204 | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 719 | private void setSyncableStateForEndPoint(EndPoint target, int syncable) { |
Matthew Williams | 7a2ab3a | 2013-09-11 14:25:51 -0700 | [diff] [blame] | 720 | AuthorityInfo aInfo; |
| 721 | synchronized (mAuthorities) { |
| 722 | aInfo = getOrCreateAuthorityLocked(target, -1, false); |
Matthew Williams | 53abfdb | 2015-06-10 20:06:37 -0700 | [diff] [blame] | 723 | if (syncable < AuthorityInfo.NOT_INITIALIZED) { |
| 724 | syncable = AuthorityInfo.NOT_INITIALIZED; |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 725 | } |
Matthew Williams | 7a2ab3a | 2013-09-11 14:25:51 -0700 | [diff] [blame] | 726 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 727 | Slog.d(TAG, "setIsSyncable: " + aInfo.toString() + " -> " + syncable); |
Matthew Williams | 7a2ab3a | 2013-09-11 14:25:51 -0700 | [diff] [blame] | 728 | } |
| 729 | if (aInfo.syncable == syncable) { |
| 730 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 731 | Slog.d(TAG, "setIsSyncable: already set to " + syncable + ", doing nothing"); |
Matthew Williams | 7a2ab3a | 2013-09-11 14:25:51 -0700 | [diff] [blame] | 732 | } |
| 733 | return; |
| 734 | } |
| 735 | aInfo.syncable = syncable; |
| 736 | writeAccountInfoLocked(); |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 737 | } |
Matthew Williams | 53abfdb | 2015-06-10 20:06:37 -0700 | [diff] [blame] | 738 | if (syncable == AuthorityInfo.SYNCABLE) { |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 739 | requestSync(aInfo, SyncOperation.REASON_IS_SYNCABLE, new Bundle()); |
Fred Quintana | 5e787c4 | 2009-08-16 23:13:53 -0700 | [diff] [blame] | 740 | } |
| 741 | reportChange(ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS); |
| 742 | } |
| 743 | |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 744 | public Pair<Long, Long> getBackoff(EndPoint info) { |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 745 | synchronized (mAuthorities) { |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 746 | AuthorityInfo authority = getAuthorityLocked(info, "getBackoff"); |
| 747 | if (authority != null) { |
| 748 | return Pair.create(authority.backoffTime, authority.backoffDelay); |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 749 | } |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 750 | return null; |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 751 | } |
| 752 | } |
| 753 | |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 754 | /** |
| 755 | * Update the backoff for the given endpoint. The endpoint may be for a provider/account and |
| 756 | * the account or provider info be null, which signifies all accounts or providers. |
| 757 | */ |
| 758 | public void setBackoff(EndPoint info, long nextSyncTime, long nextDelay) { |
| 759 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 760 | Slog.v(TAG, "setBackoff: " + info |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 761 | + " -> nextSyncTime " + nextSyncTime + ", nextDelay " + nextDelay); |
| 762 | } |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 763 | boolean changed; |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 764 | synchronized (mAuthorities) { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 765 | if (info.account == null || info.provider == null) { |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 766 | // Do more work for a provider sync if the provided info has specified all |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 767 | // accounts/providers. |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 768 | changed = setBackoffLocked( |
| 769 | info.account /* may be null */, |
| 770 | info.userId, |
| 771 | info.provider /* may be null */, |
| 772 | nextSyncTime, nextDelay); |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 773 | } else { |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 774 | AuthorityInfo authorityInfo = |
| 775 | getOrCreateAuthorityLocked(info, -1 /* ident */, true); |
| 776 | if (authorityInfo.backoffTime == nextSyncTime |
| 777 | && authorityInfo.backoffDelay == nextDelay) { |
| 778 | changed = false; |
| 779 | } else { |
| 780 | authorityInfo.backoffTime = nextSyncTime; |
| 781 | authorityInfo.backoffDelay = nextDelay; |
| 782 | changed = true; |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 783 | } |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 784 | } |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 785 | } |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 786 | if (changed) { |
| 787 | reportChange(ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS); |
| 788 | } |
| 789 | } |
| 790 | |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 791 | /** |
| 792 | * Either set backoff for a specific authority, or set backoff for all the |
| 793 | * accounts on a specific adapter/all adapters. |
| 794 | * |
| 795 | * @param account account for which to set backoff. Null to specify all accounts. |
| 796 | * @param userId id of the user making this request. |
| 797 | * @param providerName provider for which to set backoff. Null to specify all providers. |
Matthew Williams | 7a2ab3a | 2013-09-11 14:25:51 -0700 | [diff] [blame] | 798 | * @return true if a change occured. |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 799 | */ |
| 800 | private boolean setBackoffLocked(Account account, int userId, String providerName, |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 801 | long nextSyncTime, long nextDelay) { |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 802 | boolean changed = false; |
| 803 | for (AccountInfo accountInfo : mAccounts.values()) { |
| 804 | if (account != null && !account.equals(accountInfo.accountAndUser.account) |
| 805 | && userId != accountInfo.accountAndUser.userId) { |
| 806 | continue; |
| 807 | } |
| 808 | for (AuthorityInfo authorityInfo : accountInfo.authorities.values()) { |
| 809 | if (providerName != null |
Matthew Williams | 8ef2204 | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 810 | && !providerName.equals(authorityInfo.target.provider)) { |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 811 | continue; |
| 812 | } |
| 813 | if (authorityInfo.backoffTime != nextSyncTime |
| 814 | || authorityInfo.backoffDelay != nextDelay) { |
| 815 | authorityInfo.backoffTime = nextSyncTime; |
| 816 | authorityInfo.backoffDelay = nextDelay; |
| 817 | changed = true; |
| 818 | } |
| 819 | } |
| 820 | } |
| 821 | return changed; |
| 822 | } |
| 823 | |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 824 | public void clearAllBackoffsLocked() { |
Alon Albert | 744e310f | 2010-12-14 11:37:20 -0800 | [diff] [blame] | 825 | boolean changed = false; |
| 826 | synchronized (mAuthorities) { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 827 | // Clear backoff for all sync adapters. |
| 828 | for (AccountInfo accountInfo : mAccounts.values()) { |
| 829 | for (AuthorityInfo authorityInfo : accountInfo.authorities.values()) { |
| 830 | if (authorityInfo.backoffTime != NOT_IN_BACKOFF_MODE |
| 831 | || authorityInfo.backoffDelay != NOT_IN_BACKOFF_MODE) { |
| 832 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 833 | Slog.v(TAG, "clearAllBackoffsLocked:" |
| 834 | + " authority:" + authorityInfo.target |
| 835 | + " account:" + accountInfo.accountAndUser.account.name |
| 836 | + " user:" + accountInfo.accountAndUser.userId |
| 837 | + " backoffTime was: " + authorityInfo.backoffTime |
| 838 | + " backoffDelay was: " + authorityInfo.backoffDelay); |
Alon Albert | 744e310f | 2010-12-14 11:37:20 -0800 | [diff] [blame] | 839 | } |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 840 | authorityInfo.backoffTime = NOT_IN_BACKOFF_MODE; |
| 841 | authorityInfo.backoffDelay = NOT_IN_BACKOFF_MODE; |
| 842 | changed = true; |
Alon Albert | 744e310f | 2010-12-14 11:37:20 -0800 | [diff] [blame] | 843 | } |
| 844 | } |
| 845 | } |
| 846 | } |
| 847 | |
| 848 | if (changed) { |
| 849 | reportChange(ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS); |
| 850 | } |
| 851 | } |
| 852 | |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 853 | public long getDelayUntilTime(EndPoint info) { |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 854 | synchronized (mAuthorities) { |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 855 | AuthorityInfo authority = getAuthorityLocked(info, "getDelayUntil"); |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 856 | if (authority == null) { |
| 857 | return 0; |
| 858 | } |
| 859 | return authority.delayUntil; |
| 860 | } |
| 861 | } |
| 862 | |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 863 | public void setDelayUntilTime(EndPoint info, long delayUntil) { |
| 864 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 865 | Slog.v(TAG, "setDelayUntil: " + info |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 866 | + " -> delayUntil " + delayUntil); |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 867 | } |
| 868 | synchronized (mAuthorities) { |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 869 | AuthorityInfo authority = getOrCreateAuthorityLocked(info, -1, true); |
| 870 | if (authority.delayUntil == delayUntil) { |
| 871 | return; |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 872 | } |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 873 | authority.delayUntil = delayUntil; |
| 874 | } |
| 875 | reportChange(ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS); |
| 876 | } |
| 877 | |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 878 | /** |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 879 | * Restore all periodic syncs read from persisted files. Used to restore periodic syncs |
| 880 | * after an OS update. |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 881 | */ |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 882 | boolean restoreAllPeriodicSyncs() { |
| 883 | if (mPeriodicSyncAddedListener == null) { |
| 884 | return false; |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 885 | } |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 886 | synchronized (mAuthorities) { |
| 887 | for (int i=0; i<mAuthorities.size(); i++) { |
| 888 | AuthorityInfo authority = mAuthorities.valueAt(i); |
| 889 | for (PeriodicSync periodicSync: authority.periodicSyncs) { |
| 890 | mPeriodicSyncAddedListener.onPeriodicSyncAdded(authority.target, |
| 891 | periodicSync.extras, periodicSync.period, periodicSync.flexTime); |
| 892 | } |
| 893 | authority.periodicSyncs.clear(); |
| 894 | } |
| 895 | writeAccountInfoLocked(); |
| 896 | } |
| 897 | return true; |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 898 | } |
| 899 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 900 | public void setMasterSyncAutomatically(boolean flag, int userId) { |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 901 | synchronized (mAuthorities) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 902 | Boolean auto = mMasterSyncAutomatically.get(userId); |
Matthew Williams | 8ef2204 | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 903 | if (auto != null && auto.equals(flag)) { |
Fred Quintana | 77c560f | 2010-03-29 22:20:26 -0700 | [diff] [blame] | 904 | return; |
| 905 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 906 | mMasterSyncAutomatically.put(userId, flag); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 907 | writeAccountInfoLocked(); |
| 908 | } |
Fred Quintana | 77c560f | 2010-03-29 22:20:26 -0700 | [diff] [blame] | 909 | if (flag) { |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 910 | requestSync(null, userId, SyncOperation.REASON_MASTER_SYNC_AUTO, null, |
| 911 | new Bundle()); |
Joe Onorato | 8294fad | 2009-07-15 16:08:44 -0700 | [diff] [blame] | 912 | } |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 913 | reportChange(ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS); |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 914 | mContext.sendBroadcast(ContentResolver.ACTION_SYNC_CONN_STATUS_CHANGED); |
Marvin Paul | a653325 | 2014-11-24 12:57:48 -0800 | [diff] [blame] | 915 | queueBackup(); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 916 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 917 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 918 | public boolean getMasterSyncAutomatically(int userId) { |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 919 | synchronized (mAuthorities) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 920 | Boolean auto = mMasterSyncAutomatically.get(userId); |
Yameng Huang | 2b5d0ea | 2011-01-11 14:00:19 +0800 | [diff] [blame] | 921 | return auto == null ? mDefaultMasterSyncAutomatically : auto; |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 922 | } |
| 923 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 924 | |
Makoto Onuki | b47e894 | 2017-09-18 14:03:03 -0700 | [diff] [blame] | 925 | public int getAuthorityCount() { |
| 926 | synchronized (mAuthorities) { |
| 927 | return mAuthorities.size(); |
| 928 | } |
| 929 | } |
| 930 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 931 | public AuthorityInfo getAuthority(int authorityId) { |
| 932 | synchronized (mAuthorities) { |
| 933 | return mAuthorities.get(authorityId); |
| 934 | } |
| 935 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 936 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 937 | /** |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 938 | * Returns true if there is currently a sync operation being actively processed for the given |
Matthew Williams | 8ef2204 | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 939 | * target. |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 940 | */ |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 941 | public boolean isSyncActive(EndPoint info) { |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 942 | synchronized (mAuthorities) { |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 943 | for (SyncInfo syncInfo : getCurrentSyncs(info.userId)) { |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 944 | AuthorityInfo ainfo = getAuthority(syncInfo.authorityId); |
Matthew Williams | 8ef2204 | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 945 | if (ainfo != null && ainfo.target.matchesSpec(info)) { |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 946 | return true; |
| 947 | } |
| 948 | } |
| 949 | } |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 950 | return false; |
| 951 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 952 | |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 953 | public void markPending(EndPoint info, boolean pendingValue) { |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 954 | synchronized (mAuthorities) { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 955 | AuthorityInfo authority = getOrCreateAuthorityLocked(info, |
| 956 | -1 /* desired identifier */, |
| 957 | true /* write accounts to storage */); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 958 | if (authority == null) { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 959 | return; |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 960 | } |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 961 | SyncStatusInfo status = getOrCreateSyncStatusLocked(authority.ident); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 962 | status.pending = pendingValue; |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 963 | } |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 964 | reportChange(ContentResolver.SYNC_OBSERVER_TYPE_PENDING); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 965 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 966 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 967 | /** |
| 968 | * Called when the set of account has changed, given the new array of |
| 969 | * active accounts. |
| 970 | */ |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 971 | public void doDatabaseCleanup(Account[] accounts, int userId) { |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 972 | synchronized (mAuthorities) { |
Matthew Williams | 8ef2204 | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 973 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 974 | Slog.v(TAG, "Updating for new accounts..."); |
Matthew Williams | 8ef2204 | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 975 | } |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 976 | SparseArray<AuthorityInfo> removing = new SparseArray<AuthorityInfo>(); |
| 977 | Iterator<AccountInfo> accIt = mAccounts.values().iterator(); |
| 978 | while (accIt.hasNext()) { |
| 979 | AccountInfo acc = accIt.next(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 980 | if (!ArrayUtils.contains(accounts, acc.accountAndUser.account) |
| 981 | && acc.accountAndUser.userId == userId) { |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 982 | // This account no longer exists... |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 983 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 984 | Slog.v(TAG, "Account removed: " + acc.accountAndUser); |
Fred Quintana | 77c560f | 2010-03-29 22:20:26 -0700 | [diff] [blame] | 985 | } |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 986 | for (AuthorityInfo auth : acc.authorities.values()) { |
| 987 | removing.put(auth.ident, auth); |
| 988 | } |
| 989 | accIt.remove(); |
| 990 | } |
| 991 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 992 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 993 | // Clean out all data structures. |
| 994 | int i = removing.size(); |
| 995 | if (i > 0) { |
| 996 | while (i > 0) { |
| 997 | i--; |
| 998 | int ident = removing.keyAt(i); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 999 | AuthorityInfo auth = removing.valueAt(i); |
| 1000 | if (mAuthorityRemovedListener != null) { |
| 1001 | mAuthorityRemovedListener.onAuthorityRemoved(auth.target); |
| 1002 | } |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1003 | mAuthorities.remove(ident); |
| 1004 | int j = mSyncStatus.size(); |
| 1005 | while (j > 0) { |
| 1006 | j--; |
| 1007 | if (mSyncStatus.keyAt(j) == ident) { |
| 1008 | mSyncStatus.remove(mSyncStatus.keyAt(j)); |
| 1009 | } |
| 1010 | } |
| 1011 | j = mSyncHistory.size(); |
| 1012 | while (j > 0) { |
| 1013 | j--; |
| 1014 | if (mSyncHistory.get(j).authorityId == ident) { |
| 1015 | mSyncHistory.remove(j); |
| 1016 | } |
| 1017 | } |
| 1018 | } |
| 1019 | writeAccountInfoLocked(); |
| 1020 | writeStatusLocked(); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1021 | writeStatisticsLocked(); |
| 1022 | } |
| 1023 | } |
| 1024 | } |
| 1025 | |
| 1026 | /** |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 1027 | * Called when a sync is starting. Supply a valid ActiveSyncContext with information |
| 1028 | * about the sync. |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1029 | */ |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 1030 | public SyncInfo addActiveSync(SyncManager.ActiveSyncContext activeSyncContext) { |
| 1031 | final SyncInfo syncInfo; |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1032 | synchronized (mAuthorities) { |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 1033 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1034 | Slog.v(TAG, "setActiveSync: account=" |
| 1035 | + " auth=" + activeSyncContext.mSyncOperation.target |
| 1036 | + " src=" + activeSyncContext.mSyncOperation.syncSource |
| 1037 | + " extras=" + activeSyncContext.mSyncOperation.extras); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1038 | } |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 1039 | final EndPoint info = activeSyncContext.mSyncOperation.target; |
| 1040 | AuthorityInfo authorityInfo = getOrCreateAuthorityLocked( |
| 1041 | info, |
Matthew Williams | 8ef2204 | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 1042 | -1 /* assign a new identifier if creating a new target */, |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 1043 | true /* write to storage if this results in a change */); |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 1044 | syncInfo = new SyncInfo( |
| 1045 | authorityInfo.ident, |
Matthew Williams | 8ef2204 | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 1046 | authorityInfo.target.account, |
| 1047 | authorityInfo.target.provider, |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 1048 | activeSyncContext.mStartTime); |
Matthew Williams | 8ef2204 | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 1049 | getCurrentSyncs(authorityInfo.target.userId).add(syncInfo); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1050 | } |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 1051 | reportActiveChange(); |
| 1052 | return syncInfo; |
| 1053 | } |
| 1054 | |
| 1055 | /** |
| 1056 | * Called to indicate that a previously active sync is no longer active. |
| 1057 | */ |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1058 | public void removeActiveSync(SyncInfo syncInfo, int userId) { |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 1059 | synchronized (mAuthorities) { |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 1060 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1061 | Slog.v(TAG, "removeActiveSync: account=" + syncInfo.account |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1062 | + " user=" + userId |
Matthew Williams | 5a9decd | 2014-06-04 09:25:11 -0700 | [diff] [blame] | 1063 | + " auth=" + syncInfo.authority); |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 1064 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1065 | getCurrentSyncs(userId).remove(syncInfo); |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 1066 | } |
| 1067 | |
| 1068 | reportActiveChange(); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1069 | } |
| 1070 | |
| 1071 | /** |
| 1072 | * To allow others to send active change reports, to poke clients. |
| 1073 | */ |
| 1074 | public void reportActiveChange() { |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 1075 | reportChange(ContentResolver.SYNC_OBSERVER_TYPE_ACTIVE); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1076 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 1077 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1078 | /** |
Matthew Williams | 8ef2204 | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 1079 | * Note that sync has started for the given operation. |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1080 | */ |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 1081 | public long insertStartSyncEvent(SyncOperation op, long now) { |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1082 | long id; |
| 1083 | synchronized (mAuthorities) { |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 1084 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1085 | Slog.v(TAG, "insertStartSyncEvent: " + op); |
Fred Quintana | 77c560f | 2010-03-29 22:20:26 -0700 | [diff] [blame] | 1086 | } |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 1087 | AuthorityInfo authority = getAuthorityLocked(op.target, "insertStartSyncEvent"); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1088 | if (authority == null) { |
| 1089 | return -1; |
| 1090 | } |
| 1091 | SyncHistoryItem item = new SyncHistoryItem(); |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 1092 | item.initialization = op.isInitialization(); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1093 | item.authorityId = authority.ident; |
| 1094 | item.historyId = mNextHistoryId++; |
| 1095 | if (mNextHistoryId < 0) mNextHistoryId = 0; |
| 1096 | item.eventTime = now; |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 1097 | item.source = op.syncSource; |
| 1098 | item.reason = op.reason; |
| 1099 | item.extras = op.extras; |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1100 | item.event = EVENT_START; |
| 1101 | mSyncHistory.add(0, item); |
| 1102 | while (mSyncHistory.size() > MAX_HISTORY) { |
| 1103 | mSyncHistory.remove(mSyncHistory.size()-1); |
| 1104 | } |
| 1105 | id = item.historyId; |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1106 | if (Log.isLoggable(TAG, Log.VERBOSE)) Slog.v(TAG, "returning historyId " + id); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1107 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 1108 | |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 1109 | reportChange(ContentResolver.SYNC_OBSERVER_TYPE_STATUS); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1110 | return id; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1111 | } |
| 1112 | |
Fred Quintana | 77c560f | 2010-03-29 22:20:26 -0700 | [diff] [blame] | 1113 | public void stopSyncEvent(long historyId, long elapsedTime, String resultMessage, |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1114 | long downstreamActivity, long upstreamActivity) { |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1115 | synchronized (mAuthorities) { |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 1116 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1117 | Slog.v(TAG, "stopSyncEvent: historyId=" + historyId); |
Fred Quintana | 77c560f | 2010-03-29 22:20:26 -0700 | [diff] [blame] | 1118 | } |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1119 | SyncHistoryItem item = null; |
| 1120 | int i = mSyncHistory.size(); |
| 1121 | while (i > 0) { |
| 1122 | i--; |
| 1123 | item = mSyncHistory.get(i); |
| 1124 | if (item.historyId == historyId) { |
| 1125 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1126 | } |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1127 | item = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1128 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 1129 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1130 | if (item == null) { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1131 | Slog.w(TAG, "stopSyncEvent: no history for id " + historyId); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1132 | return; |
| 1133 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 1134 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1135 | item.elapsedTime = elapsedTime; |
| 1136 | item.event = EVENT_STOP; |
| 1137 | item.mesg = resultMessage; |
| 1138 | item.downstreamActivity = downstreamActivity; |
| 1139 | item.upstreamActivity = upstreamActivity; |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 1140 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1141 | SyncStatusInfo status = getOrCreateSyncStatusLocked(item.authorityId); |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 1142 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1143 | status.numSyncs++; |
| 1144 | status.totalElapsedTime += elapsedTime; |
| 1145 | switch (item.source) { |
| 1146 | case SOURCE_LOCAL: |
| 1147 | status.numSourceLocal++; |
| 1148 | break; |
| 1149 | case SOURCE_POLL: |
| 1150 | status.numSourcePoll++; |
| 1151 | break; |
| 1152 | case SOURCE_USER: |
| 1153 | status.numSourceUser++; |
| 1154 | break; |
| 1155 | case SOURCE_SERVER: |
| 1156 | status.numSourceServer++; |
| 1157 | break; |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 1158 | case SOURCE_PERIODIC: |
| 1159 | status.numSourcePeriodic++; |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 1160 | break; |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1161 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 1162 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1163 | boolean writeStatisticsNow = false; |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 1164 | int day = getCurrentDayLocked(); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1165 | if (mDayStats[0] == null) { |
| 1166 | mDayStats[0] = new DayStats(day); |
| 1167 | } else if (day != mDayStats[0].day) { |
| 1168 | System.arraycopy(mDayStats, 0, mDayStats, 1, mDayStats.length-1); |
| 1169 | mDayStats[0] = new DayStats(day); |
| 1170 | writeStatisticsNow = true; |
| 1171 | } else if (mDayStats[0] == null) { |
| 1172 | } |
| 1173 | final DayStats ds = mDayStats[0]; |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 1174 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1175 | final long lastSyncTime = (item.eventTime + elapsedTime); |
| 1176 | boolean writeStatusNow = false; |
| 1177 | if (MESG_SUCCESS.equals(resultMessage)) { |
| 1178 | // - if successful, update the successful columns |
| 1179 | if (status.lastSuccessTime == 0 || status.lastFailureTime != 0) { |
| 1180 | writeStatusNow = true; |
| 1181 | } |
| 1182 | status.lastSuccessTime = lastSyncTime; |
| 1183 | status.lastSuccessSource = item.source; |
| 1184 | status.lastFailureTime = 0; |
| 1185 | status.lastFailureSource = -1; |
| 1186 | status.lastFailureMesg = null; |
| 1187 | status.initialFailureTime = 0; |
| 1188 | ds.successCount++; |
| 1189 | ds.successTime += elapsedTime; |
| 1190 | } else if (!MESG_CANCELED.equals(resultMessage)) { |
| 1191 | if (status.lastFailureTime == 0) { |
| 1192 | writeStatusNow = true; |
| 1193 | } |
| 1194 | status.lastFailureTime = lastSyncTime; |
| 1195 | status.lastFailureSource = item.source; |
| 1196 | status.lastFailureMesg = resultMessage; |
| 1197 | if (status.initialFailureTime == 0) { |
| 1198 | status.initialFailureTime = lastSyncTime; |
| 1199 | } |
| 1200 | ds.failureCount++; |
| 1201 | ds.failureTime += elapsedTime; |
| 1202 | } |
Makoto Onuki | 15e7a25 | 2017-06-08 17:12:05 -0700 | [diff] [blame] | 1203 | final StringBuilder event = new StringBuilder(); |
| 1204 | event.append("" + resultMessage + " Source=" + SyncStorageEngine.SOURCES[item.source] |
| 1205 | + " Elapsed="); |
| 1206 | SyncManager.formatDurationHMS(event, elapsedTime); |
| 1207 | event.append(" Reason="); |
| 1208 | event.append(SyncOperation.reasonToString(null, item.reason)); |
| 1209 | event.append(" Extras="); |
| 1210 | SyncOperation.extrasToStringBuilder(item.extras, event); |
| 1211 | |
| 1212 | status.addEvent(event.toString()); |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 1213 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1214 | if (writeStatusNow) { |
| 1215 | writeStatusLocked(); |
Makoto Onuki | 6963bea7 | 2017-12-12 10:42:39 -0800 | [diff] [blame^] | 1216 | } else if (!mHandler.hasMessages(MSG_WRITE_STATUS)) { |
| 1217 | mHandler.sendMessageDelayed(mHandler.obtainMessage(MSG_WRITE_STATUS), |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1218 | WRITE_STATUS_DELAY); |
| 1219 | } |
| 1220 | if (writeStatisticsNow) { |
| 1221 | writeStatisticsLocked(); |
Makoto Onuki | 6963bea7 | 2017-12-12 10:42:39 -0800 | [diff] [blame^] | 1222 | } else if (!mHandler.hasMessages(MSG_WRITE_STATISTICS)) { |
| 1223 | mHandler.sendMessageDelayed(mHandler.obtainMessage(MSG_WRITE_STATISTICS), |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1224 | WRITE_STATISTICS_DELAY); |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 1225 | } |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1226 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 1227 | |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 1228 | reportChange(ContentResolver.SYNC_OBSERVER_TYPE_STATUS); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1229 | } |
| 1230 | |
| 1231 | /** |
Matthew Williams | a7456e4 | 2013-11-12 14:41:02 -0800 | [diff] [blame] | 1232 | * Return a list of the currently active syncs. Note that the returned |
| 1233 | * items are the real, live active sync objects, so be careful what you do |
| 1234 | * with it. |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 1235 | */ |
Matthew Williams | a7456e4 | 2013-11-12 14:41:02 -0800 | [diff] [blame] | 1236 | private List<SyncInfo> getCurrentSyncs(int userId) { |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 1237 | synchronized (mAuthorities) { |
Matthew Williams | a7456e4 | 2013-11-12 14:41:02 -0800 | [diff] [blame] | 1238 | return getCurrentSyncsLocked(userId); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1239 | } |
| 1240 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 1241 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1242 | /** |
Matthew Williams | f39549e | 2016-01-19 23:04:04 +0000 | [diff] [blame] | 1243 | * @param userId Id of user to return current sync info. |
| 1244 | * @param canAccessAccounts Determines whether to redact Account information from the result. |
| 1245 | * @return a copy of the current syncs data structure. Will not return null. |
Matthew Williams | a7456e4 | 2013-11-12 14:41:02 -0800 | [diff] [blame] | 1246 | */ |
Matthew Williams | f39549e | 2016-01-19 23:04:04 +0000 | [diff] [blame] | 1247 | public List<SyncInfo> getCurrentSyncsCopy(int userId, boolean canAccessAccounts) { |
Matthew Williams | a7456e4 | 2013-11-12 14:41:02 -0800 | [diff] [blame] | 1248 | synchronized (mAuthorities) { |
| 1249 | final List<SyncInfo> syncs = getCurrentSyncsLocked(userId); |
| 1250 | final List<SyncInfo> syncsCopy = new ArrayList<SyncInfo>(); |
| 1251 | for (SyncInfo sync : syncs) { |
Matthew Williams | f39549e | 2016-01-19 23:04:04 +0000 | [diff] [blame] | 1252 | SyncInfo copy; |
| 1253 | if (!canAccessAccounts) { |
| 1254 | copy = SyncInfo.createAccountRedacted( |
| 1255 | sync.authorityId, sync.authority, sync.startTime); |
| 1256 | } else { |
| 1257 | copy = new SyncInfo(sync); |
| 1258 | } |
| 1259 | syncsCopy.add(copy); |
Matthew Williams | a7456e4 | 2013-11-12 14:41:02 -0800 | [diff] [blame] | 1260 | } |
| 1261 | return syncsCopy; |
| 1262 | } |
| 1263 | } |
| 1264 | |
| 1265 | private List<SyncInfo> getCurrentSyncsLocked(int userId) { |
| 1266 | ArrayList<SyncInfo> syncs = mCurrentSyncs.get(userId); |
| 1267 | if (syncs == null) { |
| 1268 | syncs = new ArrayList<SyncInfo>(); |
| 1269 | mCurrentSyncs.put(userId, syncs); |
| 1270 | } |
| 1271 | return syncs; |
| 1272 | } |
| 1273 | |
| 1274 | /** |
Matthew Williams | 8ef2204 | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 1275 | * Return a copy of the specified target with the corresponding sync status |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 1276 | */ |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 1277 | public Pair<AuthorityInfo, SyncStatusInfo> getCopyOfAuthorityWithSyncStatus(EndPoint info) { |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 1278 | synchronized (mAuthorities) { |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 1279 | AuthorityInfo authorityInfo = getOrCreateAuthorityLocked(info, |
Matthew Williams | 8ef2204 | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 1280 | -1 /* assign a new identifier if creating a new target */, |
Georgi Nikolov | dbe846b | 2013-06-25 14:09:56 -0700 | [diff] [blame] | 1281 | true /* write to storage if this results in a change */); |
| 1282 | return createCopyPairOfAuthorityWithSyncStatusLocked(authorityInfo); |
| 1283 | } |
| 1284 | } |
| 1285 | |
| 1286 | /** |
Matthew Williams | 8ef2204 | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 1287 | * Returns the status that matches the target. |
Costin Manolache | b752098 | 2009-09-02 18:03:05 -0700 | [diff] [blame] | 1288 | * |
Matthew Williams | 8ef2204 | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 1289 | * @param info the endpoint target we are querying status info for. |
| 1290 | * @return the SyncStatusInfo for the endpoint. |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1291 | */ |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 1292 | public SyncStatusInfo getStatusByAuthority(EndPoint info) { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1293 | if (info.account == null || info.provider == null) { |
Matthew Williams | d08d668 | 2013-10-14 10:39:41 -0700 | [diff] [blame] | 1294 | return null; |
Costin Manolache | b752098 | 2009-09-02 18:03:05 -0700 | [diff] [blame] | 1295 | } |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1296 | synchronized (mAuthorities) { |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1297 | final int N = mSyncStatus.size(); |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 1298 | for (int i = 0; i < N; i++) { |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 1299 | SyncStatusInfo cur = mSyncStatus.valueAt(i); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1300 | AuthorityInfo ainfo = mAuthorities.get(cur.authorityId); |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 1301 | if (ainfo != null |
Matthew Williams | 8ef2204 | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 1302 | && ainfo.target.matchesSpec(info)) { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1303 | return cur; |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1304 | } |
| 1305 | } |
Costin Manolache | b752098 | 2009-09-02 18:03:05 -0700 | [diff] [blame] | 1306 | return null; |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1307 | } |
| 1308 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 1309 | |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 1310 | /** Return true if the pending status is true of any matching authorities. */ |
| 1311 | public boolean isSyncPending(EndPoint info) { |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1312 | synchronized (mAuthorities) { |
| 1313 | final int N = mSyncStatus.size(); |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 1314 | for (int i = 0; i < N; i++) { |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 1315 | SyncStatusInfo cur = mSyncStatus.valueAt(i); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1316 | AuthorityInfo ainfo = mAuthorities.get(cur.authorityId); |
| 1317 | if (ainfo == null) { |
| 1318 | continue; |
| 1319 | } |
Matthew Williams | 8ef2204 | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 1320 | if (!ainfo.target.matchesSpec(info)) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1321 | continue; |
| 1322 | } |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 1323 | if (cur.pending) { |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1324 | return true; |
| 1325 | } |
| 1326 | } |
| 1327 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1328 | } |
| 1329 | } |
| 1330 | |
| 1331 | /** |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1332 | * Return an array of the current sync status for all authorities. Note |
| 1333 | * that the objects inside the array are the real, live status objects, |
| 1334 | * so be careful what you do with them. |
| 1335 | */ |
| 1336 | public ArrayList<SyncHistoryItem> getSyncHistory() { |
| 1337 | synchronized (mAuthorities) { |
| 1338 | final int N = mSyncHistory.size(); |
| 1339 | ArrayList<SyncHistoryItem> items = new ArrayList<SyncHistoryItem>(N); |
| 1340 | for (int i=0; i<N; i++) { |
| 1341 | items.add(mSyncHistory.get(i)); |
| 1342 | } |
| 1343 | return items; |
| 1344 | } |
| 1345 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 1346 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1347 | /** |
| 1348 | * Return an array of the current per-day statistics. Note |
| 1349 | * that the objects inside the array are the real, live status objects, |
| 1350 | * so be careful what you do with them. |
| 1351 | */ |
| 1352 | public DayStats[] getDayStatistics() { |
| 1353 | synchronized (mAuthorities) { |
| 1354 | DayStats[] ds = new DayStats[mDayStats.length]; |
| 1355 | System.arraycopy(mDayStats, 0, ds, 0, ds.length); |
| 1356 | return ds; |
| 1357 | } |
| 1358 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 1359 | |
Georgi Nikolov | dbe846b | 2013-06-25 14:09:56 -0700 | [diff] [blame] | 1360 | private Pair<AuthorityInfo, SyncStatusInfo> createCopyPairOfAuthorityWithSyncStatusLocked( |
| 1361 | AuthorityInfo authorityInfo) { |
| 1362 | SyncStatusInfo syncStatusInfo = getOrCreateSyncStatusLocked(authorityInfo.ident); |
| 1363 | return Pair.create(new AuthorityInfo(authorityInfo), new SyncStatusInfo(syncStatusInfo)); |
| 1364 | } |
| 1365 | |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 1366 | private int getCurrentDayLocked() { |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1367 | mCal.setTimeInMillis(System.currentTimeMillis()); |
| 1368 | final int dayOfYear = mCal.get(Calendar.DAY_OF_YEAR); |
| 1369 | if (mYear != mCal.get(Calendar.YEAR)) { |
| 1370 | mYear = mCal.get(Calendar.YEAR); |
| 1371 | mCal.clear(); |
| 1372 | mCal.set(Calendar.YEAR, mYear); |
| 1373 | mYearInDays = (int)(mCal.getTimeInMillis()/86400000); |
| 1374 | } |
| 1375 | return dayOfYear + mYearInDays; |
| 1376 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 1377 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1378 | /** |
Matthew Williams | 8ef2204 | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 1379 | * Retrieve a target's full info, returning null if one does not exist. |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 1380 | * |
Matthew Williams | 8ef2204 | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 1381 | * @param info info of the target to look up. |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1382 | * @param tag If non-null, this will be used in a log message if the |
Matthew Williams | 8ef2204 | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 1383 | * requested target does not exist. |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1384 | */ |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 1385 | private AuthorityInfo getAuthorityLocked(EndPoint info, String tag) { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1386 | AccountAndUser au = new AccountAndUser(info.account, info.userId); |
| 1387 | AccountInfo accountInfo = mAccounts.get(au); |
| 1388 | if (accountInfo == null) { |
| 1389 | if (tag != null) { |
| 1390 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1391 | Slog.v(TAG, tag + ": unknown account " + au); |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 1392 | } |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 1393 | } |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1394 | return null; |
| 1395 | } |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1396 | AuthorityInfo authority = accountInfo.authorities.get(info.provider); |
| 1397 | if (authority == null) { |
| 1398 | if (tag != null) { |
| 1399 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1400 | Slog.v(TAG, tag + ": unknown provider " + info.provider); |
| 1401 | } |
| 1402 | } |
| 1403 | return null; |
| 1404 | } |
| 1405 | return authority; |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1406 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 1407 | |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 1408 | /** |
Matthew Williams | 8ef2204 | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 1409 | * @param info info identifying target. |
| 1410 | * @param ident unique identifier for target. -1 for none. |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 1411 | * @param doWrite if true, update the accounts.xml file on the disk. |
Matthew Williams | 8ef2204 | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 1412 | * @return the authority that corresponds to the provided sync target, creating it if none |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 1413 | * exists. |
| 1414 | */ |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 1415 | private AuthorityInfo getOrCreateAuthorityLocked(EndPoint info, int ident, boolean doWrite) { |
| 1416 | AuthorityInfo authority = null; |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1417 | AccountAndUser au = new AccountAndUser(info.account, info.userId); |
| 1418 | AccountInfo account = mAccounts.get(au); |
| 1419 | if (account == null) { |
| 1420 | account = new AccountInfo(au); |
| 1421 | mAccounts.put(au, account); |
| 1422 | } |
| 1423 | authority = account.authorities.get(info.provider); |
| 1424 | if (authority == null) { |
| 1425 | authority = createAuthorityLocked(info, ident, doWrite); |
| 1426 | account.authorities.put(info.provider, authority); |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 1427 | } |
| 1428 | return authority; |
| 1429 | } |
| 1430 | |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 1431 | private AuthorityInfo createAuthorityLocked(EndPoint info, int ident, boolean doWrite) { |
| 1432 | AuthorityInfo authority; |
| 1433 | if (ident < 0) { |
| 1434 | ident = mNextAuthorityId; |
| 1435 | mNextAuthorityId++; |
| 1436 | doWrite = true; |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1437 | } |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 1438 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1439 | Slog.v(TAG, "created a new AuthorityInfo for " + info); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1440 | } |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 1441 | authority = new AuthorityInfo(info, ident); |
| 1442 | mAuthorities.put(ident, authority); |
| 1443 | if (doWrite) { |
| 1444 | writeAccountInfoLocked(); |
| 1445 | } |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1446 | return authority; |
| 1447 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 1448 | |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 1449 | public void removeAuthority(EndPoint info) { |
| 1450 | synchronized (mAuthorities) { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1451 | removeAuthorityLocked(info.account, info.userId, info.provider, true /* doWrite */); |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 1452 | } |
| 1453 | } |
| 1454 | |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1455 | |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 1456 | /** |
| 1457 | * Remove an authority associated with a provider. Needs to be a standalone function for |
| 1458 | * backward compatibility. |
| 1459 | */ |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1460 | private void removeAuthorityLocked(Account account, int userId, String authorityName, |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1461 | boolean doWrite) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1462 | AccountInfo accountInfo = mAccounts.get(new AccountAndUser(account, userId)); |
Fred Quintana | 7620f1a | 2010-03-16 15:58:44 -0700 | [diff] [blame] | 1463 | if (accountInfo != null) { |
Fred Quintana | fb08440 | 2010-03-23 17:57:03 -0700 | [diff] [blame] | 1464 | final AuthorityInfo authorityInfo = accountInfo.authorities.remove(authorityName); |
| 1465 | if (authorityInfo != null) { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1466 | if (mAuthorityRemovedListener != null) { |
| 1467 | mAuthorityRemovedListener.onAuthorityRemoved(authorityInfo.target); |
| 1468 | } |
Fred Quintana | fb08440 | 2010-03-23 17:57:03 -0700 | [diff] [blame] | 1469 | mAuthorities.remove(authorityInfo.ident); |
Fred Quintana | 77c560f | 2010-03-29 22:20:26 -0700 | [diff] [blame] | 1470 | if (doWrite) { |
| 1471 | writeAccountInfoLocked(); |
| 1472 | } |
Fred Quintana | 7620f1a | 2010-03-16 15:58:44 -0700 | [diff] [blame] | 1473 | } |
| 1474 | } |
| 1475 | } |
| 1476 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1477 | private SyncStatusInfo getOrCreateSyncStatusLocked(int authorityId) { |
| 1478 | SyncStatusInfo status = mSyncStatus.get(authorityId); |
| 1479 | if (status == null) { |
| 1480 | status = new SyncStatusInfo(authorityId); |
| 1481 | mSyncStatus.put(authorityId, status); |
| 1482 | } |
| 1483 | return status; |
| 1484 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 1485 | |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 1486 | public void writeAllState() { |
| 1487 | synchronized (mAuthorities) { |
| 1488 | // Account info is always written so no need to do it here. |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 1489 | writeStatusLocked(); |
| 1490 | writeStatisticsLocked(); |
| 1491 | } |
| 1492 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 1493 | |
Svet Ganov | 65712b0 | 2016-09-01 10:24:11 -0700 | [diff] [blame] | 1494 | public boolean shouldGrantSyncAdaptersAccountAccess() { |
| 1495 | return mGrantSyncAdaptersAccountAccess; |
| 1496 | } |
| 1497 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1498 | /** |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 1499 | * public for testing |
| 1500 | */ |
| 1501 | public void clearAndReadState() { |
| 1502 | synchronized (mAuthorities) { |
| 1503 | mAuthorities.clear(); |
| 1504 | mAccounts.clear(); |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 1505 | mServices.clear(); |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 1506 | mSyncStatus.clear(); |
| 1507 | mSyncHistory.clear(); |
| 1508 | |
| 1509 | readAccountInfoLocked(); |
| 1510 | readStatusLocked(); |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 1511 | readStatisticsLocked(); |
Fred Quintana | 77c560f | 2010-03-29 22:20:26 -0700 | [diff] [blame] | 1512 | readAndDeleteLegacyAccountInfoLocked(); |
| 1513 | writeAccountInfoLocked(); |
| 1514 | writeStatusLocked(); |
Fred Quintana | 77c560f | 2010-03-29 22:20:26 -0700 | [diff] [blame] | 1515 | writeStatisticsLocked(); |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 1516 | } |
| 1517 | } |
| 1518 | |
| 1519 | /** |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1520 | * Read all account information back in to the initial engine state. |
| 1521 | */ |
| 1522 | private void readAccountInfoLocked() { |
Fred Quintana | 77c560f | 2010-03-29 22:20:26 -0700 | [diff] [blame] | 1523 | int highestAuthorityId = -1; |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1524 | FileInputStream fis = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1525 | try { |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1526 | fis = mAccountInfoFile.openRead(); |
Matthew Williams | ba35271 | 2013-08-13 15:53:31 -0700 | [diff] [blame] | 1527 | if (Log.isLoggable(TAG_FILE, Log.VERBOSE)) { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1528 | Slog.v(TAG_FILE, "Reading " + mAccountInfoFile.getBaseFile()); |
Matthew Williams | ba35271 | 2013-08-13 15:53:31 -0700 | [diff] [blame] | 1529 | } |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1530 | XmlPullParser parser = Xml.newPullParser(); |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 1531 | parser.setInput(fis, StandardCharsets.UTF_8.name()); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1532 | int eventType = parser.getEventType(); |
Narayan Kamath | 2ac3cb7 | 2014-01-06 11:31:35 +0000 | [diff] [blame] | 1533 | while (eventType != XmlPullParser.START_TAG && |
| 1534 | eventType != XmlPullParser.END_DOCUMENT) { |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1535 | eventType = parser.next(); |
| 1536 | } |
Narayan Kamath | 2ac3cb7 | 2014-01-06 11:31:35 +0000 | [diff] [blame] | 1537 | if (eventType == XmlPullParser.END_DOCUMENT) { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1538 | Slog.i(TAG, "No initial accounts"); |
Narayan Kamath | 2ac3cb7 | 2014-01-06 11:31:35 +0000 | [diff] [blame] | 1539 | return; |
| 1540 | } |
| 1541 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1542 | String tagName = parser.getName(); |
| 1543 | if ("accounts".equals(tagName)) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1544 | String listen = parser.getAttributeValue(null, XML_ATTR_LISTEN_FOR_TICKLES); |
Fred Quintana | c2e4691 | 2010-03-15 16:10:44 -0700 | [diff] [blame] | 1545 | String versionString = parser.getAttributeValue(null, "version"); |
| 1546 | int version; |
| 1547 | try { |
| 1548 | version = (versionString == null) ? 0 : Integer.parseInt(versionString); |
| 1549 | } catch (NumberFormatException e) { |
| 1550 | version = 0; |
| 1551 | } |
Svet Ganov | 65712b0 | 2016-09-01 10:24:11 -0700 | [diff] [blame] | 1552 | |
| 1553 | if (version < 3) { |
| 1554 | mGrantSyncAdaptersAccountAccess = true; |
| 1555 | } |
| 1556 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1557 | String nextIdString = parser.getAttributeValue(null, XML_ATTR_NEXT_AUTHORITY_ID); |
Fred Quintana | 77c560f | 2010-03-29 22:20:26 -0700 | [diff] [blame] | 1558 | try { |
| 1559 | int id = (nextIdString == null) ? 0 : Integer.parseInt(nextIdString); |
| 1560 | mNextAuthorityId = Math.max(mNextAuthorityId, id); |
| 1561 | } catch (NumberFormatException e) { |
| 1562 | // don't care |
Fred Quintana | c2e4691 | 2010-03-15 16:10:44 -0700 | [diff] [blame] | 1563 | } |
Ashish Sharma | 69d95de | 2012-04-11 17:27:24 -0700 | [diff] [blame] | 1564 | String offsetString = parser.getAttributeValue(null, XML_ATTR_SYNC_RANDOM_OFFSET); |
| 1565 | try { |
| 1566 | mSyncRandomOffset = (offsetString == null) ? 0 : Integer.parseInt(offsetString); |
| 1567 | } catch (NumberFormatException e) { |
| 1568 | mSyncRandomOffset = 0; |
| 1569 | } |
| 1570 | if (mSyncRandomOffset == 0) { |
| 1571 | Random random = new Random(System.currentTimeMillis()); |
| 1572 | mSyncRandomOffset = random.nextInt(86400); |
| 1573 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1574 | mMasterSyncAutomatically.put(0, listen == null || Boolean.parseBoolean(listen)); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1575 | eventType = parser.next(); |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 1576 | AuthorityInfo authority = null; |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 1577 | PeriodicSync periodicSync = null; |
Suprabh Shukla | 042a478 | 2017-05-12 15:26:54 -0700 | [diff] [blame] | 1578 | AccountAuthorityValidator validator = new AccountAuthorityValidator(mContext); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1579 | do { |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 1580 | if (eventType == XmlPullParser.START_TAG) { |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1581 | tagName = parser.getName(); |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 1582 | if (parser.getDepth() == 2) { |
| 1583 | if ("authority".equals(tagName)) { |
Suprabh Shukla | 042a478 | 2017-05-12 15:26:54 -0700 | [diff] [blame] | 1584 | authority = parseAuthority(parser, version, validator); |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 1585 | periodicSync = null; |
Shreyas Basarge | 11cb4e2 | 2016-01-13 14:27:16 +0000 | [diff] [blame] | 1586 | if (authority != null) { |
| 1587 | if (authority.ident > highestAuthorityId) { |
| 1588 | highestAuthorityId = authority.ident; |
| 1589 | } |
Shreyas Basarge | bae9ded | 2016-02-17 13:54:44 +0000 | [diff] [blame] | 1590 | } else { |
| 1591 | EventLog.writeEvent(0x534e4554, "26513719", -1, |
| 1592 | "Malformed authority"); |
Fred Quintana | 77c560f | 2010-03-29 22:20:26 -0700 | [diff] [blame] | 1593 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1594 | } else if (XML_TAG_LISTEN_FOR_TICKLES.equals(tagName)) { |
| 1595 | parseListenForTickles(parser); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1596 | } |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 1597 | } else if (parser.getDepth() == 3) { |
| 1598 | if ("periodicSync".equals(tagName) && authority != null) { |
| 1599 | periodicSync = parsePeriodicSync(parser, authority); |
| 1600 | } |
| 1601 | } else if (parser.getDepth() == 4 && periodicSync != null) { |
| 1602 | if ("extra".equals(tagName)) { |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 1603 | parseExtra(parser, periodicSync.extras); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1604 | } |
| 1605 | } |
| 1606 | } |
| 1607 | eventType = parser.next(); |
| 1608 | } while (eventType != XmlPullParser.END_DOCUMENT); |
| 1609 | } |
| 1610 | } catch (XmlPullParserException e) { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1611 | Slog.w(TAG, "Error reading accounts", e); |
Fred Quintana | c2e4691 | 2010-03-15 16:10:44 -0700 | [diff] [blame] | 1612 | return; |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1613 | } catch (java.io.IOException e) { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1614 | if (fis == null) Slog.i(TAG, "No initial accounts"); |
| 1615 | else Slog.w(TAG, "Error reading accounts", e); |
Fred Quintana | c2e4691 | 2010-03-15 16:10:44 -0700 | [diff] [blame] | 1616 | return; |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1617 | } finally { |
Fred Quintana | 77c560f | 2010-03-29 22:20:26 -0700 | [diff] [blame] | 1618 | mNextAuthorityId = Math.max(highestAuthorityId + 1, mNextAuthorityId); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1619 | if (fis != null) { |
| 1620 | try { |
| 1621 | fis.close(); |
| 1622 | } catch (java.io.IOException e1) { |
| 1623 | } |
| 1624 | } |
| 1625 | } |
Fred Quintana | c2e4691 | 2010-03-15 16:10:44 -0700 | [diff] [blame] | 1626 | |
Fred Quintana | 77c560f | 2010-03-29 22:20:26 -0700 | [diff] [blame] | 1627 | maybeMigrateSettingsForRenamedAuthorities(); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1628 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 1629 | |
Fred Quintana | fb08440 | 2010-03-23 17:57:03 -0700 | [diff] [blame] | 1630 | /** |
Matthew Williams | ba35271 | 2013-08-13 15:53:31 -0700 | [diff] [blame] | 1631 | * Ensure the old pending.bin is deleted, as it has been changed to pending.xml. |
| 1632 | * pending.xml was used starting in KLP. |
| 1633 | * @param syncDir directory where the sync files are located. |
| 1634 | */ |
| 1635 | private void maybeDeleteLegacyPendingInfoLocked(File syncDir) { |
| 1636 | File file = new File(syncDir, "pending.bin"); |
| 1637 | if (!file.exists()) { |
| 1638 | return; |
| 1639 | } else { |
| 1640 | file.delete(); |
| 1641 | } |
| 1642 | } |
| 1643 | |
| 1644 | /** |
Fred Quintana | fb08440 | 2010-03-23 17:57:03 -0700 | [diff] [blame] | 1645 | * some authority names have changed. copy over their settings and delete the old ones |
| 1646 | * @return true if a change was made |
| 1647 | */ |
| 1648 | private boolean maybeMigrateSettingsForRenamedAuthorities() { |
| 1649 | boolean writeNeeded = false; |
| 1650 | |
| 1651 | ArrayList<AuthorityInfo> authoritiesToRemove = new ArrayList<AuthorityInfo>(); |
| 1652 | final int N = mAuthorities.size(); |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 1653 | for (int i = 0; i < N; i++) { |
Fred Quintana | fb08440 | 2010-03-23 17:57:03 -0700 | [diff] [blame] | 1654 | AuthorityInfo authority = mAuthorities.valueAt(i); |
| 1655 | // skip this authority if it isn't one of the renamed ones |
Matthew Williams | 8ef2204 | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 1656 | final String newAuthorityName = sAuthorityRenames.get(authority.target.provider); |
Fred Quintana | fb08440 | 2010-03-23 17:57:03 -0700 | [diff] [blame] | 1657 | if (newAuthorityName == null) { |
| 1658 | continue; |
| 1659 | } |
| 1660 | |
| 1661 | // remember this authority so we can remove it later. we can't remove it |
| 1662 | // now without messing up this loop iteration |
| 1663 | authoritiesToRemove.add(authority); |
| 1664 | |
| 1665 | // this authority isn't enabled, no need to copy it to the new authority name since |
| 1666 | // the default is "disabled" |
| 1667 | if (!authority.enabled) { |
| 1668 | continue; |
| 1669 | } |
| 1670 | |
| 1671 | // if we already have a record of this new authority then don't copy over the settings |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 1672 | EndPoint newInfo = |
Matthew Williams | 8ef2204 | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 1673 | new EndPoint(authority.target.account, |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 1674 | newAuthorityName, |
Matthew Williams | 8ef2204 | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 1675 | authority.target.userId); |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 1676 | if (getAuthorityLocked(newInfo, "cleanup") != null) { |
Fred Quintana | fb08440 | 2010-03-23 17:57:03 -0700 | [diff] [blame] | 1677 | continue; |
| 1678 | } |
| 1679 | |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 1680 | AuthorityInfo newAuthority = |
| 1681 | getOrCreateAuthorityLocked(newInfo, -1 /* ident */, false /* doWrite */); |
Fred Quintana | fb08440 | 2010-03-23 17:57:03 -0700 | [diff] [blame] | 1682 | newAuthority.enabled = true; |
| 1683 | writeNeeded = true; |
| 1684 | } |
| 1685 | |
| 1686 | for (AuthorityInfo authorityInfo : authoritiesToRemove) { |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 1687 | removeAuthorityLocked( |
Matthew Williams | 8ef2204 | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 1688 | authorityInfo.target.account, |
| 1689 | authorityInfo.target.userId, |
| 1690 | authorityInfo.target.provider, |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 1691 | false /* doWrite */); |
Fred Quintana | fb08440 | 2010-03-23 17:57:03 -0700 | [diff] [blame] | 1692 | writeNeeded = true; |
| 1693 | } |
| 1694 | |
| 1695 | return writeNeeded; |
| 1696 | } |
| 1697 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1698 | private void parseListenForTickles(XmlPullParser parser) { |
| 1699 | String user = parser.getAttributeValue(null, XML_ATTR_USER); |
| 1700 | int userId = 0; |
| 1701 | try { |
| 1702 | userId = Integer.parseInt(user); |
| 1703 | } catch (NumberFormatException e) { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1704 | Slog.e(TAG, "error parsing the user for listen-for-tickles", e); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1705 | } catch (NullPointerException e) { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1706 | Slog.e(TAG, "the user in listen-for-tickles is null", e); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1707 | } |
| 1708 | String enabled = parser.getAttributeValue(null, XML_ATTR_ENABLED); |
| 1709 | boolean listen = enabled == null || Boolean.parseBoolean(enabled); |
| 1710 | mMasterSyncAutomatically.put(userId, listen); |
| 1711 | } |
| 1712 | |
Suprabh Shukla | 042a478 | 2017-05-12 15:26:54 -0700 | [diff] [blame] | 1713 | private AuthorityInfo parseAuthority(XmlPullParser parser, int version, |
| 1714 | AccountAuthorityValidator validator) { |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 1715 | AuthorityInfo authority = null; |
| 1716 | int id = -1; |
| 1717 | try { |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 1718 | id = Integer.parseInt(parser.getAttributeValue(null, "id")); |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 1719 | } catch (NumberFormatException e) { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1720 | Slog.e(TAG, "error parsing the id of the authority", e); |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 1721 | } catch (NullPointerException e) { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1722 | Slog.e(TAG, "the id of the authority is null", e); |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 1723 | } |
| 1724 | if (id >= 0) { |
Fred Quintana | fb08440 | 2010-03-23 17:57:03 -0700 | [diff] [blame] | 1725 | String authorityName = parser.getAttributeValue(null, "authority"); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1726 | String enabled = parser.getAttributeValue(null, XML_ATTR_ENABLED); |
Fred Quintana | fb08440 | 2010-03-23 17:57:03 -0700 | [diff] [blame] | 1727 | String syncable = parser.getAttributeValue(null, "syncable"); |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 1728 | String accountName = parser.getAttributeValue(null, "account"); |
| 1729 | String accountType = parser.getAttributeValue(null, "type"); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1730 | String user = parser.getAttributeValue(null, XML_ATTR_USER); |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 1731 | String packageName = parser.getAttributeValue(null, "package"); |
| 1732 | String className = parser.getAttributeValue(null, "class"); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1733 | int userId = user == null ? 0 : Integer.parseInt(user); |
Matthew Williams | 8ef2204 | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 1734 | if (accountType == null && packageName == null) { |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 1735 | accountType = "com.google"; |
Matthew Williams | 53abfdb | 2015-06-10 20:06:37 -0700 | [diff] [blame] | 1736 | syncable = String.valueOf(AuthorityInfo.NOT_INITIALIZED); |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 1737 | } |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 1738 | authority = mAuthorities.get(id); |
Matthew Williams | ba35271 | 2013-08-13 15:53:31 -0700 | [diff] [blame] | 1739 | if (Log.isLoggable(TAG_FILE, Log.VERBOSE)) { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1740 | Slog.v(TAG_FILE, "Adding authority:" |
Matthew Williams | 8ef2204 | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 1741 | + " account=" + accountName |
| 1742 | + " accountType=" + accountType |
| 1743 | + " auth=" + authorityName |
| 1744 | + " package=" + packageName |
| 1745 | + " class=" + className |
Matthew Williams | ba35271 | 2013-08-13 15:53:31 -0700 | [diff] [blame] | 1746 | + " user=" + userId |
| 1747 | + " enabled=" + enabled |
| 1748 | + " syncable=" + syncable); |
| 1749 | } |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 1750 | if (authority == null) { |
Matthew Williams | ba35271 | 2013-08-13 15:53:31 -0700 | [diff] [blame] | 1751 | if (Log.isLoggable(TAG_FILE, Log.VERBOSE)) { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1752 | Slog.v(TAG_FILE, "Creating authority entry"); |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 1753 | } |
Matthew Williams | 8ef2204 | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 1754 | if (accountName != null && authorityName != null) { |
Suprabh Shukla | 042a478 | 2017-05-12 15:26:54 -0700 | [diff] [blame] | 1755 | EndPoint info = new EndPoint( |
Matthew Williams | 8ef2204 | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 1756 | new Account(accountName, accountType), |
| 1757 | authorityName, userId); |
Suprabh Shukla | 042a478 | 2017-05-12 15:26:54 -0700 | [diff] [blame] | 1758 | if (validator.isAccountValid(info.account, userId) |
| 1759 | && validator.isAuthorityValid(authorityName, userId)) { |
| 1760 | authority = getOrCreateAuthorityLocked(info, id, false); |
| 1761 | // If the version is 0 then we are upgrading from a file format that did not |
| 1762 | // know about periodic syncs. In that case don't clear the list since we |
| 1763 | // want the default, which is a daily periodic sync. |
| 1764 | // Otherwise clear out this default list since we will populate it later |
| 1765 | // with |
| 1766 | // the periodic sync descriptions that are read from the configuration file. |
| 1767 | if (version > 0) { |
| 1768 | authority.periodicSyncs.clear(); |
| 1769 | } |
| 1770 | } else { |
| 1771 | EventLog.writeEvent(0x534e4554, "35028827", -1, |
| 1772 | "account:" + info.account + " provider:" + authorityName + " user:" |
| 1773 | + userId); |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 1774 | } |
Fred Quintana | c2e4691 | 2010-03-15 16:10:44 -0700 | [diff] [blame] | 1775 | } |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 1776 | } |
| 1777 | if (authority != null) { |
| 1778 | authority.enabled = enabled == null || Boolean.parseBoolean(enabled); |
Matthew Williams | 53abfdb | 2015-06-10 20:06:37 -0700 | [diff] [blame] | 1779 | try { |
| 1780 | authority.syncable = (syncable == null) ? |
| 1781 | AuthorityInfo.NOT_INITIALIZED : Integer.parseInt(syncable); |
| 1782 | } catch (NumberFormatException e) { |
| 1783 | // On L we stored this as {"unknown", "true", "false"} so fall back to this |
| 1784 | // format. |
| 1785 | if ("unknown".equals(syncable)) { |
| 1786 | authority.syncable = AuthorityInfo.NOT_INITIALIZED; |
| 1787 | } else { |
| 1788 | authority.syncable = Boolean.parseBoolean(syncable) ? |
| 1789 | AuthorityInfo.SYNCABLE : AuthorityInfo.NOT_SYNCABLE; |
| 1790 | } |
| 1791 | |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 1792 | } |
| 1793 | } else { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1794 | Slog.w(TAG, "Failure adding authority: account=" |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 1795 | + accountName + " auth=" + authorityName |
| 1796 | + " enabled=" + enabled |
| 1797 | + " syncable=" + syncable); |
| 1798 | } |
| 1799 | } |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 1800 | return authority; |
| 1801 | } |
| 1802 | |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 1803 | /** |
| 1804 | * Parse a periodic sync from accounts.xml. Sets the bundle to be empty. |
| 1805 | */ |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 1806 | private PeriodicSync parsePeriodicSync(XmlPullParser parser, AuthorityInfo authorityInfo) { |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 1807 | Bundle extras = new Bundle(); // Gets filled in later. |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 1808 | String periodValue = parser.getAttributeValue(null, "period"); |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 1809 | String flexValue = parser.getAttributeValue(null, "flex"); |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 1810 | final long period; |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 1811 | long flextime; |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 1812 | try { |
| 1813 | period = Long.parseLong(periodValue); |
| 1814 | } catch (NumberFormatException e) { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1815 | Slog.e(TAG, "error parsing the period of a periodic sync", e); |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 1816 | return null; |
| 1817 | } catch (NullPointerException e) { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1818 | Slog.e(TAG, "the period of a periodic sync is null", e); |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 1819 | return null; |
| 1820 | } |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 1821 | try { |
| 1822 | flextime = Long.parseLong(flexValue); |
| 1823 | } catch (NumberFormatException e) { |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 1824 | flextime = calculateDefaultFlexTime(period); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1825 | Slog.e(TAG, "Error formatting value parsed for periodic sync flex: " + flexValue |
Matthew Williams | 8ef2204 | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 1826 | + ", using default: " |
| 1827 | + flextime); |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 1828 | } catch (NullPointerException expected) { |
| 1829 | flextime = calculateDefaultFlexTime(period); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1830 | Slog.d(TAG, "No flex time specified for this sync, using a default. period: " |
| 1831 | + period + " flex: " + flextime); |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 1832 | } |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 1833 | PeriodicSync periodicSync; |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1834 | periodicSync = |
Matthew Williams | 8ef2204 | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 1835 | new PeriodicSync(authorityInfo.target.account, |
| 1836 | authorityInfo.target.provider, |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 1837 | extras, |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 1838 | period, flextime); |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 1839 | authorityInfo.periodicSyncs.add(periodicSync); |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 1840 | return periodicSync; |
| 1841 | } |
| 1842 | |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 1843 | private void parseExtra(XmlPullParser parser, Bundle extras) { |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 1844 | String name = parser.getAttributeValue(null, "name"); |
| 1845 | String type = parser.getAttributeValue(null, "type"); |
| 1846 | String value1 = parser.getAttributeValue(null, "value1"); |
| 1847 | String value2 = parser.getAttributeValue(null, "value2"); |
| 1848 | |
| 1849 | try { |
| 1850 | if ("long".equals(type)) { |
| 1851 | extras.putLong(name, Long.parseLong(value1)); |
| 1852 | } else if ("integer".equals(type)) { |
| 1853 | extras.putInt(name, Integer.parseInt(value1)); |
| 1854 | } else if ("double".equals(type)) { |
| 1855 | extras.putDouble(name, Double.parseDouble(value1)); |
| 1856 | } else if ("float".equals(type)) { |
| 1857 | extras.putFloat(name, Float.parseFloat(value1)); |
| 1858 | } else if ("boolean".equals(type)) { |
| 1859 | extras.putBoolean(name, Boolean.parseBoolean(value1)); |
| 1860 | } else if ("string".equals(type)) { |
| 1861 | extras.putString(name, value1); |
| 1862 | } else if ("account".equals(type)) { |
| 1863 | extras.putParcelable(name, new Account(value1, value2)); |
| 1864 | } |
| 1865 | } catch (NumberFormatException e) { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1866 | Slog.e(TAG, "error parsing bundle value", e); |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 1867 | } catch (NullPointerException e) { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1868 | Slog.e(TAG, "error parsing bundle value", e); |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 1869 | } |
| 1870 | } |
| 1871 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1872 | /** |
| 1873 | * Write all account information to the account file. |
| 1874 | */ |
| 1875 | private void writeAccountInfoLocked() { |
Matthew Williams | ba35271 | 2013-08-13 15:53:31 -0700 | [diff] [blame] | 1876 | if (Log.isLoggable(TAG_FILE, Log.VERBOSE)) { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1877 | Slog.v(TAG_FILE, "Writing new " + mAccountInfoFile.getBaseFile()); |
Matthew Williams | ba35271 | 2013-08-13 15:53:31 -0700 | [diff] [blame] | 1878 | } |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1879 | FileOutputStream fos = null; |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 1880 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1881 | try { |
| 1882 | fos = mAccountInfoFile.startWrite(); |
| 1883 | XmlSerializer out = new FastXmlSerializer(); |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 1884 | out.setOutput(fos, StandardCharsets.UTF_8.name()); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1885 | out.startDocument(null, true); |
| 1886 | out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true); |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 1887 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1888 | out.startTag(null, "accounts"); |
Fred Quintana | c2e4691 | 2010-03-15 16:10:44 -0700 | [diff] [blame] | 1889 | out.attribute(null, "version", Integer.toString(ACCOUNTS_VERSION)); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1890 | out.attribute(null, XML_ATTR_NEXT_AUTHORITY_ID, Integer.toString(mNextAuthorityId)); |
Ashish Sharma | 69d95de | 2012-04-11 17:27:24 -0700 | [diff] [blame] | 1891 | out.attribute(null, XML_ATTR_SYNC_RANDOM_OFFSET, Integer.toString(mSyncRandomOffset)); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1892 | |
| 1893 | // Write the Sync Automatically flags for each user |
| 1894 | final int M = mMasterSyncAutomatically.size(); |
| 1895 | for (int m = 0; m < M; m++) { |
| 1896 | int userId = mMasterSyncAutomatically.keyAt(m); |
| 1897 | Boolean listen = mMasterSyncAutomatically.valueAt(m); |
| 1898 | out.startTag(null, XML_TAG_LISTEN_FOR_TICKLES); |
| 1899 | out.attribute(null, XML_ATTR_USER, Integer.toString(userId)); |
| 1900 | out.attribute(null, XML_ATTR_ENABLED, Boolean.toString(listen)); |
| 1901 | out.endTag(null, XML_TAG_LISTEN_FOR_TICKLES); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1902 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 1903 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1904 | final int N = mAuthorities.size(); |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 1905 | for (int i = 0; i < N; i++) { |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 1906 | AuthorityInfo authority = mAuthorities.valueAt(i); |
Matthew Williams | 8ef2204 | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 1907 | EndPoint info = authority.target; |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1908 | out.startTag(null, "authority"); |
| 1909 | out.attribute(null, "id", Integer.toString(authority.ident)); |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 1910 | out.attribute(null, XML_ATTR_USER, Integer.toString(info.userId)); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1911 | out.attribute(null, XML_ATTR_ENABLED, Boolean.toString(authority.enabled)); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1912 | out.attribute(null, "account", info.account.name); |
| 1913 | out.attribute(null, "type", info.account.type); |
| 1914 | out.attribute(null, "authority", info.provider); |
Matthew Williams | 53abfdb | 2015-06-10 20:06:37 -0700 | [diff] [blame] | 1915 | out.attribute(null, "syncable", Integer.toString(authority.syncable)); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1916 | out.endTag(null, "authority"); |
| 1917 | } |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1918 | out.endTag(null, "accounts"); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1919 | out.endDocument(); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1920 | mAccountInfoFile.finishWrite(fos); |
| 1921 | } catch (java.io.IOException e1) { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1922 | Slog.w(TAG, "Error writing accounts", e1); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1923 | if (fos != null) { |
| 1924 | mAccountInfoFile.failWrite(fos); |
| 1925 | } |
| 1926 | } |
| 1927 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 1928 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1929 | static int getIntColumn(Cursor c, String name) { |
| 1930 | return c.getInt(c.getColumnIndex(name)); |
| 1931 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 1932 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1933 | static long getLongColumn(Cursor c, String name) { |
| 1934 | return c.getLong(c.getColumnIndex(name)); |
| 1935 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 1936 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1937 | /** |
| 1938 | * Load sync engine state from the old syncmanager database, and then |
| 1939 | * erase it. Note that we don't deal with pending operations, active |
| 1940 | * sync, or history. |
| 1941 | */ |
Fred Quintana | 77c560f | 2010-03-29 22:20:26 -0700 | [diff] [blame] | 1942 | private void readAndDeleteLegacyAccountInfoLocked() { |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1943 | // Look for old database to initialize from. |
| 1944 | File file = mContext.getDatabasePath("syncmanager.db"); |
| 1945 | if (!file.exists()) { |
| 1946 | return; |
| 1947 | } |
| 1948 | String path = file.getPath(); |
| 1949 | SQLiteDatabase db = null; |
| 1950 | try { |
| 1951 | db = SQLiteDatabase.openDatabase(path, null, |
| 1952 | SQLiteDatabase.OPEN_READONLY); |
| 1953 | } catch (SQLiteException e) { |
| 1954 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 1955 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1956 | if (db != null) { |
Dianne Hackborn | 2d5ed1f | 2009-05-06 15:22:38 -0700 | [diff] [blame] | 1957 | final boolean hasType = db.getVersion() >= 11; |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 1958 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1959 | // Copy in all of the status information, as well as accounts. |
Matthew Williams | ba35271 | 2013-08-13 15:53:31 -0700 | [diff] [blame] | 1960 | if (Log.isLoggable(TAG_FILE, Log.VERBOSE)) { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1961 | Slog.v(TAG_FILE, "Reading legacy sync accounts db"); |
Matthew Williams | ba35271 | 2013-08-13 15:53:31 -0700 | [diff] [blame] | 1962 | } |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1963 | SQLiteQueryBuilder qb = new SQLiteQueryBuilder(); |
| 1964 | qb.setTables("stats, status"); |
| 1965 | HashMap<String,String> map = new HashMap<String,String>(); |
| 1966 | map.put("_id", "status._id as _id"); |
| 1967 | map.put("account", "stats.account as account"); |
Dianne Hackborn | 2d5ed1f | 2009-05-06 15:22:38 -0700 | [diff] [blame] | 1968 | if (hasType) { |
| 1969 | map.put("account_type", "stats.account_type as account_type"); |
| 1970 | } |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1971 | map.put("authority", "stats.authority as authority"); |
| 1972 | map.put("totalElapsedTime", "totalElapsedTime"); |
| 1973 | map.put("numSyncs", "numSyncs"); |
| 1974 | map.put("numSourceLocal", "numSourceLocal"); |
| 1975 | map.put("numSourcePoll", "numSourcePoll"); |
| 1976 | map.put("numSourceServer", "numSourceServer"); |
| 1977 | map.put("numSourceUser", "numSourceUser"); |
| 1978 | map.put("lastSuccessSource", "lastSuccessSource"); |
| 1979 | map.put("lastSuccessTime", "lastSuccessTime"); |
| 1980 | map.put("lastFailureSource", "lastFailureSource"); |
| 1981 | map.put("lastFailureTime", "lastFailureTime"); |
| 1982 | map.put("lastFailureMesg", "lastFailureMesg"); |
| 1983 | map.put("pending", "pending"); |
| 1984 | qb.setProjectionMap(map); |
| 1985 | qb.appendWhere("stats._id = status.stats_id"); |
| 1986 | Cursor c = qb.query(db, null, null, null, null, null, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1987 | while (c.moveToNext()) { |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1988 | String accountName = c.getString(c.getColumnIndex("account")); |
Dianne Hackborn | 2d5ed1f | 2009-05-06 15:22:38 -0700 | [diff] [blame] | 1989 | String accountType = hasType |
| 1990 | ? c.getString(c.getColumnIndex("account_type")) : null; |
Dianne Hackborn | 7a13559 | 2009-05-06 00:28:37 -0700 | [diff] [blame] | 1991 | if (accountType == null) { |
Costin Manolache | 3348f14 | 2009-09-29 18:58:36 -0700 | [diff] [blame] | 1992 | accountType = "com.google"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1993 | } |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1994 | String authorityName = c.getString(c.getColumnIndex("authority")); |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 1995 | AuthorityInfo authority = |
| 1996 | this.getOrCreateAuthorityLocked( |
| 1997 | new EndPoint(new Account(accountName, accountType), |
| 1998 | authorityName, |
| 1999 | 0 /* legacy is single-user */) |
| 2000 | , -1, |
| 2001 | false); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 2002 | if (authority != null) { |
| 2003 | int i = mSyncStatus.size(); |
| 2004 | boolean found = false; |
| 2005 | SyncStatusInfo st = null; |
| 2006 | while (i > 0) { |
| 2007 | i--; |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 2008 | st = mSyncStatus.valueAt(i); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 2009 | if (st.authorityId == authority.ident) { |
| 2010 | found = true; |
| 2011 | break; |
| 2012 | } |
| 2013 | } |
| 2014 | if (!found) { |
| 2015 | st = new SyncStatusInfo(authority.ident); |
| 2016 | mSyncStatus.put(authority.ident, st); |
| 2017 | } |
| 2018 | st.totalElapsedTime = getLongColumn(c, "totalElapsedTime"); |
| 2019 | st.numSyncs = getIntColumn(c, "numSyncs"); |
| 2020 | st.numSourceLocal = getIntColumn(c, "numSourceLocal"); |
| 2021 | st.numSourcePoll = getIntColumn(c, "numSourcePoll"); |
| 2022 | st.numSourceServer = getIntColumn(c, "numSourceServer"); |
| 2023 | st.numSourceUser = getIntColumn(c, "numSourceUser"); |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 2024 | st.numSourcePeriodic = 0; |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 2025 | st.lastSuccessSource = getIntColumn(c, "lastSuccessSource"); |
| 2026 | st.lastSuccessTime = getLongColumn(c, "lastSuccessTime"); |
| 2027 | st.lastFailureSource = getIntColumn(c, "lastFailureSource"); |
| 2028 | st.lastFailureTime = getLongColumn(c, "lastFailureTime"); |
| 2029 | st.lastFailureMesg = c.getString(c.getColumnIndex("lastFailureMesg")); |
| 2030 | st.pending = getIntColumn(c, "pending") != 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2031 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2032 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 2033 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2034 | c.close(); |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 2035 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 2036 | // Retrieve the settings. |
| 2037 | qb = new SQLiteQueryBuilder(); |
| 2038 | qb.setTables("settings"); |
| 2039 | c = qb.query(db, null, null, null, null, null, null); |
| 2040 | while (c.moveToNext()) { |
| 2041 | String name = c.getString(c.getColumnIndex("name")); |
| 2042 | String value = c.getString(c.getColumnIndex("value")); |
| 2043 | if (name == null) continue; |
| 2044 | if (name.equals("listen_for_tickles")) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2045 | setMasterSyncAutomatically(value == null || Boolean.parseBoolean(value), 0); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 2046 | } else if (name.startsWith("sync_provider_")) { |
| 2047 | String provider = name.substring("sync_provider_".length(), |
| 2048 | name.length()); |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 2049 | int i = mAuthorities.size(); |
| 2050 | while (i > 0) { |
| 2051 | i--; |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 2052 | AuthorityInfo authority = mAuthorities.valueAt(i); |
Matthew Williams | 8ef2204 | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 2053 | if (authority.target.provider.equals(provider)) { |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 2054 | authority.enabled = value == null || Boolean.parseBoolean(value); |
Fred Quintana | 5e787c4 | 2009-08-16 23:13:53 -0700 | [diff] [blame] | 2055 | authority.syncable = 1; |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 2056 | } |
| 2057 | } |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 2058 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2059 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 2060 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 2061 | c.close(); |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 2062 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 2063 | db.close(); |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 2064 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 2065 | (new File(path)).delete(); |
| 2066 | } |
| 2067 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 2068 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 2069 | public static final int STATUS_FILE_END = 0; |
| 2070 | public static final int STATUS_FILE_ITEM = 100; |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 2071 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 2072 | /** |
| 2073 | * Read all sync status back in to the initial engine state. |
| 2074 | */ |
| 2075 | private void readStatusLocked() { |
Matthew Williams | ba35271 | 2013-08-13 15:53:31 -0700 | [diff] [blame] | 2076 | if (Log.isLoggable(TAG_FILE, Log.VERBOSE)) { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 2077 | Slog.v(TAG_FILE, "Reading " + mStatusFile.getBaseFile()); |
Matthew Williams | ba35271 | 2013-08-13 15:53:31 -0700 | [diff] [blame] | 2078 | } |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 2079 | try { |
| 2080 | byte[] data = mStatusFile.readFully(); |
| 2081 | Parcel in = Parcel.obtain(); |
| 2082 | in.unmarshall(data, 0, data.length); |
| 2083 | in.setDataPosition(0); |
| 2084 | int token; |
| 2085 | while ((token=in.readInt()) != STATUS_FILE_END) { |
| 2086 | if (token == STATUS_FILE_ITEM) { |
| 2087 | SyncStatusInfo status = new SyncStatusInfo(in); |
| 2088 | if (mAuthorities.indexOfKey(status.authorityId) >= 0) { |
| 2089 | status.pending = false; |
Matthew Williams | ba35271 | 2013-08-13 15:53:31 -0700 | [diff] [blame] | 2090 | if (Log.isLoggable(TAG_FILE, Log.VERBOSE)) { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 2091 | Slog.v(TAG_FILE, "Adding status for id " + status.authorityId); |
Matthew Williams | ba35271 | 2013-08-13 15:53:31 -0700 | [diff] [blame] | 2092 | } |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 2093 | mSyncStatus.put(status.authorityId, status); |
| 2094 | } |
| 2095 | } else { |
| 2096 | // Ooops. |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 2097 | Slog.w(TAG, "Unknown status token: " + token); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 2098 | break; |
| 2099 | } |
| 2100 | } |
| 2101 | } catch (java.io.IOException e) { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 2102 | Slog.i(TAG, "No initial status"); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 2103 | } |
| 2104 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 2105 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 2106 | /** |
| 2107 | * Write all sync status to the sync status file. |
| 2108 | */ |
| 2109 | private void writeStatusLocked() { |
Matthew Williams | ba35271 | 2013-08-13 15:53:31 -0700 | [diff] [blame] | 2110 | if (Log.isLoggable(TAG_FILE, Log.VERBOSE)) { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 2111 | Slog.v(TAG_FILE, "Writing new " + mStatusFile.getBaseFile()); |
Matthew Williams | ba35271 | 2013-08-13 15:53:31 -0700 | [diff] [blame] | 2112 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 2113 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 2114 | // The file is being written, so we don't need to have a scheduled |
| 2115 | // write until the next change. |
Makoto Onuki | 6963bea7 | 2017-12-12 10:42:39 -0800 | [diff] [blame^] | 2116 | mHandler.removeMessages(MSG_WRITE_STATUS); |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 2117 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 2118 | FileOutputStream fos = null; |
| 2119 | try { |
| 2120 | fos = mStatusFile.startWrite(); |
| 2121 | Parcel out = Parcel.obtain(); |
| 2122 | final int N = mSyncStatus.size(); |
| 2123 | for (int i=0; i<N; i++) { |
| 2124 | SyncStatusInfo status = mSyncStatus.valueAt(i); |
| 2125 | out.writeInt(STATUS_FILE_ITEM); |
| 2126 | status.writeToParcel(out, 0); |
| 2127 | } |
| 2128 | out.writeInt(STATUS_FILE_END); |
| 2129 | fos.write(out.marshall()); |
| 2130 | out.recycle(); |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 2131 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 2132 | mStatusFile.finishWrite(fos); |
| 2133 | } catch (java.io.IOException e1) { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 2134 | Slog.w(TAG, "Error writing status", e1); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 2135 | if (fos != null) { |
| 2136 | mStatusFile.failWrite(fos); |
| 2137 | } |
| 2138 | } |
| 2139 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 2140 | |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 2141 | private void requestSync(AuthorityInfo authorityInfo, int reason, Bundle extras) { |
| 2142 | if (android.os.Process.myUid() == android.os.Process.SYSTEM_UID |
| 2143 | && mSyncRequestListener != null) { |
Matthew Williams | 8ef2204 | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 2144 | mSyncRequestListener.onSyncRequest(authorityInfo.target, reason, extras); |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 2145 | } else { |
| 2146 | SyncRequest.Builder req = |
| 2147 | new SyncRequest.Builder() |
Nick Kralevich | 69002ae | 2013-10-19 08:43:08 -0700 | [diff] [blame] | 2148 | .syncOnce() |
Matthew Williams | 8ef2204 | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 2149 | .setExtras(extras); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 2150 | req.setSyncAdapter(authorityInfo.target.account, authorityInfo.target.provider); |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 2151 | ContentResolver.requestSync(req.build()); |
| 2152 | } |
| 2153 | } |
| 2154 | |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 2155 | private void requestSync(Account account, int userId, int reason, String authority, |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 2156 | Bundle extras) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2157 | // If this is happening in the system process, then call the syncrequest listener |
| 2158 | // to make a request back to the SyncManager directly. |
| 2159 | // If this is probably a test instance, then call back through the ContentResolver |
| 2160 | // which will know which userId to apply based on the Binder id. |
| 2161 | if (android.os.Process.myUid() == android.os.Process.SYSTEM_UID |
| 2162 | && mSyncRequestListener != null) { |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 2163 | mSyncRequestListener.onSyncRequest( |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 2164 | new EndPoint(account, authority, userId), |
| 2165 | reason, |
| 2166 | extras); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2167 | } else { |
| 2168 | ContentResolver.requestSync(account, authority, extras); |
| 2169 | } |
| 2170 | } |
| 2171 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 2172 | public static final int STATISTICS_FILE_END = 0; |
| 2173 | public static final int STATISTICS_FILE_ITEM_OLD = 100; |
| 2174 | public static final int STATISTICS_FILE_ITEM = 101; |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 2175 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 2176 | /** |
| 2177 | * Read all sync statistics back in to the initial engine state. |
| 2178 | */ |
| 2179 | private void readStatisticsLocked() { |
| 2180 | try { |
| 2181 | byte[] data = mStatisticsFile.readFully(); |
| 2182 | Parcel in = Parcel.obtain(); |
| 2183 | in.unmarshall(data, 0, data.length); |
| 2184 | in.setDataPosition(0); |
| 2185 | int token; |
| 2186 | int index = 0; |
| 2187 | while ((token=in.readInt()) != STATISTICS_FILE_END) { |
| 2188 | if (token == STATISTICS_FILE_ITEM |
| 2189 | || token == STATISTICS_FILE_ITEM_OLD) { |
| 2190 | int day = in.readInt(); |
| 2191 | if (token == STATISTICS_FILE_ITEM_OLD) { |
| 2192 | day = day - 2009 + 14245; // Magic! |
| 2193 | } |
| 2194 | DayStats ds = new DayStats(day); |
| 2195 | ds.successCount = in.readInt(); |
| 2196 | ds.successTime = in.readLong(); |
| 2197 | ds.failureCount = in.readInt(); |
| 2198 | ds.failureTime = in.readLong(); |
| 2199 | if (index < mDayStats.length) { |
| 2200 | mDayStats[index] = ds; |
| 2201 | index++; |
| 2202 | } |
| 2203 | } else { |
| 2204 | // Ooops. |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 2205 | Slog.w(TAG, "Unknown stats token: " + token); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 2206 | break; |
| 2207 | } |
| 2208 | } |
| 2209 | } catch (java.io.IOException e) { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 2210 | Slog.i(TAG, "No initial statistics"); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 2211 | } |
| 2212 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 2213 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 2214 | /** |
| 2215 | * Write all sync statistics to the sync status file. |
| 2216 | */ |
| 2217 | private void writeStatisticsLocked() { |
Matthew Williams | ba35271 | 2013-08-13 15:53:31 -0700 | [diff] [blame] | 2218 | if (Log.isLoggable(TAG_FILE, Log.VERBOSE)) { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 2219 | Slog.v(TAG, "Writing new " + mStatisticsFile.getBaseFile()); |
Matthew Williams | ba35271 | 2013-08-13 15:53:31 -0700 | [diff] [blame] | 2220 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 2221 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 2222 | // The file is being written, so we don't need to have a scheduled |
| 2223 | // write until the next change. |
Makoto Onuki | 6963bea7 | 2017-12-12 10:42:39 -0800 | [diff] [blame^] | 2224 | mHandler.removeMessages(MSG_WRITE_STATISTICS); |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 2225 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 2226 | FileOutputStream fos = null; |
| 2227 | try { |
| 2228 | fos = mStatisticsFile.startWrite(); |
| 2229 | Parcel out = Parcel.obtain(); |
| 2230 | final int N = mDayStats.length; |
| 2231 | for (int i=0; i<N; i++) { |
| 2232 | DayStats ds = mDayStats[i]; |
| 2233 | if (ds == null) { |
| 2234 | break; |
| 2235 | } |
| 2236 | out.writeInt(STATISTICS_FILE_ITEM); |
| 2237 | out.writeInt(ds.day); |
| 2238 | out.writeInt(ds.successCount); |
| 2239 | out.writeLong(ds.successTime); |
| 2240 | out.writeInt(ds.failureCount); |
| 2241 | out.writeLong(ds.failureTime); |
| 2242 | } |
| 2243 | out.writeInt(STATISTICS_FILE_END); |
| 2244 | fos.write(out.marshall()); |
| 2245 | out.recycle(); |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 2246 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 2247 | mStatisticsFile.finishWrite(fos); |
| 2248 | } catch (java.io.IOException e1) { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 2249 | Slog.w(TAG, "Error writing stats", e1); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 2250 | if (fos != null) { |
| 2251 | mStatisticsFile.failWrite(fos); |
| 2252 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2253 | } |
| 2254 | } |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 2255 | |
| 2256 | /** |
Marvin Paul | a653325 | 2014-11-24 12:57:48 -0800 | [diff] [blame] | 2257 | * Let the BackupManager know that account sync settings have changed. This will trigger |
| 2258 | * {@link com.android.server.backup.SystemBackupAgent} to run. |
| 2259 | */ |
| 2260 | public void queueBackup() { |
| 2261 | BackupManager.dataChanged("android"); |
| 2262 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2263 | } |