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