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.graphics.drawable.Drawable;
     22 import android.telecom.PhoneAccount;
     23 import android.telecom.PhoneAccountHandle;
     24 import android.telecom.TelecomManager;
     25 import android.text.TextUtils;
     26 
     27 /**
     28  * Methods to help extract {@code PhoneAccount} information from database and Telecomm sources
     29  */
     30 public 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