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