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.graphics.drawable.Drawable;
19 
20 import com.google.common.base.Function;
21 import com.google.common.base.Preconditions;
22 import com.google.common.collect.Lists;
23 
24 import java.util.Collections;
25 import java.util.Comparator;
26 import java.util.List;
27 import java.util.Objects;
28 
29 /**
30  * Holds an {@link AccountWithDataSet} and the corresponding {@link AccountType} for an account.
31  */
32 public class AccountInfo {
33 
34     private final AccountDisplayInfo mDisplayInfo;
35     private final AccountType mType;
36 
AccountInfo(AccountDisplayInfo displayInfo, AccountType type)37     public AccountInfo(AccountDisplayInfo displayInfo, AccountType type) {
38         this.mDisplayInfo = displayInfo;
39         this.mType = type;
40     }
41 
getType()42     public AccountType getType() {
43         return mType;
44     }
45 
getAccount()46     public AccountWithDataSet getAccount() {
47         return mDisplayInfo.getSource();
48     }
49 
50     /**
51      * Returns the displayable account name label for the account
52      */
getNameLabel()53     public CharSequence getNameLabel() {
54         return mDisplayInfo.getNameLabel();
55     }
56 
57     /**
58      * Returns the displayable account type label for the account
59      */
getTypeLabel()60     public CharSequence getTypeLabel() {
61         return mDisplayInfo.getTypeLabel();
62     }
63 
64     /**
65      * Returns the icon for the account type
66      */
getIcon()67     public Drawable getIcon() {
68         return mDisplayInfo.getIcon();
69     }
70 
hasDistinctName()71     public boolean hasDistinctName() {
72         return mDisplayInfo.hasDistinctName();
73     }
74 
isDeviceAccount()75     public boolean isDeviceAccount() {
76         return mDisplayInfo.isDeviceAccount();
77     }
78 
hasGoogleAccountType()79     public boolean hasGoogleAccountType() {
80         return mDisplayInfo.hasGoogleAccountType();
81     }
82 
sameAccount(AccountInfo other)83     public boolean sameAccount(AccountInfo other) {
84         return sameAccount(other.getAccount());
85     }
86 
sameAccount(AccountWithDataSet other)87     public boolean sameAccount(AccountWithDataSet other) {
88         return Objects.equals(getAccount(), other);
89     }
90 
91     /**
92      * Returns whether accounts contains an account that is the same as account
93      *
94      * <p>This does not use equality rather checks whether the source account ({@link #getAccount()}
95      * is the same</p>
96      */
contains(List<AccountInfo> accounts, AccountInfo account)97     public static boolean contains(List<AccountInfo> accounts, AccountInfo account) {
98         return contains(accounts, account.getAccount());
99     }
100 
101     /**
102      * Returns whether accounts contains an account that is the same as account
103      *
104      * <p>This does not use equality rather checks whether the source account ({@link #getAccount()}
105      * is the same</p>
106      */
contains(List<AccountInfo> accounts, AccountWithDataSet account)107     public static boolean contains(List<AccountInfo> accounts, AccountWithDataSet account) {
108         return getAccount(accounts, account) != null;
109     }
110 
111     /**
112      * Returns the AccountInfo from the list that has the specified account as it's source account
113      */
getAccount(List<AccountInfo> accounts, AccountWithDataSet account)114     public static AccountInfo getAccount(List<AccountInfo> accounts, AccountWithDataSet account) {
115         Preconditions.checkNotNull(accounts);
116 
117         for (AccountInfo info : accounts) {
118             if (info.sameAccount(account)) {
119                 return info;
120             }
121         }
122         return null;
123     }
124 
125     /**
126      * Sorts the accounts using the same ordering as {@link AccountComparator}
127      */
sortAccounts(AccountWithDataSet defaultAccount, List<AccountInfo> accounts)128     public static void sortAccounts(AccountWithDataSet defaultAccount, List<AccountInfo> accounts) {
129         Collections.sort(accounts, sourceComparator(defaultAccount));
130     }
131 
132     /**
133      * Gets a list of the AccountWithDataSet for accounts
134      */
extractAccounts(List<AccountInfo> accounts)135     public static List<AccountWithDataSet> extractAccounts(List<AccountInfo> accounts) {
136         return Lists.transform(accounts, ACCOUNT_EXTRACTOR);
137     }
138 
sourceComparator(AccountWithDataSet defaultAccount)139     private static Comparator<AccountInfo> sourceComparator(AccountWithDataSet defaultAccount) {
140         final AccountComparator accountComparator = new AccountComparator(defaultAccount);
141         return new Comparator<AccountInfo>() {
142             @Override
143             public int compare(AccountInfo o1, AccountInfo o2) {
144                 return accountComparator.compare(o1.getAccount(), o2.getAccount());
145             }
146         };
147     }
148 
149     public static final Function<AccountInfo, AccountWithDataSet> ACCOUNT_EXTRACTOR =
150             new Function<AccountInfo, AccountWithDataSet>() {
151                 @Override
152                 public AccountWithDataSet apply(AccountInfo from) {
153                     return from.getAccount();
154                 }
155             };
156 }
157