1 /*
2  * Copyright (C) 2007 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 
17 package android.content;
18 
19 import android.os.Parcelable;
20 import android.os.Parcel;
21 
22 /**
23  * Used to record various statistics about the result of a sync operation. The SyncManager
24  * gets access to these via a {@link SyncResult} and uses some of them to determine the
25  * disposition of the sync. See {@link SyncResult} for further dicussion on how the
26  * SyncManager uses these values.
27  */
28 public class SyncStats implements Parcelable {
29     /**
30      * The SyncAdapter was unable to authenticate the {@link android.accounts.Account}
31      * that was specified in the request. The user needs to take some action to resolve
32      * before a future request can expect to succeed. This is considered a hard error.
33      */
34     public long numAuthExceptions;
35 
36     /**
37      * The SyncAdapter had a problem, most likely with the network connectivity or a timeout
38      * while waiting for a network response. The request may succeed if it is tried again
39      * later. This is considered a soft error.
40      */
41     public long numIoExceptions;
42 
43     /**
44      * The SyncAdapter had a problem with the data it received from the server or the storage
45      * later. This problem will likely repeat if the request is tried again. The problem
46      * will need to be cleared up by either the server or the storage layer (likely with help
47      * from the user). If the SyncAdapter cleans up the data itself then it typically won't
48      * increment this value although it may still do so in order to record that it had to
49      * perform some cleanup. E.g., if the SyncAdapter received a bad entry from the server
50      * when processing a feed of entries, it may choose to drop the entry and thus make
51      * progress and still increment this value just so the SyncAdapter can record that an
52      * error occurred. This is considered a hard error.
53      */
54     public long numParseExceptions;
55 
56     /**
57      * The SyncAdapter detected that there was an unrecoverable version conflict when it
58      * attempted to update or delete a version of a resource on the server. This is expected
59      * to clear itself automatically once the new state is retrieved from the server,
60      * though it may remain until the user intervenes manually, perhaps by clearing the
61      * local storage and starting over from scratch. This is considered a hard error.
62      */
63     public long numConflictDetectedExceptions;
64 
65     /**
66      * Counter for tracking how many inserts were performed by the sync operation, as defined
67      * by the SyncAdapter.
68      */
69     public long numInserts;
70 
71     /**
72      * Counter for tracking how many updates were performed by the sync operation, as defined
73      * by the SyncAdapter.
74      */
75     public long numUpdates;
76 
77     /**
78      * Counter for tracking how many deletes were performed by the sync operation, as defined
79      * by the SyncAdapter.
80      */
81     public long numDeletes;
82 
83     /**
84      * Counter for tracking how many entries were affected by the sync operation, as defined
85      * by the SyncAdapter.
86      */
87     public long numEntries;
88 
89     /**
90      * Counter for tracking how many entries, either from the server or the local store, were
91      * ignored during the sync operation. This could happen if the SyncAdapter detected some
92      * unparsable data but decided to skip it and move on rather than failing immediately.
93      */
94     public long numSkippedEntries;
95 
SyncStats()96     public SyncStats() {
97         numAuthExceptions = 0;
98         numIoExceptions = 0;
99         numParseExceptions = 0;
100         numConflictDetectedExceptions = 0;
101         numInserts = 0;
102         numUpdates = 0;
103         numDeletes = 0;
104         numEntries = 0;
105         numSkippedEntries = 0;
106     }
107 
SyncStats(Parcel in)108     public SyncStats(Parcel in) {
109         numAuthExceptions = in.readLong();
110         numIoExceptions = in.readLong();
111         numParseExceptions = in.readLong();
112         numConflictDetectedExceptions = in.readLong();
113         numInserts = in.readLong();
114         numUpdates = in.readLong();
115         numDeletes = in.readLong();
116         numEntries = in.readLong();
117         numSkippedEntries = in.readLong();
118     }
119 
120     @Override
toString()121     public String toString() {
122         StringBuilder sb = new StringBuilder();
123         sb.append(" stats [");
124         if (numAuthExceptions > 0) sb.append(" numAuthExceptions: ").append(numAuthExceptions);
125         if (numIoExceptions > 0) sb.append(" numIoExceptions: ").append(numIoExceptions);
126         if (numParseExceptions > 0) sb.append(" numParseExceptions: ").append(numParseExceptions);
127         if (numConflictDetectedExceptions > 0)
128             sb.append(" numConflictDetectedExceptions: ").append(numConflictDetectedExceptions);
129         if (numInserts > 0) sb.append(" numInserts: ").append(numInserts);
130         if (numUpdates > 0) sb.append(" numUpdates: ").append(numUpdates);
131         if (numDeletes > 0) sb.append(" numDeletes: ").append(numDeletes);
132         if (numEntries > 0) sb.append(" numEntries: ").append(numEntries);
133         if (numSkippedEntries > 0) sb.append(" numSkippedEntries: ").append(numSkippedEntries);
134         sb.append("]");
135         return sb.toString();
136     }
137 
138     /**
139      * Reset all the counters to 0.
140      */
clear()141     public void clear() {
142         numAuthExceptions = 0;
143         numIoExceptions = 0;
144         numParseExceptions = 0;
145         numConflictDetectedExceptions = 0;
146         numInserts = 0;
147         numUpdates = 0;
148         numDeletes = 0;
149         numEntries = 0;
150         numSkippedEntries = 0;
151     }
152 
describeContents()153     public int describeContents() {
154         return 0;
155     }
156 
writeToParcel(Parcel dest, int flags)157     public void writeToParcel(Parcel dest, int flags) {
158         dest.writeLong(numAuthExceptions);
159         dest.writeLong(numIoExceptions);
160         dest.writeLong(numParseExceptions);
161         dest.writeLong(numConflictDetectedExceptions);
162         dest.writeLong(numInserts);
163         dest.writeLong(numUpdates);
164         dest.writeLong(numDeletes);
165         dest.writeLong(numEntries);
166         dest.writeLong(numSkippedEntries);
167     }
168 
169     public static final @android.annotation.NonNull Creator<SyncStats> CREATOR = new Creator<SyncStats>() {
170         public SyncStats createFromParcel(Parcel in) {
171             return new SyncStats(in);
172         }
173 
174         public SyncStats[] newArray(int size) {
175             return new SyncStats[size];
176         }
177     };
178 }
179