Home | History | Annotate | Download | only in content
      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;
     20 import android.os.Parcelable;
     21 import 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  */
     27 public 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 
     34     public SyncAdapterType(String authority, String accountType, boolean userVisible,
     35             boolean supportsUploading) {
     36         if (TextUtils.isEmpty(authority)) {
     37             throw new IllegalArgumentException("the authority must not be empty: " + authority);
     38         }
     39         if (TextUtils.isEmpty(accountType)) {
     40             throw new IllegalArgumentException("the accountType must not be empty: " + accountType);
     41         }
     42         this.authority = authority;
     43         this.accountType = accountType;
     44         this.userVisible = userVisible;
     45         this.supportsUploading = supportsUploading;
     46         this.isKey = false;
     47     }
     48 
     49     private SyncAdapterType(String authority, String accountType) {
     50         if (TextUtils.isEmpty(authority)) {
     51             throw new IllegalArgumentException("the authority must not be empty: " + authority);
     52         }
     53         if (TextUtils.isEmpty(accountType)) {
     54             throw new IllegalArgumentException("the accountType must not be empty: " + accountType);
     55         }
     56         this.authority = authority;
     57         this.accountType = accountType;
     58         this.userVisible = true;
     59         this.supportsUploading = true;
     60         this.isKey = true;
     61     }
     62 
     63     public boolean supportsUploading() {
     64         if (isKey) {
     65             throw new IllegalStateException(
     66                     "this method is not allowed to be called when this is a key");
     67         }
     68         return supportsUploading;
     69     }
     70 
     71     public boolean isUserVisible() {
     72         if (isKey) {
     73             throw new IllegalStateException(
     74                     "this method is not allowed to be called when this is a key");
     75         }
     76         return userVisible;
     77     }
     78 
     79     public static SyncAdapterType newKey(String authority, String accountType) {
     80         return new SyncAdapterType(authority, accountType);
     81     }
     82 
     83     public boolean equals(Object o) {
     84         if (o == this) return true;
     85         if (!(o instanceof SyncAdapterType)) return false;
     86         final SyncAdapterType other = (SyncAdapterType)o;
     87         // don't include userVisible or supportsUploading in the equality check
     88         return authority.equals(other.authority) && accountType.equals(other.accountType);
     89     }
     90 
     91     public int hashCode() {
     92         int result = 17;
     93         result = 31 * result + authority.hashCode();
     94         result = 31 * result + accountType.hashCode();
     95         // don't include userVisible or supportsUploading  the hash
     96         return result;
     97     }
     98 
     99     public String toString() {
    100         if (isKey) {
    101             return "SyncAdapterType Key {name=" + authority
    102                     + ", type=" + accountType
    103                     + "}";
    104         } else {
    105             return "SyncAdapterType {name=" + authority
    106                     + ", type=" + accountType
    107                     + ", userVisible=" + userVisible
    108                     + ", supportsUploading=" + supportsUploading
    109                     + "}";
    110         }
    111     }
    112 
    113     public int describeContents() {
    114         return 0;
    115     }
    116 
    117     public void writeToParcel(Parcel dest, int flags) {
    118         if (isKey) {
    119             throw new IllegalStateException("keys aren't parcelable");
    120         }
    121 
    122         dest.writeString(authority);
    123         dest.writeString(accountType);
    124         dest.writeInt(userVisible ? 1 : 0);
    125         dest.writeInt(supportsUploading ? 1 : 0);
    126     }
    127 
    128     public SyncAdapterType(Parcel source) {
    129         this(
    130                 source.readString(),
    131                 source.readString(),
    132                 source.readInt() != 0,
    133                 source.readInt() != 0);
    134     }
    135 
    136     public static final Creator<SyncAdapterType> CREATOR = new Creator<SyncAdapterType>() {
    137         public SyncAdapterType createFromParcel(Parcel source) {
    138             return new SyncAdapterType(source);
    139         }
    140 
    141         public SyncAdapterType[] newArray(int size) {
    142             return new SyncAdapterType[size];
    143         }
    144     };
    145 }