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