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