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