blob: 73fa92ada38ff8a785a91f0115758069206a8d92 [file] [log] [blame]
Chihhang Chuanga2181142018-06-05 15:29:06 +08001/*
2 * Copyright (C) 2018 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
17package com.android.phone;
18
19import android.annotation.Nullable;
20import android.content.Context;
21import android.content.Intent;
22import android.content.pm.ResolveInfo;
23import android.graphics.Bitmap;
24import android.graphics.drawable.Drawable;
25import android.os.UserHandle;
26import android.os.UserManager;
27import android.telephony.TelephonyManager;
Billy Chifef11c42018-06-15 19:00:15 +080028import android.text.TextUtils;
Chihhang Chuanga2181142018-06-05 15:29:06 +080029import android.util.AttributeSet;
30import android.view.View;
Chihhang Chuang0808a092018-07-02 11:08:50 +080031import android.widget.FrameLayout;
Chihhang Chuanga2181142018-06-05 15:29:06 +080032import android.widget.ImageView;
Chihhang Chuanga2181142018-06-05 15:29:06 +080033import android.widget.TextView;
34
35import androidx.core.graphics.drawable.RoundedBitmapDrawable;
36import androidx.core.graphics.drawable.RoundedBitmapDrawableFactory;
37
38import com.android.internal.util.UserIcons;
39
40import java.util.List;
41
42/**
43 * EmergencyInfoGroup display user icon and user name. And it is an entry point to
44 * Emergency Information.
45 */
Chihhang Chuang0808a092018-07-02 11:08:50 +080046public class EmergencyInfoGroup extends FrameLayout {
Chihhang Chuanga2181142018-06-05 15:29:06 +080047 private ImageView mEmergencyInfoImage;
Chihhang Chuang0808a092018-07-02 11:08:50 +080048 private TextView mEmergencyInfoName;
Chihhang Chuanga2181142018-06-05 15:29:06 +080049 private View mEmergencyInfoButton;
50
51 public EmergencyInfoGroup(Context context, @Nullable AttributeSet attrs) {
52 super(context, attrs);
53 }
54
55 @Override
56 protected void onFinishInflate() {
57 super.onFinishInflate();
Chihhang Chuanga2181142018-06-05 15:29:06 +080058 mEmergencyInfoButton = findViewById(R.id.emergency_info_button);
59 mEmergencyInfoImage = (ImageView) findViewById(R.id.emergency_info_image);
Chihhang Chuang0808a092018-07-02 11:08:50 +080060 mEmergencyInfoName = (TextView) findViewById(R.id.emergency_info_name);
Chihhang Chuanga2181142018-06-05 15:29:06 +080061 }
62
63 @Override
64 protected void onWindowVisibilityChanged(int visibility) {
65 super.onWindowVisibilityChanged(visibility);
66 if (visibility == View.VISIBLE) {
67 setupButtonInfo();
68 }
69 }
70
71 private void setupButtonInfo() {
72 List<ResolveInfo> infos;
73
74 if (TelephonyManager.EMERGENCY_ASSISTANCE_ENABLED) {
75 infos = EmergencyAssistanceHelper.resolveAssistPackageAndQueryActivities(getContext());
76 } else {
77 infos = null;
78 }
79
80 boolean visible = false;
81
82 if (infos != null && infos.size() > 0) {
83 final String packageName = infos.get(0).activityInfo.packageName;
84 final Intent intent = new Intent(TelephonyManager.ACTION_EMERGENCY_ASSISTANCE)
85 .setPackage(packageName);
86 mEmergencyInfoButton.setTag(R.id.tag_intent, intent);
87 mEmergencyInfoImage.setImageDrawable(getCircularUserIcon());
Chihhang Chuang0808a092018-07-02 11:08:50 +080088
Chihhang Chuanga2181142018-06-05 15:29:06 +080089 visible = true;
90 }
91
92 setVisibility(visible ? View.VISIBLE : View.GONE);
93 }
94
Chihhang Chuang0808a092018-07-02 11:08:50 +080095 /**
96 * Get user icon.
97 *
98 * @return user icon, or anonymous avatar if user do not set photo.
99 */
Chihhang Chuanga2181142018-06-05 15:29:06 +0800100 private Drawable getCircularUserIcon() {
Chihhang Chuang0808a092018-07-02 11:08:50 +0800101 final int userId = UserHandle.getCallingUserId();
102
Chihhang Chuanga2181142018-06-05 15:29:06 +0800103 final UserManager userManager = (UserManager) getContext().getSystemService(
104 Context.USER_SERVICE);
Chihhang Chuang0808a092018-07-02 11:08:50 +0800105
106 // get user icon.
107 Bitmap bitmapUserIcon = userManager.getUserIcon(userId);
Chihhang Chuanga2181142018-06-05 15:29:06 +0800108
109 if (bitmapUserIcon == null) {
Chihhang Chuang0808a092018-07-02 11:08:50 +0800110 // use anonymous avatar.
111 return getContext().getDrawable(R.drawable.logo_avatar_anonymous_120);
Chihhang Chuanga2181142018-06-05 15:29:06 +0800112 }
113
Chihhang Chuang0808a092018-07-02 11:08:50 +0800114 // get default user icon.
115 Drawable drawableDefaultUserIcon = UserIcons.getDefaultUserIcon(
116 getContext().getResources(), userId, false);
117 Bitmap bitmapDefaultUserIcon = UserIcons.convertToBitmap(drawableDefaultUserIcon);
118
119 // User icon is default icon that means user do not set photo, replacing default icon
120 // with anonymous avatar on emergency info button.
121 if (bitmapUserIcon.sameAs(bitmapDefaultUserIcon)) {
122 return getContext().getDrawable(R.drawable.logo_avatar_anonymous_120);
123 }
124
125 // set user icon circular.
Chihhang Chuanga2181142018-06-05 15:29:06 +0800126 RoundedBitmapDrawable drawableUserIcon = RoundedBitmapDrawableFactory.create(
127 getContext().getResources(), bitmapUserIcon);
128 drawableUserIcon.setCircular(true);
129
130 return drawableUserIcon;
131 }
Billy Chifef11c42018-06-15 19:00:15 +0800132
133 void updateEmergencyInfo(String emergencyInfoName) {
Billy Chifef11c42018-06-15 19:00:15 +0800134 if (TextUtils.isEmpty(emergencyInfoName)) {
Chihhang Chuang0808a092018-07-02 11:08:50 +0800135 emergencyInfoName = getContext().getString(R.string.emergency_information_owner_hint);
Billy Chifef11c42018-06-15 19:00:15 +0800136 }
Chihhang Chuang0808a092018-07-02 11:08:50 +0800137 mEmergencyInfoName.setText(emergencyInfoName);
138
139 final String infoNameDescription = getContext().getString(
140 R.string.emergency_information_owner_content_description, emergencyInfoName);
141 mEmergencyInfoName.setContentDescription(infoNameDescription);
Billy Chifef11c42018-06-15 19:00:15 +0800142 }
143}