1231cc608d06ffc31c24bf8aa8c8275bdd2636581Dianne Hackborn/* 2231cc608d06ffc31c24bf8aa8c8275bdd2636581Dianne Hackborn * Copyright (C) 2009 The Android Open Source Project 3231cc608d06ffc31c24bf8aa8c8275bdd2636581Dianne Hackborn * 4231cc608d06ffc31c24bf8aa8c8275bdd2636581Dianne Hackborn * Licensed under the Apache License, Version 2.0 (the "License"); 5231cc608d06ffc31c24bf8aa8c8275bdd2636581Dianne Hackborn * you may not use this file except in compliance with the License. 6231cc608d06ffc31c24bf8aa8c8275bdd2636581Dianne Hackborn * You may obtain a copy of the License at 7231cc608d06ffc31c24bf8aa8c8275bdd2636581Dianne Hackborn * 8231cc608d06ffc31c24bf8aa8c8275bdd2636581Dianne Hackborn * http://www.apache.org/licenses/LICENSE-2.0 9231cc608d06ffc31c24bf8aa8c8275bdd2636581Dianne Hackborn * 10231cc608d06ffc31c24bf8aa8c8275bdd2636581Dianne Hackborn * Unless required by applicable law or agreed to in writing, software 11231cc608d06ffc31c24bf8aa8c8275bdd2636581Dianne Hackborn * distributed under the License is distributed on an "AS IS" BASIS, 12231cc608d06ffc31c24bf8aa8c8275bdd2636581Dianne Hackborn * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13231cc608d06ffc31c24bf8aa8c8275bdd2636581Dianne Hackborn * See the License for the specific language governing permissions and 14231cc608d06ffc31c24bf8aa8c8275bdd2636581Dianne Hackborn * limitations under the License. 15231cc608d06ffc31c24bf8aa8c8275bdd2636581Dianne Hackborn */ 16231cc608d06ffc31c24bf8aa8c8275bdd2636581Dianne Hackborn 17231cc608d06ffc31c24bf8aa8c8275bdd2636581Dianne Hackbornpackage android.content; 18231cc608d06ffc31c24bf8aa8c8275bdd2636581Dianne Hackborn 19231cc608d06ffc31c24bf8aa8c8275bdd2636581Dianne Hackbornimport android.os.Parcel; 20231cc608d06ffc31c24bf8aa8c8275bdd2636581Dianne Hackbornimport android.os.Parcelable; 21231cc608d06ffc31c24bf8aa8c8275bdd2636581Dianne Hackbornimport android.util.Log; 22231cc608d06ffc31c24bf8aa8c8275bdd2636581Dianne Hackborn 23c5d1c6db61f208b206b260f897bb5bbc64be4d97Fred Quintanaimport java.util.ArrayList; 24c5d1c6db61f208b206b260f897bb5bbc64be4d97Fred Quintana 25231cc608d06ffc31c24bf8aa8c8275bdd2636581Dianne Hackborn/** @hide */ 26231cc608d06ffc31c24bf8aa8c8275bdd2636581Dianne Hackbornpublic class SyncStatusInfo implements Parcelable { 27c5d1c6db61f208b206b260f897bb5bbc64be4d97Fred Quintana static final int VERSION = 2; 28c5d1c6db61f208b206b260f897bb5bbc64be4d97Fred Quintana 29231cc608d06ffc31c24bf8aa8c8275bdd2636581Dianne Hackborn public final int authorityId; 30231cc608d06ffc31c24bf8aa8c8275bdd2636581Dianne Hackborn public long totalElapsedTime; 31231cc608d06ffc31c24bf8aa8c8275bdd2636581Dianne Hackborn public int numSyncs; 32231cc608d06ffc31c24bf8aa8c8275bdd2636581Dianne Hackborn public int numSourcePoll; 33231cc608d06ffc31c24bf8aa8c8275bdd2636581Dianne Hackborn public int numSourceServer; 34231cc608d06ffc31c24bf8aa8c8275bdd2636581Dianne Hackborn public int numSourceLocal; 35231cc608d06ffc31c24bf8aa8c8275bdd2636581Dianne Hackborn public int numSourceUser; 36c5d1c6db61f208b206b260f897bb5bbc64be4d97Fred Quintana public int numSourcePeriodic; 37231cc608d06ffc31c24bf8aa8c8275bdd2636581Dianne Hackborn public long lastSuccessTime; 38231cc608d06ffc31c24bf8aa8c8275bdd2636581Dianne Hackborn public int lastSuccessSource; 39231cc608d06ffc31c24bf8aa8c8275bdd2636581Dianne Hackborn public long lastFailureTime; 40231cc608d06ffc31c24bf8aa8c8275bdd2636581Dianne Hackborn public int lastFailureSource; 41231cc608d06ffc31c24bf8aa8c8275bdd2636581Dianne Hackborn public String lastFailureMesg; 42231cc608d06ffc31c24bf8aa8c8275bdd2636581Dianne Hackborn public long initialFailureTime; 43231cc608d06ffc31c24bf8aa8c8275bdd2636581Dianne Hackborn public boolean pending; 445ed64cd06ce308e91a5ab74e51bcea5fba9a3860Costin Manolache public boolean initialize; 45dbe846b02e6f6f715787cf8621587f7bc25deaacGeorgi Nikolov 46dbe846b02e6f6f715787cf8621587f7bc25deaacGeorgi Nikolov // Warning: It is up to the external caller to ensure there are 47dbe846b02e6f6f715787cf8621587f7bc25deaacGeorgi Nikolov // no race conditions when accessing this list 48dbe846b02e6f6f715787cf8621587f7bc25deaacGeorgi Nikolov private ArrayList<Long> periodicSyncTimes; 49c5d1c6db61f208b206b260f897bb5bbc64be4d97Fred Quintana 50c5d1c6db61f208b206b260f897bb5bbc64be4d97Fred Quintana private static final String TAG = "Sync"; 51c5d1c6db61f208b206b260f897bb5bbc64be4d97Fred Quintana 527a96c39c510923ef73bbb06ab20109f0168b8eb1Jeff Sharkey public SyncStatusInfo(int authorityId) { 53231cc608d06ffc31c24bf8aa8c8275bdd2636581Dianne Hackborn this.authorityId = authorityId; 54231cc608d06ffc31c24bf8aa8c8275bdd2636581Dianne Hackborn } 55231cc608d06ffc31c24bf8aa8c8275bdd2636581Dianne Hackborn 56231cc608d06ffc31c24bf8aa8c8275bdd2636581Dianne Hackborn public int getLastFailureMesgAsInt(int def) { 575c113faba6b644d1851c9281614aa0edd175fc48Alon Albert final int i = ContentResolver.syncErrorStringToInt(lastFailureMesg); 585c113faba6b644d1851c9281614aa0edd175fc48Alon Albert if (i > 0) { 595c113faba6b644d1851c9281614aa0edd175fc48Alon Albert return i; 605c113faba6b644d1851c9281614aa0edd175fc48Alon Albert } else { 615c113faba6b644d1851c9281614aa0edd175fc48Alon Albert Log.d(TAG, "Unknown lastFailureMesg:" + lastFailureMesg); 625c113faba6b644d1851c9281614aa0edd175fc48Alon Albert return def; 63231cc608d06ffc31c24bf8aa8c8275bdd2636581Dianne Hackborn } 64231cc608d06ffc31c24bf8aa8c8275bdd2636581Dianne Hackborn } 65c5d1c6db61f208b206b260f897bb5bbc64be4d97Fred Quintana 66231cc608d06ffc31c24bf8aa8c8275bdd2636581Dianne Hackborn public int describeContents() { 67231cc608d06ffc31c24bf8aa8c8275bdd2636581Dianne Hackborn return 0; 68231cc608d06ffc31c24bf8aa8c8275bdd2636581Dianne Hackborn } 69231cc608d06ffc31c24bf8aa8c8275bdd2636581Dianne Hackborn 70231cc608d06ffc31c24bf8aa8c8275bdd2636581Dianne Hackborn public void writeToParcel(Parcel parcel, int flags) { 71231cc608d06ffc31c24bf8aa8c8275bdd2636581Dianne Hackborn parcel.writeInt(VERSION); 72231cc608d06ffc31c24bf8aa8c8275bdd2636581Dianne Hackborn parcel.writeInt(authorityId); 73231cc608d06ffc31c24bf8aa8c8275bdd2636581Dianne Hackborn parcel.writeLong(totalElapsedTime); 74231cc608d06ffc31c24bf8aa8c8275bdd2636581Dianne Hackborn parcel.writeInt(numSyncs); 75231cc608d06ffc31c24bf8aa8c8275bdd2636581Dianne Hackborn parcel.writeInt(numSourcePoll); 76231cc608d06ffc31c24bf8aa8c8275bdd2636581Dianne Hackborn parcel.writeInt(numSourceServer); 77231cc608d06ffc31c24bf8aa8c8275bdd2636581Dianne Hackborn parcel.writeInt(numSourceLocal); 78231cc608d06ffc31c24bf8aa8c8275bdd2636581Dianne Hackborn parcel.writeInt(numSourceUser); 79231cc608d06ffc31c24bf8aa8c8275bdd2636581Dianne Hackborn parcel.writeLong(lastSuccessTime); 80231cc608d06ffc31c24bf8aa8c8275bdd2636581Dianne Hackborn parcel.writeInt(lastSuccessSource); 81231cc608d06ffc31c24bf8aa8c8275bdd2636581Dianne Hackborn parcel.writeLong(lastFailureTime); 82231cc608d06ffc31c24bf8aa8c8275bdd2636581Dianne Hackborn parcel.writeInt(lastFailureSource); 83231cc608d06ffc31c24bf8aa8c8275bdd2636581Dianne Hackborn parcel.writeString(lastFailureMesg); 84231cc608d06ffc31c24bf8aa8c8275bdd2636581Dianne Hackborn parcel.writeLong(initialFailureTime); 85231cc608d06ffc31c24bf8aa8c8275bdd2636581Dianne Hackborn parcel.writeInt(pending ? 1 : 0); 865ed64cd06ce308e91a5ab74e51bcea5fba9a3860Costin Manolache parcel.writeInt(initialize ? 1 : 0); 87c5d1c6db61f208b206b260f897bb5bbc64be4d97Fred Quintana if (periodicSyncTimes != null) { 88c5d1c6db61f208b206b260f897bb5bbc64be4d97Fred Quintana parcel.writeInt(periodicSyncTimes.size()); 89c5d1c6db61f208b206b260f897bb5bbc64be4d97Fred Quintana for (long periodicSyncTime : periodicSyncTimes) { 90c5d1c6db61f208b206b260f897bb5bbc64be4d97Fred Quintana parcel.writeLong(periodicSyncTime); 91c5d1c6db61f208b206b260f897bb5bbc64be4d97Fred Quintana } 92c5d1c6db61f208b206b260f897bb5bbc64be4d97Fred Quintana } else { 93c5d1c6db61f208b206b260f897bb5bbc64be4d97Fred Quintana parcel.writeInt(-1); 94c5d1c6db61f208b206b260f897bb5bbc64be4d97Fred Quintana } 95231cc608d06ffc31c24bf8aa8c8275bdd2636581Dianne Hackborn } 96231cc608d06ffc31c24bf8aa8c8275bdd2636581Dianne Hackborn 977a96c39c510923ef73bbb06ab20109f0168b8eb1Jeff Sharkey public SyncStatusInfo(Parcel parcel) { 98231cc608d06ffc31c24bf8aa8c8275bdd2636581Dianne Hackborn int version = parcel.readInt(); 99c5d1c6db61f208b206b260f897bb5bbc64be4d97Fred Quintana if (version != VERSION && version != 1) { 100231cc608d06ffc31c24bf8aa8c8275bdd2636581Dianne Hackborn Log.w("SyncStatusInfo", "Unknown version: " + version); 101231cc608d06ffc31c24bf8aa8c8275bdd2636581Dianne Hackborn } 102231cc608d06ffc31c24bf8aa8c8275bdd2636581Dianne Hackborn authorityId = parcel.readInt(); 103231cc608d06ffc31c24bf8aa8c8275bdd2636581Dianne Hackborn totalElapsedTime = parcel.readLong(); 104231cc608d06ffc31c24bf8aa8c8275bdd2636581Dianne Hackborn numSyncs = parcel.readInt(); 105231cc608d06ffc31c24bf8aa8c8275bdd2636581Dianne Hackborn numSourcePoll = parcel.readInt(); 106231cc608d06ffc31c24bf8aa8c8275bdd2636581Dianne Hackborn numSourceServer = parcel.readInt(); 107231cc608d06ffc31c24bf8aa8c8275bdd2636581Dianne Hackborn numSourceLocal = parcel.readInt(); 108231cc608d06ffc31c24bf8aa8c8275bdd2636581Dianne Hackborn numSourceUser = parcel.readInt(); 109231cc608d06ffc31c24bf8aa8c8275bdd2636581Dianne Hackborn lastSuccessTime = parcel.readLong(); 110231cc608d06ffc31c24bf8aa8c8275bdd2636581Dianne Hackborn lastSuccessSource = parcel.readInt(); 111231cc608d06ffc31c24bf8aa8c8275bdd2636581Dianne Hackborn lastFailureTime = parcel.readLong(); 112231cc608d06ffc31c24bf8aa8c8275bdd2636581Dianne Hackborn lastFailureSource = parcel.readInt(); 113231cc608d06ffc31c24bf8aa8c8275bdd2636581Dianne Hackborn lastFailureMesg = parcel.readString(); 114231cc608d06ffc31c24bf8aa8c8275bdd2636581Dianne Hackborn initialFailureTime = parcel.readLong(); 115231cc608d06ffc31c24bf8aa8c8275bdd2636581Dianne Hackborn pending = parcel.readInt() != 0; 1165ed64cd06ce308e91a5ab74e51bcea5fba9a3860Costin Manolache initialize = parcel.readInt() != 0; 117c5d1c6db61f208b206b260f897bb5bbc64be4d97Fred Quintana if (version == 1) { 118c5d1c6db61f208b206b260f897bb5bbc64be4d97Fred Quintana periodicSyncTimes = null; 119c5d1c6db61f208b206b260f897bb5bbc64be4d97Fred Quintana } else { 120c5d1c6db61f208b206b260f897bb5bbc64be4d97Fred Quintana int N = parcel.readInt(); 121c5d1c6db61f208b206b260f897bb5bbc64be4d97Fred Quintana if (N < 0) { 122c5d1c6db61f208b206b260f897bb5bbc64be4d97Fred Quintana periodicSyncTimes = null; 123c5d1c6db61f208b206b260f897bb5bbc64be4d97Fred Quintana } else { 124c5d1c6db61f208b206b260f897bb5bbc64be4d97Fred Quintana periodicSyncTimes = new ArrayList<Long>(); 125c5d1c6db61f208b206b260f897bb5bbc64be4d97Fred Quintana for (int i=0; i<N; i++) { 126c5d1c6db61f208b206b260f897bb5bbc64be4d97Fred Quintana periodicSyncTimes.add(parcel.readLong()); 127c5d1c6db61f208b206b260f897bb5bbc64be4d97Fred Quintana } 128c5d1c6db61f208b206b260f897bb5bbc64be4d97Fred Quintana } 129c5d1c6db61f208b206b260f897bb5bbc64be4d97Fred Quintana } 130231cc608d06ffc31c24bf8aa8c8275bdd2636581Dianne Hackborn } 131c5d1c6db61f208b206b260f897bb5bbc64be4d97Fred Quintana 132dbe846b02e6f6f715787cf8621587f7bc25deaacGeorgi Nikolov public SyncStatusInfo(SyncStatusInfo other) { 133dbe846b02e6f6f715787cf8621587f7bc25deaacGeorgi Nikolov authorityId = other.authorityId; 134dbe846b02e6f6f715787cf8621587f7bc25deaacGeorgi Nikolov totalElapsedTime = other.totalElapsedTime; 135dbe846b02e6f6f715787cf8621587f7bc25deaacGeorgi Nikolov numSyncs = other.numSyncs; 136dbe846b02e6f6f715787cf8621587f7bc25deaacGeorgi Nikolov numSourcePoll = other.numSourcePoll; 137dbe846b02e6f6f715787cf8621587f7bc25deaacGeorgi Nikolov numSourceServer = other.numSourceServer; 138dbe846b02e6f6f715787cf8621587f7bc25deaacGeorgi Nikolov numSourceLocal = other.numSourceLocal; 139dbe846b02e6f6f715787cf8621587f7bc25deaacGeorgi Nikolov numSourceUser = other.numSourceUser; 140dbe846b02e6f6f715787cf8621587f7bc25deaacGeorgi Nikolov numSourcePeriodic = other.numSourcePeriodic; 141dbe846b02e6f6f715787cf8621587f7bc25deaacGeorgi Nikolov lastSuccessTime = other.lastSuccessTime; 142dbe846b02e6f6f715787cf8621587f7bc25deaacGeorgi Nikolov lastSuccessSource = other.lastSuccessSource; 143dbe846b02e6f6f715787cf8621587f7bc25deaacGeorgi Nikolov lastFailureTime = other.lastFailureTime; 144dbe846b02e6f6f715787cf8621587f7bc25deaacGeorgi Nikolov lastFailureSource = other.lastFailureSource; 145dbe846b02e6f6f715787cf8621587f7bc25deaacGeorgi Nikolov lastFailureMesg = other.lastFailureMesg; 146dbe846b02e6f6f715787cf8621587f7bc25deaacGeorgi Nikolov initialFailureTime = other.initialFailureTime; 147dbe846b02e6f6f715787cf8621587f7bc25deaacGeorgi Nikolov pending = other.pending; 148dbe846b02e6f6f715787cf8621587f7bc25deaacGeorgi Nikolov initialize = other.initialize; 149dbe846b02e6f6f715787cf8621587f7bc25deaacGeorgi Nikolov if (other.periodicSyncTimes != null) { 150dbe846b02e6f6f715787cf8621587f7bc25deaacGeorgi Nikolov periodicSyncTimes = new ArrayList<Long>(other.periodicSyncTimes); 151dbe846b02e6f6f715787cf8621587f7bc25deaacGeorgi Nikolov } 152dbe846b02e6f6f715787cf8621587f7bc25deaacGeorgi Nikolov } 153dbe846b02e6f6f715787cf8621587f7bc25deaacGeorgi Nikolov 154c5d1c6db61f208b206b260f897bb5bbc64be4d97Fred Quintana public void setPeriodicSyncTime(int index, long when) { 155dbe846b02e6f6f715787cf8621587f7bc25deaacGeorgi Nikolov // The list is initialized lazily when scheduling occurs so we need to make sure 156dbe846b02e6f6f715787cf8621587f7bc25deaacGeorgi Nikolov // we initialize elements < index to zero (zero is ignore for scheduling purposes) 157c5d1c6db61f208b206b260f897bb5bbc64be4d97Fred Quintana ensurePeriodicSyncTimeSize(index); 158c5d1c6db61f208b206b260f897bb5bbc64be4d97Fred Quintana periodicSyncTimes.set(index, when); 159c5d1c6db61f208b206b260f897bb5bbc64be4d97Fred Quintana } 160c5d1c6db61f208b206b260f897bb5bbc64be4d97Fred Quintana 161c5d1c6db61f208b206b260f897bb5bbc64be4d97Fred Quintana public long getPeriodicSyncTime(int index) { 162dbe846b02e6f6f715787cf8621587f7bc25deaacGeorgi Nikolov if (periodicSyncTimes != null && index < periodicSyncTimes.size()) { 163dbe846b02e6f6f715787cf8621587f7bc25deaacGeorgi Nikolov return periodicSyncTimes.get(index); 164dbe846b02e6f6f715787cf8621587f7bc25deaacGeorgi Nikolov } else { 165c5d1c6db61f208b206b260f897bb5bbc64be4d97Fred Quintana return 0; 166c5d1c6db61f208b206b260f897bb5bbc64be4d97Fred Quintana } 167c5d1c6db61f208b206b260f897bb5bbc64be4d97Fred Quintana } 168c5d1c6db61f208b206b260f897bb5bbc64be4d97Fred Quintana 169c5d1c6db61f208b206b260f897bb5bbc64be4d97Fred Quintana public void removePeriodicSyncTime(int index) { 170dbe846b02e6f6f715787cf8621587f7bc25deaacGeorgi Nikolov if (periodicSyncTimes != null && index < periodicSyncTimes.size()) { 171dbe846b02e6f6f715787cf8621587f7bc25deaacGeorgi Nikolov periodicSyncTimes.remove(index); 172dbe846b02e6f6f715787cf8621587f7bc25deaacGeorgi Nikolov } 173c5d1c6db61f208b206b260f897bb5bbc64be4d97Fred Quintana } 174c5d1c6db61f208b206b260f897bb5bbc64be4d97Fred Quintana 175231cc608d06ffc31c24bf8aa8c8275bdd2636581Dianne Hackborn public static final Creator<SyncStatusInfo> CREATOR = new Creator<SyncStatusInfo>() { 176231cc608d06ffc31c24bf8aa8c8275bdd2636581Dianne Hackborn public SyncStatusInfo createFromParcel(Parcel in) { 177231cc608d06ffc31c24bf8aa8c8275bdd2636581Dianne Hackborn return new SyncStatusInfo(in); 178231cc608d06ffc31c24bf8aa8c8275bdd2636581Dianne Hackborn } 179231cc608d06ffc31c24bf8aa8c8275bdd2636581Dianne Hackborn 180231cc608d06ffc31c24bf8aa8c8275bdd2636581Dianne Hackborn public SyncStatusInfo[] newArray(int size) { 181231cc608d06ffc31c24bf8aa8c8275bdd2636581Dianne Hackborn return new SyncStatusInfo[size]; 182231cc608d06ffc31c24bf8aa8c8275bdd2636581Dianne Hackborn } 183231cc608d06ffc31c24bf8aa8c8275bdd2636581Dianne Hackborn }; 184dbe846b02e6f6f715787cf8621587f7bc25deaacGeorgi Nikolov 185dbe846b02e6f6f715787cf8621587f7bc25deaacGeorgi Nikolov private void ensurePeriodicSyncTimeSize(int index) { 186dbe846b02e6f6f715787cf8621587f7bc25deaacGeorgi Nikolov if (periodicSyncTimes == null) { 187dbe846b02e6f6f715787cf8621587f7bc25deaacGeorgi Nikolov periodicSyncTimes = new ArrayList<Long>(0); 188dbe846b02e6f6f715787cf8621587f7bc25deaacGeorgi Nikolov } 189dbe846b02e6f6f715787cf8621587f7bc25deaacGeorgi Nikolov 190dbe846b02e6f6f715787cf8621587f7bc25deaacGeorgi Nikolov final int requiredSize = index + 1; 191dbe846b02e6f6f715787cf8621587f7bc25deaacGeorgi Nikolov if (periodicSyncTimes.size() < requiredSize) { 192dbe846b02e6f6f715787cf8621587f7bc25deaacGeorgi Nikolov for (int i = periodicSyncTimes.size(); i < requiredSize; i++) { 193dbe846b02e6f6f715787cf8621587f7bc25deaacGeorgi Nikolov periodicSyncTimes.add((long) 0); 194dbe846b02e6f6f715787cf8621587f7bc25deaacGeorgi Nikolov } 195dbe846b02e6f6f715787cf8621587f7bc25deaacGeorgi Nikolov } 196dbe846b02e6f6f715787cf8621587f7bc25deaacGeorgi Nikolov } 197231cc608d06ffc31c24bf8aa8c8275bdd2636581Dianne Hackborn}