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