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  */
16 package com.android.contacts.model.account;
17 
18 import android.content.Context;
19 
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;
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 
AccountDisplayInfoFactory(Context context, List<AccountWithDataSet> accounts)46     public AccountDisplayInfoFactory(Context context, List<AccountWithDataSet> accounts) {
47         this(context, AccountTypeManager.getInstance(context),
48                 ObjectFactory.getDeviceLocalAccountTypeFactory(context), accounts);
49     }
50 
AccountDisplayInfoFactory(Context context, AccountTypeManager accountTypeManager, DeviceLocalAccountTypeFactory deviceAccountTypeFactory, List<AccountWithDataSet> accounts)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 
getAccountDisplayInfo(AccountWithDataSet account)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 
getAccountDisplayInfoFor(ContactListFilter filter)73     public AccountDisplayInfo getAccountDisplayInfoFor(ContactListFilter filter) {
74         return getAccountDisplayInfo(filter.toAccountWithDataSet());
75     }
76 
getAccountDisplayInfoFor(RawContactDelta delta)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 
fromListFilters(Context context, List<ContactListFilter> filters)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 
shouldUseTypeLabelForName(AccountWithDataSet account)92     private boolean shouldUseTypeLabelForName(AccountWithDataSet account) {
93         final int type = mDeviceAccountTypeFactory.classifyAccount(account.type);
94         return (type == DeviceLocalAccountTypeFactory.TYPE_SIM && mSimAccountCount == 1)
95                 || (type == DeviceLocalAccountTypeFactory.TYPE_DEVICE && mDeviceAccountCount == 1)
96                 || account.name == null;
97 
98     }
99 
countOfType(@eviceLocalAccountTypeFactory.LocalAccountType int type, List<AccountWithDataSet> accounts)100     private int countOfType(@DeviceLocalAccountTypeFactory.LocalAccountType int type,
101             List<AccountWithDataSet> accounts) {
102         int count = 0;
103         for (AccountWithDataSet account : accounts) {
104             if (mDeviceAccountTypeFactory.classifyAccount(account.type) == type) {
105                 count++;
106             }
107         }
108         return count;
109     }
110 }
111