SyncAdapterType.java revision 4a6679b97e0285c5b65ec5c0d9080ff90d3e9e81
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 userVisible;
31
32    public SyncAdapterType(String authority, String accountType, boolean userVisible) {
33        if (TextUtils.isEmpty(authority)) {
34            throw new IllegalArgumentException("the authority must not be empty: " + authority);
35        }
36        if (TextUtils.isEmpty(accountType)) {
37            throw new IllegalArgumentException("the accountType must not be empty: " + accountType);
38        }
39        this.authority = authority;
40        this.accountType = accountType;
41        this.userVisible = userVisible;
42    }
43
44    public static SyncAdapterType newKey(String authority, String accountType) {
45        return new SyncAdapterType(authority, accountType, true);
46    }
47
48    public boolean equals(Object o) {
49        if (o == this) return true;
50        if (!(o instanceof SyncAdapterType)) return false;
51        final SyncAdapterType other = (SyncAdapterType)o;
52        // don't include userVisible in the equality check
53        return authority.equals(other.authority) && accountType.equals(other.accountType);
54    }
55
56    public int hashCode() {
57        int result = 17;
58        result = 31 * result + authority.hashCode();
59        result = 31 * result + accountType.hashCode();
60        // don't include userVisible in the hash
61        return result;
62    }
63
64    public String toString() {
65        return "SyncAdapterType {name=" + authority + ", type=" + accountType
66                + ", userVisible=" + userVisible + "}";
67    }
68
69    public int describeContents() {
70        return 0;
71    }
72
73    public void writeToParcel(Parcel dest, int flags) {
74        dest.writeString(authority);
75        dest.writeString(accountType);
76        dest.writeInt(userVisible ? 1 : 0);
77    }
78
79    public SyncAdapterType(Parcel source) {
80        this(source.readString(), source.readString(), source.readInt() != 0);
81    }
82
83    public static final Creator<SyncAdapterType> CREATOR = new Creator<SyncAdapterType>() {
84        public SyncAdapterType createFromParcel(Parcel source) {
85            return new SyncAdapterType(source);
86        }
87
88        public SyncAdapterType[] newArray(int size) {
89            return new SyncAdapterType[size];
90        }
91    };
92}