summaryrefslogtreecommitdiff
path: root/java/com/android/contacts/common/model/account/AccountTypeWithDataSet.java
blob: a32ebe139bc6aa59b77d2ceb47d5706c84e995b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.contacts.common.model.account;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.BaseColumns;
import android.provider.ContactsContract;
import android.provider.ContactsContract.RawContacts;
import android.text.TextUtils;
import java.util.Objects;

/** Encapsulates an "account type" string and a "data set" string. */
public class AccountTypeWithDataSet {

  private static final String[] ID_PROJECTION = new String[] {BaseColumns._ID};
  private static final Uri RAW_CONTACTS_URI_LIMIT_1 =
      RawContacts.CONTENT_URI
          .buildUpon()
          .appendQueryParameter(ContactsContract.LIMIT_PARAM_KEY, "1")
          .build();

  /** account type. Can be null for fallback type. */
  public final String accountType;

  /** dataSet may be null, but never be "". */
  public final String dataSet;

  private AccountTypeWithDataSet(String accountType, String dataSet) {
    this.accountType = TextUtils.isEmpty(accountType) ? null : accountType;
    this.dataSet = TextUtils.isEmpty(dataSet) ? null : dataSet;
  }

  public static AccountTypeWithDataSet get(String accountType, String dataSet) {
    return new AccountTypeWithDataSet(accountType, dataSet);
  }

  /**
   * Return true if there are any contacts in the database with this account type and data set.
   * Touches DB. Don't use in the UI thread.
   */
  public boolean hasData(Context context) {
    final String BASE_SELECTION = RawContacts.ACCOUNT_TYPE + " = ?";
    final String selection;
    final String[] args;
    if (TextUtils.isEmpty(dataSet)) {
      selection = BASE_SELECTION + " AND " + RawContacts.DATA_SET + " IS NULL";
      args = new String[] {accountType};
    } else {
      selection = BASE_SELECTION + " AND " + RawContacts.DATA_SET + " = ?";
      args = new String[] {accountType, dataSet};
    }

    final Cursor c =
        context
            .getContentResolver()
            .query(RAW_CONTACTS_URI_LIMIT_1, ID_PROJECTION, selection, args, null);
    if (c == null) {
      return false;
    }
    try {
      return c.moveToFirst();
    } finally {
      c.close();
    }
  }

  @Override
  public boolean equals(Object o) {
    if (!(o instanceof AccountTypeWithDataSet)) {
      return false;
    }

    AccountTypeWithDataSet other = (AccountTypeWithDataSet) o;
    return Objects.equals(accountType, other.accountType) && Objects.equals(dataSet, other.dataSet);
  }

  @Override
  public int hashCode() {
    return (accountType == null ? 0 : accountType.hashCode())
        ^ (dataSet == null ? 0 : dataSet.hashCode());
  }

  @Override
  public String toString() {
    return "[" + accountType + "/" + dataSet + "]";
  }
}