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