Chiao Cheng | e88fcd3 | 2012-11-13 18:38:05 -0800 | [diff] [blame] | 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 | |
Gary Mai | 69c182a | 2016-12-05 13:07:03 -0800 | [diff] [blame] | 17 | package com.android.contacts.model.account; |
Chiao Cheng | e88fcd3 | 2012-11-13 18:38:05 -0800 | [diff] [blame] | 18 | |
| 19 | import android.content.Context; |
| 20 | import android.database.Cursor; |
| 21 | import android.net.Uri; |
| 22 | import android.provider.BaseColumns; |
| 23 | import android.provider.ContactsContract; |
| 24 | import android.provider.ContactsContract.RawContacts; |
| 25 | import android.text.TextUtils; |
| 26 | |
| 27 | import com.google.common.base.Objects; |
| 28 | |
| 29 | |
| 30 | /** |
| 31 | * Encapsulates an "account type" string and a "data set" string. |
| 32 | */ |
| 33 | public class AccountTypeWithDataSet { |
| 34 | |
| 35 | private static final String[] ID_PROJECTION = new String[] {BaseColumns._ID}; |
| 36 | private static final Uri RAW_CONTACTS_URI_LIMIT_1 = RawContacts.CONTENT_URI.buildUpon() |
| 37 | .appendQueryParameter(ContactsContract.LIMIT_PARAM_KEY, "1").build(); |
| 38 | |
| 39 | /** account type. Can be null for fallback type. */ |
| 40 | public final String accountType; |
| 41 | |
| 42 | /** dataSet may be null, but never be "". */ |
| 43 | public final String dataSet; |
| 44 | |
| 45 | private AccountTypeWithDataSet(String accountType, String dataSet) { |
| 46 | this.accountType = TextUtils.isEmpty(accountType) ? null : accountType; |
| 47 | this.dataSet = TextUtils.isEmpty(dataSet) ? null : dataSet; |
| 48 | } |
| 49 | |
| 50 | public static AccountTypeWithDataSet get(String accountType, String dataSet) { |
| 51 | return new AccountTypeWithDataSet(accountType, dataSet); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Return true if there are any contacts in the database with this account type and data set. |
| 56 | * Touches DB. Don't use in the UI thread. |
| 57 | */ |
| 58 | public boolean hasData(Context context) { |
| 59 | final String BASE_SELECTION = RawContacts.ACCOUNT_TYPE + " = ?"; |
| 60 | final String selection; |
| 61 | final String[] args; |
| 62 | if (TextUtils.isEmpty(dataSet)) { |
| 63 | selection = BASE_SELECTION + " AND " + RawContacts.DATA_SET + " IS NULL"; |
| 64 | args = new String[] {accountType}; |
| 65 | } else { |
| 66 | selection = BASE_SELECTION + " AND " + RawContacts.DATA_SET + " = ?"; |
| 67 | args = new String[] {accountType, dataSet}; |
| 68 | } |
| 69 | |
| 70 | final Cursor c = context.getContentResolver().query(RAW_CONTACTS_URI_LIMIT_1, |
| 71 | ID_PROJECTION, selection, args, null); |
| 72 | if (c == null) return false; |
| 73 | try { |
| 74 | return c.moveToFirst(); |
| 75 | } finally { |
| 76 | c.close(); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public boolean equals(Object o) { |
| 82 | if (!(o instanceof AccountTypeWithDataSet)) return false; |
| 83 | |
| 84 | AccountTypeWithDataSet other = (AccountTypeWithDataSet) o; |
| 85 | return Objects.equal(accountType, other.accountType) |
| 86 | && Objects.equal(dataSet, other.dataSet); |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public int hashCode() { |
| 91 | return (accountType == null ? 0 : accountType.hashCode()) |
| 92 | ^ (dataSet == null ? 0 : dataSet.hashCode()); |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public String toString() { |
| 97 | return "[" + accountType + "/" + dataSet + "]"; |
| 98 | } |
| 99 | } |