Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 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 | |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 23 | import java.util.ArrayList; |
| 24 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 25 | /** @hide */ |
| 26 | public class SyncStatusInfo implements Parcelable { |
Makoto Onuki | 15e7a25 | 2017-06-08 17:12:05 -0700 | [diff] [blame^] | 27 | private static final String TAG = "Sync"; |
| 28 | |
| 29 | static final int VERSION = 3; |
| 30 | |
| 31 | private static final int MAX_EVENT_COUNT = 10; |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 32 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 33 | public final int authorityId; |
| 34 | public long totalElapsedTime; |
| 35 | public int numSyncs; |
| 36 | public int numSourcePoll; |
| 37 | public int numSourceServer; |
| 38 | public int numSourceLocal; |
| 39 | public int numSourceUser; |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 40 | public int numSourcePeriodic; |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 41 | public long lastSuccessTime; |
| 42 | public int lastSuccessSource; |
| 43 | public long lastFailureTime; |
| 44 | public int lastFailureSource; |
| 45 | public String lastFailureMesg; |
| 46 | public long initialFailureTime; |
| 47 | public boolean pending; |
Costin Manolache | 5ed64cd | 2009-09-22 14:41:46 -0700 | [diff] [blame] | 48 | public boolean initialize; |
Georgi Nikolov | dbe846b | 2013-06-25 14:09:56 -0700 | [diff] [blame] | 49 | |
| 50 | // Warning: It is up to the external caller to ensure there are |
| 51 | // no race conditions when accessing this list |
| 52 | private ArrayList<Long> periodicSyncTimes; |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 53 | |
Makoto Onuki | 15e7a25 | 2017-06-08 17:12:05 -0700 | [diff] [blame^] | 54 | private final ArrayList<Long> mLastEventTimes = new ArrayList<>(); |
| 55 | private final ArrayList<String> mLastEvents = new ArrayList<>(); |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 56 | |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 57 | public SyncStatusInfo(int authorityId) { |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 58 | this.authorityId = authorityId; |
| 59 | } |
| 60 | |
| 61 | public int getLastFailureMesgAsInt(int def) { |
Alon Albert | 5c113fa | 2013-02-07 08:07:32 -0800 | [diff] [blame] | 62 | final int i = ContentResolver.syncErrorStringToInt(lastFailureMesg); |
| 63 | if (i > 0) { |
| 64 | return i; |
| 65 | } else { |
| 66 | Log.d(TAG, "Unknown lastFailureMesg:" + lastFailureMesg); |
| 67 | return def; |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 68 | } |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 69 | } |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 70 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 71 | public int describeContents() { |
| 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | public void writeToParcel(Parcel parcel, int flags) { |
| 76 | parcel.writeInt(VERSION); |
| 77 | parcel.writeInt(authorityId); |
| 78 | parcel.writeLong(totalElapsedTime); |
| 79 | parcel.writeInt(numSyncs); |
| 80 | parcel.writeInt(numSourcePoll); |
| 81 | parcel.writeInt(numSourceServer); |
| 82 | parcel.writeInt(numSourceLocal); |
| 83 | parcel.writeInt(numSourceUser); |
| 84 | parcel.writeLong(lastSuccessTime); |
| 85 | parcel.writeInt(lastSuccessSource); |
| 86 | parcel.writeLong(lastFailureTime); |
| 87 | parcel.writeInt(lastFailureSource); |
| 88 | parcel.writeString(lastFailureMesg); |
| 89 | parcel.writeLong(initialFailureTime); |
| 90 | parcel.writeInt(pending ? 1 : 0); |
Costin Manolache | 5ed64cd | 2009-09-22 14:41:46 -0700 | [diff] [blame] | 91 | parcel.writeInt(initialize ? 1 : 0); |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 92 | if (periodicSyncTimes != null) { |
| 93 | parcel.writeInt(periodicSyncTimes.size()); |
| 94 | for (long periodicSyncTime : periodicSyncTimes) { |
| 95 | parcel.writeLong(periodicSyncTime); |
| 96 | } |
| 97 | } else { |
| 98 | parcel.writeInt(-1); |
| 99 | } |
Makoto Onuki | 15e7a25 | 2017-06-08 17:12:05 -0700 | [diff] [blame^] | 100 | parcel.writeInt(mLastEventTimes.size()); |
| 101 | for (int i = 0; i < mLastEventTimes.size(); i++) { |
| 102 | parcel.writeLong(mLastEventTimes.get(i)); |
| 103 | parcel.writeString(mLastEvents.get(i)); |
| 104 | } |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 107 | public SyncStatusInfo(Parcel parcel) { |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 108 | int version = parcel.readInt(); |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 109 | if (version != VERSION && version != 1) { |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 110 | Log.w("SyncStatusInfo", "Unknown version: " + version); |
| 111 | } |
| 112 | authorityId = parcel.readInt(); |
| 113 | totalElapsedTime = parcel.readLong(); |
| 114 | numSyncs = parcel.readInt(); |
| 115 | numSourcePoll = parcel.readInt(); |
| 116 | numSourceServer = parcel.readInt(); |
| 117 | numSourceLocal = parcel.readInt(); |
| 118 | numSourceUser = parcel.readInt(); |
| 119 | lastSuccessTime = parcel.readLong(); |
| 120 | lastSuccessSource = parcel.readInt(); |
| 121 | lastFailureTime = parcel.readLong(); |
| 122 | lastFailureSource = parcel.readInt(); |
| 123 | lastFailureMesg = parcel.readString(); |
| 124 | initialFailureTime = parcel.readLong(); |
| 125 | pending = parcel.readInt() != 0; |
Costin Manolache | 5ed64cd | 2009-09-22 14:41:46 -0700 | [diff] [blame] | 126 | initialize = parcel.readInt() != 0; |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 127 | if (version == 1) { |
| 128 | periodicSyncTimes = null; |
| 129 | } else { |
Makoto Onuki | 15e7a25 | 2017-06-08 17:12:05 -0700 | [diff] [blame^] | 130 | final int count = parcel.readInt(); |
| 131 | if (count < 0) { |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 132 | periodicSyncTimes = null; |
| 133 | } else { |
| 134 | periodicSyncTimes = new ArrayList<Long>(); |
Makoto Onuki | 15e7a25 | 2017-06-08 17:12:05 -0700 | [diff] [blame^] | 135 | for (int i = 0; i < count; i++) { |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 136 | periodicSyncTimes.add(parcel.readLong()); |
| 137 | } |
| 138 | } |
Makoto Onuki | 15e7a25 | 2017-06-08 17:12:05 -0700 | [diff] [blame^] | 139 | if (version >= 3) { |
| 140 | mLastEventTimes.clear(); |
| 141 | mLastEvents.clear(); |
| 142 | final int nEvents = parcel.readInt(); |
| 143 | for (int i = 0; i < nEvents; i++) { |
| 144 | mLastEventTimes.add(parcel.readLong()); |
| 145 | mLastEvents.add(parcel.readString()); |
| 146 | } |
| 147 | } |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 148 | } |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 149 | } |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 150 | |
Georgi Nikolov | dbe846b | 2013-06-25 14:09:56 -0700 | [diff] [blame] | 151 | public SyncStatusInfo(SyncStatusInfo other) { |
| 152 | authorityId = other.authorityId; |
| 153 | totalElapsedTime = other.totalElapsedTime; |
| 154 | numSyncs = other.numSyncs; |
| 155 | numSourcePoll = other.numSourcePoll; |
| 156 | numSourceServer = other.numSourceServer; |
| 157 | numSourceLocal = other.numSourceLocal; |
| 158 | numSourceUser = other.numSourceUser; |
| 159 | numSourcePeriodic = other.numSourcePeriodic; |
| 160 | lastSuccessTime = other.lastSuccessTime; |
| 161 | lastSuccessSource = other.lastSuccessSource; |
| 162 | lastFailureTime = other.lastFailureTime; |
| 163 | lastFailureSource = other.lastFailureSource; |
| 164 | lastFailureMesg = other.lastFailureMesg; |
| 165 | initialFailureTime = other.initialFailureTime; |
| 166 | pending = other.pending; |
| 167 | initialize = other.initialize; |
| 168 | if (other.periodicSyncTimes != null) { |
| 169 | periodicSyncTimes = new ArrayList<Long>(other.periodicSyncTimes); |
| 170 | } |
Makoto Onuki | 15e7a25 | 2017-06-08 17:12:05 -0700 | [diff] [blame^] | 171 | mLastEventTimes.addAll(other.mLastEventTimes); |
| 172 | mLastEvents.addAll(other.mLastEvents); |
Georgi Nikolov | dbe846b | 2013-06-25 14:09:56 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 175 | public void setPeriodicSyncTime(int index, long when) { |
Georgi Nikolov | dbe846b | 2013-06-25 14:09:56 -0700 | [diff] [blame] | 176 | // The list is initialized lazily when scheduling occurs so we need to make sure |
| 177 | // we initialize elements < index to zero (zero is ignore for scheduling purposes) |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 178 | ensurePeriodicSyncTimeSize(index); |
| 179 | periodicSyncTimes.set(index, when); |
| 180 | } |
| 181 | |
Georgi Nikolov | dbe846b | 2013-06-25 14:09:56 -0700 | [diff] [blame] | 182 | public long getPeriodicSyncTime(int index) { |
| 183 | if (periodicSyncTimes != null && index < periodicSyncTimes.size()) { |
| 184 | return periodicSyncTimes.get(index); |
| 185 | } else { |
| 186 | return 0; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | public void removePeriodicSyncTime(int index) { |
| 191 | if (periodicSyncTimes != null && index < periodicSyncTimes.size()) { |
| 192 | periodicSyncTimes.remove(index); |
| 193 | } |
| 194 | } |
| 195 | |
Makoto Onuki | 15e7a25 | 2017-06-08 17:12:05 -0700 | [diff] [blame^] | 196 | /** */ |
| 197 | public void addEvent(String message) { |
| 198 | if (mLastEventTimes.size() >= MAX_EVENT_COUNT) { |
| 199 | mLastEventTimes.remove(MAX_EVENT_COUNT - 1); |
| 200 | mLastEvents.remove(MAX_EVENT_COUNT - 1); |
| 201 | } |
| 202 | mLastEventTimes.add(0, System.currentTimeMillis()); |
| 203 | mLastEvents.add(0, message); |
| 204 | } |
| 205 | |
| 206 | /** */ |
| 207 | public int getEventCount() { |
| 208 | return mLastEventTimes.size(); |
| 209 | } |
| 210 | |
| 211 | /** */ |
| 212 | public long getEventTime(int i) { |
| 213 | return mLastEventTimes.get(i); |
| 214 | } |
| 215 | |
| 216 | /** */ |
| 217 | public String getEvent(int i) { |
| 218 | return mLastEvents.get(i); |
| 219 | } |
| 220 | |
Georgi Nikolov | dbe846b | 2013-06-25 14:09:56 -0700 | [diff] [blame] | 221 | public static final Creator<SyncStatusInfo> CREATOR = new Creator<SyncStatusInfo>() { |
| 222 | public SyncStatusInfo createFromParcel(Parcel in) { |
| 223 | return new SyncStatusInfo(in); |
| 224 | } |
| 225 | |
| 226 | public SyncStatusInfo[] newArray(int size) { |
| 227 | return new SyncStatusInfo[size]; |
| 228 | } |
| 229 | }; |
| 230 | |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 231 | private void ensurePeriodicSyncTimeSize(int index) { |
| 232 | if (periodicSyncTimes == null) { |
| 233 | periodicSyncTimes = new ArrayList<Long>(0); |
| 234 | } |
| 235 | |
| 236 | final int requiredSize = index + 1; |
| 237 | if (periodicSyncTimes.size() < requiredSize) { |
| 238 | for (int i = periodicSyncTimes.size(); i < requiredSize; i++) { |
| 239 | periodicSyncTimes.add((long) 0); |
| 240 | } |
| 241 | } |
| 242 | } |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 243 | } |