blob: 0441cccdd4f306c669c8b8b0a7ed455d94cfa1c2 [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
Matthew Williams9ad2c842015-10-16 12:01:31 -070024import java.util.Objects;
25
Fred Quintanac5d1c6d2010-01-27 12:17:49 -080026/**
Matthew Williamsfa774182013-06-18 15:44:11 -070027 * Value type that contains information about a periodic sync.
Fred Quintanac5d1c6d2010-01-27 12:17:49 -080028 */
29public class PeriodicSync implements Parcelable {
Matthew Williamsfa774182013-06-18 15:44:11 -070030 /** The account to be synced. Can be null. */
Fred Quintanac5d1c6d2010-01-27 12:17:49 -080031 public final Account account;
Matthew Williamsfa774182013-06-18 15:44:11 -070032 /** The authority of the sync. Can be null. */
Fred Quintanac5d1c6d2010-01-27 12:17:49 -080033 public final String authority;
34 /** 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 Williamsfa774182013-06-18 15:44:11 -070038 /**
39 * How much flexibility can be taken in scheduling the sync, in seconds.
40 * {@hide}
41 */
42 public final long flexTime;
Fred Quintanac5d1c6d2010-01-27 12:17:49 -080043
Matthew Williamsfa774182013-06-18 15:44:11 -070044 /**
Matthew Williams5a9decd2014-06-04 09:25:11 -070045 * Creates a new PeriodicSync, copying the Bundle. This constructor is no longer used.
Matthew Williamsfa774182013-06-18 15:44:11 -070046 */
47 public PeriodicSync(Account account, String authority, Bundle extras, long periodInSeconds) {
Fred Quintanac5d1c6d2010-01-27 12:17:49 -080048 this.account = account;
49 this.authority = authority;
Matthew Williamsfa774182013-06-18 15:44:11 -070050 if (extras == null) {
51 this.extras = new Bundle();
52 } else {
53 this.extras = new Bundle(extras);
54 }
55 this.period = periodInSeconds;
56 // Old API uses default flex time. No-one should be using this ctor anyway.
57 this.flexTime = 0L;
Fred Quintanac5d1c6d2010-01-27 12:17:49 -080058 }
59
Matthew Williamsfa774182013-06-18 15:44:11 -070060 /**
61 * Create a copy of a periodic sync.
62 * {@hide}
63 */
64 public PeriodicSync(PeriodicSync other) {
65 this.account = other.account;
66 this.authority = other.authority;
Matthew Williamsfa774182013-06-18 15:44:11 -070067 this.extras = new Bundle(other.extras);
68 this.period = other.period;
69 this.flexTime = other.flexTime;
70 }
71
72 /**
73 * A PeriodicSync for a sync with a specified provider.
74 * {@hide}
75 */
76 public PeriodicSync(Account account, String authority, Bundle extras,
77 long period, long flexTime) {
78 this.account = account;
79 this.authority = authority;
Matthew Williamsfa774182013-06-18 15:44:11 -070080 this.extras = new Bundle(extras);
81 this.period = period;
82 this.flexTime = flexTime;
83 }
84
85 private PeriodicSync(Parcel in) {
Matthew Williams5a9decd2014-06-04 09:25:11 -070086 this.account = in.readParcelable(null);
87 this.authority = in.readString();
Matthew Williamsfa774182013-06-18 15:44:11 -070088 this.extras = in.readBundle();
89 this.period = in.readLong();
90 this.flexTime = in.readLong();
91 }
92
93 @Override
Fred Quintanac5d1c6d2010-01-27 12:17:49 -080094 public int describeContents() {
95 return 0;
96 }
97
Matthew Williamsfa774182013-06-18 15:44:11 -070098 @Override
Fred Quintanac5d1c6d2010-01-27 12:17:49 -080099 public void writeToParcel(Parcel dest, int flags) {
Matthew Williams5a9decd2014-06-04 09:25:11 -0700100 dest.writeParcelable(account, flags);
101 dest.writeString(authority);
Fred Quintanac5d1c6d2010-01-27 12:17:49 -0800102 dest.writeBundle(extras);
103 dest.writeLong(period);
Matthew Williamsfa774182013-06-18 15:44:11 -0700104 dest.writeLong(flexTime);
Fred Quintanac5d1c6d2010-01-27 12:17:49 -0800105 }
106
107 public static final Creator<PeriodicSync> CREATOR = new Creator<PeriodicSync>() {
Matthew Williamsfa774182013-06-18 15:44:11 -0700108 @Override
Fred Quintanac5d1c6d2010-01-27 12:17:49 -0800109 public PeriodicSync createFromParcel(Parcel source) {
Matthew Williamsfa774182013-06-18 15:44:11 -0700110 return new PeriodicSync(source);
Fred Quintanac5d1c6d2010-01-27 12:17:49 -0800111 }
112
Matthew Williamsfa774182013-06-18 15:44:11 -0700113 @Override
Fred Quintanac5d1c6d2010-01-27 12:17:49 -0800114 public PeriodicSync[] newArray(int size) {
115 return new PeriodicSync[size];
116 }
117 };
118
Matthew Williamsfa774182013-06-18 15:44:11 -0700119 @Override
Fred Quintanac5d1c6d2010-01-27 12:17:49 -0800120 public boolean equals(Object o) {
121 if (o == this) {
122 return true;
123 }
Fred Quintanac5d1c6d2010-01-27 12:17:49 -0800124 if (!(o instanceof PeriodicSync)) {
125 return false;
126 }
Fred Quintanac5d1c6d2010-01-27 12:17:49 -0800127 final PeriodicSync other = (PeriodicSync) o;
Matthew Williams5a9decd2014-06-04 09:25:11 -0700128 return account.equals(other.account)
129 && authority.equals(other.authority)
130 && period == other.period
131 && syncExtrasEquals(extras, other.extras);
Jeff Sharkey7a96c392012-11-15 14:01:46 -0800132 }
133
Matthew Williamsfa774182013-06-18 15:44:11 -0700134 /**
Matthew Williams5a9decd2014-06-04 09:25:11 -0700135 * Periodic sync extra comparison function.
Matthew Williamsfa774182013-06-18 15:44:11 -0700136 * {@hide}
137 */
Jeff Sharkey7a96c392012-11-15 14:01:46 -0800138 public static boolean syncExtrasEquals(Bundle b1, Bundle b2) {
139 if (b1.size() != b2.size()) {
140 return false;
141 }
142 if (b1.isEmpty()) {
143 return true;
144 }
145 for (String key : b1.keySet()) {
146 if (!b2.containsKey(key)) {
147 return false;
148 }
Matthew Williams9ad2c842015-10-16 12:01:31 -0700149 // Null check. According to ContentResolver#validateSyncExtrasBundle null-valued keys
150 // are allowed in the bundle.
151 if (!Objects.equals(b1.get(key), b2.get(key))) {
Jeff Sharkey7a96c392012-11-15 14:01:46 -0800152 return false;
153 }
154 }
155 return true;
Fred Quintanac5d1c6d2010-01-27 12:17:49 -0800156 }
Matthew Williamsfa774182013-06-18 15:44:11 -0700157
158 @Override
159 public String toString() {
160 return "account: " + account +
161 ", authority: " + authority +
Matthew Williamsfa774182013-06-18 15:44:11 -0700162 ". period: " + period + "s " +
163 ", flex: " + flexTime;
164 }
Fred Quintanac5d1c6d2010-01-27 12:17:49 -0800165}