1/*
2 * Copyright (C) 2009 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
17package com.android.contacts.model.account;
18
19import android.accounts.AuthenticatorDescription;
20import android.content.Context;
21import android.graphics.PorterDuff;
22import android.graphics.drawable.Drawable;
23import android.support.v4.content.ContextCompat;
24import android.support.v4.content.res.ResourcesCompat;
25
26import com.android.contacts.R;
27import com.android.contacts.model.dataitem.DataKind;
28import com.android.contactsbind.FeedbackHelper;
29
30public class FallbackAccountType extends BaseAccountType {
31    private static final String TAG = "FallbackAccountType";
32
33    private FallbackAccountType(Context context, String resPackageName) {
34        this.accountType = null;
35        this.dataSet = null;
36        this.titleRes = R.string.account_phone;
37        this.iconRes = R.drawable.quantum_ic_smartphone_vd_theme_24;
38
39        // Note those are only set for unit tests.
40        this.resourcePackageName = resPackageName;
41        this.syncAdapterPackageName = resPackageName;
42
43        try {
44            addDataKindStructuredName(context);
45            addDataKindName(context);
46            addDataKindPhoneticName(context);
47            addDataKindNickname(context);
48            addDataKindPhone(context);
49            addDataKindEmail(context);
50            addDataKindStructuredPostal(context);
51            addDataKindIm(context);
52            addDataKindOrganization(context);
53            addDataKindPhoto(context);
54            addDataKindNote(context);
55            addDataKindWebsite(context);
56            addDataKindSipAddress(context);
57            addDataKindGroupMembership(context);
58
59            mIsInitialized = true;
60        } catch (DefinitionException e) {
61            FeedbackHelper.sendFeedback(context, TAG, "Failed to build fallback account type", e);
62        }
63    }
64
65    @Override
66    public Drawable getDisplayIcon(Context context) {
67        final Drawable icon = ResourcesCompat.getDrawable(context.getResources(), iconRes, null);
68        icon.mutate().setColorFilter(ContextCompat.getColor(context,
69                R.color.actionbar_icon_color_grey), PorterDuff.Mode.SRC_ATOP);
70        return icon;
71    }
72
73    public FallbackAccountType(Context context) {
74        this(context, null);
75    }
76
77    /**
78     * Used to compare with an {@link ExternalAccountType} built from a test contacts.xml.
79     * In order to build {@link DataKind}s with the same resource package name,
80     * {@code resPackageName} is injectable.
81     */
82    static AccountType createWithPackageNameForTest(Context context, String resPackageName) {
83        return new FallbackAccountType(context, resPackageName);
84    }
85
86    @Override
87    public void initializeFieldsFromAuthenticator(AuthenticatorDescription authenticator) {
88        // Do nothing. For "Device" accounts we want to just display them using our own strings
89        // and icons.
90    }
91
92    @Override
93    public boolean areContactsWritable() {
94        return true;
95    }
96
97
98    /**
99     * {@inheritDoc}
100     *
101     * <p>This is overriden because the base class validates that the account.type matches
102     * {@link #accountType} but for the fallback case we want to be more permissive</p>
103     */
104    @Override
105    public AccountInfo wrapAccount(Context context, AccountWithDataSet account) {
106        return new AccountInfo(
107                new AccountDisplayInfo(account, account.name,
108                        getDisplayLabel(context), getDisplayIcon(context), false), this);
109    }
110}
111