Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 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 | |
| 17 | package android.content; |
| 18 | |
| 19 | import android.os.Parcelable; |
| 20 | import android.os.Bundle; |
| 21 | import android.os.Parcel; |
| 22 | import android.accounts.Account; |
| 23 | |
| 24 | /** |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 25 | * Value type that contains information about a periodic sync. |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 26 | */ |
| 27 | public class PeriodicSync implements Parcelable { |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 28 | /** The account to be synced. Can be null. */ |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 29 | public final Account account; |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 30 | /** The authority of the sync. Can be null. */ |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 31 | public final String authority; |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 32 | /** The service for syncing, if this is an anonymous sync. Can be null.*/ |
| 33 | public final ComponentName service; |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 34 | /** Any extras that parameters that are to be passed to the sync adapter. */ |
| 35 | public final Bundle extras; |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 36 | /** How frequently the sync should be scheduled, in seconds. Kept around for API purposes. */ |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 37 | public final long period; |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 38 | /** Whether this periodic sync runs on a {@link SyncService}. */ |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 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; |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 45 | |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 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) { |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 53 | this.account = account; |
| 54 | this.authority = authority; |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 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; |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 65 | } |
| 66 | |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 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 |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 129 | public int describeContents() { |
| 130 | return 0; |
| 131 | } |
| 132 | |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 133 | @Override |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 134 | public void writeToParcel(Parcel dest, int flags) { |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 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 | } |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 142 | dest.writeBundle(extras); |
| 143 | dest.writeLong(period); |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 144 | dest.writeLong(flexTime); |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | public static final Creator<PeriodicSync> CREATOR = new Creator<PeriodicSync>() { |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 148 | @Override |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 149 | public PeriodicSync createFromParcel(Parcel source) { |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 150 | return new PeriodicSync(source); |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 151 | } |
| 152 | |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 153 | @Override |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 154 | public PeriodicSync[] newArray(int size) { |
| 155 | return new PeriodicSync[size]; |
| 156 | } |
| 157 | }; |
| 158 | |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 159 | @Override |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 160 | public boolean equals(Object o) { |
| 161 | if (o == this) { |
| 162 | return true; |
| 163 | } |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 164 | if (!(o instanceof PeriodicSync)) { |
| 165 | return false; |
| 166 | } |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 167 | final PeriodicSync other = (PeriodicSync) o; |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 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); |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 181 | } |
| 182 | |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 183 | /** |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 184 | * Periodic sync extra comparison function. Duplicated from |
| 185 | * {@link com.android.server.content.SyncManager#syncExtrasEquals(Bundle b1, Bundle b2)} |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 186 | * {@hide} |
| 187 | */ |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 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; |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 204 | } |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 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 | } |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 214 | } |