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