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