SyncResult.java revision 15a4d2ffd04dc6c70f2cd17dae12ac6bc14c69ab
1/*
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
17package android.content;
18
19import android.os.Parcel;
20import android.os.Parcelable;
21
22/**
23 * This class is used to store information about the result of a sync
24 */
25public final class SyncResult implements Parcelable {
26    public final boolean syncAlreadyInProgress;
27    public boolean tooManyDeletions;
28    public boolean tooManyRetries;
29    public boolean databaseError;
30    public boolean fullSyncRequested;
31    public boolean partialSyncUnavailable;
32    public boolean moreRecordsToGet;
33
34    // in seconds since epoch
35    public long delayUntil;
36    public final SyncStats stats;
37    public static final SyncResult ALREADY_IN_PROGRESS;
38
39    static {
40        ALREADY_IN_PROGRESS = new SyncResult(true);
41    }
42
43    public SyncResult() {
44        this(false);
45    }
46
47    private SyncResult(boolean syncAlreadyInProgress) {
48        this.syncAlreadyInProgress = syncAlreadyInProgress;
49        this.tooManyDeletions = false;
50        this.tooManyRetries = false;
51        this.fullSyncRequested = false;
52        this.partialSyncUnavailable = false;
53        this.moreRecordsToGet = false;
54        this.delayUntil = 0;
55        this.stats = new SyncStats();
56    }
57
58    private SyncResult(Parcel parcel) {
59        syncAlreadyInProgress = parcel.readInt() != 0;
60        tooManyDeletions = parcel.readInt() != 0;
61        tooManyRetries = parcel.readInt() != 0;
62        databaseError = parcel.readInt() != 0;
63        fullSyncRequested = parcel.readInt() != 0;
64        partialSyncUnavailable = parcel.readInt() != 0;
65        moreRecordsToGet = parcel.readInt() != 0;
66        delayUntil = parcel.readLong();
67        stats = new SyncStats(parcel);
68    }
69
70    public boolean hasHardError() {
71        return stats.numParseExceptions > 0
72                || stats.numConflictDetectedExceptions > 0
73                || stats.numAuthExceptions > 0
74                || tooManyDeletions
75                || tooManyRetries
76                || databaseError;
77    }
78
79    public boolean hasSoftError() {
80        return syncAlreadyInProgress || stats.numIoExceptions > 0;
81    }
82
83    public boolean hasError() {
84        return hasSoftError() || hasHardError();
85    }
86
87    public boolean madeSomeProgress() {
88        return ((stats.numDeletes > 0) && !tooManyDeletions)
89                || stats.numInserts > 0
90                || stats.numUpdates > 0;
91    }
92
93    public void clear() {
94        if (syncAlreadyInProgress) {
95            throw new UnsupportedOperationException(
96                    "you are not allowed to clear the ALREADY_IN_PROGRESS SyncStats");
97        }
98        tooManyDeletions = false;
99        tooManyRetries = false;
100        databaseError = false;
101        fullSyncRequested = false;
102        partialSyncUnavailable = false;
103        moreRecordsToGet = false;
104        delayUntil = 0;
105        stats.clear();
106    }
107
108    public static final Creator<SyncResult> CREATOR = new Creator<SyncResult>() {
109        public SyncResult createFromParcel(Parcel in) {
110            return new SyncResult(in);
111        }
112
113        public SyncResult[] newArray(int size) {
114            return new SyncResult[size];
115        }
116    };
117
118    public int describeContents() {
119        return 0;
120    }
121
122    public void writeToParcel(Parcel parcel, int flags) {
123        parcel.writeInt(syncAlreadyInProgress ? 1 : 0);
124        parcel.writeInt(tooManyDeletions ? 1 : 0);
125        parcel.writeInt(tooManyRetries ? 1 : 0);
126        parcel.writeInt(databaseError ? 1 : 0);
127        parcel.writeInt(fullSyncRequested ? 1 : 0);
128        parcel.writeInt(partialSyncUnavailable ? 1 : 0);
129        parcel.writeInt(moreRecordsToGet ? 1 : 0);
130        parcel.writeLong(delayUntil);
131        stats.writeToParcel(parcel, flags);
132    }
133
134    @Override
135    public String toString() {
136        StringBuilder sb = new StringBuilder();
137        sb.append("SyncResult:");
138        if (syncAlreadyInProgress) {
139            sb.append(" syncAlreadyInProgress: ").append(syncAlreadyInProgress);
140        }
141        if (tooManyDeletions) sb.append(" tooManyDeletions: ").append(tooManyDeletions);
142        if (tooManyRetries) sb.append(" tooManyRetries: ").append(tooManyRetries);
143        if (databaseError) sb.append(" databaseError: ").append(databaseError);
144        if (fullSyncRequested) sb.append(" fullSyncRequested: ").append(fullSyncRequested);
145        if (partialSyncUnavailable) {
146            sb.append(" partialSyncUnavailable: ").append(partialSyncUnavailable);
147        }
148        if (moreRecordsToGet) sb.append(" moreRecordsToGet: ").append(moreRecordsToGet);
149        if (delayUntil > 0) sb.append(" delayUntil: ").append(delayUntil);
150        sb.append(stats);
151        return sb.toString();
152    }
153
154    /**
155     * Generates a debugging string indicating the status.
156     * The string consist of a sequence of code letter followed by the count.
157     * Code letters are f - fullSyncRequested, r - partialSyncUnavailable,
158     * X - hardError, e - numParseExceptions, c - numConflictDetectedExceptions,
159     * a - numAuthExceptions, D - tooManyDeletions, R - tooManyRetries,
160     * b - databaseError, x - softError, l - syncAlreadyInProgress,
161     * I - numIoExceptions
162     * @return debugging string.
163     */
164    public String toDebugString() {
165        StringBuffer sb = new StringBuffer();
166
167        if (fullSyncRequested) {
168            sb.append("f1");
169        }
170        if (partialSyncUnavailable) {
171            sb.append("r1");
172        }
173        if (hasHardError()) {
174            sb.append("X1");
175        }
176        if (stats.numParseExceptions > 0) {
177            sb.append("e").append(stats.numParseExceptions);
178        }
179        if (stats.numConflictDetectedExceptions > 0) {
180            sb.append("c").append(stats.numConflictDetectedExceptions);
181        }
182        if (stats.numAuthExceptions > 0) {
183            sb.append("a").append(stats.numAuthExceptions);
184        }
185        if (tooManyDeletions) {
186            sb.append("D1");
187        }
188        if (tooManyRetries) {
189            sb.append("R1");
190        }
191        if (databaseError) {
192            sb.append("b1");
193        }
194        if (hasSoftError()) {
195            sb.append("x1");
196        }
197        if (syncAlreadyInProgress) {
198            sb.append("l1");
199        }
200        if (stats.numIoExceptions > 0) {
201            sb.append("I").append(stats.numIoExceptions);
202        }
203        return sb.toString();
204    }
205}
206