• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2009 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.Parcel;
20 import android.os.Parcelable;
21 import android.util.Log;
22 
23 import java.util.ArrayList;
24 
25 /** @hide */
26 public class SyncStatusInfo implements Parcelable {
27     static final int VERSION = 2;
28 
29     public final int authorityId;
30     public long totalElapsedTime;
31     public int numSyncs;
32     public int numSourcePoll;
33     public int numSourceServer;
34     public int numSourceLocal;
35     public int numSourceUser;
36     public int numSourcePeriodic;
37     public long lastSuccessTime;
38     public int lastSuccessSource;
39     public long lastFailureTime;
40     public int lastFailureSource;
41     public String lastFailureMesg;
42     public long initialFailureTime;
43     public boolean pending;
44     public boolean initialize;
45     public ArrayList<Long> periodicSyncTimes;
46 
47     private static final String TAG = "Sync";
48 
SyncStatusInfo(int authorityId)49     SyncStatusInfo(int authorityId) {
50         this.authorityId = authorityId;
51     }
52 
getLastFailureMesgAsInt(int def)53     public int getLastFailureMesgAsInt(int def) {
54         try {
55             if (lastFailureMesg != null) {
56                 return Integer.parseInt(lastFailureMesg);
57             }
58         } catch (NumberFormatException e) {
59             Log.d(TAG, "error parsing lastFailureMesg of " + lastFailureMesg, e);
60         }
61         return def;
62     }
63 
describeContents()64     public int describeContents() {
65         return 0;
66     }
67 
writeToParcel(Parcel parcel, int flags)68     public void writeToParcel(Parcel parcel, int flags) {
69         parcel.writeInt(VERSION);
70         parcel.writeInt(authorityId);
71         parcel.writeLong(totalElapsedTime);
72         parcel.writeInt(numSyncs);
73         parcel.writeInt(numSourcePoll);
74         parcel.writeInt(numSourceServer);
75         parcel.writeInt(numSourceLocal);
76         parcel.writeInt(numSourceUser);
77         parcel.writeLong(lastSuccessTime);
78         parcel.writeInt(lastSuccessSource);
79         parcel.writeLong(lastFailureTime);
80         parcel.writeInt(lastFailureSource);
81         parcel.writeString(lastFailureMesg);
82         parcel.writeLong(initialFailureTime);
83         parcel.writeInt(pending ? 1 : 0);
84         parcel.writeInt(initialize ? 1 : 0);
85         if (periodicSyncTimes != null) {
86             parcel.writeInt(periodicSyncTimes.size());
87             for (long periodicSyncTime : periodicSyncTimes) {
88                 parcel.writeLong(periodicSyncTime);
89             }
90         } else {
91             parcel.writeInt(-1);
92         }
93     }
94 
SyncStatusInfo(Parcel parcel)95     SyncStatusInfo(Parcel parcel) {
96         int version = parcel.readInt();
97         if (version != VERSION && version != 1) {
98             Log.w("SyncStatusInfo", "Unknown version: " + version);
99         }
100         authorityId = parcel.readInt();
101         totalElapsedTime = parcel.readLong();
102         numSyncs = parcel.readInt();
103         numSourcePoll = parcel.readInt();
104         numSourceServer = parcel.readInt();
105         numSourceLocal = parcel.readInt();
106         numSourceUser = parcel.readInt();
107         lastSuccessTime = parcel.readLong();
108         lastSuccessSource = parcel.readInt();
109         lastFailureTime = parcel.readLong();
110         lastFailureSource = parcel.readInt();
111         lastFailureMesg = parcel.readString();
112         initialFailureTime = parcel.readLong();
113         pending = parcel.readInt() != 0;
114         initialize = parcel.readInt() != 0;
115         if (version == 1) {
116             periodicSyncTimes = null;
117         } else {
118             int N = parcel.readInt();
119             if (N < 0) {
120                 periodicSyncTimes = null;
121             } else {
122                 periodicSyncTimes = new ArrayList<Long>();
123                 for (int i=0; i<N; i++) {
124                     periodicSyncTimes.add(parcel.readLong());
125                 }
126             }
127         }
128     }
129 
setPeriodicSyncTime(int index, long when)130     public void setPeriodicSyncTime(int index, long when) {
131         ensurePeriodicSyncTimeSize(index);
132         periodicSyncTimes.set(index, when);
133     }
134 
ensurePeriodicSyncTimeSize(int index)135     private void ensurePeriodicSyncTimeSize(int index) {
136         if (periodicSyncTimes == null) {
137             periodicSyncTimes = new ArrayList<Long>(0);
138         }
139 
140         final int requiredSize = index + 1;
141         if (periodicSyncTimes.size() < requiredSize) {
142             for (int i = periodicSyncTimes.size(); i < requiredSize; i++) {
143                 periodicSyncTimes.add((long) 0);
144             }
145         }
146     }
147 
getPeriodicSyncTime(int index)148     public long getPeriodicSyncTime(int index) {
149         if (periodicSyncTimes == null || periodicSyncTimes.size() < (index + 1)) {
150             return 0;
151         }
152         return periodicSyncTimes.get(index);
153     }
154 
removePeriodicSyncTime(int index)155     public void removePeriodicSyncTime(int index) {
156         ensurePeriodicSyncTimeSize(index);
157         periodicSyncTimes.remove(index);
158     }
159 
160     public static final Creator<SyncStatusInfo> CREATOR = new Creator<SyncStatusInfo>() {
161         public SyncStatusInfo createFromParcel(Parcel in) {
162             return new SyncStatusInfo(in);
163         }
164 
165         public SyncStatusInfo[] newArray(int size) {
166             return new SyncStatusInfo[size];
167         }
168     };
169 }