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