blob: b586eec76748bafba7019bf03d4ced5d163c11fe [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;
32 /** Any extras that parameters that are to be passed to the sync adapter. */
33 public final Bundle extras;
Matthew Williamsfa774182013-06-18 15:44:11 -070034 /** How frequently the sync should be scheduled, in seconds. Kept around for API purposes. */
Fred Quintanac5d1c6d2010-01-27 12:17:49 -080035 public final long period;
Matthew Williamsfa774182013-06-18 15:44:11 -070036 /**
Matthew Williamsfa774182013-06-18 15:44:11 -070037 * {@hide}
Matthew Williams62222882013-08-20 15:32:20 -070038 * How much flexibility can be taken in scheduling the sync, in seconds.
Matthew Williamsfa774182013-06-18 15:44:11 -070039 */
40 public final long flexTime;
Fred Quintanac5d1c6d2010-01-27 12:17:49 -080041
Matthew Williamsfa774182013-06-18 15:44:11 -070042 /**
43 * Creates a new PeriodicSync, copying the Bundle. SM no longer uses this ctor - kept around
44 * becuse it is part of the API.
45 * Note - even calls to the old API will not use this ctor, as
46 * they are given a default flex time.
47 */
48 public PeriodicSync(Account account, String authority, Bundle extras, long periodInSeconds) {
Fred Quintanac5d1c6d2010-01-27 12:17:49 -080049 this.account = account;
50 this.authority = authority;
Matthew Williamsfa774182013-06-18 15:44:11 -070051 if (extras == null) {
52 this.extras = new Bundle();
53 } else {
54 this.extras = new Bundle(extras);
55 }
56 this.period = periodInSeconds;
Matthew Williams62222882013-08-20 15:32:20 -070057 // Initialise to a sane value.
Matthew Williamsfa774182013-06-18 15:44:11 -070058 this.flexTime = 0L;
Fred Quintanac5d1c6d2010-01-27 12:17:49 -080059 }
60
Matthew Williamsfa774182013-06-18 15:44:11 -070061 /**
Matthew Williamsfa774182013-06-18 15:44:11 -070062 * {@hide}
Matthew Williams62222882013-08-20 15:32:20 -070063 * Create a copy of a periodic sync.
Matthew Williamsfa774182013-06-18 15:44:11 -070064 */
65 public PeriodicSync(PeriodicSync other) {
66 this.account = other.account;
67 this.authority = other.authority;
Matthew Williamsfa774182013-06-18 15:44:11 -070068 this.extras = new Bundle(other.extras);
69 this.period = other.period;
70 this.flexTime = other.flexTime;
71 }
72
73 /**
Matthew Williamsfa774182013-06-18 15:44:11 -070074 * {@hide}
Matthew Williams62222882013-08-20 15:32:20 -070075 * A PeriodicSync for a sync with a specified provider.
Matthew Williamsfa774182013-06-18 15:44:11 -070076 */
77 public PeriodicSync(Account account, String authority, Bundle extras,
78 long period, long flexTime) {
79 this.account = account;
80 this.authority = authority;
Matthew Williamsfa774182013-06-18 15:44:11 -070081 this.extras = new Bundle(extras);
82 this.period = period;
83 this.flexTime = flexTime;
84 }
85
86 private PeriodicSync(Parcel in) {
Matthew Williams62222882013-08-20 15:32:20 -070087 this.account = in.readParcelable(null);
88 this.authority = in.readString();
Matthew Williamsfa774182013-06-18 15:44:11 -070089 this.extras = in.readBundle();
90 this.period = in.readLong();
91 this.flexTime = in.readLong();
92 }
93
94 @Override
Fred Quintanac5d1c6d2010-01-27 12:17:49 -080095 public int describeContents() {
96 return 0;
97 }
98
Matthew Williamsfa774182013-06-18 15:44:11 -070099 @Override
Fred Quintanac5d1c6d2010-01-27 12:17:49 -0800100 public void writeToParcel(Parcel dest, int flags) {
Matthew Williams62222882013-08-20 15:32:20 -0700101 dest.writeParcelable(account, flags);
102 dest.writeString(authority);
Fred Quintanac5d1c6d2010-01-27 12:17:49 -0800103 dest.writeBundle(extras);
104 dest.writeLong(period);
Matthew Williamsfa774182013-06-18 15:44:11 -0700105 dest.writeLong(flexTime);
Fred Quintanac5d1c6d2010-01-27 12:17:49 -0800106 }
107
108 public static final Creator<PeriodicSync> CREATOR = new Creator<PeriodicSync>() {
Matthew Williamsfa774182013-06-18 15:44:11 -0700109 @Override
Fred Quintanac5d1c6d2010-01-27 12:17:49 -0800110 public PeriodicSync createFromParcel(Parcel source) {
Matthew Williamsfa774182013-06-18 15:44:11 -0700111 return new PeriodicSync(source);
Fred Quintanac5d1c6d2010-01-27 12:17:49 -0800112 }
113
Matthew Williamsfa774182013-06-18 15:44:11 -0700114 @Override
Fred Quintanac5d1c6d2010-01-27 12:17:49 -0800115 public PeriodicSync[] newArray(int size) {
116 return new PeriodicSync[size];
117 }
118 };
119
Matthew Williamsfa774182013-06-18 15:44:11 -0700120 @Override
Fred Quintanac5d1c6d2010-01-27 12:17:49 -0800121 public boolean equals(Object o) {
122 if (o == this) {
123 return true;
124 }
Fred Quintanac5d1c6d2010-01-27 12:17:49 -0800125 if (!(o instanceof PeriodicSync)) {
126 return false;
127 }
Fred Quintanac5d1c6d2010-01-27 12:17:49 -0800128 final PeriodicSync other = (PeriodicSync) o;
Matthew Williams62222882013-08-20 15:32:20 -0700129 return account.equals(other.account)
130 && authority.equals(other.authority)
Matthew Williamsfa774182013-06-18 15:44:11 -0700131 && period == other.period
132 && syncExtrasEquals(extras, other.extras);
Jeff Sharkey7a96c392012-11-15 14:01:46 -0800133 }
134
Matthew Williamsfa774182013-06-18 15:44:11 -0700135 /**
136 * Periodic sync extra comparison function.
137 * {@hide}
138 */
Jeff Sharkey7a96c392012-11-15 14:01:46 -0800139 public static boolean syncExtrasEquals(Bundle b1, Bundle b2) {
140 if (b1.size() != b2.size()) {
141 return false;
142 }
143 if (b1.isEmpty()) {
144 return true;
145 }
146 for (String key : b1.keySet()) {
147 if (!b2.containsKey(key)) {
148 return false;
149 }
150 if (!b1.get(key).equals(b2.get(key))) {
151 return false;
152 }
153 }
154 return true;
Fred Quintanac5d1c6d2010-01-27 12:17:49 -0800155 }
Matthew Williamsfa774182013-06-18 15:44:11 -0700156
157 @Override
158 public String toString() {
159 return "account: " + account +
160 ", authority: " + authority +
Matthew Williamsfa774182013-06-18 15:44:11 -0700161 ". period: " + period + "s " +
162 ", flex: " + flexTime;
163 }
Fred Quintanac5d1c6d2010-01-27 12:17:49 -0800164}