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