1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
|
/*
* Copyright (C) 2009 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.content;
import android.annotation.UnsupportedAppUsage;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
/** @hide */
public class SyncStatusInfo implements Parcelable {
private static final String TAG = "Sync";
static final int VERSION = 6;
private static final int MAX_EVENT_COUNT = 10;
/**
* Number of sync sources. KEEP THIS AND SyncStorageEngine.SOURCES IN SYNC.
*/
private static final int SOURCE_COUNT = 6;
@UnsupportedAppUsage
public final int authorityId;
/**
* # of syncs for each sync source, etc.
*/
public static class Stats {
public long totalElapsedTime;
public int numSyncs;
public int numSourcePoll;
public int numSourceOther;
public int numSourceLocal;
public int numSourceUser;
public int numSourcePeriodic;
public int numSourceFeed;
public int numFailures;
public int numCancels;
/** Copy all the stats to another instance. */
public void copyTo(Stats to) {
to.totalElapsedTime = totalElapsedTime;
to.numSyncs = numSyncs;
to.numSourcePoll = numSourcePoll;
to.numSourceOther = numSourceOther;
to.numSourceLocal = numSourceLocal;
to.numSourceUser = numSourceUser;
to.numSourcePeriodic = numSourcePeriodic;
to.numSourceFeed = numSourceFeed;
to.numFailures = numFailures;
to.numCancels = numCancels;
}
/** Clear all the stats. */
public void clear() {
totalElapsedTime = 0;
numSyncs = 0;
numSourcePoll = 0;
numSourceOther = 0;
numSourceLocal = 0;
numSourceUser = 0;
numSourcePeriodic = 0;
numSourceFeed = 0;
numFailures = 0;
numCancels = 0;
}
/** Write all the stats to a parcel. */
public void writeToParcel(Parcel parcel) {
parcel.writeLong(totalElapsedTime);
parcel.writeInt(numSyncs);
parcel.writeInt(numSourcePoll);
parcel.writeInt(numSourceOther);
parcel.writeInt(numSourceLocal);
parcel.writeInt(numSourceUser);
parcel.writeInt(numSourcePeriodic);
parcel.writeInt(numSourceFeed);
parcel.writeInt(numFailures);
parcel.writeInt(numCancels);
}
/** Read all the stats from a parcel. */
public void readFromParcel(Parcel parcel) {
totalElapsedTime = parcel.readLong();
numSyncs = parcel.readInt();
numSourcePoll = parcel.readInt();
numSourceOther = parcel.readInt();
numSourceLocal = parcel.readInt();
numSourceUser = parcel.readInt();
numSourcePeriodic = parcel.readInt();
numSourceFeed = parcel.readInt();
numFailures = parcel.readInt();
numCancels = parcel.readInt();
}
}
public long lastTodayResetTime;
public final Stats totalStats = new Stats();
public final Stats todayStats = new Stats();
public final Stats yesterdayStats = new Stats();
@UnsupportedAppUsage
public long lastSuccessTime;
@UnsupportedAppUsage
public int lastSuccessSource;
@UnsupportedAppUsage
public long lastFailureTime;
@UnsupportedAppUsage
public int lastFailureSource;
@UnsupportedAppUsage
public String lastFailureMesg;
@UnsupportedAppUsage
public long initialFailureTime;
@UnsupportedAppUsage
public boolean pending;
@UnsupportedAppUsage
public boolean initialize;
public final long[] perSourceLastSuccessTimes = new long[SOURCE_COUNT];
public final long[] perSourceLastFailureTimes = new long[SOURCE_COUNT];
// Warning: It is up to the external caller to ensure there are
// no race conditions when accessing this list
@UnsupportedAppUsage
private ArrayList<Long> periodicSyncTimes;
private final ArrayList<Long> mLastEventTimes = new ArrayList<>();
private final ArrayList<String> mLastEvents = new ArrayList<>();
@UnsupportedAppUsage
public SyncStatusInfo(int authorityId) {
this.authorityId = authorityId;
}
@UnsupportedAppUsage
public int getLastFailureMesgAsInt(int def) {
final int i = ContentResolver.syncErrorStringToInt(lastFailureMesg);
if (i > 0) {
return i;
} else {
Log.d(TAG, "Unknown lastFailureMesg:" + lastFailureMesg);
return def;
}
}
public int describeContents() {
return 0;
}
public void writeToParcel(Parcel parcel, int flags) {
parcel.writeInt(VERSION);
parcel.writeInt(authorityId);
// Note we can't use Stats.writeToParcel() here; see the below constructor for the reason.
parcel.writeLong(totalStats.totalElapsedTime);
parcel.writeInt(totalStats.numSyncs);
parcel.writeInt(totalStats.numSourcePoll);
parcel.writeInt(totalStats.numSourceOther);
parcel.writeInt(totalStats.numSourceLocal);
parcel.writeInt(totalStats.numSourceUser);
parcel.writeLong(lastSuccessTime);
parcel.writeInt(lastSuccessSource);
parcel.writeLong(lastFailureTime);
parcel.writeInt(lastFailureSource);
parcel.writeString(lastFailureMesg);
parcel.writeLong(initialFailureTime);
parcel.writeInt(pending ? 1 : 0);
parcel.writeInt(initialize ? 1 : 0);
if (periodicSyncTimes != null) {
parcel.writeInt(periodicSyncTimes.size());
for (long periodicSyncTime : periodicSyncTimes) {
parcel.writeLong(periodicSyncTime);
}
} else {
parcel.writeInt(-1);
}
parcel.writeInt(mLastEventTimes.size());
for (int i = 0; i < mLastEventTimes.size(); i++) {
parcel.writeLong(mLastEventTimes.get(i));
parcel.writeString(mLastEvents.get(i));
}
// Version 4
parcel.writeInt(totalStats.numSourcePeriodic);
// Version 5
parcel.writeInt(totalStats.numSourceFeed);
parcel.writeInt(totalStats.numFailures);
parcel.writeInt(totalStats.numCancels);
parcel.writeLong(lastTodayResetTime);
todayStats.writeToParcel(parcel);
yesterdayStats.writeToParcel(parcel);
// Version 6.
parcel.writeLongArray(perSourceLastSuccessTimes);
parcel.writeLongArray(perSourceLastFailureTimes);
}
@UnsupportedAppUsage
public SyncStatusInfo(Parcel parcel) {
int version = parcel.readInt();
if (version != VERSION && version != 1) {
Log.w("SyncStatusInfo", "Unknown version: " + version);
}
authorityId = parcel.readInt();
// Note we can't use Stats.writeToParcel() here because the data is persisted and we need
// to be able to read from the old format too.
totalStats.totalElapsedTime = parcel.readLong();
totalStats.numSyncs = parcel.readInt();
totalStats.numSourcePoll = parcel.readInt();
totalStats.numSourceOther = parcel.readInt();
totalStats.numSourceLocal = parcel.readInt();
totalStats.numSourceUser = parcel.readInt();
lastSuccessTime = parcel.readLong();
lastSuccessSource = parcel.readInt();
lastFailureTime = parcel.readLong();
lastFailureSource = parcel.readInt();
lastFailureMesg = parcel.readString();
initialFailureTime = parcel.readLong();
pending = parcel.readInt() != 0;
initialize = parcel.readInt() != 0;
if (version == 1) {
periodicSyncTimes = null;
} else {
final int count = parcel.readInt();
if (count < 0) {
periodicSyncTimes = null;
} else {
periodicSyncTimes = new ArrayList<Long>();
for (int i = 0; i < count; i++) {
periodicSyncTimes.add(parcel.readLong());
}
}
if (version >= 3) {
mLastEventTimes.clear();
mLastEvents.clear();
final int nEvents = parcel.readInt();
for (int i = 0; i < nEvents; i++) {
mLastEventTimes.add(parcel.readLong());
mLastEvents.add(parcel.readString());
}
}
}
if (version < 4) {
// Before version 4, numSourcePeriodic wasn't persisted.
totalStats.numSourcePeriodic =
totalStats.numSyncs - totalStats.numSourceLocal - totalStats.numSourcePoll
- totalStats.numSourceOther
- totalStats.numSourceUser;
if (totalStats.numSourcePeriodic < 0) { // Sanity check.
totalStats.numSourcePeriodic = 0;
}
} else {
totalStats.numSourcePeriodic = parcel.readInt();
}
if (version >= 5) {
totalStats.numSourceFeed = parcel.readInt();
totalStats.numFailures = parcel.readInt();
totalStats.numCancels = parcel.readInt();
lastTodayResetTime = parcel.readLong();
todayStats.readFromParcel(parcel);
yesterdayStats.readFromParcel(parcel);
}
if (version >= 6) {
parcel.readLongArray(perSourceLastSuccessTimes);
parcel.readLongArray(perSourceLastFailureTimes);
}
}
public SyncStatusInfo(SyncStatusInfo other) {
authorityId = other.authorityId;
other.totalStats.copyTo(totalStats);
other.todayStats.copyTo(todayStats);
other.yesterdayStats.copyTo(yesterdayStats);
lastTodayResetTime = other.lastTodayResetTime;
lastSuccessTime = other.lastSuccessTime;
lastSuccessSource = other.lastSuccessSource;
lastFailureTime = other.lastFailureTime;
lastFailureSource = other.lastFailureSource;
lastFailureMesg = other.lastFailureMesg;
initialFailureTime = other.initialFailureTime;
pending = other.pending;
initialize = other.initialize;
if (other.periodicSyncTimes != null) {
periodicSyncTimes = new ArrayList<Long>(other.periodicSyncTimes);
}
mLastEventTimes.addAll(other.mLastEventTimes);
mLastEvents.addAll(other.mLastEvents);
copy(perSourceLastSuccessTimes, other.perSourceLastSuccessTimes);
copy(perSourceLastFailureTimes, other.perSourceLastFailureTimes);
}
private static void copy(long[] to, long[] from) {
System.arraycopy(from, 0, to, 0, to.length);
}
@UnsupportedAppUsage
public void setPeriodicSyncTime(int index, long when) {
// The list is initialized lazily when scheduling occurs so we need to make sure
// we initialize elements < index to zero (zero is ignore for scheduling purposes)
ensurePeriodicSyncTimeSize(index);
periodicSyncTimes.set(index, when);
}
@UnsupportedAppUsage
public long getPeriodicSyncTime(int index) {
if (periodicSyncTimes != null && index < periodicSyncTimes.size()) {
return periodicSyncTimes.get(index);
} else {
return 0;
}
}
@UnsupportedAppUsage
public void removePeriodicSyncTime(int index) {
if (periodicSyncTimes != null && index < periodicSyncTimes.size()) {
periodicSyncTimes.remove(index);
}
}
/** */
public void addEvent(String message) {
if (mLastEventTimes.size() >= MAX_EVENT_COUNT) {
mLastEventTimes.remove(MAX_EVENT_COUNT - 1);
mLastEvents.remove(MAX_EVENT_COUNT - 1);
}
mLastEventTimes.add(0, System.currentTimeMillis());
mLastEvents.add(0, message);
}
/** */
public int getEventCount() {
return mLastEventTimes.size();
}
/** */
public long getEventTime(int i) {
return mLastEventTimes.get(i);
}
/** */
public String getEvent(int i) {
return mLastEvents.get(i);
}
/** Call this when a sync has succeeded. */
public void setLastSuccess(int source, long lastSyncTime) {
lastSuccessTime = lastSyncTime;
lastSuccessSource = source;
lastFailureTime = 0;
lastFailureSource = -1;
lastFailureMesg = null;
initialFailureTime = 0;
if (0 <= source && source < perSourceLastSuccessTimes.length) {
perSourceLastSuccessTimes[source] = lastSyncTime;
}
}
/** Call this when a sync has failed. */
public void setLastFailure(int source, long lastSyncTime, String failureMessage) {
lastFailureTime = lastSyncTime;
lastFailureSource = source;
lastFailureMesg = failureMessage;
if (initialFailureTime == 0) {
initialFailureTime = lastSyncTime;
}
if (0 <= source && source < perSourceLastFailureTimes.length) {
perSourceLastFailureTimes[source] = lastSyncTime;
}
}
@UnsupportedAppUsage
public static final @android.annotation.NonNull Creator<SyncStatusInfo> CREATOR = new Creator<SyncStatusInfo>() {
public SyncStatusInfo createFromParcel(Parcel in) {
return new SyncStatusInfo(in);
}
public SyncStatusInfo[] newArray(int size) {
return new SyncStatusInfo[size];
}
};
@UnsupportedAppUsage
private void ensurePeriodicSyncTimeSize(int index) {
if (periodicSyncTimes == null) {
periodicSyncTimes = new ArrayList<Long>(0);
}
final int requiredSize = index + 1;
if (periodicSyncTimes.size() < requiredSize) {
for (int i = periodicSyncTimes.size(); i < requiredSize; i++) {
periodicSyncTimes.add((long) 0);
}
}
}
/**
* If the last reset was not today, move today's stats to yesterday's and clear today's.
*/
public void maybeResetTodayStats(boolean clockValid, boolean force) {
final long now = System.currentTimeMillis();
if (!force) {
// Last reset was the same day, nothing to do.
if (areSameDates(now, lastTodayResetTime)) {
return;
}
// Hack -- on devices with no RTC, until the NTP kicks in, the device won't have the
// correct time. So if the time goes back, don't reset, unless we're sure the current
// time is correct.
if (now < lastTodayResetTime && !clockValid) {
return;
}
}
lastTodayResetTime = now;
todayStats.copyTo(yesterdayStats);
todayStats.clear();
}
private static boolean areSameDates(long time1, long time2) {
final Calendar c1 = new GregorianCalendar();
final Calendar c2 = new GregorianCalendar();
c1.setTimeInMillis(time1);
c2.setTimeInMillis(time2);
return c1.get(Calendar.YEAR) == c2.get(Calendar.YEAR)
&& c1.get(Calendar.DAY_OF_YEAR) == c2.get(Calendar.DAY_OF_YEAR);
}
}
|