blob: 836c6f8c6abfd94de08b8912a1e4c2de07a1ebec [file] [log] [blame]
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001/*
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/**
Matthew Williamsfa774182013-06-18 15:44:11 -070025 * Value type that contains information about a periodic sync.
Fred Quintanac5d1c6d2010-01-27 12:17:49 -080026 */
27public class PeriodicSync implements Parcelable {
Matthew Williamsfa774182013-06-18 15:44:11 -070028 /** The account to be synced. Can be null. */
Fred Quintanac5d1c6d2010-01-27 12:17:49 -080029 public final Account account;
Matthew Williamsfa774182013-06-18 15:44:11 -070030 /** The authority of the sync. Can be null. */
Fred Quintanac5d1c6d2010-01-27 12:17:49 -080031 public final String authority;
Matthew Williamsfa774182013-06-18 15:44:11 -070032 /** The service for syncing, if this is an anonymous sync. Can be null.*/
33 public final ComponentName service;
Fred Quintanac5d1c6d2010-01-27 12:17:49 -080034 /** Any extras that parameters that are to be passed to the sync adapter. */
35 public final Bundle extras;
Matthew Williamsfa774182013-06-18 15:44:11 -070036 /** How frequently the sync should be scheduled, in seconds. Kept around for API purposes. */
Fred Quintanac5d1c6d2010-01-27 12:17:49 -080037 public final long period;
Matthew Williams56dbf8f2013-07-26 12:56:39 -070038 /** Whether this periodic sync runs on a {@link SyncService}. */
Matthew Williamsfa774182013-06-18 15:44:11 -070039 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 Quintanac5d1c6d2010-01-27 12:17:49 -080045
Matthew Williamsfa774182013-06-18 15:44:11 -070046 /**
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 Quintanac5d1c6d2010-01-27 12:17:49 -080053 this.account = account;
54 this.authority = authority;
Matthew Williamsfa774182013-06-18 15:44:11 -070055 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 Quintanac5d1c6d2010-01-27 12:17:49 -080065 }
66
Matthew Williamsfa774182013-06-18 15:44:11 -070067 /**
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 Quintanac5d1c6d2010-01-27 12:17:49 -0800129 public int describeContents() {
130 return 0;
131 }
132
Matthew Williamsfa774182013-06-18 15:44:11 -0700133 @Override
Fred Quintanac5d1c6d2010-01-27 12:17:49 -0800134 public void writeToParcel(Parcel dest, int flags) {
Matthew Williamsfa774182013-06-18 15:44:11 -0700135 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 Quintanac5d1c6d2010-01-27 12:17:49 -0800142 dest.writeBundle(extras);
143 dest.writeLong(period);
Matthew Williamsfa774182013-06-18 15:44:11 -0700144 dest.writeLong(flexTime);
Fred Quintanac5d1c6d2010-01-27 12:17:49 -0800145 }
146
147 public static final Creator<PeriodicSync> CREATOR = new Creator<PeriodicSync>() {
Matthew Williamsfa774182013-06-18 15:44:11 -0700148 @Override
Fred Quintanac5d1c6d2010-01-27 12:17:49 -0800149 public PeriodicSync createFromParcel(Parcel source) {
Matthew Williamsfa774182013-06-18 15:44:11 -0700150 return new PeriodicSync(source);
Fred Quintanac5d1c6d2010-01-27 12:17:49 -0800151 }
152
Matthew Williamsfa774182013-06-18 15:44:11 -0700153 @Override
Fred Quintanac5d1c6d2010-01-27 12:17:49 -0800154 public PeriodicSync[] newArray(int size) {
155 return new PeriodicSync[size];
156 }
157 };
158
Matthew Williamsfa774182013-06-18 15:44:11 -0700159 @Override
Fred Quintanac5d1c6d2010-01-27 12:17:49 -0800160 public boolean equals(Object o) {
161 if (o == this) {
162 return true;
163 }
Fred Quintanac5d1c6d2010-01-27 12:17:49 -0800164 if (!(o instanceof PeriodicSync)) {
165 return false;
166 }
Fred Quintanac5d1c6d2010-01-27 12:17:49 -0800167 final PeriodicSync other = (PeriodicSync) o;
Matthew Williamsfa774182013-06-18 15:44:11 -0700168 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 Sharkey7a96c392012-11-15 14:01:46 -0800181 }
182
Matthew Williamsfa774182013-06-18 15:44:11 -0700183 /**
Matthew Williams56dbf8f2013-07-26 12:56:39 -0700184 * Periodic sync extra comparison function. Duplicated from
185 * {@link com.android.server.content.SyncManager#syncExtrasEquals(Bundle b1, Bundle b2)}
Matthew Williamsfa774182013-06-18 15:44:11 -0700186 * {@hide}
187 */
Jeff Sharkey7a96c392012-11-15 14:01:46 -0800188 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 Quintanac5d1c6d2010-01-27 12:17:49 -0800204 }
Matthew Williamsfa774182013-06-18 15:44:11 -0700205
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 Quintanac5d1c6d2010-01-27 12:17:49 -0800214}