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