Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 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.text.TextUtils; |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 20 | import android.os.Parcelable; |
| 21 | import android.os.Parcel; |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 22 | |
| 23 | /** |
| 24 | * Value type that represents a SyncAdapterType. This object overrides {@link #equals} and |
| 25 | * {@link #hashCode}, making it suitable for use as the key of a {@link java.util.Map} |
| 26 | */ |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 27 | public class SyncAdapterType implements Parcelable { |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 28 | public final String authority; |
| 29 | public final String accountType; |
Fred Quintana | e0616ff | 2009-08-19 13:13:18 -0700 | [diff] [blame] | 30 | public final boolean isKey; |
| 31 | private final boolean userVisible; |
| 32 | private final boolean supportsUploading; |
Fred Quintana | 0c4d04a | 2010-11-03 17:02:55 -0700 | [diff] [blame] | 33 | private final boolean isAlwaysSyncable; |
| 34 | private final boolean allowParallelSyncs; |
Fred Quintana | e6d60ec | 2011-08-24 11:29:00 -0700 | [diff] [blame] | 35 | private final String settingsActivity; |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 36 | |
Fred Quintana | 0c4d04a | 2010-11-03 17:02:55 -0700 | [diff] [blame] | 37 | public SyncAdapterType(String authority, String accountType, boolean userVisible, |
Fred Quintana | e0616ff | 2009-08-19 13:13:18 -0700 | [diff] [blame] | 38 | boolean supportsUploading) { |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 39 | if (TextUtils.isEmpty(authority)) { |
| 40 | throw new IllegalArgumentException("the authority must not be empty: " + authority); |
| 41 | } |
| 42 | if (TextUtils.isEmpty(accountType)) { |
| 43 | throw new IllegalArgumentException("the accountType must not be empty: " + accountType); |
| 44 | } |
| 45 | this.authority = authority; |
| 46 | this.accountType = accountType; |
Fred Quintana | 4a6679b | 2009-08-17 13:05:39 -0700 | [diff] [blame] | 47 | this.userVisible = userVisible; |
Fred Quintana | e0616ff | 2009-08-19 13:13:18 -0700 | [diff] [blame] | 48 | this.supportsUploading = supportsUploading; |
Fred Quintana | 0c4d04a | 2010-11-03 17:02:55 -0700 | [diff] [blame] | 49 | this.isAlwaysSyncable = false; |
| 50 | this.allowParallelSyncs = false; |
Fred Quintana | e6d60ec | 2011-08-24 11:29:00 -0700 | [diff] [blame] | 51 | this.settingsActivity = null; |
Fred Quintana | 0c4d04a | 2010-11-03 17:02:55 -0700 | [diff] [blame] | 52 | this.isKey = false; |
| 53 | } |
| 54 | |
| 55 | /** @hide */ |
| 56 | public SyncAdapterType(String authority, String accountType, boolean userVisible, |
| 57 | boolean supportsUploading, |
| 58 | boolean isAlwaysSyncable, |
Fred Quintana | e6d60ec | 2011-08-24 11:29:00 -0700 | [diff] [blame] | 59 | boolean allowParallelSyncs, |
| 60 | String settingsActivity) { |
Fred Quintana | 0c4d04a | 2010-11-03 17:02:55 -0700 | [diff] [blame] | 61 | if (TextUtils.isEmpty(authority)) { |
| 62 | throw new IllegalArgumentException("the authority must not be empty: " + authority); |
| 63 | } |
| 64 | if (TextUtils.isEmpty(accountType)) { |
| 65 | throw new IllegalArgumentException("the accountType must not be empty: " + accountType); |
| 66 | } |
| 67 | this.authority = authority; |
| 68 | this.accountType = accountType; |
| 69 | this.userVisible = userVisible; |
| 70 | this.supportsUploading = supportsUploading; |
| 71 | this.isAlwaysSyncable = isAlwaysSyncable; |
| 72 | this.allowParallelSyncs = allowParallelSyncs; |
Fred Quintana | e6d60ec | 2011-08-24 11:29:00 -0700 | [diff] [blame] | 73 | this.settingsActivity = settingsActivity; |
Fred Quintana | e0616ff | 2009-08-19 13:13:18 -0700 | [diff] [blame] | 74 | this.isKey = false; |
| 75 | } |
| 76 | |
| 77 | private SyncAdapterType(String authority, String accountType) { |
| 78 | if (TextUtils.isEmpty(authority)) { |
| 79 | throw new IllegalArgumentException("the authority must not be empty: " + authority); |
| 80 | } |
| 81 | if (TextUtils.isEmpty(accountType)) { |
| 82 | throw new IllegalArgumentException("the accountType must not be empty: " + accountType); |
| 83 | } |
| 84 | this.authority = authority; |
| 85 | this.accountType = accountType; |
| 86 | this.userVisible = true; |
| 87 | this.supportsUploading = true; |
Fred Quintana | 0c4d04a | 2010-11-03 17:02:55 -0700 | [diff] [blame] | 88 | this.isAlwaysSyncable = false; |
| 89 | this.allowParallelSyncs = false; |
Fred Quintana | e6d60ec | 2011-08-24 11:29:00 -0700 | [diff] [blame] | 90 | this.settingsActivity = null; |
Fred Quintana | e0616ff | 2009-08-19 13:13:18 -0700 | [diff] [blame] | 91 | this.isKey = true; |
| 92 | } |
| 93 | |
| 94 | public boolean supportsUploading() { |
| 95 | if (isKey) { |
| 96 | throw new IllegalStateException( |
| 97 | "this method is not allowed to be called when this is a key"); |
| 98 | } |
| 99 | return supportsUploading; |
| 100 | } |
| 101 | |
| 102 | public boolean isUserVisible() { |
| 103 | if (isKey) { |
| 104 | throw new IllegalStateException( |
| 105 | "this method is not allowed to be called when this is a key"); |
| 106 | } |
| 107 | return userVisible; |
Fred Quintana | 4a6679b | 2009-08-17 13:05:39 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Fred Quintana | 0c4d04a | 2010-11-03 17:02:55 -0700 | [diff] [blame] | 110 | /** |
| 111 | * @return True if this SyncAdapter supports syncing multiple accounts simultaneously. |
| 112 | * If false then the SyncManager will take care to only start one sync at a time |
| 113 | * using this SyncAdapter. |
| 114 | */ |
| 115 | public boolean allowParallelSyncs() { |
| 116 | if (isKey) { |
| 117 | throw new IllegalStateException( |
| 118 | "this method is not allowed to be called when this is a key"); |
| 119 | } |
| 120 | return allowParallelSyncs; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * If true then the SyncManager will never issue an initialization sync to the SyncAdapter |
| 125 | * and will instead automatically call |
| 126 | * {@link ContentResolver#setIsSyncable(android.accounts.Account, String, int)} with a |
| 127 | * value of 1 for each account and provider that this sync adapter supports. |
| 128 | * @return true if the SyncAdapter does not require initialization and if it is ok for the |
| 129 | * SyncAdapter to treat it as syncable automatically. |
| 130 | */ |
| 131 | public boolean isAlwaysSyncable() { |
| 132 | if (isKey) { |
| 133 | throw new IllegalStateException( |
| 134 | "this method is not allowed to be called when this is a key"); |
| 135 | } |
| 136 | return isAlwaysSyncable; |
| 137 | } |
| 138 | |
Fred Quintana | e6d60ec | 2011-08-24 11:29:00 -0700 | [diff] [blame] | 139 | /** |
| 140 | * @return The activity to use to invoke this SyncAdapter's settings activity. |
| 141 | * May be null. |
| 142 | */ |
| 143 | public String getSettingsActivity() { |
| 144 | if (isKey) { |
| 145 | throw new IllegalStateException( |
| 146 | "this method is not allowed to be called when this is a key"); |
| 147 | } |
| 148 | return settingsActivity; |
| 149 | } |
| 150 | |
Fred Quintana | 4a6679b | 2009-08-17 13:05:39 -0700 | [diff] [blame] | 151 | public static SyncAdapterType newKey(String authority, String accountType) { |
Fred Quintana | e0616ff | 2009-08-19 13:13:18 -0700 | [diff] [blame] | 152 | return new SyncAdapterType(authority, accountType); |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | public boolean equals(Object o) { |
| 156 | if (o == this) return true; |
| 157 | if (!(o instanceof SyncAdapterType)) return false; |
| 158 | final SyncAdapterType other = (SyncAdapterType)o; |
Fred Quintana | e0616ff | 2009-08-19 13:13:18 -0700 | [diff] [blame] | 159 | // don't include userVisible or supportsUploading in the equality check |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 160 | return authority.equals(other.authority) && accountType.equals(other.accountType); |
| 161 | } |
| 162 | |
| 163 | public int hashCode() { |
| 164 | int result = 17; |
| 165 | result = 31 * result + authority.hashCode(); |
| 166 | result = 31 * result + accountType.hashCode(); |
Fred Quintana | e0616ff | 2009-08-19 13:13:18 -0700 | [diff] [blame] | 167 | // don't include userVisible or supportsUploading the hash |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 168 | return result; |
| 169 | } |
| 170 | |
| 171 | public String toString() { |
Fred Quintana | e0616ff | 2009-08-19 13:13:18 -0700 | [diff] [blame] | 172 | if (isKey) { |
| 173 | return "SyncAdapterType Key {name=" + authority |
| 174 | + ", type=" + accountType |
| 175 | + "}"; |
| 176 | } else { |
| 177 | return "SyncAdapterType {name=" + authority |
| 178 | + ", type=" + accountType |
| 179 | + ", userVisible=" + userVisible |
| 180 | + ", supportsUploading=" + supportsUploading |
Fred Quintana | 0c4d04a | 2010-11-03 17:02:55 -0700 | [diff] [blame] | 181 | + ", isAlwaysSyncable=" + isAlwaysSyncable |
| 182 | + ", allowParallelSyncs=" + allowParallelSyncs |
Fred Quintana | e6d60ec | 2011-08-24 11:29:00 -0700 | [diff] [blame] | 183 | + ", settingsActivity=" + settingsActivity |
Fred Quintana | e0616ff | 2009-08-19 13:13:18 -0700 | [diff] [blame] | 184 | + "}"; |
| 185 | } |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 186 | } |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 187 | |
| 188 | public int describeContents() { |
| 189 | return 0; |
| 190 | } |
| 191 | |
| 192 | public void writeToParcel(Parcel dest, int flags) { |
Fred Quintana | e0616ff | 2009-08-19 13:13:18 -0700 | [diff] [blame] | 193 | if (isKey) { |
| 194 | throw new IllegalStateException("keys aren't parcelable"); |
| 195 | } |
| 196 | |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 197 | dest.writeString(authority); |
| 198 | dest.writeString(accountType); |
Fred Quintana | 4a6679b | 2009-08-17 13:05:39 -0700 | [diff] [blame] | 199 | dest.writeInt(userVisible ? 1 : 0); |
Fred Quintana | e0616ff | 2009-08-19 13:13:18 -0700 | [diff] [blame] | 200 | dest.writeInt(supportsUploading ? 1 : 0); |
Fred Quintana | 0c4d04a | 2010-11-03 17:02:55 -0700 | [diff] [blame] | 201 | dest.writeInt(isAlwaysSyncable ? 1 : 0); |
| 202 | dest.writeInt(allowParallelSyncs ? 1 : 0); |
Fred Quintana | e6d60ec | 2011-08-24 11:29:00 -0700 | [diff] [blame] | 203 | dest.writeString(settingsActivity); |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | public SyncAdapterType(Parcel source) { |
Fred Quintana | e0616ff | 2009-08-19 13:13:18 -0700 | [diff] [blame] | 207 | this( |
| 208 | source.readString(), |
| 209 | source.readString(), |
| 210 | source.readInt() != 0, |
Fred Quintana | 0c4d04a | 2010-11-03 17:02:55 -0700 | [diff] [blame] | 211 | source.readInt() != 0, |
| 212 | source.readInt() != 0, |
Fred Quintana | e6d60ec | 2011-08-24 11:29:00 -0700 | [diff] [blame] | 213 | source.readInt() != 0, |
| 214 | source.readString()); |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | public static final Creator<SyncAdapterType> CREATOR = new Creator<SyncAdapterType>() { |
| 218 | public SyncAdapterType createFromParcel(Parcel source) { |
| 219 | return new SyncAdapterType(source); |
| 220 | } |
| 221 | |
| 222 | public SyncAdapterType[] newArray(int size) { |
| 223 | return new SyncAdapterType[size]; |
| 224 | } |
| 225 | }; |
Fred Quintana | 0c4d04a | 2010-11-03 17:02:55 -0700 | [diff] [blame] | 226 | } |