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; |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 20 | import android.app.job.JobInfo; |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 21 | import android.content.pm.PackageManager; |
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; |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 24 | import android.os.PersistableBundle; |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 25 | import android.os.UserHandle; |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 26 | import android.util.Slog; |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 27 | |
| 28 | /** |
| 29 | * Value type that represents a sync operation. |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 30 | * This holds all information related to a sync operation - both one off and periodic. |
| 31 | * Data stored in this is used to schedule a job with the JobScheduler. |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 32 | * {@hide} |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 33 | */ |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 34 | public class SyncOperation { |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 35 | public static final String TAG = "SyncManager"; |
| 36 | |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 37 | /** |
| 38 | * This is used in the {@link #sourcePeriodicId} field if the operation is not initiated by a failed |
| 39 | * periodic sync. |
| 40 | */ |
| 41 | public static final int NO_JOB_ID = -1; |
| 42 | |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 43 | public static final int REASON_BACKGROUND_DATA_SETTINGS_CHANGED = -1; |
| 44 | public static final int REASON_ACCOUNTS_UPDATED = -2; |
| 45 | public static final int REASON_SERVICE_CHANGED = -3; |
| 46 | public static final int REASON_PERIODIC = -4; |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 47 | /** Sync started because it has just been set to isSyncable. */ |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 48 | public static final int REASON_IS_SYNCABLE = -5; |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 49 | /** Sync started because it has just been set to sync automatically. */ |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 50 | public static final int REASON_SYNC_AUTO = -6; |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 51 | /** Sync started because master sync automatically has been set to true. */ |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 52 | public static final int REASON_MASTER_SYNC_AUTO = -7; |
| 53 | public static final int REASON_USER_START = -8; |
| 54 | |
| 55 | private static String[] REASON_NAMES = new String[] { |
| 56 | "DataSettingsChanged", |
| 57 | "AccountsUpdated", |
| 58 | "ServiceChanged", |
| 59 | "Periodic", |
| 60 | "IsSyncable", |
| 61 | "AutoSync", |
| 62 | "MasterSyncAuto", |
| 63 | "UserStart", |
| 64 | }; |
| 65 | |
Matthew Williams | 8ef2204 | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 66 | /** Identifying info for the target for this operation. */ |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 67 | public final SyncStorageEngine.EndPoint target; |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 68 | public final int owningUid; |
| 69 | public final String owningPackage; |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 70 | /** Why this sync was kicked off. {@link #REASON_NAMES} */ |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 71 | public final int reason; |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 72 | /** Where this sync was initiated. */ |
Matthew Williams | 6428046 | 2014-01-09 10:49:23 -0800 | [diff] [blame] | 73 | public final int syncSource; |
Fred Quintana | 0c4d04a | 2010-11-03 17:02:55 -0700 | [diff] [blame] | 74 | public final boolean allowParallelSyncs; |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 75 | public final Bundle extras; |
| 76 | public final boolean isPeriodic; |
| 77 | /** jobId of the periodic SyncOperation that initiated this one */ |
| 78 | public final int sourcePeriodicId; |
| 79 | /** Operations are considered duplicates if keys are equal */ |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 80 | public final String key; |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 81 | |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 82 | /** Poll frequency of periodic sync in milliseconds */ |
Shreyas Basarge | 2b4c8f9 | 2016-02-08 23:52:27 +0000 | [diff] [blame] | 83 | public final long periodMillis; |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 84 | /** Flex time of periodic sync in milliseconds */ |
Shreyas Basarge | 2b4c8f9 | 2016-02-08 23:52:27 +0000 | [diff] [blame] | 85 | public final long flexMillis; |
Dianne Hackborn | d45665b | 2014-02-26 12:35:32 -0800 | [diff] [blame] | 86 | /** Descriptive string key for this operation */ |
| 87 | public String wakeLockName; |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 88 | /** |
| 89 | * Used when duplicate pending syncs are present. The one with the lowest expectedRuntime |
| 90 | * is kept, others are discarded. |
| 91 | */ |
| 92 | public long expectedRuntime; |
Dianne Hackborn | d45665b | 2014-02-26 12:35:32 -0800 | [diff] [blame] | 93 | |
Shreyas Basarge | 2b4c8f9 | 2016-02-08 23:52:27 +0000 | [diff] [blame] | 94 | /** Stores the number of times this sync operation failed and had to be retried. */ |
| 95 | int retries; |
| 96 | |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 97 | /** jobId of the JobScheduler job corresponding to this sync */ |
| 98 | public int jobId; |
Amith Yamasani | 96a0fd65 | 2015-04-10 16:16:30 -0700 | [diff] [blame] | 99 | |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 100 | public SyncOperation(Account account, int userId, int owningUid, String owningPackage, |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 101 | int reason, int source, String provider, Bundle extras, |
| 102 | boolean allowParallelSyncs) { |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 103 | this(new SyncStorageEngine.EndPoint(account, provider, userId), owningUid, owningPackage, |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 104 | reason, source, extras, allowParallelSyncs); |
Matthew Williams | 6428046 | 2014-01-09 10:49:23 -0800 | [diff] [blame] | 105 | } |
| 106 | |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 107 | private SyncOperation(SyncStorageEngine.EndPoint info, int owningUid, String owningPackage, |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 108 | int reason, int source, Bundle extras, boolean allowParallelSyncs) { |
| 109 | this(info, owningUid, owningPackage, reason, source, extras, allowParallelSyncs, false, |
Shreyas Basarge | 2b4c8f9 | 2016-02-08 23:52:27 +0000 | [diff] [blame] | 110 | NO_JOB_ID, 0, 0); |
| 111 | } |
| 112 | |
| 113 | public SyncOperation(SyncOperation op, long periodMillis, long flexMillis) { |
| 114 | this(op.target, op.owningUid, op.owningPackage, op.reason, op.syncSource, |
| 115 | new Bundle(op.extras), op.allowParallelSyncs, op.isPeriodic, op.sourcePeriodicId, |
| 116 | periodMillis, flexMillis); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | public SyncOperation(SyncStorageEngine.EndPoint info, int owningUid, String owningPackage, |
| 120 | int reason, int source, Bundle extras, boolean allowParallelSyncs, |
Shreyas Basarge | 2b4c8f9 | 2016-02-08 23:52:27 +0000 | [diff] [blame] | 121 | boolean isPeriodic, int sourcePeriodicId, long periodMillis, |
| 122 | long flexMillis) { |
Matthew Williams | 6428046 | 2014-01-09 10:49:23 -0800 | [diff] [blame] | 123 | this.target = info; |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 124 | this.owningUid = owningUid; |
| 125 | this.owningPackage = owningPackage; |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 126 | this.reason = reason; |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 127 | this.syncSource = source; |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 128 | this.extras = new Bundle(extras); |
Matthew Williams | 6428046 | 2014-01-09 10:49:23 -0800 | [diff] [blame] | 129 | this.allowParallelSyncs = allowParallelSyncs; |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 130 | this.isPeriodic = isPeriodic; |
| 131 | this.sourcePeriodicId = sourcePeriodicId; |
Shreyas Basarge | 2b4c8f9 | 2016-02-08 23:52:27 +0000 | [diff] [blame] | 132 | this.periodMillis = periodMillis; |
| 133 | this.flexMillis = flexMillis; |
| 134 | this.jobId = NO_JOB_ID; |
| 135 | this.key = toKey(); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | /* Get a one off sync operation instance from a periodic sync. */ |
| 139 | public SyncOperation createOneTimeSyncOperation() { |
| 140 | if (!isPeriodic) { |
| 141 | return null; |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 142 | } |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 143 | SyncOperation op = new SyncOperation(target, owningUid, owningPackage, reason, syncSource, |
Shreyas Basarge | 2b4c8f9 | 2016-02-08 23:52:27 +0000 | [diff] [blame] | 144 | new Bundle(extras), allowParallelSyncs, false, jobId /* sourcePeriodicId */, |
| 145 | periodMillis, flexMillis); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 146 | return op; |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 147 | } |
| 148 | |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 149 | public SyncOperation(SyncOperation other) { |
| 150 | target = other.target; |
| 151 | owningUid = other.owningUid; |
| 152 | owningPackage = other.owningPackage; |
| 153 | reason = other.reason; |
| 154 | syncSource = other.syncSource; |
| 155 | allowParallelSyncs = other.allowParallelSyncs; |
| 156 | extras = new Bundle(other.extras); |
| 157 | wakeLockName = other.wakeLockName(); |
| 158 | isPeriodic = other.isPeriodic; |
| 159 | sourcePeriodicId = other.sourcePeriodicId; |
| 160 | periodMillis = other.periodMillis; |
| 161 | flexMillis = other.flexMillis; |
| 162 | this.key = other.key; |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * All fields are stored in a corresponding key in the persistable bundle. |
| 167 | * |
| 168 | * {@link #extras} is a Bundle and can contain parcelable objects. But only the type Account |
| 169 | * is allowed {@link ContentResolver#validateSyncExtrasBundle(Bundle)} that can't be stored in |
| 170 | * a PersistableBundle. For every value of type Account with key 'key', we store a |
| 171 | * PersistableBundle containing account information at key 'ACCOUNT:key'. The Account object |
| 172 | * can be reconstructed using this. |
| 173 | * |
| 174 | * We put a flag with key 'SyncManagerJob', to identify while reconstructing a sync operation |
| 175 | * from a bundle whether the bundle actually contains information about a sync. |
| 176 | * @return A persistable bundle containing all information to re-construct the sync operation. |
| 177 | */ |
| 178 | PersistableBundle toJobInfoExtras() { |
| 179 | // This will be passed as extras bundle to a JobScheduler job. |
| 180 | PersistableBundle jobInfoExtras = new PersistableBundle(); |
| 181 | |
| 182 | PersistableBundle syncExtrasBundle = new PersistableBundle(); |
| 183 | for (String key: extras.keySet()) { |
| 184 | Object value = extras.get(key); |
| 185 | if (value instanceof Account) { |
| 186 | Account account = (Account) value; |
| 187 | PersistableBundle accountBundle = new PersistableBundle(); |
| 188 | accountBundle.putString("accountName", account.name); |
| 189 | accountBundle.putString("accountType", account.type); |
| 190 | // This is stored in jobInfoExtras so that we don't override a user specified |
| 191 | // sync extra with the same key. |
| 192 | jobInfoExtras.putPersistableBundle("ACCOUNT:" + key, accountBundle); |
| 193 | } else if (value instanceof Long) { |
| 194 | syncExtrasBundle.putLong(key, (Long) value); |
| 195 | } else if (value instanceof Integer) { |
| 196 | syncExtrasBundle.putInt(key, (Integer) value); |
| 197 | } else if (value instanceof Boolean) { |
| 198 | syncExtrasBundle.putBoolean(key, (Boolean) value); |
| 199 | } else if (value instanceof Float) { |
Shreyas Basarge | ace6f6d | 2016-06-24 18:45:51 +0100 | [diff] [blame] | 200 | syncExtrasBundle.putDouble(key, (double) (float) value); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 201 | } else if (value instanceof Double) { |
| 202 | syncExtrasBundle.putDouble(key, (Double) value); |
| 203 | } else if (value instanceof String) { |
| 204 | syncExtrasBundle.putString(key, (String) value); |
| 205 | } else if (value == null) { |
| 206 | syncExtrasBundle.putString(key, null); |
| 207 | } else { |
| 208 | Slog.e(TAG, "Unknown extra type."); |
| 209 | } |
| 210 | } |
| 211 | jobInfoExtras.putPersistableBundle("syncExtras", syncExtrasBundle); |
| 212 | |
| 213 | jobInfoExtras.putBoolean("SyncManagerJob", true); |
| 214 | |
| 215 | jobInfoExtras.putString("provider", target.provider); |
| 216 | jobInfoExtras.putString("accountName", target.account.name); |
| 217 | jobInfoExtras.putString("accountType", target.account.type); |
| 218 | jobInfoExtras.putInt("userId", target.userId); |
| 219 | jobInfoExtras.putInt("owningUid", owningUid); |
| 220 | jobInfoExtras.putString("owningPackage", owningPackage); |
| 221 | jobInfoExtras.putInt("reason", reason); |
| 222 | jobInfoExtras.putInt("source", syncSource); |
| 223 | jobInfoExtras.putBoolean("allowParallelSyncs", allowParallelSyncs); |
| 224 | jobInfoExtras.putInt("jobId", jobId); |
| 225 | jobInfoExtras.putBoolean("isPeriodic", isPeriodic); |
| 226 | jobInfoExtras.putInt("sourcePeriodicId", sourcePeriodicId); |
| 227 | jobInfoExtras.putLong("periodMillis", periodMillis); |
| 228 | jobInfoExtras.putLong("flexMillis", flexMillis); |
| 229 | jobInfoExtras.putLong("expectedRuntime", expectedRuntime); |
Shreyas Basarge | 2b4c8f9 | 2016-02-08 23:52:27 +0000 | [diff] [blame] | 230 | jobInfoExtras.putInt("retries", retries); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 231 | return jobInfoExtras; |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * Reconstructs a sync operation from an extras Bundle. Returns null if the bundle doesn't |
| 236 | * contain a valid sync operation. |
| 237 | */ |
| 238 | static SyncOperation maybeCreateFromJobExtras(PersistableBundle jobExtras) { |
| 239 | String accountName, accountType; |
| 240 | String provider; |
| 241 | int userId, owningUid; |
| 242 | String owningPackage; |
| 243 | int reason, source; |
| 244 | int initiatedBy; |
| 245 | Bundle extras; |
| 246 | boolean allowParallelSyncs, isPeriodic; |
Shreyas Basarge | 2b4c8f9 | 2016-02-08 23:52:27 +0000 | [diff] [blame] | 247 | long periodMillis, flexMillis; |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 248 | |
| 249 | if (!jobExtras.getBoolean("SyncManagerJob", false)) { |
| 250 | return null; |
| 251 | } |
| 252 | |
| 253 | accountName = jobExtras.getString("accountName"); |
| 254 | accountType = jobExtras.getString("accountType"); |
| 255 | provider = jobExtras.getString("provider"); |
| 256 | userId = jobExtras.getInt("userId", Integer.MAX_VALUE); |
| 257 | owningUid = jobExtras.getInt("owningUid"); |
| 258 | owningPackage = jobExtras.getString("owningPackage"); |
| 259 | reason = jobExtras.getInt("reason", Integer.MAX_VALUE); |
| 260 | source = jobExtras.getInt("source", Integer.MAX_VALUE); |
| 261 | allowParallelSyncs = jobExtras.getBoolean("allowParallelSyncs", false); |
| 262 | isPeriodic = jobExtras.getBoolean("isPeriodic", false); |
| 263 | initiatedBy = jobExtras.getInt("sourcePeriodicId", NO_JOB_ID); |
Shreyas Basarge | 2b4c8f9 | 2016-02-08 23:52:27 +0000 | [diff] [blame] | 264 | periodMillis = jobExtras.getLong("periodMillis"); |
| 265 | flexMillis = jobExtras.getLong("flexMillis"); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 266 | extras = new Bundle(); |
| 267 | |
| 268 | PersistableBundle syncExtras = jobExtras.getPersistableBundle("syncExtras"); |
| 269 | if (syncExtras != null) { |
| 270 | extras.putAll(syncExtras); |
| 271 | } |
| 272 | |
| 273 | for (String key: jobExtras.keySet()) { |
| 274 | if (key!= null && key.startsWith("ACCOUNT:")) { |
| 275 | String newKey = key.substring(8); // Strip off the 'ACCOUNT:' prefix. |
| 276 | PersistableBundle accountsBundle = jobExtras.getPersistableBundle(key); |
| 277 | Account account = new Account(accountsBundle.getString("accountName"), |
| 278 | accountsBundle.getString("accountType")); |
| 279 | extras.putParcelable(newKey, account); |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | Account account = new Account(accountName, accountType); |
| 284 | SyncStorageEngine.EndPoint target = |
| 285 | new SyncStorageEngine.EndPoint(account, provider, userId); |
| 286 | SyncOperation op = new SyncOperation(target, owningUid, owningPackage, reason, source, |
Shreyas Basarge | 2b4c8f9 | 2016-02-08 23:52:27 +0000 | [diff] [blame] | 287 | extras, allowParallelSyncs, isPeriodic, initiatedBy, periodMillis, flexMillis); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 288 | op.jobId = jobExtras.getInt("jobId"); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 289 | op.expectedRuntime = jobExtras.getLong("expectedRuntime"); |
Shreyas Basarge | 2b4c8f9 | 2016-02-08 23:52:27 +0000 | [diff] [blame] | 290 | op.retries = jobExtras.getInt("retries"); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 291 | return op; |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | /** |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 295 | * Determine whether if this sync operation is running, the provided operation would conflict |
| 296 | * with it. |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 297 | * Parallel syncs allow multiple accounts to be synced at the same time. |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 298 | */ |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 299 | boolean isConflict(SyncOperation toRun) { |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 300 | final SyncStorageEngine.EndPoint other = toRun.target; |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 301 | return target.account.type.equals(other.account.type) |
| 302 | && target.provider.equals(other.provider) |
| 303 | && target.userId == other.userId |
| 304 | && (!allowParallelSyncs |
| 305 | || target.account.name.equals(other.account.name)); |
| 306 | } |
| 307 | |
| 308 | boolean isReasonPeriodic() { |
| 309 | return reason == REASON_PERIODIC; |
| 310 | } |
| 311 | |
Shreyas Basarge | 2b4c8f9 | 2016-02-08 23:52:27 +0000 | [diff] [blame] | 312 | boolean matchesPeriodicOperation(SyncOperation other) { |
| 313 | return target.matchesSpec(other.target) |
| 314 | && SyncManager.syncExtrasEquals(extras, other.extras, true) |
| 315 | && periodMillis == other.periodMillis && flexMillis == other.flexMillis; |
| 316 | } |
| 317 | |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 318 | boolean isDerivedFromFailedPeriodicSync() { |
| 319 | return sourcePeriodicId != NO_JOB_ID; |
| 320 | } |
| 321 | |
| 322 | int findPriority() { |
| 323 | if (isInitialization()) { |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 324 | return JobInfo.PRIORITY_SYNC_INITIALIZATION; |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 325 | } else if (isExpedited()) { |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 326 | return JobInfo.PRIORITY_SYNC_EXPEDITED; |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 327 | } |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 328 | return JobInfo.PRIORITY_DEFAULT; |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 329 | } |
| 330 | |
Shreyas Basarge | 2b4c8f9 | 2016-02-08 23:52:27 +0000 | [diff] [blame] | 331 | private String toKey() { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 332 | StringBuilder sb = new StringBuilder(); |
Shreyas Basarge | 2b4c8f9 | 2016-02-08 23:52:27 +0000 | [diff] [blame] | 333 | sb.append("provider: ").append(target.provider); |
| 334 | sb.append(" account {name=" + target.account.name |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 335 | + ", user=" |
Shreyas Basarge | 2b4c8f9 | 2016-02-08 23:52:27 +0000 | [diff] [blame] | 336 | + target.userId |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 337 | + ", type=" |
Shreyas Basarge | 2b4c8f9 | 2016-02-08 23:52:27 +0000 | [diff] [blame] | 338 | + target.account.type |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 339 | + "}"); |
Shreyas Basarge | 2b4c8f9 | 2016-02-08 23:52:27 +0000 | [diff] [blame] | 340 | sb.append(" isPeriodic: ").append(isPeriodic); |
| 341 | sb.append(" period: ").append(periodMillis); |
| 342 | sb.append(" flex: ").append(flexMillis); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 343 | sb.append(" extras: "); |
| 344 | extrasToStringBuilder(extras, sb); |
| 345 | return sb.toString(); |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 346 | } |
| 347 | |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 348 | @Override |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 349 | public String toString() { |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 350 | return dump(null, true); |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 351 | } |
| 352 | |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 353 | String dump(PackageManager pm, boolean useOneLine) { |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 354 | StringBuilder sb = new StringBuilder(); |
Shreyas Basarge | 2b4c8f9 | 2016-02-08 23:52:27 +0000 | [diff] [blame] | 355 | sb.append("JobId: ").append(jobId) |
| 356 | .append(", ") |
| 357 | .append(target.account.name) |
Alon Albert | 8e28555 | 2012-09-17 15:05:27 -0700 | [diff] [blame] | 358 | .append(" u") |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 359 | .append(target.userId).append(" (") |
| 360 | .append(target.account.type) |
Alon Albert | 8e28555 | 2012-09-17 15:05:27 -0700 | [diff] [blame] | 361 | .append(")") |
| 362 | .append(", ") |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 363 | .append(target.provider) |
| 364 | .append(", "); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 365 | sb.append(SyncStorageEngine.SOURCES[syncSource]); |
| 366 | if (extras.getBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, false)) { |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 367 | sb.append(", EXPEDITED"); |
| 368 | } |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 369 | sb.append(", reason: "); |
| 370 | sb.append(reasonToString(pm, reason)); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 371 | if (isPeriodic) { |
| 372 | sb.append(", period: " + periodMillis).append(", flexMillis: " + flexMillis); |
| 373 | } |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 374 | if (!useOneLine) { |
| 375 | sb.append("\n "); |
| 376 | sb.append("owningUid="); |
| 377 | UserHandle.formatUid(sb, owningUid); |
| 378 | sb.append(" owningPackage="); |
| 379 | sb.append(owningPackage); |
| 380 | } |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 381 | if (!useOneLine && !extras.keySet().isEmpty()) { |
| 382 | sb.append("\n "); |
| 383 | extrasToStringBuilder(extras, sb); |
| 384 | } |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 385 | return sb.toString(); |
| 386 | } |
| 387 | |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 388 | static String reasonToString(PackageManager pm, int reason) { |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 389 | if (reason >= 0) { |
| 390 | if (pm != null) { |
| 391 | final String[] packages = pm.getPackagesForUid(reason); |
| 392 | if (packages != null && packages.length == 1) { |
| 393 | return packages[0]; |
| 394 | } |
| 395 | final String name = pm.getNameForUid(reason); |
| 396 | if (name != null) { |
| 397 | return name; |
| 398 | } |
| 399 | return String.valueOf(reason); |
| 400 | } else { |
| 401 | return String.valueOf(reason); |
| 402 | } |
| 403 | } else { |
| 404 | final int index = -reason - 1; |
| 405 | if (index >= REASON_NAMES.length) { |
| 406 | return String.valueOf(reason); |
| 407 | } else { |
| 408 | return REASON_NAMES[index]; |
| 409 | } |
| 410 | } |
| 411 | } |
| 412 | |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 413 | boolean isInitialization() { |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 414 | return extras.getBoolean(ContentResolver.SYNC_EXTRAS_INITIALIZE, false); |
| 415 | } |
| 416 | |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 417 | boolean isExpedited() { |
| 418 | return extras.getBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, false); |
Fred Quintana | dc47556 | 2012-05-04 15:51:54 -0700 | [diff] [blame] | 419 | } |
| 420 | |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 421 | boolean ignoreBackoff() { |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 422 | return extras.getBoolean(ContentResolver.SYNC_EXTRAS_IGNORE_BACKOFF, false); |
| 423 | } |
| 424 | |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 425 | boolean isNotAllowedOnMetered() { |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 426 | return extras.getBoolean(ContentResolver.SYNC_EXTRAS_DISALLOW_METERED, false); |
| 427 | } |
| 428 | |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 429 | boolean isManual() { |
Matthew Williams | 92a1c09 | 2014-08-25 19:18:32 -0700 | [diff] [blame] | 430 | return extras.getBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, false); |
| 431 | } |
| 432 | |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 433 | boolean isIgnoreSettings() { |
Matthew Williams | 92a1c09 | 2014-08-25 19:18:32 -0700 | [diff] [blame] | 434 | return extras.getBoolean(ContentResolver.SYNC_EXTRAS_IGNORE_SETTINGS, false); |
| 435 | } |
| 436 | |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 437 | private static void extrasToStringBuilder(Bundle bundle, StringBuilder sb) { |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 438 | sb.append("["); |
| 439 | for (String key : bundle.keySet()) { |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 440 | sb.append(key).append("=").append(bundle.get(key)).append(" "); |
| 441 | } |
| 442 | sb.append("]"); |
| 443 | } |
| 444 | |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 445 | String wakeLockName() { |
Dianne Hackborn | d45665b | 2014-02-26 12:35:32 -0800 | [diff] [blame] | 446 | if (wakeLockName != null) { |
| 447 | return wakeLockName; |
| 448 | } |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 449 | return (wakeLockName = target.provider |
| 450 | + "/" + target.account.type |
| 451 | + "/" + target.account.name); |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 452 | } |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 453 | |
| 454 | // TODO: Test this to make sure that casting to object doesn't lose the type info for EventLog. |
| 455 | public Object[] toEventLog(int event) { |
| 456 | Object[] logArray = new Object[4]; |
| 457 | logArray[1] = event; |
| 458 | logArray[2] = syncSource; |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 459 | logArray[0] = target.provider; |
| 460 | logArray[3] = target.account.name.hashCode(); |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 461 | return logArray; |
| 462 | } |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 463 | } |