1 /* 2 * Copyright (C) 2011 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 com.android.providers.contacts; 18 19 import android.accounts.Account; 20 import android.text.TextUtils; 21 22 import com.google.common.base.Objects; 23 24 /** 25 * Account information that includes the data set, if any. 26 */ 27 public class AccountWithDataSet { 28 public static final AccountWithDataSet LOCAL = new AccountWithDataSet(null, null, null); 29 30 private final String mAccountName; 31 private final String mAccountType; 32 private final String mDataSet; 33 AccountWithDataSet(String accountName, String accountType, String dataSet)34 public AccountWithDataSet(String accountName, String accountType, String dataSet) { 35 mAccountName = emptyToNull(accountName); 36 mAccountType = emptyToNull(accountType); 37 mDataSet = emptyToNull(dataSet); 38 } 39 emptyToNull(String text)40 private static final String emptyToNull(String text) { 41 return TextUtils.isEmpty(text) ? null : text; 42 } 43 get(String accountName, String accountType, String dataSet)44 public static AccountWithDataSet get(String accountName, String accountType, String dataSet) { 45 return new AccountWithDataSet(accountName, accountType, dataSet); 46 } 47 get(Account account, String dataSet)48 public static AccountWithDataSet get(Account account, String dataSet) { 49 return new AccountWithDataSet(account.name, account.type, null); 50 } 51 getAccountName()52 public String getAccountName() { 53 return mAccountName; 54 } 55 getAccountType()56 public String getAccountType() { 57 return mAccountType; 58 } 59 getDataSet()60 public String getDataSet() { 61 return mDataSet; 62 } 63 isLocalAccount()64 public boolean isLocalAccount() { 65 return (mAccountName == null) && (mAccountType == null); 66 } 67 68 @Override equals(Object obj)69 public boolean equals(Object obj) { 70 if (obj instanceof AccountWithDataSet) { 71 AccountWithDataSet other = (AccountWithDataSet) obj; 72 return Objects.equal(mAccountName, other.getAccountName()) 73 && Objects.equal(mAccountType, other.getAccountType()) 74 && Objects.equal(mDataSet, other.getDataSet()); 75 } 76 return false; 77 } 78 79 @Override hashCode()80 public int hashCode() { 81 int result = mAccountName != null ? mAccountName.hashCode() : 0; 82 result = 31 * result + (mAccountType != null ? mAccountType.hashCode() : 0); 83 result = 31 * result + (mDataSet != null ? mDataSet.hashCode() : 0); 84 return result; 85 } 86 87 @Override toString()88 public String toString() { 89 return "AccountWithDataSet {name=" + mAccountName + ", type=" + mAccountType + ", dataSet=" 90 + mDataSet + "}"; 91 } 92 93 /** 94 * @return {@code true} if the owning {@link Account} is in the passed array. 95 */ inSystemAccounts(Account[] systemAccounts)96 public boolean inSystemAccounts(Account[] systemAccounts) { 97 // Note we don't want to create a new Account object from this instance, as it may contain 98 // null account name/type, which Account wouldn't accept. So we need to compare field by 99 // field. 100 for (Account systemAccount : systemAccounts) { 101 if (Objects.equal(systemAccount.name, getAccountName()) 102 && Objects.equal(systemAccount.type, getAccountType())) { 103 return true; 104 } 105 } 106 return false; 107 } 108 } 109