blob: aad1689137825eaeed3abe09af8df75502bc5312 [file] [log] [blame]
Marcus Hagerottfac695a2016-08-24 17:02:40 -07001/*
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 */
16package com.android.contacts.common.model.account;
17
18import android.content.Context;
19
20import com.android.contacts.common.list.ContactListFilter;
21import com.android.contacts.common.model.AccountTypeManager;
22import com.android.contacts.common.model.RawContactDelta;
23import com.android.contacts.common.util.DeviceLocalAccountTypeFactory;
24import com.android.contactsbind.ObjectFactory;
25
26import java.util.ArrayList;
27import 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 */
36public 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}