1 /*
2  * Copyright (C) 2019 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.car.dialer.notification;
18 
19 import android.content.Context;
20 import android.graphics.drawable.Icon;
21 import android.net.Uri;
22 
23 import androidx.annotation.Nullable;
24 import androidx.core.graphics.drawable.RoundedBitmapDrawable;
25 import androidx.core.graphics.drawable.RoundedBitmapDrawableFactory;
26 import androidx.core.util.Pair;
27 
28 import com.android.car.dialer.R;
29 import com.android.car.telephony.common.TelecomUtils;
30 
31 import java.io.FileNotFoundException;
32 import java.io.InputStream;
33 import java.util.concurrent.CompletableFuture;
34 
35 /** Util class that shares common functionality for notifications. */
36 final class NotificationUtils {
NotificationUtils()37     private NotificationUtils() {
38     }
39 
getDisplayNameAndRoundedAvatar(Context context, String number)40     static CompletableFuture<Pair<String, Icon>> getDisplayNameAndRoundedAvatar(Context context,
41             String number) {
42         return TelecomUtils.getPhoneNumberInfo(context, number)
43                 .thenApplyAsync((info) -> {
44                     int size = context.getResources()
45                             .getDimensionPixelSize(R.dimen.avatar_icon_size);
46                     Icon largeIcon = loadContactAvatar(context, info.getAvatarUri(), size);
47                     if (largeIcon == null) {
48                         float cornerRadiusPercent = context.getResources()
49                                 .getFloat(R.dimen.contact_avatar_corner_radius_percent);
50                         largeIcon = TelecomUtils.createLetterTile(context, info.getInitials(),
51                                 /* identifier */ info.getDisplayName(), size, cornerRadiusPercent);
52                     }
53 
54                     return new Pair<>(info.getDisplayName(), largeIcon);
55                 });
56     }
57 
loadContactAvatar(Context context, @Nullable Uri avatarUri, int avatarSize)58     static Icon loadContactAvatar(Context context, @Nullable Uri avatarUri, int avatarSize) {
59         if (avatarUri == null) {
60             return null;
61         }
62 
63         try {
64             InputStream input = context.getContentResolver().openInputStream(avatarUri);
65             if (input == null) {
66                 return null;
67             }
68             RoundedBitmapDrawable roundedBitmapDrawable = RoundedBitmapDrawableFactory.create(
69                     context.getResources(), input);
70             float cornerRadiusPercent = context.getResources()
71                     .getFloat(R.dimen.contact_avatar_corner_radius_percent);
72             return TelecomUtils
73                 .createFromRoundedBitmapDrawable(roundedBitmapDrawable, avatarSize,
74                     cornerRadiusPercent);
75         } catch (FileNotFoundException e) {
76             // No-op
77         }
78         return null;
79     }
80 }
81