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