Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | */ |
Gary Mai | 69c182a | 2016-12-05 13:07:03 -0800 | [diff] [blame] | 16 | package com.android.contacts.model.account; |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 17 | |
| 18 | import android.content.Context; |
| 19 | |
Gary Mai | 69c182a | 2016-12-05 13:07:03 -0800 | [diff] [blame] | 20 | import com.android.contacts.list.ContactListFilter; |
| 21 | import com.android.contacts.model.AccountTypeManager; |
| 22 | import com.android.contacts.model.RawContactDelta; |
| 23 | import com.android.contacts.util.DeviceLocalAccountTypeFactory; |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 24 | import com.android.contactsbind.ObjectFactory; |
| 25 | |
| 26 | import java.util.ArrayList; |
| 27 | import java.util.List; |
| 28 | |
| 29 | /** |
| 30 | * Provides methods to get AccountDisplayInfo instances for available accounts. |
| 31 | * |
| 32 | * For most accounts the account name will be used for the label but device accounts and |
| 33 | * SIM accounts have friendly names associated with them unless there is more than one of these |
| 34 | * types of accounts present in the list. |
| 35 | */ |
| 36 | public class AccountDisplayInfoFactory { |
| 37 | |
| 38 | private final Context mContext; |
| 39 | private final AccountTypeManager mAccountTypeManager; |
| 40 | |
| 41 | private final DeviceLocalAccountTypeFactory mDeviceAccountTypeFactory; |
| 42 | |
| 43 | private final int mDeviceAccountCount; |
| 44 | private final int mSimAccountCount; |
| 45 | |
| 46 | public AccountDisplayInfoFactory(Context context, List<AccountWithDataSet> accounts) { |
| 47 | this(context, AccountTypeManager.getInstance(context), |
| 48 | ObjectFactory.getDeviceLocalAccountTypeFactory(context), accounts); |
| 49 | } |
| 50 | |
| 51 | public AccountDisplayInfoFactory(Context context, AccountTypeManager accountTypeManager, |
| 52 | DeviceLocalAccountTypeFactory deviceAccountTypeFactory, |
| 53 | List<AccountWithDataSet> accounts) { |
| 54 | mContext = context; |
| 55 | mAccountTypeManager = accountTypeManager; |
| 56 | mDeviceAccountTypeFactory = deviceAccountTypeFactory; |
| 57 | |
| 58 | mSimAccountCount = countOfType(DeviceLocalAccountTypeFactory.TYPE_SIM, accounts); |
| 59 | mDeviceAccountCount = countOfType(DeviceLocalAccountTypeFactory.TYPE_DEVICE, accounts); |
| 60 | } |
| 61 | |
| 62 | public AccountDisplayInfo getAccountDisplayInfo(AccountWithDataSet account) { |
| 63 | final AccountType type = mAccountTypeManager.getAccountTypeForAccount(account); |
| 64 | final CharSequence name = shouldUseTypeLabelForName(account) |
| 65 | ? type.getDisplayLabel(mContext) |
| 66 | : account.name; |
| 67 | return new AccountDisplayInfo(account, name, type.getDisplayLabel(mContext), |
| 68 | type.getDisplayIcon(mContext), |
| 69 | DeviceLocalAccountTypeFactory.Util.isLocalAccountType(mDeviceAccountTypeFactory, |
| 70 | type.accountType)); |
| 71 | } |
| 72 | |
| 73 | public AccountDisplayInfo getAccountDisplayInfoFor(ContactListFilter filter) { |
| 74 | return getAccountDisplayInfo(filter.toAccountWithDataSet()); |
| 75 | } |
| 76 | |
| 77 | public AccountDisplayInfo getAccountDisplayInfoFor(RawContactDelta delta) { |
| 78 | final AccountWithDataSet account = new AccountWithDataSet(delta.getAccountName(), |
| 79 | delta.getAccountType(), delta.getDataSet()); |
| 80 | return getAccountDisplayInfo(account); |
| 81 | } |
| 82 | |
| 83 | public static AccountDisplayInfoFactory fromListFilters(Context context, |
| 84 | List<ContactListFilter> filters) { |
| 85 | final List<AccountWithDataSet> accounts = new ArrayList<>(filters.size()); |
| 86 | for (ContactListFilter filter : filters) { |
| 87 | accounts.add(filter.toAccountWithDataSet()); |
| 88 | } |
| 89 | return new AccountDisplayInfoFactory(context, accounts); |
| 90 | } |
| 91 | |
| 92 | public static AccountDisplayInfoFactory forAllAccounts(Context context) { |
| 93 | final AccountTypeManager accountTypeManager = AccountTypeManager.getInstance(context); |
| 94 | final List<AccountWithDataSet> accounts = accountTypeManager.getAccounts(false); |
| 95 | return new AccountDisplayInfoFactory(context, accounts); |
| 96 | } |
| 97 | |
| 98 | public static AccountDisplayInfoFactory forWritableAccounts(Context context) { |
| 99 | final AccountTypeManager accountTypeManager = AccountTypeManager.getInstance(context); |
| 100 | final List<AccountWithDataSet> accounts = accountTypeManager.getAccounts(true); |
| 101 | return new AccountDisplayInfoFactory(context, accounts); |
| 102 | } |
| 103 | |
| 104 | private boolean shouldUseTypeLabelForName(AccountWithDataSet account) { |
| 105 | final int type = mDeviceAccountTypeFactory.classifyAccount(account.type); |
| 106 | return (type == DeviceLocalAccountTypeFactory.TYPE_SIM && mSimAccountCount == 1) |
| 107 | || (type == DeviceLocalAccountTypeFactory.TYPE_DEVICE && mDeviceAccountCount == 1) |
| 108 | || account.name == null; |
| 109 | |
| 110 | } |
| 111 | |
| 112 | private int countOfType(@DeviceLocalAccountTypeFactory.LocalAccountType int type, |
| 113 | List<AccountWithDataSet> accounts) { |
| 114 | int count = 0; |
| 115 | for (AccountWithDataSet account : accounts) { |
| 116 | if (mDeviceAccountTypeFactory.classifyAccount(account.type) == type) { |
| 117 | count++; |
| 118 | } |
| 119 | } |
| 120 | return count; |
| 121 | } |
| 122 | } |