blob: 8280f8e637baba5d8d7e1ac7262f28202756edbe [file] [log] [blame]
Kenny Root15a4d2f2010-03-11 18:20:12 -08001/*
2 * Copyright (C) 2008 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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017package android.content;
18
19import android.os.Parcel;
20import android.os.Parcelable;
21
22/**
Fred Quintana20c640e2010-04-26 17:38:56 -070023 * This class is used to communicate the results of a sync operation to the SyncManager.
24 * Based on the values here the SyncManager will determine the disposition of the
25 * sync and whether or not a new sync operation needs to be scheduled in the future.
26 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027 */
28public final class SyncResult implements Parcelable {
Fred Quintana20c640e2010-04-26 17:38:56 -070029 /**
30 * Used to indicate that the SyncAdapter is already performing a sync operation, though
31 * not necessarily for the requested account and authority and that it wasn't able to
32 * process this request. The SyncManager will reschedule the request to run later.
33 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034 public final boolean syncAlreadyInProgress;
Fred Quintana20c640e2010-04-26 17:38:56 -070035
36 /**
37 * Used to indicate that the SyncAdapter determined that it would need to issue
38 * too many delete operations to the server in order to satisfy the request
39 * (as defined by the SyncAdapter). The SyncManager will record
40 * that the sync request failed and will cause a System Notification to be created
41 * asking the user what they want to do about this. It will give the user a chance to
42 * choose between (1) go ahead even with those deletes, (2) revert the deletes,
43 * or (3) take no action. If the user decides (1) or (2) the SyncManager will issue another
44 * sync request with either {@link ContentResolver#SYNC_EXTRAS_OVERRIDE_TOO_MANY_DELETIONS}
45 * or {@link ContentResolver#SYNC_EXTRAS_DISCARD_LOCAL_DELETIONS} set in the extras.
46 * It is then up to the SyncAdapter to decide how to honor that request.
47 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048 public boolean tooManyDeletions;
Fred Quintana20c640e2010-04-26 17:38:56 -070049
50 /**
51 * Used to indicate that the SyncAdapter experienced a hard error due to trying the same
52 * operation too many times (as defined by the SyncAdapter). The SyncManager will record
53 * that the sync request failed and it will not reschedule the request.
54 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055 public boolean tooManyRetries;
Fred Quintana20c640e2010-04-26 17:38:56 -070056
57 /**
58 * Used to indicate that the SyncAdapter experienced a hard error due to an error it
Trevor Johns05d60be2013-06-05 19:23:50 -070059 * received from interacting with the storage layer. The SyncManager will record that
Fred Quintana20c640e2010-04-26 17:38:56 -070060 * the sync request failed and it will not reschedule the request.
61 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062 public boolean databaseError;
Fred Quintana20c640e2010-04-26 17:38:56 -070063
64 /**
65 * If set the SyncManager will request an immediate sync with the same Account and authority
66 * (but empty extras Bundle) as was used in the sync request.
67 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068 public boolean fullSyncRequested;
Fred Quintana20c640e2010-04-26 17:38:56 -070069
70 /**
71 * This field is ignored by the SyncManager.
72 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073 public boolean partialSyncUnavailable;
Fred Quintana20c640e2010-04-26 17:38:56 -070074
75 /**
76 * This field is ignored by the SyncManager.
77 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078 public boolean moreRecordsToGet;
Fred Quintana307da1a2010-01-21 14:24:20 -080079
Fred Quintana20c640e2010-04-26 17:38:56 -070080 /**
81 * Used to indicate to the SyncManager that future sync requests that match the request's
Makoto Onukiacfb4172018-03-15 11:01:09 -070082 * Account and authority should be delayed until a time in seconds since Java epoch.
83 *
84 * <p>For example, if you want to delay the next sync for at least 5 minutes, then:
85 * <pre>
86 * result.delayUntil = (System.currentTimeMillis() / 1000) + 5 * 60;
87 * </pre>
88 *
89 * <p>By default, when a sync fails, the system retries later with an exponential back-off
90 * with the system default initial delay time, which always wins over {@link #delayUntil} --
91 * i.e. if the system back-off time is larger than {@link #delayUntil}, {@link #delayUntil}
92 * will essentially be ignored.
Fred Quintana20c640e2010-04-26 17:38:56 -070093 */
Fred Quintana307da1a2010-01-21 14:24:20 -080094 public long delayUntil;
Fred Quintana20c640e2010-04-26 17:38:56 -070095
96 /**
97 * Used to hold extras statistics about the sync operation. Some of these indicate that
98 * the sync request resulted in a hard or soft error, others are for purely informational
99 * purposes.
100 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101 public final SyncStats stats;
Fred Quintana20c640e2010-04-26 17:38:56 -0700102
103 /**
104 * This instance of a SyncResult is returned by the SyncAdapter in response to a
105 * sync request when a sync is already underway. The SyncManager will reschedule the
106 * sync request to try again later.
107 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108 public static final SyncResult ALREADY_IN_PROGRESS;
109
110 static {
111 ALREADY_IN_PROGRESS = new SyncResult(true);
112 }
113
Fred Quintana20c640e2010-04-26 17:38:56 -0700114 /**
115 * Create a "clean" SyncResult. If this is returned without any changes then the
116 * SyncManager will consider the sync to have completed successfully. The various fields
117 * can be set by the SyncAdapter in order to give the SyncManager more information as to
118 * the disposition of the sync.
119 * <p>
120 * The errors are classified into two broad categories: hard errors and soft errors.
121 * Soft errors are retried with exponential backoff. Hard errors are not retried (except
122 * when the hard error is for a {@link ContentResolver#SYNC_EXTRAS_UPLOAD} request,
123 * in which the request is retryed without the {@link ContentResolver#SYNC_EXTRAS_UPLOAD}
124 * extra set). The SyncManager checks the type of error by calling
125 * {@link SyncResult#hasHardError()} and {@link SyncResult#hasSoftError()}. If both are
126 * true then the SyncManager treats it as a hard error, not a soft error.
127 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128 public SyncResult() {
129 this(false);
130 }
131
Fred Quintana20c640e2010-04-26 17:38:56 -0700132 /**
133 * Internal helper for creating a clean SyncResult or one that indicated that
134 * a sync is already in progress.
135 * @param syncAlreadyInProgress if true then set the {@link #syncAlreadyInProgress} flag
136 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137 private SyncResult(boolean syncAlreadyInProgress) {
138 this.syncAlreadyInProgress = syncAlreadyInProgress;
139 this.tooManyDeletions = false;
140 this.tooManyRetries = false;
141 this.fullSyncRequested = false;
142 this.partialSyncUnavailable = false;
143 this.moreRecordsToGet = false;
Fred Quintana307da1a2010-01-21 14:24:20 -0800144 this.delayUntil = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145 this.stats = new SyncStats();
146 }
147
148 private SyncResult(Parcel parcel) {
149 syncAlreadyInProgress = parcel.readInt() != 0;
150 tooManyDeletions = parcel.readInt() != 0;
151 tooManyRetries = parcel.readInt() != 0;
152 databaseError = parcel.readInt() != 0;
153 fullSyncRequested = parcel.readInt() != 0;
154 partialSyncUnavailable = parcel.readInt() != 0;
155 moreRecordsToGet = parcel.readInt() != 0;
Fred Quintana307da1a2010-01-21 14:24:20 -0800156 delayUntil = parcel.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157 stats = new SyncStats(parcel);
158 }
159
Fred Quintana20c640e2010-04-26 17:38:56 -0700160 /**
161 * Convenience method for determining if the SyncResult indicates that a hard error
162 * occurred. See {@link #SyncResult()} for an explanation of what the SyncManager does
163 * when it sees a hard error.
164 * <p>
165 * A hard error is indicated when any of the following is true:
166 * <ul>
167 * <li> {@link SyncStats#numParseExceptions} > 0
168 * <li> {@link SyncStats#numConflictDetectedExceptions} > 0
169 * <li> {@link SyncStats#numAuthExceptions} > 0
170 * <li> {@link #tooManyDeletions}
171 * <li> {@link #tooManyRetries}
172 * <li> {@link #databaseError}
173 * @return true if a hard error is indicated
174 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175 public boolean hasHardError() {
176 return stats.numParseExceptions > 0
177 || stats.numConflictDetectedExceptions > 0
178 || stats.numAuthExceptions > 0
179 || tooManyDeletions
180 || tooManyRetries
181 || databaseError;
182 }
183
Fred Quintana20c640e2010-04-26 17:38:56 -0700184 /**
185 * Convenience method for determining if the SyncResult indicates that a soft error
186 * occurred. See {@link #SyncResult()} for an explanation of what the SyncManager does
187 * when it sees a soft error.
188 * <p>
189 * A soft error is indicated when any of the following is true:
190 * <ul>
191 * <li> {@link SyncStats#numIoExceptions} > 0
192 * <li> {@link #syncAlreadyInProgress}
193 * </ul>
Matthew Williamsfa774182013-06-18 15:44:11 -0700194 * @return true if a soft error is indicated
Fred Quintana20c640e2010-04-26 17:38:56 -0700195 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196 public boolean hasSoftError() {
197 return syncAlreadyInProgress || stats.numIoExceptions > 0;
198 }
199
Fred Quintana20c640e2010-04-26 17:38:56 -0700200 /**
201 * A convenience method for determining of the SyncResult indicates that an error occurred.
202 * @return true if either a soft or hard error occurred
203 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204 public boolean hasError() {
205 return hasSoftError() || hasHardError();
206 }
207
Matthew Williamsfa774182013-06-18 15:44:11 -0700208 /**
209 * Convenience method for determining if the Sync should be rescheduled after failing for some
210 * reason.
211 * @return true if the SyncManager should reschedule this sync.
212 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213 public boolean madeSomeProgress() {
214 return ((stats.numDeletes > 0) && !tooManyDeletions)
215 || stats.numInserts > 0
216 || stats.numUpdates > 0;
217 }
218
Fred Quintana20c640e2010-04-26 17:38:56 -0700219 /**
220 * Clears the SyncResult to a clean state. Throws an {@link UnsupportedOperationException}
221 * if this is called when {@link #syncAlreadyInProgress} is set.
222 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223 public void clear() {
224 if (syncAlreadyInProgress) {
225 throw new UnsupportedOperationException(
226 "you are not allowed to clear the ALREADY_IN_PROGRESS SyncStats");
227 }
228 tooManyDeletions = false;
229 tooManyRetries = false;
230 databaseError = false;
231 fullSyncRequested = false;
232 partialSyncUnavailable = false;
233 moreRecordsToGet = false;
Fred Quintana307da1a2010-01-21 14:24:20 -0800234 delayUntil = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235 stats.clear();
236 }
237
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -0700238 public static final @android.annotation.NonNull Creator<SyncResult> CREATOR = new Creator<SyncResult>() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800239 public SyncResult createFromParcel(Parcel in) {
240 return new SyncResult(in);
241 }
242
243 public SyncResult[] newArray(int size) {
244 return new SyncResult[size];
245 }
246 };
247
248 public int describeContents() {
249 return 0;
250 }
251
252 public void writeToParcel(Parcel parcel, int flags) {
253 parcel.writeInt(syncAlreadyInProgress ? 1 : 0);
254 parcel.writeInt(tooManyDeletions ? 1 : 0);
255 parcel.writeInt(tooManyRetries ? 1 : 0);
256 parcel.writeInt(databaseError ? 1 : 0);
257 parcel.writeInt(fullSyncRequested ? 1 : 0);
258 parcel.writeInt(partialSyncUnavailable ? 1 : 0);
259 parcel.writeInt(moreRecordsToGet ? 1 : 0);
Fred Quintana307da1a2010-01-21 14:24:20 -0800260 parcel.writeLong(delayUntil);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261 stats.writeToParcel(parcel, flags);
262 }
263
264 @Override
265 public String toString() {
266 StringBuilder sb = new StringBuilder();
Fred Quintana77709752009-08-20 17:18:58 -0700267 sb.append("SyncResult:");
268 if (syncAlreadyInProgress) {
269 sb.append(" syncAlreadyInProgress: ").append(syncAlreadyInProgress);
270 }
271 if (tooManyDeletions) sb.append(" tooManyDeletions: ").append(tooManyDeletions);
272 if (tooManyRetries) sb.append(" tooManyRetries: ").append(tooManyRetries);
273 if (databaseError) sb.append(" databaseError: ").append(databaseError);
274 if (fullSyncRequested) sb.append(" fullSyncRequested: ").append(fullSyncRequested);
275 if (partialSyncUnavailable) {
276 sb.append(" partialSyncUnavailable: ").append(partialSyncUnavailable);
277 }
278 if (moreRecordsToGet) sb.append(" moreRecordsToGet: ").append(moreRecordsToGet);
Fred Quintana307da1a2010-01-21 14:24:20 -0800279 if (delayUntil > 0) sb.append(" delayUntil: ").append(delayUntil);
Fred Quintana77709752009-08-20 17:18:58 -0700280 sb.append(stats);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281 return sb.toString();
282 }
283
284 /**
285 * Generates a debugging string indicating the status.
286 * The string consist of a sequence of code letter followed by the count.
287 * Code letters are f - fullSyncRequested, r - partialSyncUnavailable,
288 * X - hardError, e - numParseExceptions, c - numConflictDetectedExceptions,
289 * a - numAuthExceptions, D - tooManyDeletions, R - tooManyRetries,
290 * b - databaseError, x - softError, l - syncAlreadyInProgress,
291 * I - numIoExceptions
292 * @return debugging string.
293 */
294 public String toDebugString() {
295 StringBuffer sb = new StringBuffer();
296
297 if (fullSyncRequested) {
298 sb.append("f1");
299 }
300 if (partialSyncUnavailable) {
301 sb.append("r1");
302 }
303 if (hasHardError()) {
304 sb.append("X1");
305 }
306 if (stats.numParseExceptions > 0) {
307 sb.append("e").append(stats.numParseExceptions);
308 }
309 if (stats.numConflictDetectedExceptions > 0) {
310 sb.append("c").append(stats.numConflictDetectedExceptions);
311 }
312 if (stats.numAuthExceptions > 0) {
313 sb.append("a").append(stats.numAuthExceptions);
314 }
315 if (tooManyDeletions) {
316 sb.append("D1");
317 }
318 if (tooManyRetries) {
319 sb.append("R1");
320 }
321 if (databaseError) {
322 sb.append("b1");
323 }
324 if (hasSoftError()) {
325 sb.append("x1");
326 }
327 if (syncAlreadyInProgress) {
328 sb.append("l1");
329 }
330 if (stats.numIoExceptions > 0) {
331 sb.append("I").append(stats.numIoExceptions);
332 }
333 return sb.toString();
334 }
335}