PeriodicSync.java revision 96ca46cf8726abbad46857a8af5eb83b2c04ec21
1/* 2 * Copyright (C) 2010 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 17package android.content; 18 19import android.os.Parcelable; 20import android.os.Bundle; 21import android.os.Parcel; 22import android.accounts.Account; 23 24/** 25 * Value type that contains information about a periodic sync. 26 */ 27public class PeriodicSync implements Parcelable { 28 /** The account to be synced. Can be null. */ 29 public final Account account; 30 /** The authority of the sync. Can be null. */ 31 public final String authority; 32 /** The service for syncing, if this is an anonymous sync. Can be null.*/ 33 public final ComponentName service; 34 /** Any extras that parameters that are to be passed to the sync adapter. */ 35 public final Bundle extras; 36 /** How frequently the sync should be scheduled, in seconds. Kept around for API purposes. */ 37 public final long period; 38 /** Whether this periodic sync runs on a {@link SyncService}. */ 39 public final boolean isService; 40 /** 41 * How much flexibility can be taken in scheduling the sync, in seconds. 42 * {@hide} 43 */ 44 public final long flexTime; 45 46 /** 47 * Creates a new PeriodicSync, copying the Bundle. SM no longer uses this ctor - kept around 48 * becuse it is part of the API. 49 * Note - even calls to the old API will not use this ctor, as 50 * they are given a default flex time. 51 */ 52 public PeriodicSync(Account account, String authority, Bundle extras, long periodInSeconds) { 53 this.account = account; 54 this.authority = authority; 55 this.service = null; 56 this.isService = false; 57 if (extras == null) { 58 this.extras = new Bundle(); 59 } else { 60 this.extras = new Bundle(extras); 61 } 62 this.period = periodInSeconds; 63 // Old API uses default flex time. No-one should be using this ctor anyway. 64 this.flexTime = 0L; 65 } 66 67 /** 68 * Create a copy of a periodic sync. 69 * {@hide} 70 */ 71 public PeriodicSync(PeriodicSync other) { 72 this.account = other.account; 73 this.authority = other.authority; 74 this.service = other.service; 75 this.isService = other.isService; 76 this.extras = new Bundle(other.extras); 77 this.period = other.period; 78 this.flexTime = other.flexTime; 79 } 80 81 /** 82 * A PeriodicSync for a sync with a specified provider. 83 * {@hide} 84 */ 85 public PeriodicSync(Account account, String authority, Bundle extras, 86 long period, long flexTime) { 87 this.account = account; 88 this.authority = authority; 89 this.service = null; 90 this.isService = false; 91 this.extras = new Bundle(extras); 92 this.period = period; 93 this.flexTime = flexTime; 94 } 95 96 /** 97 * A PeriodicSync for a sync with a specified SyncService. 98 * {@hide} 99 */ 100 public PeriodicSync(ComponentName service, Bundle extras, 101 long period, 102 long flexTime) { 103 this.account = null; 104 this.authority = null; 105 this.service = service; 106 this.isService = true; 107 this.extras = new Bundle(extras); 108 this.period = period; 109 this.flexTime = flexTime; 110 } 111 112 private PeriodicSync(Parcel in) { 113 this.isService = (in.readInt() != 0); 114 if (this.isService) { 115 this.service = in.readParcelable(null); 116 this.account = null; 117 this.authority = null; 118 } else { 119 this.account = in.readParcelable(null); 120 this.authority = in.readString(); 121 this.service = null; 122 } 123 this.extras = in.readBundle(); 124 this.period = in.readLong(); 125 this.flexTime = in.readLong(); 126 } 127 128 @Override 129 public int describeContents() { 130 return 0; 131 } 132 133 @Override 134 public void writeToParcel(Parcel dest, int flags) { 135 dest.writeInt(isService ? 1 : 0); 136 if (account == null && authority == null) { 137 dest.writeParcelable(service, flags); 138 } else { 139 dest.writeParcelable(account, flags); 140 dest.writeString(authority); 141 } 142 dest.writeBundle(extras); 143 dest.writeLong(period); 144 dest.writeLong(flexTime); 145 } 146 147 public static final Creator<PeriodicSync> CREATOR = new Creator<PeriodicSync>() { 148 @Override 149 public PeriodicSync createFromParcel(Parcel source) { 150 return new PeriodicSync(source); 151 } 152 153 @Override 154 public PeriodicSync[] newArray(int size) { 155 return new PeriodicSync[size]; 156 } 157 }; 158 159 @Override 160 public boolean equals(Object o) { 161 if (o == this) { 162 return true; 163 } 164 if (!(o instanceof PeriodicSync)) { 165 return false; 166 } 167 final PeriodicSync other = (PeriodicSync) o; 168 if (this.isService != other.isService) { 169 return false; 170 } 171 boolean equal = false; 172 if (this.isService) { 173 equal = service.equals(other.service); 174 } else { 175 equal = account.equals(other.account) 176 && authority.equals(other.authority); 177 } 178 return equal 179 && period == other.period 180 && syncExtrasEquals(extras, other.extras); 181 } 182 183 /** 184 * Periodic sync extra comparison function. Duplicated from 185 * {@link com.android.server.content.SyncManager#syncExtrasEquals(Bundle b1, Bundle b2)} 186 * {@hide} 187 */ 188 public static boolean syncExtrasEquals(Bundle b1, Bundle b2) { 189 if (b1.size() != b2.size()) { 190 return false; 191 } 192 if (b1.isEmpty()) { 193 return true; 194 } 195 for (String key : b1.keySet()) { 196 if (!b2.containsKey(key)) { 197 return false; 198 } 199 if (!b1.get(key).equals(b2.get(key))) { 200 return false; 201 } 202 } 203 return true; 204 } 205 206 @Override 207 public String toString() { 208 return "account: " + account + 209 ", authority: " + authority + 210 ", service: " + service + 211 ". period: " + period + "s " + 212 ", flex: " + flexTime; 213 } 214} 215