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.content.res.Resources; 21 import android.database.DatabaseUtils; 22 import android.provider.ContactsContract.SimAccount; 23 import android.text.TextUtils; 24 25 import com.android.internal.R; 26 27 import com.google.common.base.Objects; 28 import com.google.common.base.Strings; 29 30 import java.util.List; 31 32 /** 33 * Account information that includes the data set, if any. 34 */ 35 public class AccountWithDataSet { 36 public static final AccountWithDataSet LOCAL; 37 38 static { 39 Resources resources = Resources.getSystem(); 40 String accountName = Strings.nullToEmpty( 41 resources.getString(R.string.config_rawContactsLocalAccountName)); 42 String accountType = Strings.nullToEmpty( 43 resources.getString(R.string.config_rawContactsLocalAccountType)); 44 LOCAL = new AccountWithDataSet(accountName, accountType, null); 45 } 46 47 private final String mAccountName; 48 private final String mAccountType; 49 private final String mDataSet; 50 AccountWithDataSet(String accountName, String accountType, String dataSet)51 public AccountWithDataSet(String accountName, String accountType, String dataSet) { 52 mAccountName = emptyToNull(accountName); 53 mAccountType = emptyToNull(accountType); 54 mDataSet = emptyToNull(dataSet); 55 } 56 emptyToNull(String text)57 private static final String emptyToNull(String text) { 58 return TextUtils.isEmpty(text) ? null : text; 59 } 60 get(String accountName, String accountType, String dataSet)61 public static AccountWithDataSet get(String accountName, String accountType, String dataSet) { 62 return new AccountWithDataSet(accountName, accountType, dataSet); 63 } 64 get(Account account, String dataSet)65 public static AccountWithDataSet get(Account account, String dataSet) { 66 return new AccountWithDataSet(account.name, account.type, null); 67 } 68 getAccountName()69 public String getAccountName() { 70 return mAccountName; 71 } 72 getAccountType()73 public String getAccountType() { 74 return mAccountType; 75 } 76 getDataSet()77 public String getDataSet() { 78 return mDataSet; 79 } 80 isLocalAccount()81 public boolean isLocalAccount() { 82 return LOCAL.equals(this) || ( 83 mAccountName == null && mAccountType == null && mDataSet == null); 84 } 85 86 @Override equals(Object obj)87 public boolean equals(Object obj) { 88 if (obj instanceof AccountWithDataSet) { 89 AccountWithDataSet other = (AccountWithDataSet) obj; 90 return Objects.equal(mAccountName, other.getAccountName()) 91 && Objects.equal(mAccountType, other.getAccountType()) 92 && Objects.equal(mDataSet, other.getDataSet()); 93 } 94 return false; 95 } 96 97 @Override hashCode()98 public int hashCode() { 99 int result = mAccountName != null ? mAccountName.hashCode() : 0; 100 result = 31 * result + (mAccountType != null ? mAccountType.hashCode() : 0); 101 result = 31 * result + (mDataSet != null ? mDataSet.hashCode() : 0); 102 return result; 103 } 104 105 @Override toString()106 public String toString() { 107 return "AccountWithDataSet {name=" + mAccountName + ", type=" + mAccountType + ", dataSet=" 108 + mDataSet + "}"; 109 } 110 111 /** 112 * @return {@code true} if the owning {@link Account} is in the passed array. 113 */ inSystemAccounts(Account[] systemAccounts)114 public boolean inSystemAccounts(Account[] systemAccounts) { 115 // Note we don't want to create a new Account object from this instance, as it may contain 116 // null account name/type, which Account wouldn't accept. So we need to compare field by 117 // field. 118 for (Account systemAccount : systemAccounts) { 119 if (Objects.equal(systemAccount.name, getAccountName()) 120 && Objects.equal(systemAccount.type, getAccountType())) { 121 return true; 122 } 123 } 124 return false; 125 } 126 127 /** 128 * @return {@code true} if there is a {@link SimAccount} with a matching account name and type 129 * in the passed list. 130 * The list should be obtained from {@link ContactsDatabaseHelper#getAllSimAccounts()} so it 131 * will already have valid SIM accounts so only name and type need to be compared. 132 */ inSimAccounts(List<SimAccount> simAccountList)133 public boolean inSimAccounts(List<SimAccount> simAccountList) { 134 // Note we don't want to create a new SimAccount object from this instance, as this method 135 // does not need to know about the SIM slot or the EF type. It only needs to know whether 136 // the name and type match since the passed list will only contain valid SIM accounts. 137 for (SimAccount simAccount : simAccountList) { 138 if (Objects.equal(simAccount.getAccountName(), getAccountName()) 139 && Objects.equal(simAccount.getAccountType(), getAccountType())) { 140 return true; 141 } 142 } 143 return false; 144 } 145 } 146