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 17 package com.android.dialer.calllog; 18 19 import android.content.ComponentName; 20 import android.content.Context; 21 import android.support.annotation.Nullable; 22 import android.telecom.PhoneAccount; 23 import android.telecom.PhoneAccountHandle; 24 import android.text.TextUtils; 25 26 import com.android.contacts.common.compat.CompatUtils; 27 import com.android.dialer.util.TelecomUtil; 28 29 import java.util.ArrayList; 30 import java.util.List; 31 32 /** 33 * Methods to help extract {@code PhoneAccount} information from database and Telecomm sources. 34 */ 35 public class PhoneAccountUtils { 36 /** 37 * Return a list of phone accounts that are subscription/SIM accounts. 38 */ 39 public static List<PhoneAccountHandle> getSubscriptionPhoneAccounts(Context context) { 40 List<PhoneAccountHandle> subscriptionAccountHandles = new ArrayList<PhoneAccountHandle>(); 41 final List<PhoneAccountHandle> accountHandles = 42 TelecomUtil.getCallCapablePhoneAccounts(context); 43 for (PhoneAccountHandle accountHandle : accountHandles) { 44 PhoneAccount account = TelecomUtil.getPhoneAccount(context, accountHandle); 45 if (account.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)) { 46 subscriptionAccountHandles.add(accountHandle); 47 } 48 } 49 return subscriptionAccountHandles; 50 } 51 52 /** 53 * Compose PhoneAccount object from component name and account id. 54 */ 55 @Nullable 56 public static PhoneAccountHandle getAccount(@Nullable String componentString, 57 @Nullable String accountId) { 58 if (TextUtils.isEmpty(componentString) || TextUtils.isEmpty(accountId)) { 59 return null; 60 } 61 final ComponentName componentName = ComponentName.unflattenFromString(componentString); 62 if (componentName == null) { 63 return null; 64 } 65 return new PhoneAccountHandle(componentName, accountId); 66 } 67 68 /** 69 * Extract account label from PhoneAccount object. 70 */ 71 @Nullable 72 public static String getAccountLabel(Context context, 73 @Nullable PhoneAccountHandle accountHandle) { 74 PhoneAccount account = getAccountOrNull(context, accountHandle); 75 if (account != null && account.getLabel() != null) { 76 return account.getLabel().toString(); 77 } 78 return null; 79 } 80 81 /** 82 * Extract account color from PhoneAccount object. 83 */ 84 public static int getAccountColor(Context context, @Nullable PhoneAccountHandle accountHandle) { 85 final PhoneAccount account = TelecomUtil.getPhoneAccount(context, accountHandle); 86 87 // For single-sim devices the PhoneAccount will be NO_HIGHLIGHT_COLOR by default, so it is 88 // safe to always use the account highlight color. 89 return account == null ? PhoneAccount.NO_HIGHLIGHT_COLOR : account.getHighlightColor(); 90 } 91 92 /** 93 * Determine whether a phone account supports call subjects. 94 * 95 * @return {@code true} if call subjects are supported, {@code false} otherwise. 96 */ 97 public static boolean getAccountSupportsCallSubject(Context context, 98 @Nullable PhoneAccountHandle accountHandle) { 99 final PhoneAccount account = TelecomUtil.getPhoneAccount(context, accountHandle); 100 101 return account == null ? false : 102 account.hasCapabilities(PhoneAccount.CAPABILITY_CALL_SUBJECT); 103 } 104 105 /** 106 * Retrieve the account metadata, but if the account does not exist or the device has only a 107 * single registered and enabled account, return null. 108 */ 109 @Nullable 110 private static PhoneAccount getAccountOrNull(Context context, 111 @Nullable PhoneAccountHandle accountHandle) { 112 if (TelecomUtil.getCallCapablePhoneAccounts(context).size() <= 1) { 113 return null; 114 } 115 return TelecomUtil.getPhoneAccount(context, accountHandle); 116 } 117 } 118