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