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