Kenny Root | 15a4d2f | 2010-03-11 18:20:12 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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; |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 18 | |
| 19 | import android.accounts.Account; |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 20 | import android.content.pm.PackageManager; |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 21 | import android.content.ComponentName; |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 22 | import android.content.ContentResolver; |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 23 | import android.os.Bundle; |
| 24 | import android.os.SystemClock; |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 25 | import android.util.Log; |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 26 | |
| 27 | /** |
| 28 | * Value type that represents a sync operation. |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 29 | * TODO: This is the class to flesh out with all the scheduling data - metered/unmetered, |
| 30 | * transfer-size, etc. |
| 31 | * {@hide} |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 32 | */ |
| 33 | public class SyncOperation implements Comparable { |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 34 | public static final String TAG = "SyncManager"; |
| 35 | |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 36 | public static final int REASON_BACKGROUND_DATA_SETTINGS_CHANGED = -1; |
| 37 | public static final int REASON_ACCOUNTS_UPDATED = -2; |
| 38 | public static final int REASON_SERVICE_CHANGED = -3; |
| 39 | public static final int REASON_PERIODIC = -4; |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 40 | /** Sync started because it has just been set to isSyncable. */ |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 41 | public static final int REASON_IS_SYNCABLE = -5; |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 42 | /** Sync started because it has just been set to sync automatically. */ |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 43 | public static final int REASON_SYNC_AUTO = -6; |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 44 | /** Sync started because master sync automatically has been set to true. */ |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 45 | public static final int REASON_MASTER_SYNC_AUTO = -7; |
| 46 | public static final int REASON_USER_START = -8; |
| 47 | |
| 48 | private static String[] REASON_NAMES = new String[] { |
| 49 | "DataSettingsChanged", |
| 50 | "AccountsUpdated", |
| 51 | "ServiceChanged", |
| 52 | "Periodic", |
| 53 | "IsSyncable", |
| 54 | "AutoSync", |
| 55 | "MasterSyncAuto", |
| 56 | "UserStart", |
| 57 | }; |
| 58 | |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 59 | public static final int SYNC_TARGET_UNKNOWN = 0; |
| 60 | public static final int SYNC_TARGET_ADAPTER = 1; |
| 61 | public static final int SYNC_TARGET_SERVICE = 2; |
| 62 | |
Matthew Williams | 8ef2204 | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 63 | /** Identifying info for the target for this operation. */ |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 64 | public final SyncStorageEngine.EndPoint target; |
| 65 | /** Why this sync was kicked off. {@link #REASON_NAMES} */ |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 66 | public final int reason; |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 67 | /** Where this sync was initiated. */ |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 68 | public int syncSource; |
Fred Quintana | 0c4d04a | 2010-11-03 17:02:55 -0700 | [diff] [blame] | 69 | public final boolean allowParallelSyncs; |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 70 | public Bundle extras; |
| 71 | public final String key; |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 72 | public boolean expedited; |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 73 | /** Bare-bones version of this operation that is persisted across reboots. */ |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 74 | public SyncStorageEngine.PendingOperation pendingOperation; |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 75 | /** Elapsed real time in millis at which to run this sync. */ |
| 76 | public long latestRunTime; |
| 77 | /** Set by the SyncManager in order to delay retries. */ |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 78 | public Long backoff; |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 79 | /** Specified by the adapter to delay subsequent sync operations. */ |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 80 | public long delayUntil; |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 81 | /** |
| 82 | * Elapsed real time in millis when this sync will be run. |
| 83 | * Depends on max(backoff, latestRunTime, and delayUntil). |
| 84 | */ |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 85 | public long effectiveRunTime; |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 86 | /** Amount of time before {@link #effectiveRunTime} from which this sync can run. */ |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 87 | public long flexTime; |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 88 | |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 89 | public SyncOperation(Account account, int userId, int reason, int source, String provider, |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 90 | Bundle extras, long runTimeFromNow, long flexTime, long backoff, |
| 91 | long delayUntil, boolean allowParallelSyncs) { |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 92 | this.target = new SyncStorageEngine.EndPoint(account, provider, userId); |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 93 | this.reason = reason; |
Torne (Richard Coles) | 4890b08 | 2013-08-12 10:26:57 +0000 | [diff] [blame] | 94 | this.allowParallelSyncs = allowParallelSyncs; |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 95 | this.key = initialiseOperation(this.target, source, extras, runTimeFromNow, flexTime, |
| 96 | backoff, delayUntil); |
| 97 | } |
| 98 | |
| 99 | public SyncOperation(ComponentName service, int userId, int reason, int source, |
| 100 | Bundle extras, long runTimeFromNow, long flexTime, long backoff, |
| 101 | long delayUntil) { |
| 102 | this.target = new SyncStorageEngine.EndPoint(service, userId); |
| 103 | // Default to true for sync service. The service itself decides how to handle this. |
| 104 | this.allowParallelSyncs = true; |
| 105 | this.reason = reason; |
| 106 | this.key = |
| 107 | initialiseOperation(this.target, |
| 108 | source, extras, runTimeFromNow, flexTime, backoff, delayUntil); |
| 109 | } |
| 110 | |
| 111 | /** Used to reschedule a sync at a new point in time. */ |
| 112 | SyncOperation(SyncOperation other, long newRunTimeFromNow) { |
| 113 | this.target = other.target; |
| 114 | this.reason = other.reason; |
| 115 | this.expedited = other.expedited; |
| 116 | this.allowParallelSyncs = other.allowParallelSyncs; |
| 117 | // re-use old flex, but only |
| 118 | long newFlexTime = Math.min(other.flexTime, newRunTimeFromNow); |
| 119 | this.key = |
| 120 | initialiseOperation(this.target, |
| 121 | other.syncSource, other.extras, |
| 122 | newRunTimeFromNow /* runTimeFromNow*/, |
| 123 | newFlexTime /* flexTime */, |
| 124 | other.backoff, |
| 125 | 0L /* delayUntil */); |
| 126 | } |
| 127 | |
| 128 | private String initialiseOperation(SyncStorageEngine.EndPoint info, int source, Bundle extras, |
| 129 | long runTimeFromNow, long flexTime, long backoff, long delayUntil) { |
| 130 | this.syncSource = source; |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 131 | this.extras = new Bundle(extras); |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 132 | cleanBundle(this.extras); |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 133 | this.delayUntil = delayUntil; |
| 134 | this.backoff = backoff; |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 135 | final long now = SystemClock.elapsedRealtime(); |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 136 | if (runTimeFromNow < 0 || isExpedited()) { |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 137 | this.expedited = true; |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 138 | this.latestRunTime = now; |
| 139 | this.flexTime = 0; |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 140 | } else { |
| 141 | this.expedited = false; |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 142 | this.latestRunTime = now + runTimeFromNow; |
| 143 | this.flexTime = flexTime; |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 144 | } |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 145 | updateEffectiveRunTime(); |
Matthew Williams | 8ef2204 | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 146 | return toKey(info, this.extras); |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 147 | } |
| 148 | |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 149 | public boolean matchesAuthority(SyncOperation other) { |
Matthew Williams | 8ef2204 | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 150 | return this.target.matchesSpec(other.target); |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Make sure the bundle attached to this SyncOperation doesn't have unnecessary |
| 155 | * flags set. |
| 156 | * @param bundle to clean. |
| 157 | */ |
| 158 | private void cleanBundle(Bundle bundle) { |
| 159 | removeFalseExtra(bundle, ContentResolver.SYNC_EXTRAS_UPLOAD); |
| 160 | removeFalseExtra(bundle, ContentResolver.SYNC_EXTRAS_MANUAL); |
| 161 | removeFalseExtra(bundle, ContentResolver.SYNC_EXTRAS_IGNORE_SETTINGS); |
| 162 | removeFalseExtra(bundle, ContentResolver.SYNC_EXTRAS_IGNORE_BACKOFF); |
| 163 | removeFalseExtra(bundle, ContentResolver.SYNC_EXTRAS_DO_NOT_RETRY); |
| 164 | removeFalseExtra(bundle, ContentResolver.SYNC_EXTRAS_DISCARD_LOCAL_DELETIONS); |
| 165 | removeFalseExtra(bundle, ContentResolver.SYNC_EXTRAS_EXPEDITED); |
| 166 | removeFalseExtra(bundle, ContentResolver.SYNC_EXTRAS_OVERRIDE_TOO_MANY_DELETIONS); |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 167 | removeFalseExtra(bundle, ContentResolver.SYNC_EXTRAS_DISALLOW_METERED); |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | private void removeFalseExtra(Bundle bundle, String extraName) { |
| 171 | if (!bundle.getBoolean(extraName, false)) { |
| 172 | bundle.remove(extraName); |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 173 | } |
| 174 | } |
| 175 | |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 176 | /** |
| 177 | * Determine whether if this sync operation is running, the provided operation would conflict |
| 178 | * with it. |
| 179 | * Parallel syncs allow multiple accounts to be synced at the same time. |
| 180 | */ |
| 181 | public boolean isConflict(SyncOperation toRun) { |
| 182 | final SyncStorageEngine.EndPoint other = toRun.target; |
| 183 | if (target.target_provider) { |
| 184 | return target.account.type.equals(other.account.type) |
| 185 | && target.provider.equals(other.provider) |
| 186 | && target.userId == other.userId |
| 187 | && (!allowParallelSyncs |
| 188 | || target.account.name.equals(other.account.name)); |
| 189 | } else { |
| 190 | // Ops that target a service default to allow parallel syncs, which is handled by the |
| 191 | // service returning SYNC_IN_PROGRESS if they don't. |
| 192 | return target.service.equals(other.service) && !allowParallelSyncs; |
| 193 | } |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 194 | } |
| 195 | |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 196 | @Override |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 197 | public String toString() { |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 198 | return dump(null, true); |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 199 | } |
| 200 | |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 201 | public String dump(PackageManager pm, boolean useOneLine) { |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 202 | StringBuilder sb = new StringBuilder(); |
| 203 | if (target.target_provider) { |
| 204 | sb.append(target.account.name) |
Alon Albert | 8e28555 | 2012-09-17 15:05:27 -0700 | [diff] [blame] | 205 | .append(" u") |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 206 | .append(target.userId).append(" (") |
| 207 | .append(target.account.type) |
Alon Albert | 8e28555 | 2012-09-17 15:05:27 -0700 | [diff] [blame] | 208 | .append(")") |
| 209 | .append(", ") |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 210 | .append(target.provider) |
| 211 | .append(", "); |
| 212 | } else if (target.target_service) { |
| 213 | sb.append(target.service.getPackageName()) |
| 214 | .append(" u") |
| 215 | .append(target.userId).append(" (") |
| 216 | .append(target.service.getClassName()).append(")") |
| 217 | .append(", "); |
| 218 | } |
| 219 | sb.append(SyncStorageEngine.SOURCES[syncSource]) |
| 220 | .append(", currentRunTime ") |
| 221 | .append(effectiveRunTime); |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 222 | if (expedited) { |
| 223 | sb.append(", EXPEDITED"); |
| 224 | } |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 225 | sb.append(", reason: "); |
| 226 | sb.append(reasonToString(pm, reason)); |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 227 | if (!useOneLine && !extras.keySet().isEmpty()) { |
| 228 | sb.append("\n "); |
| 229 | extrasToStringBuilder(extras, sb); |
| 230 | } |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 231 | return sb.toString(); |
| 232 | } |
| 233 | |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 234 | public static String reasonToString(PackageManager pm, int reason) { |
| 235 | if (reason >= 0) { |
| 236 | if (pm != null) { |
| 237 | final String[] packages = pm.getPackagesForUid(reason); |
| 238 | if (packages != null && packages.length == 1) { |
| 239 | return packages[0]; |
| 240 | } |
| 241 | final String name = pm.getNameForUid(reason); |
| 242 | if (name != null) { |
| 243 | return name; |
| 244 | } |
| 245 | return String.valueOf(reason); |
| 246 | } else { |
| 247 | return String.valueOf(reason); |
| 248 | } |
| 249 | } else { |
| 250 | final int index = -reason - 1; |
| 251 | if (index >= REASON_NAMES.length) { |
| 252 | return String.valueOf(reason); |
| 253 | } else { |
| 254 | return REASON_NAMES[index]; |
| 255 | } |
| 256 | } |
| 257 | } |
| 258 | |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 259 | public boolean isInitialization() { |
| 260 | return extras.getBoolean(ContentResolver.SYNC_EXTRAS_INITIALIZE, false); |
| 261 | } |
| 262 | |
Fred Quintana | dc47556 | 2012-05-04 15:51:54 -0700 | [diff] [blame] | 263 | public boolean isExpedited() { |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 264 | return extras.getBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, false) || expedited; |
Fred Quintana | dc47556 | 2012-05-04 15:51:54 -0700 | [diff] [blame] | 265 | } |
| 266 | |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 267 | public boolean ignoreBackoff() { |
| 268 | return extras.getBoolean(ContentResolver.SYNC_EXTRAS_IGNORE_BACKOFF, false); |
| 269 | } |
| 270 | |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 271 | public boolean isNotAllowedOnMetered() { |
| 272 | return extras.getBoolean(ContentResolver.SYNC_EXTRAS_DISALLOW_METERED, false); |
| 273 | } |
| 274 | |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 275 | /** Changed in V3. */ |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 276 | public static String toKey(SyncStorageEngine.EndPoint info, Bundle extras) { |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 277 | StringBuilder sb = new StringBuilder(); |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 278 | if (info.target_provider) { |
| 279 | sb.append("provider: ").append(info.provider); |
| 280 | sb.append(" account {name=" + info.account.name |
| 281 | + ", user=" |
| 282 | + info.userId |
| 283 | + ", type=" |
| 284 | + info.account.type |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 285 | + "}"); |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 286 | } else if (info.target_service) { |
Torne (Richard Coles) | 4890b08 | 2013-08-12 10:26:57 +0000 | [diff] [blame] | 287 | sb.append("service {package=" ) |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 288 | .append(info.service.getPackageName()) |
Torne (Richard Coles) | 4890b08 | 2013-08-12 10:26:57 +0000 | [diff] [blame] | 289 | .append(" user=") |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 290 | .append(info.userId) |
Torne (Richard Coles) | 4890b08 | 2013-08-12 10:26:57 +0000 | [diff] [blame] | 291 | .append(", class=") |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 292 | .append(info.service.getClassName()) |
Torne (Richard Coles) | 4890b08 | 2013-08-12 10:26:57 +0000 | [diff] [blame] | 293 | .append("}"); |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 294 | } else { |
| 295 | Log.v(TAG, "Converting SyncOperaton to key, invalid target: " + info.toString()); |
| 296 | return ""; |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 297 | } |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 298 | sb.append(" extras: "); |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 299 | extrasToStringBuilder(extras, sb); |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 300 | return sb.toString(); |
| 301 | } |
| 302 | |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 303 | private static void extrasToStringBuilder(Bundle bundle, StringBuilder sb) { |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 304 | sb.append("["); |
| 305 | for (String key : bundle.keySet()) { |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 306 | sb.append(key).append("=").append(bundle.get(key)).append(" "); |
| 307 | } |
| 308 | sb.append("]"); |
| 309 | } |
| 310 | |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 311 | public String wakeLockKey() { |
| 312 | if (target.target_provider) { |
| 313 | return target.account.name + "/" + target.account.type + ":" + target.provider; |
| 314 | } else if (target.target_service) { |
| 315 | return target.service.getPackageName() + "/" + target.service.getClassName(); |
| 316 | } else { |
| 317 | Log.wtf(TAG, "Invalid target getting wakelock for operation - " + key); |
| 318 | return null; |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | public String wakeLockName() { |
| 323 | if (target.target_provider) { |
| 324 | return "/" + target.provider |
| 325 | + "/" + target.account.type |
| 326 | + "/" + target.account.name; |
| 327 | } else if (target.target_service) { |
| 328 | return "/" + target.service.getPackageName() |
| 329 | + "/" + target.service.getClassName(); |
| 330 | } else { |
| 331 | Log.wtf(TAG, "Invalid target getting wakelock name for operation - " + key); |
| 332 | return null; |
| 333 | } |
| 334 | } |
| 335 | |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 336 | /** |
| 337 | * Update the effective run time of this Operation based on latestRunTime (specified at |
| 338 | * creation time of sync), delayUntil (specified by SyncAdapter), or backoff (specified by |
| 339 | * SyncManager on soft failures). |
| 340 | */ |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 341 | public void updateEffectiveRunTime() { |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 342 | // Regardless of whether we're in backoff or honouring a delayUntil, we still incorporate |
| 343 | // the flex time provided by the developer. |
| 344 | effectiveRunTime = ignoreBackoff() ? |
| 345 | latestRunTime : |
| 346 | Math.max(Math.max(latestRunTime, delayUntil), backoff); |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 347 | } |
| 348 | |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 349 | /** |
Matthew Williams | 7986fe4 | 2013-07-29 16:56:23 -0700 | [diff] [blame] | 350 | * SyncOperations are sorted based on their earliest effective run time. |
| 351 | * This comparator is used to sort the SyncOps at a given time when |
| 352 | * deciding which to run, so earliest run time is the best criteria. |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 353 | */ |
| 354 | @Override |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 355 | public int compareTo(Object o) { |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 356 | SyncOperation other = (SyncOperation) o; |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 357 | if (expedited != other.expedited) { |
| 358 | return expedited ? -1 : 1; |
| 359 | } |
Matthew Williams | 7986fe4 | 2013-07-29 16:56:23 -0700 | [diff] [blame] | 360 | long thisIntervalStart = Math.max(effectiveRunTime - flexTime, 0); |
| 361 | long otherIntervalStart = Math.max( |
| 362 | other.effectiveRunTime - other.flexTime, 0); |
| 363 | if (thisIntervalStart < otherIntervalStart) { |
| 364 | return -1; |
| 365 | } else if (otherIntervalStart < thisIntervalStart) { |
| 366 | return 1; |
| 367 | } else { |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 368 | return 0; |
| 369 | } |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 370 | } |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 371 | |
| 372 | // TODO: Test this to make sure that casting to object doesn't lose the type info for EventLog. |
| 373 | public Object[] toEventLog(int event) { |
| 374 | Object[] logArray = new Object[4]; |
| 375 | logArray[1] = event; |
| 376 | logArray[2] = syncSource; |
| 377 | if (target.target_provider) { |
| 378 | logArray[0] = target.provider; |
| 379 | logArray[3] = target.account.name.hashCode(); |
| 380 | } else if (target.target_service) { |
| 381 | logArray[0] = target.service.getPackageName(); |
| 382 | logArray[3] = target.service.hashCode(); |
| 383 | } else { |
| 384 | Log.wtf(TAG, "sync op with invalid target: " + key); |
| 385 | } |
| 386 | return logArray; |
| 387 | } |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 388 | } |