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 | |
| 17 | package com.android.contacts.common.model.account; |
| 18 | |
| 19 | import android.accounts.Account; |
| 20 | import android.content.Context; |
| 21 | import android.database.Cursor; |
| 22 | import android.net.Uri; |
| 23 | import android.os.Parcel; |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 24 | import android.os.Parcelable; |
Chiao Cheng | e88fcd3 | 2012-11-13 18:38:05 -0800 | [diff] [blame] | 25 | import android.provider.BaseColumns; |
| 26 | import android.provider.ContactsContract; |
| 27 | import android.provider.ContactsContract.RawContacts; |
| 28 | import android.text.TextUtils; |
| 29 | |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 30 | import com.android.contacts.common.model.AccountTypeManager; |
| 31 | import com.android.contacts.common.preference.ContactsPreferences; |
Chiao Cheng | 41ba9e5 | 2012-11-14 15:55:27 -0800 | [diff] [blame] | 32 | import com.google.common.base.Objects; |
Chiao Cheng | e88fcd3 | 2012-11-13 18:38:05 -0800 | [diff] [blame] | 33 | import com.google.common.collect.Lists; |
| 34 | |
| 35 | import java.util.ArrayList; |
| 36 | import java.util.List; |
| 37 | import java.util.regex.Pattern; |
| 38 | |
| 39 | /** |
| 40 | * Wrapper for an account that includes a data set (which may be null). |
| 41 | */ |
Jay Shrauner | 2ae200d | 2015-01-09 11:36:20 -0800 | [diff] [blame] | 42 | public class AccountWithDataSet implements Parcelable { |
Chiao Cheng | e88fcd3 | 2012-11-13 18:38:05 -0800 | [diff] [blame] | 43 | 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 Shrauner | 2ae200d | 2015-01-09 11:36:20 -0800 | [diff] [blame] | 51 | public final String name; |
| 52 | public final String type; |
Chiao Cheng | e88fcd3 | 2012-11-13 18:38:05 -0800 | [diff] [blame] | 53 | 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 Wang | 0ac73ba | 2016-07-05 22:33:01 -0700 | [diff] [blame] | 60 | 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 Cheng | e88fcd3 | 2012-11-13 18:38:05 -0800 | [diff] [blame] | 63 | |
| 64 | public AccountWithDataSet(String name, String type, String dataSet) { |
Jay Shrauner | 2ae200d | 2015-01-09 11:36:20 -0800 | [diff] [blame] | 65 | this.name = emptyToNull(name); |
| 66 | this.type = emptyToNull(type); |
| 67 | this.dataSet = emptyToNull(dataSet); |
Chiao Cheng | e88fcd3 | 2012-11-13 18:38:05 -0800 | [diff] [blame] | 68 | mAccountTypeWithDataSet = AccountTypeWithDataSet.get(type, dataSet); |
| 69 | } |
| 70 | |
Jay Shrauner | 2ae200d | 2015-01-09 11:36:20 -0800 | [diff] [blame] | 71 | private static final String emptyToNull(String text) { |
| 72 | return TextUtils.isEmpty(text) ? null : text; |
| 73 | } |
| 74 | |
Chiao Cheng | e88fcd3 | 2012-11-13 18:38:05 -0800 | [diff] [blame] | 75 | public AccountWithDataSet(Parcel in) { |
Jay Shrauner | 2ae200d | 2015-01-09 11:36:20 -0800 | [diff] [blame] | 76 | this.name = in.readString(); |
| 77 | this.type = in.readString(); |
Chiao Cheng | e88fcd3 | 2012-11-13 18:38:05 -0800 | [diff] [blame] | 78 | this.dataSet = in.readString(); |
| 79 | mAccountTypeWithDataSet = AccountTypeWithDataSet.get(type, dataSet); |
| 80 | } |
| 81 | |
Marcus Hagerott | 949d4e8 | 2016-09-20 13:23:05 -0700 | [diff] [blame] | 82 | public boolean isNullAccount() { |
Tingting Wang | 0ac73ba | 2016-07-05 22:33:01 -0700 | [diff] [blame] | 83 | return name == null && type == null && dataSet == null; |
| 84 | } |
| 85 | |
Marcus Hagerott | 949d4e8 | 2016-09-20 13:23:05 -0700 | [diff] [blame] | 86 | public static AccountWithDataSet getNullAccount() { |
Tingting Wang | 0ac73ba | 2016-07-05 22:33:01 -0700 | [diff] [blame] | 87 | return new AccountWithDataSet(null, null, null); |
Jay Shrauner | 2ae200d | 2015-01-09 11:36:20 -0800 | [diff] [blame] | 88 | } |
| 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 Cheng | e88fcd3 | 2012-11-13 18:38:05 -0800 | [diff] [blame] | 101 | public void writeToParcel(Parcel dest, int flags) { |
Jay Shrauner | 2ae200d | 2015-01-09 11:36:20 -0800 | [diff] [blame] | 102 | dest.writeString(name); |
| 103 | dest.writeString(type); |
Chiao Cheng | e88fcd3 | 2012-11-13 18:38:05 -0800 | [diff] [blame] | 104 | 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 Wang | 0ac73ba | 2016-07-05 22:33:01 -0700 | [diff] [blame] | 127 | String selection; |
| 128 | String[] args = null; |
Marcus Hagerott | 949d4e8 | 2016-09-20 13:23:05 -0700 | [diff] [blame] | 129 | if (isNullAccount()) { |
Tingting Wang | 0ac73ba | 2016-07-05 22:33:01 -0700 | [diff] [blame] | 130 | selection = LOCAL_ACCOUNT_SELECTION; |
Chiao Cheng | e88fcd3 | 2012-11-13 18:38:05 -0800 | [diff] [blame] | 131 | } else { |
Tingting Wang | 0ac73ba | 2016-07-05 22:33:01 -0700 | [diff] [blame] | 132 | 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 Cheng | e88fcd3 | 2012-11-13 18:38:05 -0800 | [diff] [blame] | 141 | } |
Marcus Hagerott | bc51084 | 2016-09-09 19:29:45 -0700 | [diff] [blame] | 142 | selection += " AND " + RawContacts.DELETED + "=0"; |
Chiao Cheng | e88fcd3 | 2012-11-13 18:38:05 -0800 | [diff] [blame] | 143 | |
| 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 Shrauner | 2ae200d | 2015-01-09 11:36:20 -0800 | [diff] [blame] | 154 | 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 Cheng | e88fcd3 | 2012-11-13 18:38:05 -0800 | [diff] [blame] | 162 | } |
| 163 | |
Chiao Cheng | e88fcd3 | 2012-11-13 18:38:05 -0800 | [diff] [blame] | 164 | public int hashCode() { |
Jay Shrauner | 2ae200d | 2015-01-09 11:36:20 -0800 | [diff] [blame] | 165 | 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 Cheng | e88fcd3 | 2012-11-13 18:38:05 -0800 | [diff] [blame] | 170 | } |
| 171 | |
Chiao Cheng | e88fcd3 | 2012-11-13 18:38:05 -0800 | [diff] [blame] | 172 | public String toString() { |
| 173 | return "AccountWithDataSet {name=" + name + ", type=" + type + ", dataSet=" + dataSet + "}"; |
| 174 | } |
| 175 | |
| 176 | private static StringBuilder addStringified(StringBuilder sb, AccountWithDataSet account) { |
Jay Shrauner | 2ae200d | 2015-01-09 11:36:20 -0800 | [diff] [blame] | 177 | if (!TextUtils.isEmpty(account.name)) sb.append(account.name); |
Chiao Cheng | e88fcd3 | 2012-11-13 18:38:05 -0800 | [diff] [blame] | 178 | sb.append(STRINGIFY_SEPARATOR); |
Jay Shrauner | 2ae200d | 2015-01-09 11:36:20 -0800 | [diff] [blame] | 179 | if (!TextUtils.isEmpty(account.type)) sb.append(account.type); |
Chiao Cheng | e88fcd3 | 2012-11-13 18:38:05 -0800 | [diff] [blame] | 180 | 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 Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 242 | |
| 243 | public static AccountWithDataSet getDefaultOrBestFallback(ContactsPreferences preferences, |
| 244 | AccountTypeManager accountTypeManager) { |
| 245 | if (preferences.isDefaultAccountSet()) { |
Marcus Hagerott | d7a5c2a | 2016-11-01 16:50:45 -0700 | [diff] [blame] | 246 | final AccountWithDataSet account = preferences.getDefaultAccount(); |
| 247 | if (accountTypeManager.contains(account, true)) { |
| 248 | return account; |
| 249 | } |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 250 | } |
Marcus Hagerott | d7a5c2a | 2016-11-01 16:50:45 -0700 | [diff] [blame] | 251 | final List<AccountWithDataSet> accounts = accountTypeManager |
| 252 | .getAccounts(/* writableOnly */ true); |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 253 | |
| 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 Cheng | e88fcd3 | 2012-11-13 18:38:05 -0800 | [diff] [blame] | 267 | } |