blob: 3ee0aab4a8d283eb0204b2a1a82008d651b4640c [file] [log] [blame]
Chiao Chenge88fcd32012-11-13 18:38:05 -08001/*
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
17package com.android.contacts.common.model.account;
18
19import android.accounts.Account;
20import android.content.Context;
21import android.database.Cursor;
22import android.net.Uri;
23import android.os.Parcel;
Marcus Hagerott819214d2016-09-29 14:58:27 -070024import android.os.Parcelable;
Chiao Chenge88fcd32012-11-13 18:38:05 -080025import android.provider.BaseColumns;
26import android.provider.ContactsContract;
27import android.provider.ContactsContract.RawContacts;
28import android.text.TextUtils;
29
Marcus Hagerott819214d2016-09-29 14:58:27 -070030import com.android.contacts.common.model.AccountTypeManager;
31import com.android.contacts.common.preference.ContactsPreferences;
Chiao Cheng41ba9e52012-11-14 15:55:27 -080032import com.google.common.base.Objects;
Chiao Chenge88fcd32012-11-13 18:38:05 -080033import com.google.common.collect.Lists;
34
35import java.util.ArrayList;
36import java.util.List;
37import java.util.regex.Pattern;
38
39/**
40 * Wrapper for an account that includes a data set (which may be null).
41 */
Jay Shrauner2ae200d2015-01-09 11:36:20 -080042public class AccountWithDataSet implements Parcelable {
Chiao Chenge88fcd32012-11-13 18:38:05 -080043 private static final String STRINGIFY_SEPARATOR = "\u0001";
44 private static final String ARRAY_STRINGIFY_SEPARATOR = "\u0002";
45
46 private static final Pattern STRINGIFY_SEPARATOR_PAT =
47 Pattern.compile(Pattern.quote(STRINGIFY_SEPARATOR));
48 private static final Pattern ARRAY_STRINGIFY_SEPARATOR_PAT =
49 Pattern.compile(Pattern.quote(ARRAY_STRINGIFY_SEPARATOR));
50
Jay Shrauner2ae200d2015-01-09 11:36:20 -080051 public final String name;
52 public final String type;
Chiao Chenge88fcd32012-11-13 18:38:05 -080053 public final String dataSet;
54 private final AccountTypeWithDataSet mAccountTypeWithDataSet;
55
56 private static final String[] ID_PROJECTION = new String[] {BaseColumns._ID};
57 private static final Uri RAW_CONTACTS_URI_LIMIT_1 = RawContacts.CONTENT_URI.buildUpon()
58 .appendQueryParameter(ContactsContract.LIMIT_PARAM_KEY, "1").build();
59
Tingting Wang0ac73ba2016-07-05 22:33:01 -070060 public static final String LOCAL_ACCOUNT_SELECTION = RawContacts.ACCOUNT_TYPE + " IS NULL AND "
61 + RawContacts.ACCOUNT_NAME + " IS NULL AND "
62 + RawContacts.DATA_SET + " IS NULL";
Chiao Chenge88fcd32012-11-13 18:38:05 -080063
64 public AccountWithDataSet(String name, String type, String dataSet) {
Jay Shrauner2ae200d2015-01-09 11:36:20 -080065 this.name = emptyToNull(name);
66 this.type = emptyToNull(type);
67 this.dataSet = emptyToNull(dataSet);
Chiao Chenge88fcd32012-11-13 18:38:05 -080068 mAccountTypeWithDataSet = AccountTypeWithDataSet.get(type, dataSet);
69 }
70
Jay Shrauner2ae200d2015-01-09 11:36:20 -080071 private static final String emptyToNull(String text) {
72 return TextUtils.isEmpty(text) ? null : text;
73 }
74
Chiao Chenge88fcd32012-11-13 18:38:05 -080075 public AccountWithDataSet(Parcel in) {
Jay Shrauner2ae200d2015-01-09 11:36:20 -080076 this.name = in.readString();
77 this.type = in.readString();
Chiao Chenge88fcd32012-11-13 18:38:05 -080078 this.dataSet = in.readString();
79 mAccountTypeWithDataSet = AccountTypeWithDataSet.get(type, dataSet);
80 }
81
Marcus Hagerott949d4e82016-09-20 13:23:05 -070082 public boolean isNullAccount() {
Tingting Wang0ac73ba2016-07-05 22:33:01 -070083 return name == null && type == null && dataSet == null;
84 }
85
Marcus Hagerott949d4e82016-09-20 13:23:05 -070086 public static AccountWithDataSet getNullAccount() {
Tingting Wang0ac73ba2016-07-05 22:33:01 -070087 return new AccountWithDataSet(null, null, null);
Jay Shrauner2ae200d2015-01-09 11:36:20 -080088 }
89
90 public Account getAccountOrNull() {
91 if (name != null && type != null) {
92 return new Account(name, type);
93 }
94 return null;
95 }
96
97 public int describeContents() {
98 return 0;
99 }
100
Chiao Chenge88fcd32012-11-13 18:38:05 -0800101 public void writeToParcel(Parcel dest, int flags) {
Jay Shrauner2ae200d2015-01-09 11:36:20 -0800102 dest.writeString(name);
103 dest.writeString(type);
Chiao Chenge88fcd32012-11-13 18:38:05 -0800104 dest.writeString(dataSet);
105 }
106
107 // For Parcelable
108 public static final Creator<AccountWithDataSet> CREATOR = new Creator<AccountWithDataSet>() {
109 public AccountWithDataSet createFromParcel(Parcel source) {
110 return new AccountWithDataSet(source);
111 }
112
113 public AccountWithDataSet[] newArray(int size) {
114 return new AccountWithDataSet[size];
115 }
116 };
117
118 public AccountTypeWithDataSet getAccountTypeWithDataSet() {
119 return mAccountTypeWithDataSet;
120 }
121
122 /**
123 * Return {@code true} if this account has any contacts in the database.
124 * Touches DB. Don't use in the UI thread.
125 */
126 public boolean hasData(Context context) {
Tingting Wang0ac73ba2016-07-05 22:33:01 -0700127 String selection;
128 String[] args = null;
Marcus Hagerott949d4e82016-09-20 13:23:05 -0700129 if (isNullAccount()) {
Tingting Wang0ac73ba2016-07-05 22:33:01 -0700130 selection = LOCAL_ACCOUNT_SELECTION;
Chiao Chenge88fcd32012-11-13 18:38:05 -0800131 } else {
Tingting Wang0ac73ba2016-07-05 22:33:01 -0700132 final String BASE_SELECTION =
133 RawContacts.ACCOUNT_TYPE + " = ?" + " AND " + RawContacts.ACCOUNT_NAME + " = ?";
134 if (TextUtils.isEmpty(dataSet)) {
135 selection = BASE_SELECTION + " AND " + RawContacts.DATA_SET + " IS NULL";
136 args = new String[] {type, name};
137 } else {
138 selection = BASE_SELECTION + " AND " + RawContacts.DATA_SET + " = ?";
139 args = new String[] {type, name, dataSet};
140 }
Chiao Chenge88fcd32012-11-13 18:38:05 -0800141 }
Marcus Hagerottbc510842016-09-09 19:29:45 -0700142 selection += " AND " + RawContacts.DELETED + "=0";
Chiao Chenge88fcd32012-11-13 18:38:05 -0800143
144 final Cursor c = context.getContentResolver().query(RAW_CONTACTS_URI_LIMIT_1,
145 ID_PROJECTION, selection, args, null);
146 if (c == null) return false;
147 try {
148 return c.moveToFirst();
149 } finally {
150 c.close();
151 }
152 }
153
Jay Shrauner2ae200d2015-01-09 11:36:20 -0800154 public boolean equals(Object obj) {
155 if (obj instanceof AccountWithDataSet) {
156 AccountWithDataSet other = (AccountWithDataSet) obj;
157 return Objects.equal(name, other.name)
158 && Objects.equal(type, other.type)
159 && Objects.equal(dataSet, other.dataSet);
160 }
161 return false;
Chiao Chenge88fcd32012-11-13 18:38:05 -0800162 }
163
Chiao Chenge88fcd32012-11-13 18:38:05 -0800164 public int hashCode() {
Jay Shrauner2ae200d2015-01-09 11:36:20 -0800165 int result = 17;
166 result = 31 * result + (name != null ? name.hashCode() : 0);
167 result = 31 * result + (type != null ? type.hashCode() : 0);
168 result = 31 * result + (dataSet != null ? dataSet.hashCode() : 0);
169 return result;
Chiao Chenge88fcd32012-11-13 18:38:05 -0800170 }
171
Chiao Chenge88fcd32012-11-13 18:38:05 -0800172 public String toString() {
173 return "AccountWithDataSet {name=" + name + ", type=" + type + ", dataSet=" + dataSet + "}";
174 }
175
176 private static StringBuilder addStringified(StringBuilder sb, AccountWithDataSet account) {
Jay Shrauner2ae200d2015-01-09 11:36:20 -0800177 if (!TextUtils.isEmpty(account.name)) sb.append(account.name);
Chiao Chenge88fcd32012-11-13 18:38:05 -0800178 sb.append(STRINGIFY_SEPARATOR);
Jay Shrauner2ae200d2015-01-09 11:36:20 -0800179 if (!TextUtils.isEmpty(account.type)) sb.append(account.type);
Chiao Chenge88fcd32012-11-13 18:38:05 -0800180 sb.append(STRINGIFY_SEPARATOR);
181 if (!TextUtils.isEmpty(account.dataSet)) sb.append(account.dataSet);
182
183 return sb;
184 }
185
186 /**
187 * Pack the instance into a string.
188 */
189 public String stringify() {
190 return addStringified(new StringBuilder(), this).toString();
191 }
192
193 /**
194 * Unpack a string created by {@link #stringify}.
195 *
196 * @throws IllegalArgumentException if it's an invalid string.
197 */
198 public static AccountWithDataSet unstringify(String s) {
199 final String[] array = STRINGIFY_SEPARATOR_PAT.split(s, 3);
200 if (array.length < 3) {
201 throw new IllegalArgumentException("Invalid string " + s);
202 }
203 return new AccountWithDataSet(array[0], array[1],
204 TextUtils.isEmpty(array[2]) ? null : array[2]);
205 }
206
207 /**
208 * Pack a list of {@link AccountWithDataSet} into a string.
209 */
210 public static String stringifyList(List<AccountWithDataSet> accounts) {
211 final StringBuilder sb = new StringBuilder();
212
213 for (AccountWithDataSet account : accounts) {
214 if (sb.length() > 0) {
215 sb.append(ARRAY_STRINGIFY_SEPARATOR);
216 }
217 addStringified(sb, account);
218 }
219
220 return sb.toString();
221 }
222
223 /**
224 * Unpack a list of {@link AccountWithDataSet} into a string.
225 *
226 * @throws IllegalArgumentException if it's an invalid string.
227 */
228 public static List<AccountWithDataSet> unstringifyList(String s) {
229 final ArrayList<AccountWithDataSet> ret = Lists.newArrayList();
230 if (TextUtils.isEmpty(s)) {
231 return ret;
232 }
233
234 final String[] array = ARRAY_STRINGIFY_SEPARATOR_PAT.split(s);
235
236 for (int i = 0; i < array.length; i++) {
237 ret.add(unstringify(array[i]));
238 }
239
240 return ret;
241 }
Marcus Hagerott819214d2016-09-29 14:58:27 -0700242
243 public static AccountWithDataSet getDefaultOrBestFallback(ContactsPreferences preferences,
244 AccountTypeManager accountTypeManager) {
245 if (preferences.isDefaultAccountSet()) {
Marcus Hagerottd7a5c2a2016-11-01 16:50:45 -0700246 final AccountWithDataSet account = preferences.getDefaultAccount();
247 if (accountTypeManager.contains(account, true)) {
248 return account;
249 }
Marcus Hagerott819214d2016-09-29 14:58:27 -0700250 }
Marcus Hagerottd7a5c2a2016-11-01 16:50:45 -0700251 final List<AccountWithDataSet> accounts = accountTypeManager
252 .getAccounts(/* writableOnly */ true);
Marcus Hagerott819214d2016-09-29 14:58:27 -0700253
254 if (accounts.isEmpty()) {
255 return AccountWithDataSet.getNullAccount();
256 }
257
258 // Return the first google account
259 for (AccountWithDataSet account : accounts) {
260 if (GoogleAccountType.ACCOUNT_TYPE.equals(account) && account.dataSet == null) {
261 return account;
262 }
263 }
264 // Arbitrarily return the first writable account
265 return accounts.get(0);
266 }
Chiao Chenge88fcd32012-11-13 18:38:05 -0800267}