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 import android.graphics.drawable.Drawable;
20 import androidx.annotation.StringRes;
21 import android.text.TextUtils;
22 
23 /**
24  * Wrapper around AccountWithDataSet that contains user-friendly labels and an icon.
25  *
26  * The raw values for name and type in AccountWithDataSet are not always (or even usually)
27  * appropriate for direct display to the user.
28  */
29 public class AccountDisplayInfo {
30     private final AccountWithDataSet mSource;
31 
32     private final CharSequence mName;
33     private final CharSequence mType;
34     private final Drawable mIcon;
35 
36     private final boolean mIsDeviceAccount;
37 
AccountDisplayInfo(AccountWithDataSet account, CharSequence name, CharSequence type, Drawable icon, boolean isDeviceAccount)38     public AccountDisplayInfo(AccountWithDataSet account, CharSequence name, CharSequence type,
39             Drawable icon, boolean isDeviceAccount) {
40         mSource = account;
41         mName = name;
42         mType = type;
43         mIcon = icon;
44         mIsDeviceAccount = isDeviceAccount;
45     }
46 
getSource()47     public AccountWithDataSet getSource() {
48         return mSource;
49     }
50 
getNameLabel()51     public CharSequence getNameLabel() {
52         return mName;
53     }
54 
getTypeLabel()55     public CharSequence getTypeLabel() {
56         return mType;
57     }
58 
getIcon()59     public Drawable getIcon() {
60         return mIcon;
61     }
62 
hasGoogleAccountType()63     public boolean hasGoogleAccountType() {
64         return GoogleAccountType.ACCOUNT_TYPE.equals(mSource.type);
65     }
66 
isGoogleAccount()67     public boolean isGoogleAccount() {
68         return GoogleAccountType.ACCOUNT_TYPE.equals(mSource.type) && mSource.dataSet == null;
69     }
70 
isDeviceAccount()71     public boolean isDeviceAccount() {
72         return mIsDeviceAccount;
73     }
74 
hasDistinctName()75     public boolean hasDistinctName() {
76         return !TextUtils.equals(mName, mType);
77     }
78 
withName(CharSequence name)79     public AccountDisplayInfo withName(CharSequence name) {
80         return withNameAndType(name, mType);
81     }
82 
withType(CharSequence type)83     public AccountDisplayInfo withType(CharSequence type) {
84         return withNameAndType(mName, type);
85     }
86 
withNameAndType(CharSequence name, CharSequence type)87     public AccountDisplayInfo withNameAndType(CharSequence name, CharSequence type) {
88         return new AccountDisplayInfo(mSource, name, type, mIcon, mIsDeviceAccount);
89     }
90 
formatted(Context context, @StringRes int nameFormat, @StringRes int typeFormat)91     public AccountDisplayInfo formatted(Context context, @StringRes int nameFormat,
92             @StringRes int typeFormat) {
93         return new AccountDisplayInfo(mSource, context.getString(nameFormat, mName),
94                 context.getString(typeFormat, mType), mIcon, mIsDeviceAccount);
95     }
96 
withFormattedName(Context context, @StringRes int nameFormat)97     public AccountDisplayInfo withFormattedName(Context context, @StringRes int nameFormat) {
98         return withName(context.getString(nameFormat, mName));
99     }
100 }
101