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