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