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