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