blob: 898af32cd8ce5004283d8e2c01ae35034498fb47 [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;
Wesley.CW Wang7fce0e82018-07-17 18:05:09 +080033import android.widget.LinearLayout;
Chihhang Chuanga2181142018-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 Chuang0808a092018-07-02 11:08:50 +080047public class EmergencyInfoGroup extends FrameLayout {
Chihhang Chuanga2181142018-06-05 15:29:06 +080048 private ImageView mEmergencyInfoImage;
Chihhang Chuang0808a092018-07-02 11:08:50 +080049 private TextView mEmergencyInfoName;
Wesley.CW Wang7fce0e82018-07-17 18:05:09 +080050 private TextView mEmergencyInfoHint;
Chihhang Chuanga2181142018-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 Chuanga2181142018-06-05 15:29:06 +080060 mEmergencyInfoButton = findViewById(R.id.emergency_info_button);
61 mEmergencyInfoImage = (ImageView) findViewById(R.id.emergency_info_image);
Chihhang Chuang0808a092018-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 Chuanga2181142018-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 Chuanga2181142018-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 Chuang0808a092018-07-02 11:08:50 +080097
Chihhang Chuanga2181142018-06-05 15:29:06 +080098 visible = true;
99 }
Wesley.CW Wanga7c20a72018-07-24 12:20:20 +0800100 mEmergencyInfoName.setText(getUserName());
Chihhang Chuanga2181142018-06-05 15:29:06 +0800101
102 setVisibility(visible ? View.VISIBLE : View.GONE);
103 }
104
Chihhang Chuang0808a092018-07-02 11:08:50 +0800105 /**
106 * Get user icon.
107 *
Wesley.CW Wanga7c20a72018-07-24 12:20:20 +0800108 * @return user icon, or default user icon if user do not set photo.
Chihhang Chuang0808a092018-07-02 11:08:50 +0800109 */
Chihhang Chuanga2181142018-06-05 15:29:06 +0800110 private Drawable getCircularUserIcon() {
111 final UserManager userManager = (UserManager) getContext().getSystemService(
112 Context.USER_SERVICE);
Wesley.CW Wanga7c20a72018-07-24 12:20:20 +0800113 Bitmap bitmapUserIcon = userManager.getUserIcon(UserHandle.getCallingUserId());
Chihhang Chuanga2181142018-06-05 15:29:06 +0800114
115 if (bitmapUserIcon == null) {
Wesley.CW Wanga7c20a72018-07-24 12:20:20 +0800116 // get default user icon.
117 final Drawable defaultUserIcon = UserIcons.getDefaultUserIcon(
118 getContext().getResources(), UserHandle.myUserId(), false);
119 bitmapUserIcon = UserIcons.convertToBitmap(defaultUserIcon);
Chihhang Chuanga2181142018-06-05 15:29:06 +0800120 }
Chihhang Chuanga2181142018-06-05 15:29:06 +0800121 RoundedBitmapDrawable drawableUserIcon = RoundedBitmapDrawableFactory.create(
122 getContext().getResources(), bitmapUserIcon);
123 drawableUserIcon.setCircular(true);
124
125 return drawableUserIcon;
126 }
Billy Chifef11c42018-06-15 19:00:15 +0800127
Wesley.CW Wanga7c20a72018-07-24 12:20:20 +0800128 private CharSequence getUserName() {
129 final UserManager userManager = (UserManager) getContext().getSystemService(
130 Context.USER_SERVICE);
131 final String userName = userManager.getUserName();
Chihhang Chuang0808a092018-07-02 11:08:50 +0800132
Wesley.CW Wanga7c20a72018-07-24 12:20:20 +0800133 return TextUtils.isEmpty(userName) ? getContext().getText(
134 R.string.emergency_information_owner_hint) : userName;
Billy Chifef11c42018-06-15 19:00:15 +0800135 }
Wesley.CW Wanga7c20a72018-07-24 12:20:20 +0800136
Wesley.CW Wang7fce0e82018-07-17 18:05:09 +0800137 private void updateLayoutHeight() {
138 LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) getLayoutParams();
139 // Update height if mEmergencyInfoHint text line more than 1.
140 // EmergencyInfoGroup max line is 2, eclipse type "end" will be adopt if string too long.
141 params.height =
142 mEmergencyInfoHint.getLineCount() > 1 ? getResources().getDimensionPixelSize(
143 R.dimen.emergency_info_button_multiline_height)
144 : getResources().getDimensionPixelSize(
145 R.dimen.emergency_info_button_singleline_height);
146 setLayoutParams(params);
147 }
CY Cheng62afa1c2018-08-24 18:33:10 +0800148}