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