blob: 576f3fcc09f5c08dcb2083347777358d37412b5d [file] [log] [blame]
Chihhang Chuangf264cfb2018-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 Chi17ec2282018-06-15 19:00:15 +080028import android.text.TextUtils;
Chihhang Chuangf264cfb2018-06-05 15:29:06 +080029import android.util.AttributeSet;
30import android.view.View;
Chihhang Chuangf8d33002018-07-02 11:08:50 +080031import android.widget.FrameLayout;
Chihhang Chuangf264cfb2018-06-05 15:29:06 +080032import android.widget.ImageView;
Wesley.CW Wang7fce0e82018-07-17 18:05:09 +080033import android.widget.LinearLayout;
Chihhang Chuangf264cfb2018-06-05 15:29:06 +080034import android.widget.TextView;
35
36import androidx.core.graphics.drawable.RoundedBitmapDrawable;
37import androidx.core.graphics.drawable.RoundedBitmapDrawableFactory;
38
39import com.android.internal.util.UserIcons;
40
41import java.util.List;
42
43/**
44 * EmergencyInfoGroup display user icon and user name. And it is an entry point to
45 * Emergency Information.
46 */
Chihhang Chuangf8d33002018-07-02 11:08:50 +080047public class EmergencyInfoGroup extends FrameLayout {
Chihhang Chuangf264cfb2018-06-05 15:29:06 +080048 private ImageView mEmergencyInfoImage;
Chihhang Chuangf8d33002018-07-02 11:08:50 +080049 private TextView mEmergencyInfoName;
Wesley.CW Wang7fce0e82018-07-17 18:05:09 +080050 private TextView mEmergencyInfoHint;
Chihhang Chuangf264cfb2018-06-05 15:29:06 +080051 private View mEmergencyInfoButton;
52
53 public EmergencyInfoGroup(Context context, @Nullable AttributeSet attrs) {
54 super(context, attrs);
55 }
56
57 @Override
58 protected void onFinishInflate() {
59 super.onFinishInflate();
Chihhang Chuangf264cfb2018-06-05 15:29:06 +080060 mEmergencyInfoButton = findViewById(R.id.emergency_info_button);
61 mEmergencyInfoImage = (ImageView) findViewById(R.id.emergency_info_image);
Chihhang Chuangf8d33002018-07-02 11:08:50 +080062 mEmergencyInfoName = (TextView) findViewById(R.id.emergency_info_name);
Wesley.CW Wang7fce0e82018-07-17 18:05:09 +080063 mEmergencyInfoHint = (TextView) findViewById(R.id.emergency_info_hint);
Chihhang Chuangf264cfb2018-06-05 15:29:06 +080064 }
65
66 @Override
67 protected void onWindowVisibilityChanged(int visibility) {
68 super.onWindowVisibilityChanged(visibility);
69 if (visibility == View.VISIBLE) {
70 setupButtonInfo();
71 }
72 }
73
Wesley.CW Wang7fce0e82018-07-17 18:05:09 +080074 @Override
75 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
76 super.onLayout(changed, left, top, right, bottom);
77 updateLayoutHeight();
78 }
79
Chihhang Chuangf264cfb2018-06-05 15:29:06 +080080 private void setupButtonInfo() {
81 List<ResolveInfo> infos;
82
83 if (TelephonyManager.EMERGENCY_ASSISTANCE_ENABLED) {
84 infos = EmergencyAssistanceHelper.resolveAssistPackageAndQueryActivities(getContext());
85 } else {
86 infos = null;
87 }
88
89 boolean visible = false;
90
91 if (infos != null && infos.size() > 0) {
92 final String packageName = infos.get(0).activityInfo.packageName;
93 final Intent intent = new Intent(TelephonyManager.ACTION_EMERGENCY_ASSISTANCE)
94 .setPackage(packageName);
95 mEmergencyInfoButton.setTag(R.id.tag_intent, intent);
96 mEmergencyInfoImage.setImageDrawable(getCircularUserIcon());
Chihhang Chuangf8d33002018-07-02 11:08:50 +080097
Chihhang Chuangf264cfb2018-06-05 15:29:06 +080098 visible = true;
99 }
100
101 setVisibility(visible ? View.VISIBLE : View.GONE);
102 }
103
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800104 /**
105 * Get user icon.
106 *
107 * @return user icon, or anonymous avatar if user do not set photo.
108 */
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800109 private Drawable getCircularUserIcon() {
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800110 final int userId = UserHandle.getCallingUserId();
111
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800112 final UserManager userManager = (UserManager) getContext().getSystemService(
113 Context.USER_SERVICE);
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800114
115 // get user icon.
116 Bitmap bitmapUserIcon = userManager.getUserIcon(userId);
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800117
118 if (bitmapUserIcon == null) {
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800119 // use anonymous avatar.
120 return getContext().getDrawable(R.drawable.logo_avatar_anonymous_120);
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800121 }
122
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800123 // get default user icon.
124 Drawable drawableDefaultUserIcon = UserIcons.getDefaultUserIcon(
125 getContext().getResources(), userId, false);
126 Bitmap bitmapDefaultUserIcon = UserIcons.convertToBitmap(drawableDefaultUserIcon);
127
128 // User icon is default icon that means user do not set photo, replacing default icon
129 // with anonymous avatar on emergency info button.
130 if (bitmapUserIcon.sameAs(bitmapDefaultUserIcon)) {
131 return getContext().getDrawable(R.drawable.logo_avatar_anonymous_120);
132 }
133
134 // set user icon circular.
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800135 RoundedBitmapDrawable drawableUserIcon = RoundedBitmapDrawableFactory.create(
136 getContext().getResources(), bitmapUserIcon);
137 drawableUserIcon.setCircular(true);
138
139 return drawableUserIcon;
140 }
Billy Chi17ec2282018-06-15 19:00:15 +0800141
Wesley.CW Wang7fce0e82018-07-17 18:05:09 +0800142 private void updateLayoutHeight() {
143 LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) getLayoutParams();
144 // Update height if mEmergencyInfoHint text line more than 1.
145 // EmergencyInfoGroup max line is 2, eclipse type "end" will be adopt if string too long.
146 params.height =
147 mEmergencyInfoHint.getLineCount() > 1 ? getResources().getDimensionPixelSize(
148 R.dimen.emergency_info_button_multiline_height)
149 : getResources().getDimensionPixelSize(
150 R.dimen.emergency_info_button_singleline_height);
151 setLayoutParams(params);
152 }
153
Billy Chi17ec2282018-06-15 19:00:15 +0800154 void updateEmergencyInfo(String emergencyInfoName) {
Billy Chi17ec2282018-06-15 19:00:15 +0800155 if (TextUtils.isEmpty(emergencyInfoName)) {
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800156 emergencyInfoName = getContext().getString(R.string.emergency_information_owner_hint);
Billy Chi17ec2282018-06-15 19:00:15 +0800157 }
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800158 mEmergencyInfoName.setText(emergencyInfoName);
Billy Chi17ec2282018-06-15 19:00:15 +0800159 }
160}