Home | History | Annotate | Download | only in content
      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 
     49     public SyncStatusInfo(int authorityId) {
     50         this.authorityId = authorityId;
     51     }
     52 
     53     public int getLastFailureMesgAsInt(int def) {
     54         final int i = ContentResolver.syncErrorStringToInt(lastFailureMesg);
     55         if (i > 0) {
     56             return i;
     57         } else {
     58             Log.d(TAG, "Unknown lastFailureMesg:" + lastFailureMesg);
     59             return def;
     60         }
     61     }
     62 
     63     public int describeContents() {
     64         return 0;
     65     }
     66 
     67     public void writeToParcel(Parcel parcel, int flags) {
     68         parcel.writeInt(VERSION);
     69         parcel.writeInt(authorityId);
     70         parcel.writeLong(totalElapsedTime);
     71         parcel.writeInt(numSyncs);
     72         parcel.writeInt(numSourcePoll);
     73         parcel.writeInt(numSourceServer);
     74         parcel.writeInt(numSourceLocal);
     75         parcel.writeInt(numSourceUser);
     76         parcel.writeLong(lastSuccessTime);
     77         parcel.writeInt(lastSuccessSource);
     78         parcel.writeLong(lastFailureTime);
     79         parcel.writeInt(lastFailureSource);
     80         parcel.writeString(lastFailureMesg);
     81         parcel.writeLong(initialFailureTime);
     82         parcel.writeInt(pending ? 1 : 0);
     83         parcel.writeInt(initialize ? 1 : 0);
     84         if (periodicSyncTimes != null) {
     85             parcel.writeInt(periodicSyncTimes.size());
     86             for (long periodicSyncTime : periodicSyncTimes) {
     87                 parcel.writeLong(periodicSyncTime);
     88             }
     89         } else {
     90             parcel.writeInt(-1);
     91         }
     92     }
     93 
     94     public SyncStatusInfo(Parcel parcel) {
     95         int version = parcel.readInt();
     96         if (version != VERSION && version != 1) {
     97             Log.w("SyncStatusInfo", "Unknown version: " + version);
     98         }
     99         authorityId = parcel.readInt();
    100         totalElapsedTime = parcel.readLong();
    101         numSyncs = parcel.readInt();
    102         numSourcePoll = parcel.readInt();
    103         numSourceServer = parcel.readInt();
    104         numSourceLocal = parcel.readInt();
    105         numSourceUser = parcel.readInt();
    106         lastSuccessTime = parcel.readLong();
    107         lastSuccessSource = parcel.readInt();
    108         lastFailureTime = parcel.readLong();
    109         lastFailureSource = parcel.readInt();
    110         lastFailureMesg = parcel.readString();
    111         initialFailureTime = parcel.readLong();
    112         pending = parcel.readInt() != 0;
    113         initialize = parcel.readInt() != 0;
    114         if (version == 1) {
    115             periodicSyncTimes = null;
    116         } else {
    117             int N = parcel.readInt();
    118             if (N < 0) {
    119                 periodicSyncTimes = null;
    120             } else {
    121                 periodicSyncTimes = new ArrayList<Long>();
    122                 for (int i=0; i<N; i++) {
    123                     periodicSyncTimes.add(parcel.readLong());
    124                 }
    125             }
    126         }
    127     }
    128 
    129     public void setPeriodicSyncTime(int index, long when) {
    130         ensurePeriodicSyncTimeSize(index);
    131         periodicSyncTimes.set(index, when);
    132     }
    133 
    134     private void ensurePeriodicSyncTimeSize(int index) {
    135         if (periodicSyncTimes == null) {
    136             periodicSyncTimes = new ArrayList<Long>(0);
    137         }
    138 
    139         final int requiredSize = index + 1;
    140         if (periodicSyncTimes.size() < requiredSize) {
    141             for (int i = periodicSyncTimes.size(); i < requiredSize; i++) {
    142                 periodicSyncTimes.add((long) 0);
    143             }
    144         }
    145     }
    146 
    147     public long getPeriodicSyncTime(int index) {
    148         if (periodicSyncTimes == null || periodicSyncTimes.size() < (index + 1)) {
    149             return 0;
    150         }
    151         return periodicSyncTimes.get(index);
    152     }
    153 
    154     public void removePeriodicSyncTime(int index) {
    155         ensurePeriodicSyncTimeSize(index);
    156         periodicSyncTimes.remove(index);
    157     }
    158 
    159     public static final Creator<SyncStatusInfo> CREATOR = new Creator<SyncStatusInfo>() {
    160         public SyncStatusInfo createFromParcel(Parcel in) {
    161             return new SyncStatusInfo(in);
    162         }
    163 
    164         public SyncStatusInfo[] newArray(int size) {
    165             return new SyncStatusInfo[size];
    166         }
    167     };
    168 }