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