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