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