Makoto Onuki | 558669d | 2011-09-22 18:09:28 -0700 | [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.editor; |
| 18 | |
Makoto Onuki | 558669d | 2011-09-22 18:09:28 -0700 | [diff] [blame] | 19 | import android.accounts.Account; |
| 20 | import android.accounts.AccountManager; |
| 21 | import android.app.Activity; |
Walter Jang | 7b0970f | 2016-09-01 10:40:19 -0700 | [diff] [blame] | 22 | import android.content.ContentUris; |
Makoto Onuki | 558669d | 2011-09-22 18:09:28 -0700 | [diff] [blame] | 23 | import android.content.Context; |
| 24 | import android.content.Intent; |
Walter Jang | 7b0970f | 2016-09-01 10:40:19 -0700 | [diff] [blame] | 25 | import android.net.Uri; |
| 26 | import android.provider.ContactsContract; |
Makoto Onuki | 558669d | 2011-09-22 18:09:28 -0700 | [diff] [blame] | 27 | import android.text.TextUtils; |
| 28 | |
Chiao Cheng | 0d5588d | 2012-11-26 15:34:14 -0800 | [diff] [blame] | 29 | import com.android.contacts.common.model.AccountTypeManager; |
Chiao Cheng | 428f008 | 2012-11-13 18:38:56 -0800 | [diff] [blame] | 30 | import com.android.contacts.common.model.account.AccountType; |
| 31 | import com.android.contacts.common.model.account.AccountWithDataSet; |
Marcus Hagerott | 949d4e8 | 2016-09-20 13:23:05 -0700 | [diff] [blame] | 32 | import com.android.contacts.common.preference.ContactsPreferences; |
Chiao Cheng | e0b2f1e | 2012-06-12 13:07:56 -0700 | [diff] [blame] | 33 | import com.google.common.annotations.VisibleForTesting; |
Chiao Cheng | e0b2f1e | 2012-06-12 13:07:56 -0700 | [diff] [blame] | 34 | import com.google.common.collect.Sets; |
| 35 | |
Makoto Onuki | 558669d | 2011-09-22 18:09:28 -0700 | [diff] [blame] | 36 | import java.util.ArrayList; |
| 37 | import java.util.List; |
| 38 | import java.util.Set; |
| 39 | |
| 40 | /** |
| 41 | * Utility methods for the "account changed" notification in the new contact creation flow. |
Makoto Onuki | 558669d | 2011-09-22 18:09:28 -0700 | [diff] [blame] | 42 | */ |
| 43 | public class ContactEditorUtils { |
| 44 | private static final String TAG = "ContactEditorUtils"; |
| 45 | |
Marcus Hagerott | 949d4e8 | 2016-09-20 13:23:05 -0700 | [diff] [blame] | 46 | private final ContactsPreferences mContactsPrefs; |
Makoto Onuki | 558669d | 2011-09-22 18:09:28 -0700 | [diff] [blame] | 47 | private final AccountTypeManager mAccountTypes; |
| 48 | |
| 49 | private ContactEditorUtils(Context context) { |
| 50 | this(context, AccountTypeManager.getInstance(context)); |
| 51 | } |
| 52 | |
| 53 | @VisibleForTesting |
| 54 | ContactEditorUtils(Context context, AccountTypeManager accountTypes) { |
Marcus Hagerott | 949d4e8 | 2016-09-20 13:23:05 -0700 | [diff] [blame] | 55 | mContactsPrefs = new ContactsPreferences(context); |
Makoto Onuki | 558669d | 2011-09-22 18:09:28 -0700 | [diff] [blame] | 56 | mAccountTypes = accountTypes; |
| 57 | } |
| 58 | |
Marcus Hagerott | 949d4e8 | 2016-09-20 13:23:05 -0700 | [diff] [blame] | 59 | public static ContactEditorUtils create(Context context) { |
| 60 | return new ContactEditorUtils(context.getApplicationContext()); |
Makoto Onuki | 558669d | 2011-09-22 18:09:28 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Walter Jang | 7b0970f | 2016-09-01 10:40:19 -0700 | [diff] [blame] | 63 | /** |
| 64 | * Returns a legacy version of the given contactLookupUri if a legacy Uri was originally |
| 65 | * passed to the contact editor. |
| 66 | * |
| 67 | * @param contactLookupUri The Uri to possibly convert to legacy format. |
| 68 | * @param requestLookupUri The lookup Uri originally passed to the contact editor |
| 69 | * (via Intent data), may be null. |
| 70 | */ |
| 71 | static Uri maybeConvertToLegacyLookupUri(Context context, Uri contactLookupUri, |
| 72 | Uri requestLookupUri) { |
| 73 | final String legacyAuthority = "contacts"; |
| 74 | final String requestAuthority = requestLookupUri == null |
| 75 | ? null : requestLookupUri.getAuthority(); |
| 76 | if (legacyAuthority.equals(requestAuthority)) { |
| 77 | // Build a legacy Uri if that is what was requested by caller |
| 78 | final long contactId = ContentUris.parseId(ContactsContract.Contacts.lookupContact( |
| 79 | context.getContentResolver(), contactLookupUri)); |
| 80 | final Uri legacyContentUri = Uri.parse("content://contacts/people"); |
| 81 | return ContentUris.withAppendedId(legacyContentUri, contactId); |
| 82 | } |
| 83 | // Otherwise pass back a lookup-style Uri |
| 84 | return contactLookupUri; |
| 85 | } |
| 86 | |
Makoto Onuki | 558669d | 2011-09-22 18:09:28 -0700 | [diff] [blame] | 87 | void cleanupForTest() { |
Marcus Hagerott | 949d4e8 | 2016-09-20 13:23:05 -0700 | [diff] [blame] | 88 | mContactsPrefs.clearDefaultAccount(); |
Makoto Onuki | 558669d | 2011-09-22 18:09:28 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Makoto Onuki | 131e6ac | 2011-10-04 19:09:54 -0700 | [diff] [blame] | 91 | void removeDefaultAccountForTest() { |
Marcus Hagerott | 949d4e8 | 2016-09-20 13:23:05 -0700 | [diff] [blame] | 92 | mContactsPrefs.clearDefaultAccount(); |
Makoto Onuki | 131e6ac | 2011-10-04 19:09:54 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Makoto Onuki | 558669d | 2011-09-22 18:09:28 -0700 | [diff] [blame] | 95 | private List<AccountWithDataSet> getWritableAccounts() { |
| 96 | return mAccountTypes.getAccounts(true); |
| 97 | } |
| 98 | |
| 99 | /** |
Marcus Hagerott | 949d4e8 | 2016-09-20 13:23:05 -0700 | [diff] [blame] | 100 | * Saves the default account, which can later be obtained with {@link #getOnlyOrDefaultAccount}. |
Makoto Onuki | 558669d | 2011-09-22 18:09:28 -0700 | [diff] [blame] | 101 | * |
| 102 | * This should be called when saving a newly created contact. |
| 103 | * |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 104 | * @param defaultAccount the account used to save a newly created contact. |
Makoto Onuki | 558669d | 2011-09-22 18:09:28 -0700 | [diff] [blame] | 105 | */ |
Marcus Hagerott | 949d4e8 | 2016-09-20 13:23:05 -0700 | [diff] [blame] | 106 | public void saveDefaultAccount(AccountWithDataSet defaultAccount) { |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 107 | if (defaultAccount == null) { |
Marcus Hagerott | 949d4e8 | 2016-09-20 13:23:05 -0700 | [diff] [blame] | 108 | mContactsPrefs.clearDefaultAccount(); |
Makoto Onuki | 131e6ac | 2011-10-04 19:09:54 -0700 | [diff] [blame] | 109 | } else { |
Marcus Hagerott | 949d4e8 | 2016-09-20 13:23:05 -0700 | [diff] [blame] | 110 | mContactsPrefs.setDefaultAccount(defaultAccount); |
Makoto Onuki | 131e6ac | 2011-10-04 19:09:54 -0700 | [diff] [blame] | 111 | } |
Makoto Onuki | 558669d | 2011-09-22 18:09:28 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | /** |
Marcus Hagerott | 949d4e8 | 2016-09-20 13:23:05 -0700 | [diff] [blame] | 115 | * @return the first account if there is only a single account or the default account saved |
| 116 | * with {@link #saveDefaultAccount}. |
Makoto Onuki | 558669d | 2011-09-22 18:09:28 -0700 | [diff] [blame] | 117 | * |
Marcus Hagerott | 949d4e8 | 2016-09-20 13:23:05 -0700 | [diff] [blame] | 118 | * A null return value indicates that there is multiple accounts and a default hasn't been set |
Makoto Onuki | 558669d | 2011-09-22 18:09:28 -0700 | [diff] [blame] | 119 | * |
| 120 | * Also note that the returned account may have been removed already. |
| 121 | */ |
Marcus Hagerott | 949d4e8 | 2016-09-20 13:23:05 -0700 | [diff] [blame] | 122 | public AccountWithDataSet getOnlyOrDefaultAccount() { |
Tingting Wang | 4abdee9 | 2015-11-24 11:50:21 -0800 | [diff] [blame] | 123 | final List<AccountWithDataSet> currentWritableAccounts = getWritableAccounts(); |
| 124 | if (currentWritableAccounts.size() == 1) { |
| 125 | return currentWritableAccounts.get(0); |
| 126 | } |
| 127 | |
Marcus Hagerott | 949d4e8 | 2016-09-20 13:23:05 -0700 | [diff] [blame] | 128 | return mContactsPrefs.getDefaultAccount(); |
Makoto Onuki | 558669d | 2011-09-22 18:09:28 -0700 | [diff] [blame] | 129 | } |
| 130 | |
Makoto Onuki | 558669d | 2011-09-22 18:09:28 -0700 | [diff] [blame] | 131 | public boolean shouldShowAccountChangedNotification() { |
Marcus Hagerott | 949d4e8 | 2016-09-20 13:23:05 -0700 | [diff] [blame] | 132 | return mContactsPrefs.shouldShowAccountChangedNotification(getWritableAccounts()); |
Makoto Onuki | 558669d | 2011-09-22 18:09:28 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Gary Mai | 3107b25 | 2016-11-02 18:26:07 -0700 | [diff] [blame] | 135 | /** |
| 136 | * Sets the only non-device account to be default if it is not already. |
| 137 | */ |
| 138 | public void maybeUpdateDefaultAccount() { |
| 139 | final List<AccountWithDataSet> currentWritableAccounts = getWritableAccounts(); |
| 140 | if (currentWritableAccounts.size() == 1) { |
| 141 | final AccountWithDataSet onlyAccount = currentWritableAccounts.get(0); |
| 142 | if (!onlyAccount.isNullAccount() |
| 143 | && !onlyAccount.equals(mContactsPrefs.getDefaultAccount())) { |
| 144 | mContactsPrefs.setDefaultAccount(onlyAccount); |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | |
Makoto Onuki | 558669d | 2011-09-22 18:09:28 -0700 | [diff] [blame] | 149 | @VisibleForTesting |
| 150 | String[] getWritableAccountTypeStrings() { |
| 151 | final Set<String> types = Sets.newHashSet(); |
| 152 | for (AccountType type : mAccountTypes.getAccountTypes(true)) { |
| 153 | types.add(type.accountType); |
| 154 | } |
| 155 | return types.toArray(new String[types.size()]); |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Create an {@link Intent} to start "add new account" setup wizard. Selectable account |
| 160 | * types will be limited to ones that supports editing contacts. |
| 161 | * |
| 162 | * Use {@link Activity#startActivityForResult} or |
| 163 | * {@link android.app.Fragment#startActivityForResult} to start the wizard, and |
| 164 | * {@link Activity#onActivityResult} or {@link android.app.Fragment#onActivityResult} to |
| 165 | * get the result. |
| 166 | */ |
Makoto Onuki | 558669d | 2011-09-22 18:09:28 -0700 | [diff] [blame] | 167 | public Intent createAddWritableAccountIntent() { |
| 168 | return AccountManager.newChooseAccountIntent( |
| 169 | null, // selectedAccount |
| 170 | new ArrayList<Account>(), // allowableAccounts |
| 171 | getWritableAccountTypeStrings(), // allowableAccountTypes |
| 172 | false, // alwaysPromptForAccount |
| 173 | null, // descriptionOverrideText |
| 174 | null, // addAccountAuthTokenType |
| 175 | null, // addAccountRequiredFeatures |
| 176 | null // addAccountOptions |
| 177 | ); |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Parses a result from {@link #createAddWritableAccountIntent} and returns the created |
| 182 | * {@link Account}, or null if the user has canceled the wizard. Pass the {@code resultCode} |
| 183 | * and {@code data} parameters passed to {@link Activity#onActivityResult} or |
| 184 | * {@link android.app.Fragment#onActivityResult}. |
| 185 | * |
| 186 | * Note although the return type is {@link AccountWithDataSet}, return values from this method |
| 187 | * will never have {@link AccountWithDataSet#dataSet} set, as there's no way to create an |
| 188 | * extension package account from setup wizard. |
| 189 | */ |
Makoto Onuki | 558669d | 2011-09-22 18:09:28 -0700 | [diff] [blame] | 190 | public AccountWithDataSet getCreatedAccount(int resultCode, Intent resultData) { |
| 191 | // Javadoc doesn't say anything about resultCode but that the data intent will be non null |
| 192 | // on success. |
| 193 | if (resultData == null) return null; |
| 194 | |
| 195 | final String accountType = resultData.getStringExtra(AccountManager.KEY_ACCOUNT_TYPE); |
| 196 | final String accountName = resultData.getStringExtra(AccountManager.KEY_ACCOUNT_NAME); |
| 197 | |
| 198 | // Just in case |
| 199 | if (TextUtils.isEmpty(accountType) || TextUtils.isEmpty(accountName)) return null; |
| 200 | |
| 201 | return new AccountWithDataSet(accountName, accountType, null); |
| 202 | } |
| 203 | } |