blob: 8a16ac94522ebdd3b2bc0a58a1af58f48ed6a670 [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
19import android.text.TextUtils;
Fred Quintanaac9385e2009-06-22 18:00:59 -070020import android.os.Parcelable;
21import android.os.Parcel;
Fred Quintana718d8a22009-04-29 17:53:20 -070022
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 Quintanaac9385e2009-06-22 18:00:59 -070027public class SyncAdapterType implements Parcelable {
Fred Quintana718d8a22009-04-29 17:53:20 -070028 public final String authority;
29 public final String accountType;
Fred Quintanae0616ff2009-08-19 13:13:18 -070030 public final boolean isKey;
31 private final boolean userVisible;
32 private final boolean supportsUploading;
Fred Quintana0c4d04a2010-11-03 17:02:55 -070033 private final boolean isAlwaysSyncable;
34 private final boolean allowParallelSyncs;
Fred Quintanae6d60ec2011-08-24 11:29:00 -070035 private final String settingsActivity;
Fred Quintana718d8a22009-04-29 17:53:20 -070036
Fred Quintana0c4d04a2010-11-03 17:02:55 -070037 public SyncAdapterType(String authority, String accountType, boolean userVisible,
Fred Quintanae0616ff2009-08-19 13:13:18 -070038 boolean supportsUploading) {
Fred Quintana718d8a22009-04-29 17:53:20 -070039 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 Quintana4a6679b2009-08-17 13:05:39 -070047 this.userVisible = userVisible;
Fred Quintanae0616ff2009-08-19 13:13:18 -070048 this.supportsUploading = supportsUploading;
Fred Quintana0c4d04a2010-11-03 17:02:55 -070049 this.isAlwaysSyncable = false;
50 this.allowParallelSyncs = false;
Fred Quintanae6d60ec2011-08-24 11:29:00 -070051 this.settingsActivity = null;
Fred Quintana0c4d04a2010-11-03 17:02:55 -070052 this.isKey = false;
53 }
54
55 /** @hide */
56 public SyncAdapterType(String authority, String accountType, boolean userVisible,
57 boolean supportsUploading,
58 boolean isAlwaysSyncable,
Fred Quintanae6d60ec2011-08-24 11:29:00 -070059 boolean allowParallelSyncs,
60 String settingsActivity) {
Fred Quintana0c4d04a2010-11-03 17:02:55 -070061 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 Quintanae6d60ec2011-08-24 11:29:00 -070073 this.settingsActivity = settingsActivity;
Fred Quintanae0616ff2009-08-19 13:13:18 -070074 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 Quintana0c4d04a2010-11-03 17:02:55 -070088 this.isAlwaysSyncable = false;
89 this.allowParallelSyncs = false;
Fred Quintanae6d60ec2011-08-24 11:29:00 -070090 this.settingsActivity = null;
Fred Quintanae0616ff2009-08-19 13:13:18 -070091 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 Quintana4a6679b2009-08-17 13:05:39 -0700108 }
109
Fred Quintana0c4d04a2010-11-03 17:02:55 -0700110 /**
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 Quintanae6d60ec2011-08-24 11:29:00 -0700139 /**
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 Quintana4a6679b2009-08-17 13:05:39 -0700151 public static SyncAdapterType newKey(String authority, String accountType) {
Fred Quintanae0616ff2009-08-19 13:13:18 -0700152 return new SyncAdapterType(authority, accountType);
Fred Quintana718d8a22009-04-29 17:53:20 -0700153 }
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 Quintanae0616ff2009-08-19 13:13:18 -0700159 // don't include userVisible or supportsUploading in the equality check
Fred Quintana718d8a22009-04-29 17:53:20 -0700160 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 Quintanae0616ff2009-08-19 13:13:18 -0700167 // don't include userVisible or supportsUploading the hash
Fred Quintana718d8a22009-04-29 17:53:20 -0700168 return result;
169 }
170
171 public String toString() {
Fred Quintanae0616ff2009-08-19 13:13:18 -0700172 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 Quintana0c4d04a2010-11-03 17:02:55 -0700181 + ", isAlwaysSyncable=" + isAlwaysSyncable
182 + ", allowParallelSyncs=" + allowParallelSyncs
Fred Quintanae6d60ec2011-08-24 11:29:00 -0700183 + ", settingsActivity=" + settingsActivity
Fred Quintanae0616ff2009-08-19 13:13:18 -0700184 + "}";
185 }
Fred Quintana718d8a22009-04-29 17:53:20 -0700186 }
Fred Quintanaac9385e2009-06-22 18:00:59 -0700187
188 public int describeContents() {
189 return 0;
190 }
191
192 public void writeToParcel(Parcel dest, int flags) {
Fred Quintanae0616ff2009-08-19 13:13:18 -0700193 if (isKey) {
194 throw new IllegalStateException("keys aren't parcelable");
195 }
196
Fred Quintanaac9385e2009-06-22 18:00:59 -0700197 dest.writeString(authority);
198 dest.writeString(accountType);
Fred Quintana4a6679b2009-08-17 13:05:39 -0700199 dest.writeInt(userVisible ? 1 : 0);
Fred Quintanae0616ff2009-08-19 13:13:18 -0700200 dest.writeInt(supportsUploading ? 1 : 0);
Fred Quintana0c4d04a2010-11-03 17:02:55 -0700201 dest.writeInt(isAlwaysSyncable ? 1 : 0);
202 dest.writeInt(allowParallelSyncs ? 1 : 0);
Fred Quintanae6d60ec2011-08-24 11:29:00 -0700203 dest.writeString(settingsActivity);
Fred Quintanaac9385e2009-06-22 18:00:59 -0700204 }
205
206 public SyncAdapterType(Parcel source) {
Fred Quintanae0616ff2009-08-19 13:13:18 -0700207 this(
208 source.readString(),
209 source.readString(),
210 source.readInt() != 0,
Fred Quintana0c4d04a2010-11-03 17:02:55 -0700211 source.readInt() != 0,
212 source.readInt() != 0,
Fred Quintanae6d60ec2011-08-24 11:29:00 -0700213 source.readInt() != 0,
214 source.readString());
Fred Quintanaac9385e2009-06-22 18:00:59 -0700215 }
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 Quintana0c4d04a2010-11-03 17:02:55 -0700226}