blob: 8c8fe5a0688807f2b6f6ea727cc739e17e10d58a [file] [log] [blame]
Fred Quintana718d8a22009-04-29 17:53:20 -07001/*
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
17package android.content;
18
Svetoslav Ganov5cb29732016-07-11 19:32:30 -070019import android.annotation.Nullable;
Mathew Inwood5c0d3542018-08-14 13:54:31 +010020import android.annotation.UnsupportedAppUsage;
Mathew Inwood31755f92018-12-20 13:53:36 +000021import android.os.Build;
Fred Quintana718d8a22009-04-29 17:53:20 -070022import android.text.TextUtils;
Fred Quintanaac9385e2009-06-22 18:00:59 -070023import android.os.Parcelable;
24import android.os.Parcel;
Fred Quintana718d8a22009-04-29 17:53:20 -070025
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 Quintanaac9385e2009-06-22 18:00:59 -070030public class SyncAdapterType implements Parcelable {
Fred Quintana718d8a22009-04-29 17:53:20 -070031 public final String authority;
32 public final String accountType;
Fred Quintanae0616ff2009-08-19 13:13:18 -070033 public final boolean isKey;
Mathew Inwood31755f92018-12-20 13:53:36 +000034 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Fred Quintanae0616ff2009-08-19 13:13:18 -070035 private final boolean userVisible;
Mathew Inwood31755f92018-12-20 13:53:36 +000036 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Fred Quintanae0616ff2009-08-19 13:13:18 -070037 private final boolean supportsUploading;
Mathew Inwood5c0d3542018-08-14 13:54:31 +010038 @UnsupportedAppUsage
Fred Quintana0c4d04a2010-11-03 17:02:55 -070039 private final boolean isAlwaysSyncable;
Mathew Inwood5c0d3542018-08-14 13:54:31 +010040 @UnsupportedAppUsage
Fred Quintana0c4d04a2010-11-03 17:02:55 -070041 private final boolean allowParallelSyncs;
Mathew Inwood5c0d3542018-08-14 13:54:31 +010042 @UnsupportedAppUsage
Fred Quintanae6d60ec2011-08-24 11:29:00 -070043 private final String settingsActivity;
Svetoslav Ganov5cb29732016-07-11 19:32:30 -070044 private final String packageName;
Fred Quintana718d8a22009-04-29 17:53:20 -070045
Fred Quintana0c4d04a2010-11-03 17:02:55 -070046 public SyncAdapterType(String authority, String accountType, boolean userVisible,
Fred Quintanae0616ff2009-08-19 13:13:18 -070047 boolean supportsUploading) {
Fred Quintana718d8a22009-04-29 17:53:20 -070048 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 Quintana4a6679b2009-08-17 13:05:39 -070056 this.userVisible = userVisible;
Fred Quintanae0616ff2009-08-19 13:13:18 -070057 this.supportsUploading = supportsUploading;
Fred Quintana0c4d04a2010-11-03 17:02:55 -070058 this.isAlwaysSyncable = false;
59 this.allowParallelSyncs = false;
Fred Quintanae6d60ec2011-08-24 11:29:00 -070060 this.settingsActivity = null;
Fred Quintana0c4d04a2010-11-03 17:02:55 -070061 this.isKey = false;
Svetoslav Ganov5cb29732016-07-11 19:32:30 -070062 this.packageName = null;
Fred Quintana0c4d04a2010-11-03 17:02:55 -070063 }
64
65 /** @hide */
66 public SyncAdapterType(String authority, String accountType, boolean userVisible,
67 boolean supportsUploading,
68 boolean isAlwaysSyncable,
Fred Quintanae6d60ec2011-08-24 11:29:00 -070069 boolean allowParallelSyncs,
Svetoslav Ganov5cb29732016-07-11 19:32:30 -070070 String settingsActivity,
71 String packageName) {
Fred Quintana0c4d04a2010-11-03 17:02:55 -070072 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 Quintanae6d60ec2011-08-24 11:29:00 -070084 this.settingsActivity = settingsActivity;
Fred Quintanae0616ff2009-08-19 13:13:18 -070085 this.isKey = false;
Svetoslav Ganov5cb29732016-07-11 19:32:30 -070086 this.packageName = packageName;
Fred Quintanae0616ff2009-08-19 13:13:18 -070087 }
88
Mathew Inwood31755f92018-12-20 13:53:36 +000089 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Fred Quintanae0616ff2009-08-19 13:13:18 -070090 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 Quintana0c4d04a2010-11-03 17:02:55 -0700101 this.isAlwaysSyncable = false;
102 this.allowParallelSyncs = false;
Fred Quintanae6d60ec2011-08-24 11:29:00 -0700103 this.settingsActivity = null;
Fred Quintanae0616ff2009-08-19 13:13:18 -0700104 this.isKey = true;
Svetoslav Ganov5cb29732016-07-11 19:32:30 -0700105 this.packageName = null;
Fred Quintanae0616ff2009-08-19 13:13:18 -0700106 }
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 Quintana4a6679b2009-08-17 13:05:39 -0700122 }
123
Fred Quintana0c4d04a2010-11-03 17:02:55 -0700124 /**
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 Quintanae6d60ec2011-08-24 11:29:00 -0700153 /**
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 Ganov5cb29732016-07-11 19:32:30 -0700165 /**
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 Quintana4a6679b2009-08-17 13:05:39 -0700175 public static SyncAdapterType newKey(String authority, String accountType) {
Fred Quintanae0616ff2009-08-19 13:13:18 -0700176 return new SyncAdapterType(authority, accountType);
Fred Quintana718d8a22009-04-29 17:53:20 -0700177 }
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 Quintanae0616ff2009-08-19 13:13:18 -0700183 // don't include userVisible or supportsUploading in the equality check
Fred Quintana718d8a22009-04-29 17:53:20 -0700184 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 Quintanae0616ff2009-08-19 13:13:18 -0700191 // don't include userVisible or supportsUploading the hash
Fred Quintana718d8a22009-04-29 17:53:20 -0700192 return result;
193 }
194
195 public String toString() {
Fred Quintanae0616ff2009-08-19 13:13:18 -0700196 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 Quintana0c4d04a2010-11-03 17:02:55 -0700205 + ", isAlwaysSyncable=" + isAlwaysSyncable
206 + ", allowParallelSyncs=" + allowParallelSyncs
Fred Quintanae6d60ec2011-08-24 11:29:00 -0700207 + ", settingsActivity=" + settingsActivity
Svetoslav Ganov5cb29732016-07-11 19:32:30 -0700208 + ", packageName=" + packageName
Fred Quintanae0616ff2009-08-19 13:13:18 -0700209 + "}";
210 }
Fred Quintana718d8a22009-04-29 17:53:20 -0700211 }
Fred Quintanaac9385e2009-06-22 18:00:59 -0700212
213 public int describeContents() {
214 return 0;
215 }
216
217 public void writeToParcel(Parcel dest, int flags) {
Fred Quintanae0616ff2009-08-19 13:13:18 -0700218 if (isKey) {
219 throw new IllegalStateException("keys aren't parcelable");
220 }
221
Fred Quintanaac9385e2009-06-22 18:00:59 -0700222 dest.writeString(authority);
223 dest.writeString(accountType);
Fred Quintana4a6679b2009-08-17 13:05:39 -0700224 dest.writeInt(userVisible ? 1 : 0);
Fred Quintanae0616ff2009-08-19 13:13:18 -0700225 dest.writeInt(supportsUploading ? 1 : 0);
Fred Quintana0c4d04a2010-11-03 17:02:55 -0700226 dest.writeInt(isAlwaysSyncable ? 1 : 0);
227 dest.writeInt(allowParallelSyncs ? 1 : 0);
Fred Quintanae6d60ec2011-08-24 11:29:00 -0700228 dest.writeString(settingsActivity);
Svetoslav Ganov5cb29732016-07-11 19:32:30 -0700229 dest.writeString(packageName);
Fred Quintanaac9385e2009-06-22 18:00:59 -0700230 }
231
232 public SyncAdapterType(Parcel source) {
Fred Quintanae0616ff2009-08-19 13:13:18 -0700233 this(
234 source.readString(),
235 source.readString(),
236 source.readInt() != 0,
Fred Quintana0c4d04a2010-11-03 17:02:55 -0700237 source.readInt() != 0,
238 source.readInt() != 0,
Fred Quintanae6d60ec2011-08-24 11:29:00 -0700239 source.readInt() != 0,
Svetoslav Ganov5cb29732016-07-11 19:32:30 -0700240 source.readString(),
Fred Quintanae6d60ec2011-08-24 11:29:00 -0700241 source.readString());
Fred Quintanaac9385e2009-06-22 18:00:59 -0700242 }
243
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -0700244 public static final @android.annotation.NonNull Creator<SyncAdapterType> CREATOR = new Creator<SyncAdapterType>() {
Fred Quintanaac9385e2009-06-22 18:00:59 -0700245 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 Quintana0c4d04a2010-11-03 17:02:55 -0700253}