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