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