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 | |
Matthew Williams | 9ad2c84 | 2015-10-16 12:01:31 -0700 | [diff] [blame] | 24 | import java.util.Objects; |
| 25 | |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 26 | /** |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 27 | * Value type that contains information about a periodic sync. |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 28 | */ |
| 29 | public class PeriodicSync implements Parcelable { |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 30 | /** The account to be synced. Can be null. */ |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 31 | public final Account account; |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 32 | /** The authority of the sync. Can be null. */ |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 33 | public final String authority; |
| 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 | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 38 | /** |
| 39 | * How much flexibility can be taken in scheduling the sync, in seconds. |
| 40 | * {@hide} |
| 41 | */ |
| 42 | public final long flexTime; |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 43 | |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 44 | /** |
Matthew Williams | 5a9decd | 2014-06-04 09:25:11 -0700 | [diff] [blame] | 45 | * Creates a new PeriodicSync, copying the Bundle. This constructor is no longer used. |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 46 | */ |
| 47 | public PeriodicSync(Account account, String authority, Bundle extras, long periodInSeconds) { |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 48 | this.account = account; |
| 49 | this.authority = authority; |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 50 | 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 Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 58 | } |
| 59 | |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 60 | /** |
| 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 Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 67 | 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 Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 80 | this.extras = new Bundle(extras); |
| 81 | this.period = period; |
| 82 | this.flexTime = flexTime; |
| 83 | } |
| 84 | |
| 85 | private PeriodicSync(Parcel in) { |
Matthew Williams | 5a9decd | 2014-06-04 09:25:11 -0700 | [diff] [blame] | 86 | this.account = in.readParcelable(null); |
| 87 | this.authority = in.readString(); |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 88 | this.extras = in.readBundle(); |
| 89 | this.period = in.readLong(); |
| 90 | this.flexTime = in.readLong(); |
| 91 | } |
| 92 | |
| 93 | @Override |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 94 | public int describeContents() { |
| 95 | return 0; |
| 96 | } |
| 97 | |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 98 | @Override |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 99 | public void writeToParcel(Parcel dest, int flags) { |
Matthew Williams | 5a9decd | 2014-06-04 09:25:11 -0700 | [diff] [blame] | 100 | dest.writeParcelable(account, flags); |
| 101 | dest.writeString(authority); |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 102 | dest.writeBundle(extras); |
| 103 | dest.writeLong(period); |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 104 | dest.writeLong(flexTime); |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | public static final Creator<PeriodicSync> CREATOR = new Creator<PeriodicSync>() { |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 108 | @Override |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 109 | public PeriodicSync createFromParcel(Parcel source) { |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 110 | return new PeriodicSync(source); |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 111 | } |
| 112 | |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 113 | @Override |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 114 | public PeriodicSync[] newArray(int size) { |
| 115 | return new PeriodicSync[size]; |
| 116 | } |
| 117 | }; |
| 118 | |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 119 | @Override |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 120 | public boolean equals(Object o) { |
| 121 | if (o == this) { |
| 122 | return true; |
| 123 | } |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 124 | if (!(o instanceof PeriodicSync)) { |
| 125 | return false; |
| 126 | } |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 127 | final PeriodicSync other = (PeriodicSync) o; |
Matthew Williams | 5a9decd | 2014-06-04 09:25:11 -0700 | [diff] [blame] | 128 | return account.equals(other.account) |
| 129 | && authority.equals(other.authority) |
| 130 | && period == other.period |
| 131 | && syncExtrasEquals(extras, other.extras); |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 132 | } |
| 133 | |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 134 | /** |
Matthew Williams | 5a9decd | 2014-06-04 09:25:11 -0700 | [diff] [blame] | 135 | * Periodic sync extra comparison function. |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 136 | * {@hide} |
| 137 | */ |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 138 | 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 Williams | 9ad2c84 | 2015-10-16 12:01:31 -0700 | [diff] [blame] | 149 | // 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 Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 152 | return false; |
| 153 | } |
| 154 | } |
| 155 | return true; |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 156 | } |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 157 | |
| 158 | @Override |
| 159 | public String toString() { |
| 160 | return "account: " + account + |
| 161 | ", authority: " + authority + |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 162 | ". period: " + period + "s " + |
| 163 | ", flex: " + flexTime; |
| 164 | } |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 165 | } |