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