1 /*
2  * Copyright (C) 2015 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 package com.android.contacts.common.compat;
17 
18 import android.content.Context;
19 import android.graphics.drawable.Drawable;
20 import android.graphics.drawable.Icon;
21 import android.support.annotation.Nullable;
22 import android.telecom.PhoneAccount;
23 import android.util.Log;
24 
25 /**
26  * Compatiblity class for {@link android.telecom.PhoneAccount}
27  */
28 public class PhoneAccountCompat {
29 
30     private static final String TAG = PhoneAccountCompat.class.getSimpleName();
31 
32     /**
33      * Gets the {@link Icon} associated with the given {@link PhoneAccount}
34      *
35      * @param phoneAccount the PhoneAccount from which to retrieve the Icon
36      * @return the Icon, or null
37      */
38     @Nullable
getIcon(@ullable PhoneAccount phoneAccount)39     public static Icon getIcon(@Nullable PhoneAccount phoneAccount) {
40         if (phoneAccount == null) {
41             return null;
42         }
43 
44         if (CompatUtils.isMarshmallowCompatible()) {
45             return phoneAccount.getIcon();
46         }
47 
48         return null;
49     }
50 
51     /**
52      * Builds and returns an icon {@code Drawable} to represent this {@code PhoneAccount} in a user
53      * interface.
54      *
55      * @param phoneAccount the PhoneAccount from which to build the icon.
56      * @param context A {@code Context} to use for loading Drawables.
57      *
58      * @return An icon for this PhoneAccount, or null
59      */
60     @Nullable
createIconDrawable(@ullable PhoneAccount phoneAccount, @Nullable Context context)61     public static Drawable createIconDrawable(@Nullable PhoneAccount phoneAccount,
62             @Nullable Context context) {
63         if (phoneAccount == null || context == null) {
64             return null;
65         }
66 
67         if (CompatUtils.isMarshmallowCompatible()) {
68             return createIconDrawableMarshmallow(phoneAccount, context);
69         }
70 
71         if (CompatUtils.isLollipopMr1Compatible()) {
72             return createIconDrawableLollipopMr1(phoneAccount, context);
73         }
74         return null;
75     }
76 
77     @Nullable
createIconDrawableMarshmallow(PhoneAccount phoneAccount, Context context)78     private static Drawable createIconDrawableMarshmallow(PhoneAccount phoneAccount,
79             Context context) {
80         Icon accountIcon = getIcon(phoneAccount);
81         if (accountIcon == null) {
82             return null;
83         }
84         return accountIcon.loadDrawable(context);
85     }
86 
87     @Nullable
createIconDrawableLollipopMr1(PhoneAccount phoneAccount, Context context)88     private static Drawable createIconDrawableLollipopMr1(PhoneAccount phoneAccount,
89             Context context) {
90         try {
91             return (Drawable) PhoneAccount.class.getMethod("createIconDrawable", Context.class)
92                     .invoke(phoneAccount, context);
93         } catch (ReflectiveOperationException e) {
94             return null;
95         } catch (Throwable t) {
96             Log.e(TAG, "Unexpected exception when attempting to call "
97                     + "android.telecom.PhoneAccount#createIconDrawable", t);
98             return null;
99         }
100     }
101 }
102