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