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.calllogutils;
18
19import android.content.Context;
20import android.support.annotation.Nullable;
21import android.telecom.PhoneAccount;
22import android.telecom.PhoneAccountHandle;
23import com.android.dialer.telecom.TelecomUtil;
24
25/** Methods to help extract {@code PhoneAccount} information from database and Telecomm sources. */
26public class PhoneAccountUtils {
27
28  /** Extract account label from PhoneAccount object. */
29  @Nullable
30  public static String getAccountLabel(
31      Context context, @Nullable PhoneAccountHandle accountHandle) {
32    PhoneAccount account = getAccountOrNull(context, accountHandle);
33    if (account != null && account.getLabel() != null) {
34      return account.getLabel().toString();
35    }
36    return null;
37  }
38
39  /** Extract account color from PhoneAccount object. */
40  public static int getAccountColor(Context context, @Nullable PhoneAccountHandle accountHandle) {
41    final PhoneAccount account = TelecomUtil.getPhoneAccount(context, accountHandle);
42
43    // For single-sim devices the PhoneAccount will be NO_HIGHLIGHT_COLOR by default, so it is
44    // safe to always use the account highlight color.
45    return account == null ? PhoneAccount.NO_HIGHLIGHT_COLOR : account.getHighlightColor();
46  }
47
48  /**
49   * Determine whether a phone account supports call subjects.
50   *
51   * @return {@code true} if call subjects are supported, {@code false} otherwise.
52   */
53  public static boolean getAccountSupportsCallSubject(
54      Context context, @Nullable PhoneAccountHandle accountHandle) {
55    final PhoneAccount account = TelecomUtil.getPhoneAccount(context, accountHandle);
56
57    return account != null && account.hasCapabilities(PhoneAccount.CAPABILITY_CALL_SUBJECT);
58  }
59
60  /**
61   * Retrieve the account metadata, but if the account does not exist or the device has only a
62   * single registered and enabled account, return null.
63   */
64  @Nullable
65  private static PhoneAccount getAccountOrNull(
66      Context context, @Nullable PhoneAccountHandle accountHandle) {
67    if (TelecomUtil.getCallCapablePhoneAccounts(context).size() <= 1) {
68      return null;
69    }
70    return TelecomUtil.getPhoneAccount(context, accountHandle);
71  }
72}
73