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; |
| 32 | /** Any extras that parameters that are to be passed to the sync adapter. */ |
| 33 | public final Bundle extras; |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 34 | /** 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] | 35 | public final long period; |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 36 | /** |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 37 | * {@hide} |
Matthew Williams | 6222288 | 2013-08-20 15:32:20 -0700 | [diff] [blame] | 38 | * How much flexibility can be taken in scheduling the sync, in seconds. |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 39 | */ |
| 40 | public final long flexTime; |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 41 | |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 42 | /** |
| 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 Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 49 | this.account = account; |
| 50 | this.authority = authority; |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 51 | if (extras == null) { |
| 52 | this.extras = new Bundle(); |
| 53 | } else { |
| 54 | this.extras = new Bundle(extras); |
| 55 | } |
| 56 | this.period = periodInSeconds; |
Matthew Williams | 6222288 | 2013-08-20 15:32:20 -0700 | [diff] [blame] | 57 | // Initialise to a sane value. |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 58 | this.flexTime = 0L; |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 59 | } |
| 60 | |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 61 | /** |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 62 | * {@hide} |
Matthew Williams | 6222288 | 2013-08-20 15:32:20 -0700 | [diff] [blame] | 63 | * Create a copy of a periodic sync. |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 64 | */ |
| 65 | public PeriodicSync(PeriodicSync other) { |
| 66 | this.account = other.account; |
| 67 | this.authority = other.authority; |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 68 | this.extras = new Bundle(other.extras); |
| 69 | this.period = other.period; |
| 70 | this.flexTime = other.flexTime; |
| 71 | } |
| 72 | |
| 73 | /** |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 74 | * {@hide} |
Matthew Williams | 6222288 | 2013-08-20 15:32:20 -0700 | [diff] [blame] | 75 | * A PeriodicSync for a sync with a specified provider. |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 76 | */ |
| 77 | public PeriodicSync(Account account, String authority, Bundle extras, |
| 78 | long period, long flexTime) { |
| 79 | this.account = account; |
| 80 | this.authority = authority; |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 81 | this.extras = new Bundle(extras); |
| 82 | this.period = period; |
| 83 | this.flexTime = flexTime; |
| 84 | } |
| 85 | |
| 86 | private PeriodicSync(Parcel in) { |
Matthew Williams | 6222288 | 2013-08-20 15:32:20 -0700 | [diff] [blame] | 87 | this.account = in.readParcelable(null); |
| 88 | this.authority = in.readString(); |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 89 | this.extras = in.readBundle(); |
| 90 | this.period = in.readLong(); |
| 91 | this.flexTime = in.readLong(); |
| 92 | } |
| 93 | |
| 94 | @Override |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 95 | public int describeContents() { |
| 96 | return 0; |
| 97 | } |
| 98 | |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 99 | @Override |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 100 | public void writeToParcel(Parcel dest, int flags) { |
Matthew Williams | 6222288 | 2013-08-20 15:32:20 -0700 | [diff] [blame] | 101 | dest.writeParcelable(account, flags); |
| 102 | dest.writeString(authority); |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 103 | dest.writeBundle(extras); |
| 104 | dest.writeLong(period); |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 105 | dest.writeLong(flexTime); |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | public static final Creator<PeriodicSync> CREATOR = new Creator<PeriodicSync>() { |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 109 | @Override |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 110 | public PeriodicSync createFromParcel(Parcel source) { |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 111 | return new PeriodicSync(source); |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 112 | } |
| 113 | |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 114 | @Override |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 115 | public PeriodicSync[] newArray(int size) { |
| 116 | return new PeriodicSync[size]; |
| 117 | } |
| 118 | }; |
| 119 | |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 120 | @Override |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 121 | public boolean equals(Object o) { |
| 122 | if (o == this) { |
| 123 | return true; |
| 124 | } |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 125 | if (!(o instanceof PeriodicSync)) { |
| 126 | return false; |
| 127 | } |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 128 | final PeriodicSync other = (PeriodicSync) o; |
Matthew Williams | 6222288 | 2013-08-20 15:32:20 -0700 | [diff] [blame] | 129 | return account.equals(other.account) |
| 130 | && authority.equals(other.authority) |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 131 | && period == other.period |
| 132 | && syncExtrasEquals(extras, other.extras); |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 133 | } |
| 134 | |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 135 | /** |
| 136 | * Periodic sync extra comparison function. |
| 137 | * {@hide} |
| 138 | */ |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 139 | 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 Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 155 | } |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 156 | |
| 157 | @Override |
| 158 | public String toString() { |
| 159 | return "account: " + account + |
| 160 | ", authority: " + authority + |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 161 | ". period: " + period + "s " + |
| 162 | ", flex: " + flexTime; |
| 163 | } |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 164 | } |