1/*
2 * Copyright (C) 2013 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.dialer.calllog;
18
19import android.content.ComponentName;
20import android.content.Context;
21import android.graphics.drawable.Drawable;
22import android.telecom.PhoneAccount;
23import android.telecom.PhoneAccountHandle;
24import android.telecom.TelecomManager;
25import android.text.TextUtils;
26
27/**
28 * Methods to help extract {@code PhoneAccount} information from database and Telecomm sources
29 */
30public class PhoneAccountUtils {
31    /**
32     * Generate account info from data in Telecomm database
33     */
34    public static PhoneAccountHandle getAccount(String componentString,
35            String accountId) {
36        if (TextUtils.isEmpty(componentString) || TextUtils.isEmpty(accountId)) {
37            return null;
38        }
39        final ComponentName componentName = ComponentName.unflattenFromString(componentString);
40        return new PhoneAccountHandle(componentName, accountId);
41    }
42
43    /**
44     * Generate account icon from data in Telecomm database
45     */
46    public static Drawable getAccountIcon(Context context, PhoneAccountHandle phoneAccount) {
47        final PhoneAccount account = getAccountOrNull(context, phoneAccount);
48        if (account == null) {
49            return null;
50        }
51        return account.getIcon(context);
52    }
53
54    /**
55     * Generate account label from data in Telecomm database
56     */
57    public static String getAccountLabel(Context context, PhoneAccountHandle phoneAccount) {
58        final PhoneAccount account = getAccountOrNull(context, phoneAccount);
59        if (account == null) {
60            return null;
61        }
62        return account.getLabel().toString();
63    }
64
65    /**
66     * Retrieve the account metadata, but if the account does not exist or the device has only a
67     * single registered and enabled account, return null.
68     */
69    private static PhoneAccount getAccountOrNull(Context context,
70            PhoneAccountHandle phoneAccount) {
71        final TelecomManager telecomManager =
72                (TelecomManager) context.getSystemService(Context.TELECOM_SERVICE);
73        final PhoneAccount account = telecomManager.getPhoneAccount(phoneAccount);
74        if (account == null || !telecomManager.hasMultipleCallCapableAccounts()) {
75            return null;
76        }
77        return account;
78    }
79
80}
81