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; |
Tom Taylor | d4a4729 | 2009-12-21 13:59:18 -0800 | [diff] [blame] | 21 | import com.android.common.FastXmlSerializer; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [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; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | import android.database.Cursor; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | import android.database.sqlite.SQLiteDatabase; |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 30 | import android.database.sqlite.SQLiteException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | import android.database.sqlite.SQLiteQueryBuilder; |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 32 | import android.os.Bundle; |
| 33 | import android.os.Environment; |
| 34 | import android.os.Handler; |
| 35 | import android.os.Message; |
| 36 | import android.os.Parcel; |
| 37 | import android.os.RemoteCallbackList; |
| 38 | import android.os.RemoteException; |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame^] | 39 | import android.os.SystemClock; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | import android.util.Log; |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 41 | import android.util.SparseArray; |
| 42 | import android.util.Xml; |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame^] | 43 | import android.util.Pair; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 44 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 45 | import java.io.File; |
| 46 | import java.io.FileInputStream; |
| 47 | import java.io.FileOutputStream; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | import java.util.ArrayList; |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 49 | import java.util.Calendar; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 50 | import java.util.HashMap; |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 51 | import java.util.Iterator; |
| 52 | import java.util.TimeZone; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 53 | |
| 54 | /** |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 55 | * Singleton that tracks the sync data and overall sync |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 56 | * history on the device. |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 57 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 58 | * @hide |
| 59 | */ |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 60 | public class SyncStorageEngine extends Handler { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 61 | private static final String TAG = "SyncManager"; |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 62 | private static final boolean DEBUG = false; |
| 63 | private static final boolean DEBUG_FILE = false; |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 64 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 65 | // @VisibleForTesting |
| 66 | 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] | 67 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 68 | /** Enum value for a sync start event. */ |
| 69 | public static final int EVENT_START = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 70 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 71 | /** Enum value for a sync stop event. */ |
| 72 | public static final int EVENT_STOP = 1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 73 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 74 | // TODO: i18n -- grab these out of resources. |
| 75 | /** String names for the sync event types. */ |
| 76 | public static final String[] EVENTS = { "START", "STOP" }; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 77 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 78 | /** Enum value for a server-initiated sync. */ |
| 79 | public static final int SOURCE_SERVER = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 80 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 81 | /** Enum value for a local-initiated sync. */ |
| 82 | public static final int SOURCE_LOCAL = 1; |
| 83 | /** |
| 84 | * Enum value for a poll-based sync (e.g., upon connection to |
| 85 | * network) |
| 86 | */ |
| 87 | public static final int SOURCE_POLL = 2; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 88 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 89 | /** Enum value for a user-initiated sync. */ |
| 90 | public static final int SOURCE_USER = 3; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 91 | |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame^] | 92 | public static final long NOT_IN_BACKOFF_MODE = -1; |
| 93 | |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 94 | private static final Intent SYNC_CONNECTION_SETTING_CHANGED_INTENT = |
| 95 | new Intent("com.android.sync.SYNC_CONN_STATUS_CHANGED"); |
| 96 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 97 | // TODO: i18n -- grab these out of resources. |
| 98 | /** String names for the sync source types. */ |
| 99 | public static final String[] SOURCES = { "SERVER", |
| 100 | "LOCAL", |
| 101 | "POLL", |
| 102 | "USER" }; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 103 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 104 | // The MESG column will contain one of these or one of the Error types. |
| 105 | public static final String MESG_SUCCESS = "success"; |
| 106 | public static final String MESG_CANCELED = "canceled"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 107 | |
Dianne Hackborn | a33e3f7 | 2009-09-29 17:28:24 -0700 | [diff] [blame] | 108 | public static final int MAX_HISTORY = 100; |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 109 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 110 | private static final int MSG_WRITE_STATUS = 1; |
| 111 | private static final long WRITE_STATUS_DELAY = 1000*60*10; // 10 minutes |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 112 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 113 | private static final int MSG_WRITE_STATISTICS = 2; |
| 114 | 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] | 115 | |
| 116 | private static final boolean SYNC_ENABLED_DEFAULT = false; |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 117 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 118 | public static class PendingOperation { |
Dianne Hackborn | 7a13559 | 2009-05-06 00:28:37 -0700 | [diff] [blame] | 119 | final Account account; |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 120 | final int syncSource; |
| 121 | final String authority; |
| 122 | final Bundle extras; // note: read-only. |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame^] | 123 | final boolean expedited; |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 124 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 125 | int authorityId; |
| 126 | byte[] flatExtras; |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 127 | |
Dianne Hackborn | 7a13559 | 2009-05-06 00:28:37 -0700 | [diff] [blame] | 128 | PendingOperation(Account account, int source, |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame^] | 129 | String authority, Bundle extras, boolean expedited) { |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 130 | this.account = account; |
| 131 | this.syncSource = source; |
| 132 | this.authority = authority; |
| 133 | this.extras = extras != null ? new Bundle(extras) : extras; |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame^] | 134 | this.expedited = expedited; |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 135 | this.authorityId = -1; |
| 136 | } |
| 137 | |
| 138 | PendingOperation(PendingOperation other) { |
| 139 | this.account = other.account; |
| 140 | this.syncSource = other.syncSource; |
| 141 | this.authority = other.authority; |
| 142 | this.extras = other.extras; |
| 143 | this.authorityId = other.authorityId; |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame^] | 144 | this.expedited = other.expedited; |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 145 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 146 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 147 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 148 | static class AccountInfo { |
Dianne Hackborn | 7a13559 | 2009-05-06 00:28:37 -0700 | [diff] [blame] | 149 | final Account account; |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 150 | final HashMap<String, AuthorityInfo> authorities = |
| 151 | new HashMap<String, AuthorityInfo>(); |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 152 | |
Dianne Hackborn | 7a13559 | 2009-05-06 00:28:37 -0700 | [diff] [blame] | 153 | AccountInfo(Account account) { |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 154 | this.account = account; |
| 155 | } |
| 156 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 157 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 158 | public static class AuthorityInfo { |
Dianne Hackborn | 7a13559 | 2009-05-06 00:28:37 -0700 | [diff] [blame] | 159 | final Account account; |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 160 | final String authority; |
| 161 | final int ident; |
| 162 | boolean enabled; |
Fred Quintana | 5e787c4 | 2009-08-16 23:13:53 -0700 | [diff] [blame] | 163 | int syncable; |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame^] | 164 | long backoffTime; |
| 165 | long backoffDelay; |
| 166 | long delayUntil; |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 167 | |
Dianne Hackborn | 7a13559 | 2009-05-06 00:28:37 -0700 | [diff] [blame] | 168 | AuthorityInfo(Account account, String authority, int ident) { |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 169 | this.account = account; |
| 170 | this.authority = authority; |
| 171 | this.ident = ident; |
Joe Onorato | 8294fad | 2009-07-15 16:08:44 -0700 | [diff] [blame] | 172 | enabled = SYNC_ENABLED_DEFAULT; |
Fred Quintana | 4a6679b | 2009-08-17 13:05:39 -0700 | [diff] [blame] | 173 | syncable = -1; // default to "unknown" |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame^] | 174 | backoffTime = -1; // if < 0 then we aren't in backoff mode |
| 175 | backoffDelay = -1; // if < 0 then we aren't in backoff mode |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 176 | } |
| 177 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 178 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 179 | public static class SyncHistoryItem { |
| 180 | int authorityId; |
| 181 | int historyId; |
| 182 | long eventTime; |
| 183 | long elapsedTime; |
| 184 | int source; |
| 185 | int event; |
| 186 | long upstreamActivity; |
| 187 | long downstreamActivity; |
| 188 | String mesg; |
| 189 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 190 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 191 | public static class DayStats { |
| 192 | public final int day; |
| 193 | public int successCount; |
| 194 | public long successTime; |
| 195 | public int failureCount; |
| 196 | public long failureTime; |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 197 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 198 | public DayStats(int day) { |
| 199 | this.day = day; |
| 200 | } |
| 201 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 202 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 203 | // Primary list of all syncable authorities. Also our global lock. |
| 204 | private final SparseArray<AuthorityInfo> mAuthorities = |
| 205 | new SparseArray<AuthorityInfo>(); |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 206 | |
Dianne Hackborn | 7a13559 | 2009-05-06 00:28:37 -0700 | [diff] [blame] | 207 | private final HashMap<Account, AccountInfo> mAccounts = |
| 208 | new HashMap<Account, AccountInfo>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 209 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 210 | private final ArrayList<PendingOperation> mPendingOperations = |
| 211 | new ArrayList<PendingOperation>(); |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 212 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 213 | private ActiveSyncInfo mActiveSync; |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 214 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 215 | private final SparseArray<SyncStatusInfo> mSyncStatus = |
| 216 | new SparseArray<SyncStatusInfo>(); |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 217 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 218 | private final ArrayList<SyncHistoryItem> mSyncHistory = |
| 219 | new ArrayList<SyncHistoryItem>(); |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 220 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 221 | private final RemoteCallbackList<ISyncStatusObserver> mChangeListeners |
| 222 | = new RemoteCallbackList<ISyncStatusObserver>(); |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 223 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 224 | // We keep 4 weeks of stats. |
| 225 | private final DayStats[] mDayStats = new DayStats[7*4]; |
| 226 | private final Calendar mCal; |
| 227 | private int mYear; |
| 228 | private int mYearInDays; |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 229 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 230 | private final Context mContext; |
| 231 | private static volatile SyncStorageEngine sSyncStorageEngine = null; |
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 | /** |
| 234 | * This file contains the core engine state: all accounts and the |
| 235 | * settings for them. It must never be lost, and should be changed |
| 236 | * infrequently, so it is stored as an XML file. |
| 237 | */ |
| 238 | private final AtomicFile mAccountInfoFile; |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 239 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 240 | /** |
| 241 | * This file contains the current sync status. We would like to retain |
| 242 | * it across boots, but its loss is not the end of the world, so we store |
| 243 | * this information as binary data. |
| 244 | */ |
| 245 | private final AtomicFile mStatusFile; |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 246 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 247 | /** |
| 248 | * This file contains sync statistics. This is purely debugging information |
| 249 | * so is written infrequently and can be thrown away at any time. |
| 250 | */ |
| 251 | private final AtomicFile mStatisticsFile; |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 252 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 253 | /** |
| 254 | * This file contains the pending sync operations. It is a binary file, |
| 255 | * which must be updated every time an operation is added or removed, |
| 256 | * so we have special handling of it. |
| 257 | */ |
| 258 | private final AtomicFile mPendingFile; |
| 259 | private static final int PENDING_FINISH_TO_WRITE = 4; |
| 260 | private int mNumPendingFinished = 0; |
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 int mNextHistoryId = 0; |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 263 | private boolean mMasterSyncAutomatically = true; |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 264 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 265 | private SyncStorageEngine(Context context) { |
| 266 | mContext = context; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 267 | sSyncStorageEngine = this; |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 268 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 269 | mCal = Calendar.getInstance(TimeZone.getTimeZone("GMT+0")); |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 270 | |
Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 271 | // This call will return the correct directory whether Encrypted File Systems is |
| 272 | // enabled or not. |
| 273 | File dataDir = Environment.getSecureDataDirectory(); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 274 | File systemDir = new File(dataDir, "system"); |
| 275 | File syncDir = new File(systemDir, "sync"); |
| 276 | mAccountInfoFile = new AtomicFile(new File(syncDir, "accounts.xml")); |
| 277 | mStatusFile = new AtomicFile(new File(syncDir, "status.bin")); |
| 278 | mPendingFile = new AtomicFile(new File(syncDir, "pending.bin")); |
| 279 | mStatisticsFile = new AtomicFile(new File(syncDir, "stats.bin")); |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 280 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 281 | readAccountInfoLocked(); |
| 282 | readStatusLocked(); |
| 283 | readPendingOperationsLocked(); |
| 284 | readStatisticsLocked(); |
| 285 | readLegacyAccountInfoLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | public static SyncStorageEngine newTestInstance(Context context) { |
| 289 | return new SyncStorageEngine(context); |
| 290 | } |
| 291 | |
| 292 | public static void init(Context context) { |
| 293 | if (sSyncStorageEngine != null) { |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame^] | 294 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 295 | } |
| 296 | sSyncStorageEngine = new SyncStorageEngine(context); |
| 297 | } |
| 298 | |
| 299 | public static SyncStorageEngine getSingleton() { |
| 300 | if (sSyncStorageEngine == null) { |
| 301 | throw new IllegalStateException("not initialized"); |
| 302 | } |
| 303 | return sSyncStorageEngine; |
| 304 | } |
| 305 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 306 | @Override public void handleMessage(Message msg) { |
| 307 | if (msg.what == MSG_WRITE_STATUS) { |
| 308 | synchronized (mAccounts) { |
| 309 | writeStatusLocked(); |
Fred Quintana | d9d2f11 | 2009-04-23 13:36:27 -0700 | [diff] [blame] | 310 | } |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 311 | } else if (msg.what == MSG_WRITE_STATISTICS) { |
| 312 | synchronized (mAccounts) { |
| 313 | writeStatisticsLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 314 | } |
| 315 | } |
| 316 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 317 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 318 | public void addStatusChangeListener(int mask, ISyncStatusObserver callback) { |
| 319 | synchronized (mAuthorities) { |
| 320 | mChangeListeners.register(callback, mask); |
| 321 | } |
| 322 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 323 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 324 | public void removeStatusChangeListener(ISyncStatusObserver callback) { |
| 325 | synchronized (mAuthorities) { |
| 326 | mChangeListeners.unregister(callback); |
| 327 | } |
| 328 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 329 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 330 | private void reportChange(int which) { |
| 331 | ArrayList<ISyncStatusObserver> reports = null; |
| 332 | synchronized (mAuthorities) { |
| 333 | int i = mChangeListeners.beginBroadcast(); |
| 334 | while (i > 0) { |
| 335 | i--; |
| 336 | Integer mask = (Integer)mChangeListeners.getBroadcastCookie(i); |
| 337 | if ((which & mask.intValue()) == 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 338 | continue; |
| 339 | } |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 340 | if (reports == null) { |
| 341 | reports = new ArrayList<ISyncStatusObserver>(i); |
| 342 | } |
| 343 | reports.add(mChangeListeners.getBroadcastItem(i)); |
| 344 | } |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 345 | mChangeListeners.finishBroadcast(); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 346 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 347 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 348 | if (DEBUG) Log.v(TAG, "reportChange " + which + " to: " + reports); |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 349 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 350 | if (reports != null) { |
| 351 | int i = reports.size(); |
| 352 | while (i > 0) { |
| 353 | i--; |
| 354 | try { |
| 355 | reports.get(i).onStatusChanged(which); |
| 356 | } catch (RemoteException e) { |
| 357 | // The remote callback list will take care of this for us. |
| 358 | } |
| 359 | } |
| 360 | } |
| 361 | } |
Amith Yamasani | 70c874b | 2009-07-06 14:53:25 -0700 | [diff] [blame] | 362 | |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 363 | public boolean getSyncAutomatically(Account account, String providerName) { |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 364 | synchronized (mAuthorities) { |
| 365 | if (account != null) { |
| 366 | AuthorityInfo authority = getAuthorityLocked(account, providerName, |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 367 | "getSyncAutomatically"); |
| 368 | return authority != null && authority.enabled; |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 369 | } |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 370 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 371 | int i = mAuthorities.size(); |
| 372 | while (i > 0) { |
| 373 | i--; |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 374 | AuthorityInfo authority = mAuthorities.valueAt(i); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 375 | if (authority.authority.equals(providerName) |
| 376 | && authority.enabled) { |
| 377 | return true; |
| 378 | } |
| 379 | } |
| 380 | return false; |
| 381 | } |
| 382 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 383 | |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 384 | public void setSyncAutomatically(Account account, String providerName, boolean sync) { |
Joe Onorato | 8294fad | 2009-07-15 16:08:44 -0700 | [diff] [blame] | 385 | boolean wasEnabled; |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 386 | synchronized (mAuthorities) { |
Joe Onorato | 8294fad | 2009-07-15 16:08:44 -0700 | [diff] [blame] | 387 | AuthorityInfo authority = getOrCreateAuthorityLocked(account, providerName, -1, false); |
| 388 | wasEnabled = authority.enabled; |
| 389 | authority.enabled = sync; |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 390 | writeAccountInfoLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 391 | } |
Joe Onorato | 8294fad | 2009-07-15 16:08:44 -0700 | [diff] [blame] | 392 | |
| 393 | if (!wasEnabled && sync) { |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame^] | 394 | ContentResolver.requestSync(account, providerName, new Bundle()); |
Joe Onorato | 8294fad | 2009-07-15 16:08:44 -0700 | [diff] [blame] | 395 | } |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 396 | reportChange(ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 397 | } |
| 398 | |
Fred Quintana | 5e787c4 | 2009-08-16 23:13:53 -0700 | [diff] [blame] | 399 | public int getIsSyncable(Account account, String providerName) { |
| 400 | synchronized (mAuthorities) { |
| 401 | if (account != null) { |
| 402 | AuthorityInfo authority = getAuthorityLocked(account, providerName, |
| 403 | "getIsSyncable"); |
| 404 | if (authority == null) { |
| 405 | return -1; |
| 406 | } |
| 407 | return authority.syncable; |
| 408 | } |
| 409 | |
| 410 | int i = mAuthorities.size(); |
| 411 | while (i > 0) { |
| 412 | i--; |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 413 | AuthorityInfo authority = mAuthorities.valueAt(i); |
Fred Quintana | 5e787c4 | 2009-08-16 23:13:53 -0700 | [diff] [blame] | 414 | if (authority.authority.equals(providerName)) { |
| 415 | return authority.syncable; |
| 416 | } |
| 417 | } |
| 418 | return -1; |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | public void setIsSyncable(Account account, String providerName, int syncable) { |
| 423 | int oldState; |
Fred Quintana | b763ab2 | 2009-08-18 18:07:30 -0700 | [diff] [blame] | 424 | if (syncable > 1) { |
| 425 | syncable = 1; |
| 426 | } else if (syncable < -1) { |
| 427 | syncable = -1; |
| 428 | } |
| 429 | Log.d(TAG, "setIsSyncable: " + account + ", provider " + providerName + " -> " + syncable); |
Fred Quintana | 5e787c4 | 2009-08-16 23:13:53 -0700 | [diff] [blame] | 430 | synchronized (mAuthorities) { |
| 431 | AuthorityInfo authority = getOrCreateAuthorityLocked(account, providerName, -1, false); |
| 432 | oldState = authority.syncable; |
| 433 | authority.syncable = syncable; |
| 434 | writeAccountInfoLocked(); |
| 435 | } |
| 436 | |
| 437 | if (oldState <= 0 && syncable > 0) { |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame^] | 438 | ContentResolver.requestSync(account, providerName, new Bundle()); |
Fred Quintana | 5e787c4 | 2009-08-16 23:13:53 -0700 | [diff] [blame] | 439 | } |
| 440 | reportChange(ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS); |
| 441 | } |
| 442 | |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame^] | 443 | public Pair<Long, Long> getBackoff(Account account, String providerName) { |
| 444 | synchronized (mAuthorities) { |
| 445 | AuthorityInfo authority = getAuthorityLocked(account, providerName, "getBackoff"); |
| 446 | if (authority == null || authority.backoffTime < 0) { |
| 447 | return null; |
| 448 | } |
| 449 | return Pair.create(authority.backoffTime, authority.backoffDelay); |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | public void setBackoff(Account account, String providerName, |
| 454 | long nextSyncTime, long nextDelay) { |
| 455 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 456 | Log.v(TAG, "setBackoff: " + account + ", provider " + providerName |
| 457 | + " -> nextSyncTime " + nextSyncTime + ", nextDelay " + nextDelay); |
| 458 | } |
| 459 | boolean changed = false; |
| 460 | synchronized (mAuthorities) { |
| 461 | if (account == null || providerName == null) { |
| 462 | for (AccountInfo accountInfo : mAccounts.values()) { |
| 463 | if (account != null && !account.equals(accountInfo.account)) continue; |
| 464 | for (AuthorityInfo authorityInfo : accountInfo.authorities.values()) { |
| 465 | if (providerName != null && !providerName.equals(authorityInfo.authority)) { |
| 466 | continue; |
| 467 | } |
| 468 | if (authorityInfo.backoffTime != nextSyncTime |
| 469 | || authorityInfo.backoffDelay != nextDelay) { |
| 470 | authorityInfo.backoffTime = nextSyncTime; |
| 471 | authorityInfo.backoffDelay = nextDelay; |
| 472 | changed = true; |
| 473 | } |
| 474 | } |
| 475 | } |
| 476 | } else { |
| 477 | AuthorityInfo authority = |
| 478 | getOrCreateAuthorityLocked(account, providerName, -1, false); |
| 479 | if (authority.backoffTime == nextSyncTime && authority.backoffDelay == nextDelay) { |
| 480 | return; |
| 481 | } |
| 482 | authority.backoffTime = nextSyncTime; |
| 483 | authority.backoffDelay = nextDelay; |
| 484 | changed = true; |
| 485 | } |
| 486 | if (changed) { |
| 487 | writeAccountInfoLocked(); |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | if (changed) { |
| 492 | reportChange(ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS); |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | public void setDelayUntilTime(Account account, String providerName, long delayUntil) { |
| 497 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 498 | Log.v(TAG, "setDelayUntil: " + account + ", provider " + providerName |
| 499 | + " -> delayUntil " + delayUntil); |
| 500 | } |
| 501 | synchronized (mAuthorities) { |
| 502 | AuthorityInfo authority = getOrCreateAuthorityLocked(account, providerName, -1, false); |
| 503 | if (authority.delayUntil == delayUntil) { |
| 504 | return; |
| 505 | } |
| 506 | authority.delayUntil = delayUntil; |
| 507 | writeAccountInfoLocked(); |
| 508 | } |
| 509 | |
| 510 | reportChange(ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS); |
| 511 | } |
| 512 | |
| 513 | public long getDelayUntilTime(Account account, String providerName) { |
| 514 | synchronized (mAuthorities) { |
| 515 | AuthorityInfo authority = getAuthorityLocked(account, providerName, "getDelayUntil"); |
| 516 | if (authority == null) { |
| 517 | return 0; |
| 518 | } |
| 519 | return authority.delayUntil; |
| 520 | } |
| 521 | } |
| 522 | |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 523 | public void setMasterSyncAutomatically(boolean flag) { |
Joe Onorato | 8294fad | 2009-07-15 16:08:44 -0700 | [diff] [blame] | 524 | boolean old; |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 525 | synchronized (mAuthorities) { |
Joe Onorato | 8294fad | 2009-07-15 16:08:44 -0700 | [diff] [blame] | 526 | old = mMasterSyncAutomatically; |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 527 | mMasterSyncAutomatically = flag; |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 528 | writeAccountInfoLocked(); |
| 529 | } |
Joe Onorato | 8294fad | 2009-07-15 16:08:44 -0700 | [diff] [blame] | 530 | if (!old && flag) { |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame^] | 531 | ContentResolver.requestSync(null, null, new Bundle()); |
Joe Onorato | 8294fad | 2009-07-15 16:08:44 -0700 | [diff] [blame] | 532 | } |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 533 | reportChange(ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS); |
| 534 | mContext.sendBroadcast(SYNC_CONNECTION_SETTING_CHANGED_INTENT); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 535 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 536 | |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 537 | public boolean getMasterSyncAutomatically() { |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 538 | synchronized (mAuthorities) { |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 539 | return mMasterSyncAutomatically; |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 540 | } |
| 541 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 542 | |
Dianne Hackborn | 7a13559 | 2009-05-06 00:28:37 -0700 | [diff] [blame] | 543 | public AuthorityInfo getAuthority(Account account, String authority) { |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 544 | synchronized (mAuthorities) { |
| 545 | return getAuthorityLocked(account, authority, null); |
| 546 | } |
| 547 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 548 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 549 | public AuthorityInfo getAuthority(int authorityId) { |
| 550 | synchronized (mAuthorities) { |
| 551 | return mAuthorities.get(authorityId); |
| 552 | } |
| 553 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 554 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 555 | /** |
| 556 | * Returns true if there is currently a sync operation for the given |
| 557 | * account or authority in the pending list, or actively being processed. |
| 558 | */ |
Dianne Hackborn | 7a13559 | 2009-05-06 00:28:37 -0700 | [diff] [blame] | 559 | public boolean isSyncActive(Account account, String authority) { |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 560 | synchronized (mAuthorities) { |
| 561 | int i = mPendingOperations.size(); |
| 562 | while (i > 0) { |
| 563 | i--; |
| 564 | // TODO(fredq): this probably shouldn't be considering |
| 565 | // pending operations. |
| 566 | PendingOperation op = mPendingOperations.get(i); |
| 567 | if (op.account.equals(account) && op.authority.equals(authority)) { |
| 568 | return true; |
| 569 | } |
| 570 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 571 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 572 | if (mActiveSync != null) { |
| 573 | AuthorityInfo ainfo = getAuthority(mActiveSync.authorityId); |
| 574 | if (ainfo != null && ainfo.account.equals(account) |
| 575 | && ainfo.authority.equals(authority)) { |
| 576 | return true; |
| 577 | } |
| 578 | } |
| 579 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 580 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 581 | return false; |
| 582 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 583 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 584 | public PendingOperation insertIntoPending(PendingOperation op) { |
| 585 | synchronized (mAuthorities) { |
| 586 | if (DEBUG) Log.v(TAG, "insertIntoPending: account=" + op.account |
| 587 | + " auth=" + op.authority |
| 588 | + " src=" + op.syncSource |
| 589 | + " extras=" + op.extras); |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 590 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 591 | AuthorityInfo authority = getOrCreateAuthorityLocked(op.account, |
| 592 | op.authority, |
| 593 | -1 /* desired identifier */, |
| 594 | true /* write accounts to storage */); |
| 595 | if (authority == null) { |
| 596 | return null; |
| 597 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 598 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 599 | op = new PendingOperation(op); |
| 600 | op.authorityId = authority.ident; |
| 601 | mPendingOperations.add(op); |
| 602 | appendPendingOperationLocked(op); |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 603 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 604 | SyncStatusInfo status = getOrCreateSyncStatusLocked(authority.ident); |
| 605 | status.pending = true; |
| 606 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 607 | |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 608 | reportChange(ContentResolver.SYNC_OBSERVER_TYPE_PENDING); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 609 | return op; |
| 610 | } |
| 611 | |
| 612 | public boolean deleteFromPending(PendingOperation op) { |
| 613 | boolean res = false; |
| 614 | synchronized (mAuthorities) { |
| 615 | if (DEBUG) Log.v(TAG, "deleteFromPending: account=" + op.account |
| 616 | + " auth=" + op.authority |
| 617 | + " src=" + op.syncSource |
| 618 | + " extras=" + op.extras); |
| 619 | if (mPendingOperations.remove(op)) { |
| 620 | if (mPendingOperations.size() == 0 |
| 621 | || mNumPendingFinished >= PENDING_FINISH_TO_WRITE) { |
| 622 | writePendingOperationsLocked(); |
| 623 | mNumPendingFinished = 0; |
| 624 | } else { |
| 625 | mNumPendingFinished++; |
| 626 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 627 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 628 | AuthorityInfo authority = getAuthorityLocked(op.account, op.authority, |
| 629 | "deleteFromPending"); |
| 630 | if (authority != null) { |
| 631 | if (DEBUG) Log.v(TAG, "removing - " + authority); |
| 632 | final int N = mPendingOperations.size(); |
| 633 | boolean morePending = false; |
| 634 | for (int i=0; i<N; i++) { |
| 635 | PendingOperation cur = mPendingOperations.get(i); |
| 636 | if (cur.account.equals(op.account) |
| 637 | && cur.authority.equals(op.authority)) { |
| 638 | morePending = true; |
| 639 | break; |
| 640 | } |
| 641 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 642 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 643 | if (!morePending) { |
| 644 | if (DEBUG) Log.v(TAG, "no more pending!"); |
| 645 | SyncStatusInfo status = getOrCreateSyncStatusLocked(authority.ident); |
| 646 | status.pending = false; |
| 647 | } |
| 648 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 649 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 650 | res = true; |
| 651 | } |
| 652 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 653 | |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 654 | reportChange(ContentResolver.SYNC_OBSERVER_TYPE_PENDING); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 655 | return res; |
| 656 | } |
| 657 | |
| 658 | public int clearPending() { |
| 659 | int num; |
| 660 | synchronized (mAuthorities) { |
| 661 | if (DEBUG) Log.v(TAG, "clearPending"); |
| 662 | num = mPendingOperations.size(); |
| 663 | mPendingOperations.clear(); |
| 664 | final int N = mSyncStatus.size(); |
| 665 | for (int i=0; i<N; i++) { |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 666 | mSyncStatus.valueAt(i).pending = false; |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 667 | } |
| 668 | writePendingOperationsLocked(); |
| 669 | } |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 670 | reportChange(ContentResolver.SYNC_OBSERVER_TYPE_PENDING); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 671 | return num; |
| 672 | } |
| 673 | |
| 674 | /** |
| 675 | * Return a copy of the current array of pending operations. The |
| 676 | * PendingOperation objects are the real objects stored inside, so that |
| 677 | * they can be used with deleteFromPending(). |
| 678 | */ |
| 679 | public ArrayList<PendingOperation> getPendingOperations() { |
| 680 | synchronized (mAuthorities) { |
| 681 | return new ArrayList<PendingOperation>(mPendingOperations); |
| 682 | } |
| 683 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 684 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 685 | /** |
| 686 | * Return the number of currently pending operations. |
| 687 | */ |
| 688 | public int getPendingOperationCount() { |
| 689 | synchronized (mAuthorities) { |
| 690 | return mPendingOperations.size(); |
| 691 | } |
| 692 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 693 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 694 | /** |
| 695 | * Called when the set of account has changed, given the new array of |
| 696 | * active accounts. |
| 697 | */ |
Dianne Hackborn | 7a13559 | 2009-05-06 00:28:37 -0700 | [diff] [blame] | 698 | public void doDatabaseCleanup(Account[] accounts) { |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 699 | synchronized (mAuthorities) { |
| 700 | if (DEBUG) Log.w(TAG, "Updating for new accounts..."); |
| 701 | SparseArray<AuthorityInfo> removing = new SparseArray<AuthorityInfo>(); |
| 702 | Iterator<AccountInfo> accIt = mAccounts.values().iterator(); |
| 703 | while (accIt.hasNext()) { |
| 704 | AccountInfo acc = accIt.next(); |
| 705 | if (!ArrayUtils.contains(accounts, acc.account)) { |
| 706 | // This account no longer exists... |
| 707 | if (DEBUG) Log.w(TAG, "Account removed: " + acc.account); |
| 708 | for (AuthorityInfo auth : acc.authorities.values()) { |
| 709 | removing.put(auth.ident, auth); |
| 710 | } |
| 711 | accIt.remove(); |
| 712 | } |
| 713 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 714 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 715 | // Clean out all data structures. |
| 716 | int i = removing.size(); |
| 717 | if (i > 0) { |
| 718 | while (i > 0) { |
| 719 | i--; |
| 720 | int ident = removing.keyAt(i); |
| 721 | mAuthorities.remove(ident); |
| 722 | int j = mSyncStatus.size(); |
| 723 | while (j > 0) { |
| 724 | j--; |
| 725 | if (mSyncStatus.keyAt(j) == ident) { |
| 726 | mSyncStatus.remove(mSyncStatus.keyAt(j)); |
| 727 | } |
| 728 | } |
| 729 | j = mSyncHistory.size(); |
| 730 | while (j > 0) { |
| 731 | j--; |
| 732 | if (mSyncHistory.get(j).authorityId == ident) { |
| 733 | mSyncHistory.remove(j); |
| 734 | } |
| 735 | } |
| 736 | } |
| 737 | writeAccountInfoLocked(); |
| 738 | writeStatusLocked(); |
| 739 | writePendingOperationsLocked(); |
| 740 | writeStatisticsLocked(); |
| 741 | } |
| 742 | } |
| 743 | } |
| 744 | |
| 745 | /** |
| 746 | * Called when the currently active sync is changing (there can only be |
| 747 | * one at a time). Either supply a valid ActiveSyncContext with information |
| 748 | * about the sync, or null to stop the currently active sync. |
| 749 | */ |
| 750 | public void setActiveSync(SyncManager.ActiveSyncContext activeSyncContext) { |
| 751 | synchronized (mAuthorities) { |
| 752 | if (activeSyncContext != null) { |
| 753 | if (DEBUG) Log.v(TAG, "setActiveSync: account=" |
| 754 | + activeSyncContext.mSyncOperation.account |
| 755 | + " auth=" + activeSyncContext.mSyncOperation.authority |
| 756 | + " src=" + activeSyncContext.mSyncOperation.syncSource |
| 757 | + " extras=" + activeSyncContext.mSyncOperation.extras); |
| 758 | if (mActiveSync != null) { |
| 759 | Log.w(TAG, "setActiveSync called with existing active sync!"); |
| 760 | } |
| 761 | AuthorityInfo authority = getAuthorityLocked( |
| 762 | activeSyncContext.mSyncOperation.account, |
| 763 | activeSyncContext.mSyncOperation.authority, |
| 764 | "setActiveSync"); |
| 765 | if (authority == null) { |
| 766 | return; |
| 767 | } |
| 768 | mActiveSync = new ActiveSyncInfo(authority.ident, |
| 769 | authority.account, authority.authority, |
| 770 | activeSyncContext.mStartTime); |
| 771 | } else { |
| 772 | if (DEBUG) Log.v(TAG, "setActiveSync: null"); |
| 773 | mActiveSync = null; |
| 774 | } |
| 775 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 776 | |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 777 | reportChange(ContentResolver.SYNC_OBSERVER_TYPE_ACTIVE); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 778 | } |
| 779 | |
| 780 | /** |
| 781 | * To allow others to send active change reports, to poke clients. |
| 782 | */ |
| 783 | public void reportActiveChange() { |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 784 | reportChange(ContentResolver.SYNC_OBSERVER_TYPE_ACTIVE); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 785 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 786 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 787 | /** |
| 788 | * Note that sync has started for the given account and authority. |
| 789 | */ |
Dianne Hackborn | 7a13559 | 2009-05-06 00:28:37 -0700 | [diff] [blame] | 790 | public long insertStartSyncEvent(Account accountName, String authorityName, |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 791 | long now, int source) { |
| 792 | long id; |
| 793 | synchronized (mAuthorities) { |
| 794 | if (DEBUG) Log.v(TAG, "insertStartSyncEvent: account=" + accountName |
| 795 | + " auth=" + authorityName + " source=" + source); |
| 796 | AuthorityInfo authority = getAuthorityLocked(accountName, authorityName, |
| 797 | "insertStartSyncEvent"); |
| 798 | if (authority == null) { |
| 799 | return -1; |
| 800 | } |
| 801 | SyncHistoryItem item = new SyncHistoryItem(); |
| 802 | item.authorityId = authority.ident; |
| 803 | item.historyId = mNextHistoryId++; |
| 804 | if (mNextHistoryId < 0) mNextHistoryId = 0; |
| 805 | item.eventTime = now; |
| 806 | item.source = source; |
| 807 | item.event = EVENT_START; |
| 808 | mSyncHistory.add(0, item); |
| 809 | while (mSyncHistory.size() > MAX_HISTORY) { |
| 810 | mSyncHistory.remove(mSyncHistory.size()-1); |
| 811 | } |
| 812 | id = item.historyId; |
| 813 | if (DEBUG) Log.v(TAG, "returning historyId " + id); |
| 814 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 815 | |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 816 | reportChange(ContentResolver.SYNC_OBSERVER_TYPE_STATUS); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 817 | return id; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 818 | } |
| 819 | |
| 820 | public void stopSyncEvent(long historyId, long elapsedTime, String resultMessage, |
| 821 | long downstreamActivity, long upstreamActivity) { |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 822 | synchronized (mAuthorities) { |
| 823 | if (DEBUG) Log.v(TAG, "stopSyncEvent: historyId=" + historyId); |
| 824 | SyncHistoryItem item = null; |
| 825 | int i = mSyncHistory.size(); |
| 826 | while (i > 0) { |
| 827 | i--; |
| 828 | item = mSyncHistory.get(i); |
| 829 | if (item.historyId == historyId) { |
| 830 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 831 | } |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 832 | item = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 833 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 834 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 835 | if (item == null) { |
| 836 | Log.w(TAG, "stopSyncEvent: no history for id " + historyId); |
| 837 | return; |
| 838 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 839 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 840 | item.elapsedTime = elapsedTime; |
| 841 | item.event = EVENT_STOP; |
| 842 | item.mesg = resultMessage; |
| 843 | item.downstreamActivity = downstreamActivity; |
| 844 | item.upstreamActivity = upstreamActivity; |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 845 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 846 | SyncStatusInfo status = getOrCreateSyncStatusLocked(item.authorityId); |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 847 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 848 | status.numSyncs++; |
| 849 | status.totalElapsedTime += elapsedTime; |
| 850 | switch (item.source) { |
| 851 | case SOURCE_LOCAL: |
| 852 | status.numSourceLocal++; |
| 853 | break; |
| 854 | case SOURCE_POLL: |
| 855 | status.numSourcePoll++; |
| 856 | break; |
| 857 | case SOURCE_USER: |
| 858 | status.numSourceUser++; |
| 859 | break; |
| 860 | case SOURCE_SERVER: |
| 861 | status.numSourceServer++; |
| 862 | break; |
| 863 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 864 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 865 | boolean writeStatisticsNow = false; |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 866 | int day = getCurrentDayLocked(); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 867 | if (mDayStats[0] == null) { |
| 868 | mDayStats[0] = new DayStats(day); |
| 869 | } else if (day != mDayStats[0].day) { |
| 870 | System.arraycopy(mDayStats, 0, mDayStats, 1, mDayStats.length-1); |
| 871 | mDayStats[0] = new DayStats(day); |
| 872 | writeStatisticsNow = true; |
| 873 | } else if (mDayStats[0] == null) { |
| 874 | } |
| 875 | final DayStats ds = mDayStats[0]; |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 876 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 877 | final long lastSyncTime = (item.eventTime + elapsedTime); |
| 878 | boolean writeStatusNow = false; |
| 879 | if (MESG_SUCCESS.equals(resultMessage)) { |
| 880 | // - if successful, update the successful columns |
| 881 | if (status.lastSuccessTime == 0 || status.lastFailureTime != 0) { |
| 882 | writeStatusNow = true; |
| 883 | } |
| 884 | status.lastSuccessTime = lastSyncTime; |
| 885 | status.lastSuccessSource = item.source; |
| 886 | status.lastFailureTime = 0; |
| 887 | status.lastFailureSource = -1; |
| 888 | status.lastFailureMesg = null; |
| 889 | status.initialFailureTime = 0; |
| 890 | ds.successCount++; |
| 891 | ds.successTime += elapsedTime; |
| 892 | } else if (!MESG_CANCELED.equals(resultMessage)) { |
| 893 | if (status.lastFailureTime == 0) { |
| 894 | writeStatusNow = true; |
| 895 | } |
| 896 | status.lastFailureTime = lastSyncTime; |
| 897 | status.lastFailureSource = item.source; |
| 898 | status.lastFailureMesg = resultMessage; |
| 899 | if (status.initialFailureTime == 0) { |
| 900 | status.initialFailureTime = lastSyncTime; |
| 901 | } |
| 902 | ds.failureCount++; |
| 903 | ds.failureTime += elapsedTime; |
| 904 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 905 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 906 | if (writeStatusNow) { |
| 907 | writeStatusLocked(); |
| 908 | } else if (!hasMessages(MSG_WRITE_STATUS)) { |
| 909 | sendMessageDelayed(obtainMessage(MSG_WRITE_STATUS), |
| 910 | WRITE_STATUS_DELAY); |
| 911 | } |
| 912 | if (writeStatisticsNow) { |
| 913 | writeStatisticsLocked(); |
| 914 | } else if (!hasMessages(MSG_WRITE_STATISTICS)) { |
| 915 | sendMessageDelayed(obtainMessage(MSG_WRITE_STATISTICS), |
| 916 | WRITE_STATISTICS_DELAY); |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 917 | } |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 918 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 919 | |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 920 | reportChange(ContentResolver.SYNC_OBSERVER_TYPE_STATUS); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 921 | } |
| 922 | |
| 923 | /** |
| 924 | * Return the currently active sync information, or null if there is no |
| 925 | * active sync. Note that the returned object is the real, live active |
| 926 | * sync object, so be careful what you do with it. |
| 927 | */ |
| 928 | public ActiveSyncInfo getActiveSync() { |
| 929 | synchronized (mAuthorities) { |
| 930 | return mActiveSync; |
| 931 | } |
| 932 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 933 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 934 | /** |
| 935 | * Return an array of the current sync status for all authorities. Note |
| 936 | * that the objects inside the array are the real, live status objects, |
| 937 | * so be careful what you do with them. |
| 938 | */ |
| 939 | public ArrayList<SyncStatusInfo> getSyncStatus() { |
| 940 | synchronized (mAuthorities) { |
| 941 | final int N = mSyncStatus.size(); |
| 942 | ArrayList<SyncStatusInfo> ops = new ArrayList<SyncStatusInfo>(N); |
| 943 | for (int i=0; i<N; i++) { |
| 944 | ops.add(mSyncStatus.valueAt(i)); |
| 945 | } |
| 946 | return ops; |
| 947 | } |
| 948 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 949 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 950 | /** |
Costin Manolache | b752098 | 2009-09-02 18:03:05 -0700 | [diff] [blame] | 951 | * Returns the status that matches the authority and account. |
| 952 | * |
| 953 | * @param account the account we want to check |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 954 | * @param authority the authority whose row should be selected |
| 955 | * @return the SyncStatusInfo for the authority, or null if none exists |
| 956 | */ |
Costin Manolache | b752098 | 2009-09-02 18:03:05 -0700 | [diff] [blame] | 957 | public SyncStatusInfo getStatusByAccountAndAuthority(Account account, String authority) { |
| 958 | if (account == null || authority == null) { |
| 959 | throw new IllegalArgumentException(); |
| 960 | } |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 961 | synchronized (mAuthorities) { |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 962 | final int N = mSyncStatus.size(); |
| 963 | for (int i=0; i<N; i++) { |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 964 | SyncStatusInfo cur = mSyncStatus.valueAt(i); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 965 | AuthorityInfo ainfo = mAuthorities.get(cur.authorityId); |
Costin Manolache | b752098 | 2009-09-02 18:03:05 -0700 | [diff] [blame] | 966 | |
| 967 | if (ainfo != null && ainfo.authority.equals(authority) && |
| 968 | account.equals(ainfo.account)) { |
| 969 | return cur; |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 970 | } |
| 971 | } |
Costin Manolache | b752098 | 2009-09-02 18:03:05 -0700 | [diff] [blame] | 972 | return null; |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 973 | } |
| 974 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 975 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 976 | /** |
| 977 | * Return true if the pending status is true of any matching authorities. |
| 978 | */ |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 979 | public boolean isSyncPending(Account account, String authority) { |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 980 | synchronized (mAuthorities) { |
| 981 | final int N = mSyncStatus.size(); |
| 982 | for (int i=0; i<N; i++) { |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 983 | SyncStatusInfo cur = mSyncStatus.valueAt(i); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 984 | AuthorityInfo ainfo = mAuthorities.get(cur.authorityId); |
| 985 | if (ainfo == null) { |
| 986 | continue; |
| 987 | } |
| 988 | if (account != null && !ainfo.account.equals(account)) { |
| 989 | continue; |
| 990 | } |
| 991 | if (ainfo.authority.equals(authority) && cur.pending) { |
| 992 | return true; |
| 993 | } |
| 994 | } |
| 995 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 996 | } |
| 997 | } |
| 998 | |
| 999 | /** |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1000 | * Return an array of the current sync status for all authorities. Note |
| 1001 | * that the objects inside the array are the real, live status objects, |
| 1002 | * so be careful what you do with them. |
| 1003 | */ |
| 1004 | public ArrayList<SyncHistoryItem> getSyncHistory() { |
| 1005 | synchronized (mAuthorities) { |
| 1006 | final int N = mSyncHistory.size(); |
| 1007 | ArrayList<SyncHistoryItem> items = new ArrayList<SyncHistoryItem>(N); |
| 1008 | for (int i=0; i<N; i++) { |
| 1009 | items.add(mSyncHistory.get(i)); |
| 1010 | } |
| 1011 | return items; |
| 1012 | } |
| 1013 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 1014 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1015 | /** |
| 1016 | * Return an array of the current per-day statistics. Note |
| 1017 | * that the objects inside the array are the real, live status objects, |
| 1018 | * so be careful what you do with them. |
| 1019 | */ |
| 1020 | public DayStats[] getDayStatistics() { |
| 1021 | synchronized (mAuthorities) { |
| 1022 | DayStats[] ds = new DayStats[mDayStats.length]; |
| 1023 | System.arraycopy(mDayStats, 0, ds, 0, ds.length); |
| 1024 | return ds; |
| 1025 | } |
| 1026 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 1027 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1028 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1029 | * If sync is failing for any of the provider/accounts then determine the time at which it |
| 1030 | * started failing and return the earliest time over all the provider/accounts. If none are |
| 1031 | * failing then return 0. |
| 1032 | */ |
| 1033 | public long getInitialSyncFailureTime() { |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1034 | synchronized (mAuthorities) { |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 1035 | if (!mMasterSyncAutomatically) { |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1036 | return 0; |
| 1037 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 1038 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1039 | long oldest = 0; |
| 1040 | int i = mSyncStatus.size(); |
| 1041 | while (i > 0) { |
| 1042 | i--; |
| 1043 | SyncStatusInfo stats = mSyncStatus.valueAt(i); |
| 1044 | AuthorityInfo authority = mAuthorities.get(stats.authorityId); |
| 1045 | if (authority != null && authority.enabled) { |
| 1046 | if (oldest == 0 || stats.initialFailureTime < oldest) { |
| 1047 | oldest = stats.initialFailureTime; |
| 1048 | } |
| 1049 | } |
| 1050 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 1051 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1052 | return oldest; |
| 1053 | } |
| 1054 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 1055 | |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 1056 | private int getCurrentDayLocked() { |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1057 | mCal.setTimeInMillis(System.currentTimeMillis()); |
| 1058 | final int dayOfYear = mCal.get(Calendar.DAY_OF_YEAR); |
| 1059 | if (mYear != mCal.get(Calendar.YEAR)) { |
| 1060 | mYear = mCal.get(Calendar.YEAR); |
| 1061 | mCal.clear(); |
| 1062 | mCal.set(Calendar.YEAR, mYear); |
| 1063 | mYearInDays = (int)(mCal.getTimeInMillis()/86400000); |
| 1064 | } |
| 1065 | return dayOfYear + mYearInDays; |
| 1066 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 1067 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1068 | /** |
| 1069 | * Retrieve an authority, returning null if one does not exist. |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 1070 | * |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1071 | * @param accountName The name of the account for the authority. |
| 1072 | * @param authorityName The name of the authority itself. |
| 1073 | * @param tag If non-null, this will be used in a log message if the |
| 1074 | * requested authority does not exist. |
| 1075 | */ |
Dianne Hackborn | 7a13559 | 2009-05-06 00:28:37 -0700 | [diff] [blame] | 1076 | private AuthorityInfo getAuthorityLocked(Account accountName, String authorityName, |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1077 | String tag) { |
| 1078 | AccountInfo account = mAccounts.get(accountName); |
| 1079 | if (account == null) { |
| 1080 | if (tag != null) { |
Fred Quintana | b763ab2 | 2009-08-18 18:07:30 -0700 | [diff] [blame] | 1081 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1082 | Log.v(TAG, tag + ": unknown account " + accountName); |
| 1083 | } |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1084 | } |
| 1085 | return null; |
| 1086 | } |
| 1087 | AuthorityInfo authority = account.authorities.get(authorityName); |
| 1088 | if (authority == null) { |
| 1089 | if (tag != null) { |
Fred Quintana | b763ab2 | 2009-08-18 18:07:30 -0700 | [diff] [blame] | 1090 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1091 | Log.v(TAG, tag + ": unknown authority " + authorityName); |
| 1092 | } |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1093 | } |
| 1094 | return null; |
| 1095 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 1096 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1097 | return authority; |
| 1098 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 1099 | |
Dianne Hackborn | 7a13559 | 2009-05-06 00:28:37 -0700 | [diff] [blame] | 1100 | private AuthorityInfo getOrCreateAuthorityLocked(Account accountName, |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1101 | String authorityName, int ident, boolean doWrite) { |
| 1102 | AccountInfo account = mAccounts.get(accountName); |
| 1103 | if (account == null) { |
| 1104 | account = new AccountInfo(accountName); |
| 1105 | mAccounts.put(accountName, account); |
| 1106 | } |
| 1107 | AuthorityInfo authority = account.authorities.get(authorityName); |
| 1108 | if (authority == null) { |
| 1109 | if (ident < 0) { |
| 1110 | // Look for a new identifier for this authority. |
| 1111 | final int N = mAuthorities.size(); |
| 1112 | ident = 0; |
| 1113 | for (int i=0; i<N; i++) { |
| 1114 | if (mAuthorities.valueAt(i).ident > ident) { |
| 1115 | break; |
| 1116 | } |
| 1117 | ident++; |
| 1118 | } |
| 1119 | } |
Dianne Hackborn | bd0a81f | 2009-10-04 13:30:50 -0700 | [diff] [blame] | 1120 | if (DEBUG) Log.v(TAG, "created a new AuthorityInfo for " + accountName |
Fred Quintana | b763ab2 | 2009-08-18 18:07:30 -0700 | [diff] [blame] | 1121 | + ", provider " + authorityName); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1122 | authority = new AuthorityInfo(accountName, authorityName, ident); |
| 1123 | account.authorities.put(authorityName, authority); |
| 1124 | mAuthorities.put(ident, authority); |
| 1125 | if (doWrite) { |
| 1126 | writeAccountInfoLocked(); |
| 1127 | } |
| 1128 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 1129 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1130 | return authority; |
| 1131 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 1132 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1133 | private SyncStatusInfo getOrCreateSyncStatusLocked(int authorityId) { |
| 1134 | SyncStatusInfo status = mSyncStatus.get(authorityId); |
| 1135 | if (status == null) { |
| 1136 | status = new SyncStatusInfo(authorityId); |
| 1137 | mSyncStatus.put(authorityId, status); |
| 1138 | } |
| 1139 | return status; |
| 1140 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 1141 | |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 1142 | public void writeAllState() { |
| 1143 | synchronized (mAuthorities) { |
| 1144 | // Account info is always written so no need to do it here. |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 1145 | |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 1146 | if (mNumPendingFinished > 0) { |
| 1147 | // Only write these if they are out of date. |
| 1148 | writePendingOperationsLocked(); |
| 1149 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 1150 | |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 1151 | // Just always write these... they are likely out of date. |
| 1152 | writeStatusLocked(); |
| 1153 | writeStatisticsLocked(); |
| 1154 | } |
| 1155 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 1156 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1157 | /** |
| 1158 | * Read all account information back in to the initial engine state. |
| 1159 | */ |
| 1160 | private void readAccountInfoLocked() { |
| 1161 | FileInputStream fis = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1162 | try { |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1163 | fis = mAccountInfoFile.openRead(); |
| 1164 | if (DEBUG_FILE) Log.v(TAG, "Reading " + mAccountInfoFile.getBaseFile()); |
| 1165 | XmlPullParser parser = Xml.newPullParser(); |
| 1166 | parser.setInput(fis, null); |
| 1167 | int eventType = parser.getEventType(); |
| 1168 | while (eventType != XmlPullParser.START_TAG) { |
| 1169 | eventType = parser.next(); |
| 1170 | } |
| 1171 | String tagName = parser.getName(); |
| 1172 | if ("accounts".equals(tagName)) { |
| 1173 | String listen = parser.getAttributeValue( |
| 1174 | null, "listen-for-tickles"); |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 1175 | mMasterSyncAutomatically = listen == null |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1176 | || Boolean.parseBoolean(listen); |
| 1177 | eventType = parser.next(); |
| 1178 | do { |
| 1179 | if (eventType == XmlPullParser.START_TAG |
| 1180 | && parser.getDepth() == 2) { |
| 1181 | tagName = parser.getName(); |
| 1182 | if ("authority".equals(tagName)) { |
| 1183 | int id = -1; |
| 1184 | try { |
| 1185 | id = Integer.parseInt(parser.getAttributeValue( |
| 1186 | null, "id")); |
| 1187 | } catch (NumberFormatException e) { |
| 1188 | } catch (NullPointerException e) { |
| 1189 | } |
| 1190 | if (id >= 0) { |
| 1191 | String accountName = parser.getAttributeValue( |
| 1192 | null, "account"); |
Dianne Hackborn | 7a13559 | 2009-05-06 00:28:37 -0700 | [diff] [blame] | 1193 | String accountType = parser.getAttributeValue( |
| 1194 | null, "type"); |
| 1195 | if (accountType == null) { |
Costin Manolache | 3348f14 | 2009-09-29 18:58:36 -0700 | [diff] [blame] | 1196 | accountType = "com.google"; |
Dianne Hackborn | 7a13559 | 2009-05-06 00:28:37 -0700 | [diff] [blame] | 1197 | } |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1198 | String authorityName = parser.getAttributeValue( |
| 1199 | null, "authority"); |
| 1200 | String enabled = parser.getAttributeValue( |
| 1201 | null, "enabled"); |
Fred Quintana | 5e787c4 | 2009-08-16 23:13:53 -0700 | [diff] [blame] | 1202 | String syncable = parser.getAttributeValue(null, "syncable"); |
| 1203 | AuthorityInfo authority = mAuthorities.get(id); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1204 | if (DEBUG_FILE) Log.v(TAG, "Adding authority: account=" |
| 1205 | + accountName + " auth=" + authorityName |
Fred Quintana | 5e787c4 | 2009-08-16 23:13:53 -0700 | [diff] [blame] | 1206 | + " enabled=" + enabled |
| 1207 | + " syncable=" + syncable); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1208 | if (authority == null) { |
| 1209 | if (DEBUG_FILE) Log.v(TAG, "Creating entry"); |
| 1210 | authority = getOrCreateAuthorityLocked( |
Dianne Hackborn | 7a13559 | 2009-05-06 00:28:37 -0700 | [diff] [blame] | 1211 | new Account(accountName, accountType), |
| 1212 | authorityName, id, false); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1213 | } |
| 1214 | if (authority != null) { |
| 1215 | authority.enabled = enabled == null |
| 1216 | || Boolean.parseBoolean(enabled); |
Fred Quintana | 5e787c4 | 2009-08-16 23:13:53 -0700 | [diff] [blame] | 1217 | if ("unknown".equals(syncable)) { |
| 1218 | authority.syncable = -1; |
| 1219 | } else { |
| 1220 | authority.syncable = |
| 1221 | (syncable == null || Boolean.parseBoolean(enabled)) |
| 1222 | ? 1 |
| 1223 | : 0; |
| 1224 | } |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1225 | } else { |
| 1226 | Log.w(TAG, "Failure adding authority: account=" |
| 1227 | + accountName + " auth=" + authorityName |
Fred Quintana | 5e787c4 | 2009-08-16 23:13:53 -0700 | [diff] [blame] | 1228 | + " enabled=" + enabled |
| 1229 | + " syncable=" + syncable); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1230 | } |
| 1231 | } |
| 1232 | } |
| 1233 | } |
| 1234 | eventType = parser.next(); |
| 1235 | } while (eventType != XmlPullParser.END_DOCUMENT); |
| 1236 | } |
| 1237 | } catch (XmlPullParserException e) { |
| 1238 | Log.w(TAG, "Error reading accounts", e); |
| 1239 | } catch (java.io.IOException e) { |
| 1240 | if (fis == null) Log.i(TAG, "No initial accounts"); |
| 1241 | else Log.w(TAG, "Error reading accounts", e); |
| 1242 | } finally { |
| 1243 | if (fis != null) { |
| 1244 | try { |
| 1245 | fis.close(); |
| 1246 | } catch (java.io.IOException e1) { |
| 1247 | } |
| 1248 | } |
| 1249 | } |
| 1250 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 1251 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1252 | /** |
| 1253 | * Write all account information to the account file. |
| 1254 | */ |
| 1255 | private void writeAccountInfoLocked() { |
| 1256 | if (DEBUG_FILE) Log.v(TAG, "Writing new " + mAccountInfoFile.getBaseFile()); |
| 1257 | FileOutputStream fos = null; |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 1258 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1259 | try { |
| 1260 | fos = mAccountInfoFile.startWrite(); |
| 1261 | XmlSerializer out = new FastXmlSerializer(); |
| 1262 | out.setOutput(fos, "utf-8"); |
| 1263 | out.startDocument(null, true); |
| 1264 | out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true); |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 1265 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1266 | out.startTag(null, "accounts"); |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 1267 | if (!mMasterSyncAutomatically) { |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1268 | out.attribute(null, "listen-for-tickles", "false"); |
| 1269 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 1270 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1271 | final int N = mAuthorities.size(); |
| 1272 | for (int i=0; i<N; i++) { |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 1273 | AuthorityInfo authority = mAuthorities.valueAt(i); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1274 | out.startTag(null, "authority"); |
| 1275 | out.attribute(null, "id", Integer.toString(authority.ident)); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1276 | out.attribute(null, "account", authority.account.name); |
| 1277 | out.attribute(null, "type", authority.account.type); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1278 | out.attribute(null, "authority", authority.authority); |
| 1279 | if (!authority.enabled) { |
| 1280 | out.attribute(null, "enabled", "false"); |
| 1281 | } |
Fred Quintana | 5e787c4 | 2009-08-16 23:13:53 -0700 | [diff] [blame] | 1282 | if (authority.syncable < 0) { |
| 1283 | out.attribute(null, "syncable", "unknown"); |
| 1284 | } else if (authority.syncable == 0) { |
| 1285 | out.attribute(null, "syncable", "false"); |
| 1286 | } |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1287 | out.endTag(null, "authority"); |
| 1288 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 1289 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1290 | out.endTag(null, "accounts"); |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 1291 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1292 | out.endDocument(); |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 1293 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1294 | mAccountInfoFile.finishWrite(fos); |
| 1295 | } catch (java.io.IOException e1) { |
| 1296 | Log.w(TAG, "Error writing accounts", e1); |
| 1297 | if (fos != null) { |
| 1298 | mAccountInfoFile.failWrite(fos); |
| 1299 | } |
| 1300 | } |
| 1301 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 1302 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1303 | static int getIntColumn(Cursor c, String name) { |
| 1304 | return c.getInt(c.getColumnIndex(name)); |
| 1305 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 1306 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1307 | static long getLongColumn(Cursor c, String name) { |
| 1308 | return c.getLong(c.getColumnIndex(name)); |
| 1309 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 1310 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1311 | /** |
| 1312 | * Load sync engine state from the old syncmanager database, and then |
| 1313 | * erase it. Note that we don't deal with pending operations, active |
| 1314 | * sync, or history. |
| 1315 | */ |
| 1316 | private void readLegacyAccountInfoLocked() { |
| 1317 | // Look for old database to initialize from. |
| 1318 | File file = mContext.getDatabasePath("syncmanager.db"); |
| 1319 | if (!file.exists()) { |
| 1320 | return; |
| 1321 | } |
| 1322 | String path = file.getPath(); |
| 1323 | SQLiteDatabase db = null; |
| 1324 | try { |
| 1325 | db = SQLiteDatabase.openDatabase(path, null, |
| 1326 | SQLiteDatabase.OPEN_READONLY); |
| 1327 | } catch (SQLiteException e) { |
| 1328 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 1329 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1330 | if (db != null) { |
Dianne Hackborn | 2d5ed1f | 2009-05-06 15:22:38 -0700 | [diff] [blame] | 1331 | final boolean hasType = db.getVersion() >= 11; |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 1332 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1333 | // Copy in all of the status information, as well as accounts. |
| 1334 | if (DEBUG_FILE) Log.v(TAG, "Reading legacy sync accounts db"); |
| 1335 | SQLiteQueryBuilder qb = new SQLiteQueryBuilder(); |
| 1336 | qb.setTables("stats, status"); |
| 1337 | HashMap<String,String> map = new HashMap<String,String>(); |
| 1338 | map.put("_id", "status._id as _id"); |
| 1339 | map.put("account", "stats.account as account"); |
Dianne Hackborn | 2d5ed1f | 2009-05-06 15:22:38 -0700 | [diff] [blame] | 1340 | if (hasType) { |
| 1341 | map.put("account_type", "stats.account_type as account_type"); |
| 1342 | } |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1343 | map.put("authority", "stats.authority as authority"); |
| 1344 | map.put("totalElapsedTime", "totalElapsedTime"); |
| 1345 | map.put("numSyncs", "numSyncs"); |
| 1346 | map.put("numSourceLocal", "numSourceLocal"); |
| 1347 | map.put("numSourcePoll", "numSourcePoll"); |
| 1348 | map.put("numSourceServer", "numSourceServer"); |
| 1349 | map.put("numSourceUser", "numSourceUser"); |
| 1350 | map.put("lastSuccessSource", "lastSuccessSource"); |
| 1351 | map.put("lastSuccessTime", "lastSuccessTime"); |
| 1352 | map.put("lastFailureSource", "lastFailureSource"); |
| 1353 | map.put("lastFailureTime", "lastFailureTime"); |
| 1354 | map.put("lastFailureMesg", "lastFailureMesg"); |
| 1355 | map.put("pending", "pending"); |
| 1356 | qb.setProjectionMap(map); |
| 1357 | qb.appendWhere("stats._id = status.stats_id"); |
| 1358 | 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] | 1359 | while (c.moveToNext()) { |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1360 | String accountName = c.getString(c.getColumnIndex("account")); |
Dianne Hackborn | 2d5ed1f | 2009-05-06 15:22:38 -0700 | [diff] [blame] | 1361 | String accountType = hasType |
| 1362 | ? c.getString(c.getColumnIndex("account_type")) : null; |
Dianne Hackborn | 7a13559 | 2009-05-06 00:28:37 -0700 | [diff] [blame] | 1363 | if (accountType == null) { |
Costin Manolache | 3348f14 | 2009-09-29 18:58:36 -0700 | [diff] [blame] | 1364 | accountType = "com.google"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1365 | } |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1366 | String authorityName = c.getString(c.getColumnIndex("authority")); |
| 1367 | AuthorityInfo authority = this.getOrCreateAuthorityLocked( |
Dianne Hackborn | 7a13559 | 2009-05-06 00:28:37 -0700 | [diff] [blame] | 1368 | new Account(accountName, accountType), |
| 1369 | authorityName, -1, false); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1370 | if (authority != null) { |
| 1371 | int i = mSyncStatus.size(); |
| 1372 | boolean found = false; |
| 1373 | SyncStatusInfo st = null; |
| 1374 | while (i > 0) { |
| 1375 | i--; |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 1376 | st = mSyncStatus.valueAt(i); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1377 | if (st.authorityId == authority.ident) { |
| 1378 | found = true; |
| 1379 | break; |
| 1380 | } |
| 1381 | } |
| 1382 | if (!found) { |
| 1383 | st = new SyncStatusInfo(authority.ident); |
| 1384 | mSyncStatus.put(authority.ident, st); |
| 1385 | } |
| 1386 | st.totalElapsedTime = getLongColumn(c, "totalElapsedTime"); |
| 1387 | st.numSyncs = getIntColumn(c, "numSyncs"); |
| 1388 | st.numSourceLocal = getIntColumn(c, "numSourceLocal"); |
| 1389 | st.numSourcePoll = getIntColumn(c, "numSourcePoll"); |
| 1390 | st.numSourceServer = getIntColumn(c, "numSourceServer"); |
| 1391 | st.numSourceUser = getIntColumn(c, "numSourceUser"); |
| 1392 | st.lastSuccessSource = getIntColumn(c, "lastSuccessSource"); |
| 1393 | st.lastSuccessTime = getLongColumn(c, "lastSuccessTime"); |
| 1394 | st.lastFailureSource = getIntColumn(c, "lastFailureSource"); |
| 1395 | st.lastFailureTime = getLongColumn(c, "lastFailureTime"); |
| 1396 | st.lastFailureMesg = c.getString(c.getColumnIndex("lastFailureMesg")); |
| 1397 | st.pending = getIntColumn(c, "pending") != 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1398 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1399 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 1400 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1401 | c.close(); |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 1402 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1403 | // Retrieve the settings. |
| 1404 | qb = new SQLiteQueryBuilder(); |
| 1405 | qb.setTables("settings"); |
| 1406 | c = qb.query(db, null, null, null, null, null, null); |
| 1407 | while (c.moveToNext()) { |
| 1408 | String name = c.getString(c.getColumnIndex("name")); |
| 1409 | String value = c.getString(c.getColumnIndex("value")); |
| 1410 | if (name == null) continue; |
| 1411 | if (name.equals("listen_for_tickles")) { |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 1412 | setMasterSyncAutomatically(value == null || Boolean.parseBoolean(value)); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1413 | } else if (name.startsWith("sync_provider_")) { |
| 1414 | String provider = name.substring("sync_provider_".length(), |
| 1415 | name.length()); |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 1416 | int i = mAuthorities.size(); |
| 1417 | while (i > 0) { |
| 1418 | i--; |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 1419 | AuthorityInfo authority = mAuthorities.valueAt(i); |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 1420 | if (authority.authority.equals(provider)) { |
| 1421 | authority.enabled = value == null || Boolean.parseBoolean(value); |
Fred Quintana | 5e787c4 | 2009-08-16 23:13:53 -0700 | [diff] [blame] | 1422 | authority.syncable = 1; |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 1423 | } |
| 1424 | } |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1425 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1426 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 1427 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1428 | c.close(); |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 1429 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1430 | db.close(); |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 1431 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1432 | writeAccountInfoLocked(); |
| 1433 | writeStatusLocked(); |
| 1434 | (new File(path)).delete(); |
| 1435 | } |
| 1436 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 1437 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1438 | public static final int STATUS_FILE_END = 0; |
| 1439 | public static final int STATUS_FILE_ITEM = 100; |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 1440 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1441 | /** |
| 1442 | * Read all sync status back in to the initial engine state. |
| 1443 | */ |
| 1444 | private void readStatusLocked() { |
| 1445 | if (DEBUG_FILE) Log.v(TAG, "Reading " + mStatusFile.getBaseFile()); |
| 1446 | try { |
| 1447 | byte[] data = mStatusFile.readFully(); |
| 1448 | Parcel in = Parcel.obtain(); |
| 1449 | in.unmarshall(data, 0, data.length); |
| 1450 | in.setDataPosition(0); |
| 1451 | int token; |
| 1452 | while ((token=in.readInt()) != STATUS_FILE_END) { |
| 1453 | if (token == STATUS_FILE_ITEM) { |
| 1454 | SyncStatusInfo status = new SyncStatusInfo(in); |
| 1455 | if (mAuthorities.indexOfKey(status.authorityId) >= 0) { |
| 1456 | status.pending = false; |
| 1457 | if (DEBUG_FILE) Log.v(TAG, "Adding status for id " |
| 1458 | + status.authorityId); |
| 1459 | mSyncStatus.put(status.authorityId, status); |
| 1460 | } |
| 1461 | } else { |
| 1462 | // Ooops. |
| 1463 | Log.w(TAG, "Unknown status token: " + token); |
| 1464 | break; |
| 1465 | } |
| 1466 | } |
| 1467 | } catch (java.io.IOException e) { |
| 1468 | Log.i(TAG, "No initial status"); |
| 1469 | } |
| 1470 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 1471 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1472 | /** |
| 1473 | * Write all sync status to the sync status file. |
| 1474 | */ |
| 1475 | private void writeStatusLocked() { |
| 1476 | if (DEBUG_FILE) Log.v(TAG, "Writing new " + mStatusFile.getBaseFile()); |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 1477 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1478 | // The file is being written, so we don't need to have a scheduled |
| 1479 | // write until the next change. |
| 1480 | removeMessages(MSG_WRITE_STATUS); |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 1481 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1482 | FileOutputStream fos = null; |
| 1483 | try { |
| 1484 | fos = mStatusFile.startWrite(); |
| 1485 | Parcel out = Parcel.obtain(); |
| 1486 | final int N = mSyncStatus.size(); |
| 1487 | for (int i=0; i<N; i++) { |
| 1488 | SyncStatusInfo status = mSyncStatus.valueAt(i); |
| 1489 | out.writeInt(STATUS_FILE_ITEM); |
| 1490 | status.writeToParcel(out, 0); |
| 1491 | } |
| 1492 | out.writeInt(STATUS_FILE_END); |
| 1493 | fos.write(out.marshall()); |
| 1494 | out.recycle(); |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 1495 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1496 | mStatusFile.finishWrite(fos); |
| 1497 | } catch (java.io.IOException e1) { |
| 1498 | Log.w(TAG, "Error writing status", e1); |
| 1499 | if (fos != null) { |
| 1500 | mStatusFile.failWrite(fos); |
| 1501 | } |
| 1502 | } |
| 1503 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 1504 | |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame^] | 1505 | public static final int PENDING_OPERATION_VERSION = 2; |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 1506 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1507 | /** |
| 1508 | * Read all pending operations back in to the initial engine state. |
| 1509 | */ |
| 1510 | private void readPendingOperationsLocked() { |
| 1511 | if (DEBUG_FILE) Log.v(TAG, "Reading " + mPendingFile.getBaseFile()); |
| 1512 | try { |
| 1513 | byte[] data = mPendingFile.readFully(); |
| 1514 | Parcel in = Parcel.obtain(); |
| 1515 | in.unmarshall(data, 0, data.length); |
| 1516 | in.setDataPosition(0); |
| 1517 | final int SIZE = in.dataSize(); |
| 1518 | while (in.dataPosition() < SIZE) { |
| 1519 | int version = in.readInt(); |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame^] | 1520 | if (version != PENDING_OPERATION_VERSION && version != 1) { |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1521 | Log.w(TAG, "Unknown pending operation version " |
| 1522 | + version + "; dropping all ops"); |
| 1523 | break; |
| 1524 | } |
| 1525 | int authorityId = in.readInt(); |
| 1526 | int syncSource = in.readInt(); |
| 1527 | byte[] flatExtras = in.createByteArray(); |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame^] | 1528 | boolean expedited; |
| 1529 | if (version == PENDING_OPERATION_VERSION) { |
| 1530 | expedited = in.readInt() != 0; |
| 1531 | } else { |
| 1532 | expedited = false; |
| 1533 | } |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1534 | AuthorityInfo authority = mAuthorities.get(authorityId); |
| 1535 | if (authority != null) { |
| 1536 | Bundle extras = null; |
| 1537 | if (flatExtras != null) { |
| 1538 | extras = unflattenBundle(flatExtras); |
| 1539 | } |
| 1540 | PendingOperation op = new PendingOperation( |
| 1541 | authority.account, syncSource, |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame^] | 1542 | authority.authority, extras, expedited); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1543 | op.authorityId = authorityId; |
| 1544 | op.flatExtras = flatExtras; |
| 1545 | if (DEBUG_FILE) Log.v(TAG, "Adding pending op: account=" + op.account |
| 1546 | + " auth=" + op.authority |
| 1547 | + " src=" + op.syncSource |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame^] | 1548 | + " expedited=" + op.expedited |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1549 | + " extras=" + op.extras); |
| 1550 | mPendingOperations.add(op); |
| 1551 | } |
| 1552 | } |
| 1553 | } catch (java.io.IOException e) { |
| 1554 | Log.i(TAG, "No initial pending operations"); |
| 1555 | } |
| 1556 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 1557 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1558 | private void writePendingOperationLocked(PendingOperation op, Parcel out) { |
| 1559 | out.writeInt(PENDING_OPERATION_VERSION); |
| 1560 | out.writeInt(op.authorityId); |
| 1561 | out.writeInt(op.syncSource); |
| 1562 | if (op.flatExtras == null && op.extras != null) { |
| 1563 | op.flatExtras = flattenBundle(op.extras); |
| 1564 | } |
| 1565 | out.writeByteArray(op.flatExtras); |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame^] | 1566 | out.writeInt(op.expedited ? 1 : 0); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1567 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 1568 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1569 | /** |
| 1570 | * Write all currently pending ops to the pending ops file. |
| 1571 | */ |
| 1572 | private void writePendingOperationsLocked() { |
| 1573 | final int N = mPendingOperations.size(); |
| 1574 | FileOutputStream fos = null; |
| 1575 | try { |
| 1576 | if (N == 0) { |
| 1577 | if (DEBUG_FILE) Log.v(TAG, "Truncating " + mPendingFile.getBaseFile()); |
| 1578 | mPendingFile.truncate(); |
| 1579 | return; |
| 1580 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 1581 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1582 | if (DEBUG_FILE) Log.v(TAG, "Writing new " + mPendingFile.getBaseFile()); |
| 1583 | fos = mPendingFile.startWrite(); |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 1584 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1585 | Parcel out = Parcel.obtain(); |
| 1586 | for (int i=0; i<N; i++) { |
| 1587 | PendingOperation op = mPendingOperations.get(i); |
| 1588 | writePendingOperationLocked(op, out); |
| 1589 | } |
| 1590 | fos.write(out.marshall()); |
| 1591 | out.recycle(); |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 1592 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1593 | mPendingFile.finishWrite(fos); |
| 1594 | } catch (java.io.IOException e1) { |
| 1595 | Log.w(TAG, "Error writing pending operations", e1); |
| 1596 | if (fos != null) { |
| 1597 | mPendingFile.failWrite(fos); |
| 1598 | } |
| 1599 | } |
| 1600 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 1601 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1602 | /** |
| 1603 | * Append the given operation to the pending ops file; if unable to, |
| 1604 | * write all pending ops. |
| 1605 | */ |
| 1606 | private void appendPendingOperationLocked(PendingOperation op) { |
| 1607 | if (DEBUG_FILE) Log.v(TAG, "Appending to " + mPendingFile.getBaseFile()); |
| 1608 | FileOutputStream fos = null; |
| 1609 | try { |
| 1610 | fos = mPendingFile.openAppend(); |
| 1611 | } catch (java.io.IOException e) { |
| 1612 | if (DEBUG_FILE) Log.v(TAG, "Failed append; writing full file"); |
| 1613 | writePendingOperationsLocked(); |
| 1614 | return; |
| 1615 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 1616 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1617 | try { |
| 1618 | Parcel out = Parcel.obtain(); |
| 1619 | writePendingOperationLocked(op, out); |
| 1620 | fos.write(out.marshall()); |
| 1621 | out.recycle(); |
| 1622 | } catch (java.io.IOException e1) { |
| 1623 | Log.w(TAG, "Error writing pending operations", e1); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1624 | } finally { |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1625 | try { |
| 1626 | fos.close(); |
| 1627 | } catch (java.io.IOException e2) { |
| 1628 | } |
| 1629 | } |
| 1630 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 1631 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1632 | static private byte[] flattenBundle(Bundle bundle) { |
| 1633 | byte[] flatData = null; |
| 1634 | Parcel parcel = Parcel.obtain(); |
| 1635 | try { |
| 1636 | bundle.writeToParcel(parcel, 0); |
| 1637 | flatData = parcel.marshall(); |
| 1638 | } finally { |
| 1639 | parcel.recycle(); |
| 1640 | } |
| 1641 | return flatData; |
| 1642 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 1643 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1644 | static private Bundle unflattenBundle(byte[] flatData) { |
| 1645 | Bundle bundle; |
| 1646 | Parcel parcel = Parcel.obtain(); |
| 1647 | try { |
| 1648 | parcel.unmarshall(flatData, 0, flatData.length); |
| 1649 | parcel.setDataPosition(0); |
| 1650 | bundle = parcel.readBundle(); |
| 1651 | } catch (RuntimeException e) { |
| 1652 | // A RuntimeException is thrown if we were unable to parse the parcel. |
| 1653 | // Create an empty parcel in this case. |
| 1654 | bundle = new Bundle(); |
| 1655 | } finally { |
| 1656 | parcel.recycle(); |
| 1657 | } |
| 1658 | return bundle; |
| 1659 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 1660 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1661 | public static final int STATISTICS_FILE_END = 0; |
| 1662 | public static final int STATISTICS_FILE_ITEM_OLD = 100; |
| 1663 | public static final int STATISTICS_FILE_ITEM = 101; |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 1664 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1665 | /** |
| 1666 | * Read all sync statistics back in to the initial engine state. |
| 1667 | */ |
| 1668 | private void readStatisticsLocked() { |
| 1669 | try { |
| 1670 | byte[] data = mStatisticsFile.readFully(); |
| 1671 | Parcel in = Parcel.obtain(); |
| 1672 | in.unmarshall(data, 0, data.length); |
| 1673 | in.setDataPosition(0); |
| 1674 | int token; |
| 1675 | int index = 0; |
| 1676 | while ((token=in.readInt()) != STATISTICS_FILE_END) { |
| 1677 | if (token == STATISTICS_FILE_ITEM |
| 1678 | || token == STATISTICS_FILE_ITEM_OLD) { |
| 1679 | int day = in.readInt(); |
| 1680 | if (token == STATISTICS_FILE_ITEM_OLD) { |
| 1681 | day = day - 2009 + 14245; // Magic! |
| 1682 | } |
| 1683 | DayStats ds = new DayStats(day); |
| 1684 | ds.successCount = in.readInt(); |
| 1685 | ds.successTime = in.readLong(); |
| 1686 | ds.failureCount = in.readInt(); |
| 1687 | ds.failureTime = in.readLong(); |
| 1688 | if (index < mDayStats.length) { |
| 1689 | mDayStats[index] = ds; |
| 1690 | index++; |
| 1691 | } |
| 1692 | } else { |
| 1693 | // Ooops. |
| 1694 | Log.w(TAG, "Unknown stats token: " + token); |
| 1695 | break; |
| 1696 | } |
| 1697 | } |
| 1698 | } catch (java.io.IOException e) { |
| 1699 | Log.i(TAG, "No initial statistics"); |
| 1700 | } |
| 1701 | } |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 1702 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1703 | /** |
| 1704 | * Write all sync statistics to the sync status file. |
| 1705 | */ |
| 1706 | private void writeStatisticsLocked() { |
| 1707 | if (DEBUG_FILE) Log.v(TAG, "Writing new " + mStatisticsFile.getBaseFile()); |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 1708 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1709 | // The file is being written, so we don't need to have a scheduled |
| 1710 | // write until the next change. |
| 1711 | removeMessages(MSG_WRITE_STATISTICS); |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 1712 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1713 | FileOutputStream fos = null; |
| 1714 | try { |
| 1715 | fos = mStatisticsFile.startWrite(); |
| 1716 | Parcel out = Parcel.obtain(); |
| 1717 | final int N = mDayStats.length; |
| 1718 | for (int i=0; i<N; i++) { |
| 1719 | DayStats ds = mDayStats[i]; |
| 1720 | if (ds == null) { |
| 1721 | break; |
| 1722 | } |
| 1723 | out.writeInt(STATISTICS_FILE_ITEM); |
| 1724 | out.writeInt(ds.day); |
| 1725 | out.writeInt(ds.successCount); |
| 1726 | out.writeLong(ds.successTime); |
| 1727 | out.writeInt(ds.failureCount); |
| 1728 | out.writeLong(ds.failureTime); |
| 1729 | } |
| 1730 | out.writeInt(STATISTICS_FILE_END); |
| 1731 | fos.write(out.marshall()); |
| 1732 | out.recycle(); |
Costin Manolache | 360e454 | 2009-09-04 13:36:04 -0700 | [diff] [blame] | 1733 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1734 | mStatisticsFile.finishWrite(fos); |
| 1735 | } catch (java.io.IOException e1) { |
| 1736 | Log.w(TAG, "Error writing stats", e1); |
| 1737 | if (fos != null) { |
| 1738 | mStatisticsFile.failWrite(fos); |
| 1739 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1740 | } |
| 1741 | } |
| 1742 | } |