Chihhang Chuang | a218114 | 2018-06-05 15:29:06 +0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.phone; |
| 18 | |
| 19 | import android.annotation.Nullable; |
| 20 | import android.content.Context; |
| 21 | import android.content.Intent; |
| 22 | import android.content.pm.ResolveInfo; |
| 23 | import android.graphics.Bitmap; |
| 24 | import android.graphics.drawable.Drawable; |
| 25 | import android.os.UserHandle; |
| 26 | import android.os.UserManager; |
| 27 | import android.telephony.TelephonyManager; |
Billy Chi | fef11c4 | 2018-06-15 19:00:15 +0800 | [diff] [blame] | 28 | import android.text.TextUtils; |
Chihhang Chuang | a218114 | 2018-06-05 15:29:06 +0800 | [diff] [blame] | 29 | import android.util.AttributeSet; |
| 30 | import android.view.View; |
Chihhang Chuang | 0808a09 | 2018-07-02 11:08:50 +0800 | [diff] [blame] | 31 | import android.widget.FrameLayout; |
Chihhang Chuang | a218114 | 2018-06-05 15:29:06 +0800 | [diff] [blame] | 32 | import android.widget.ImageView; |
Wesley.CW Wang | 7fce0e8 | 2018-07-17 18:05:09 +0800 | [diff] [blame] | 33 | import android.widget.LinearLayout; |
Chihhang Chuang | a218114 | 2018-06-05 15:29:06 +0800 | [diff] [blame] | 34 | import android.widget.TextView; |
| 35 | |
| 36 | import androidx.core.graphics.drawable.RoundedBitmapDrawable; |
| 37 | import androidx.core.graphics.drawable.RoundedBitmapDrawableFactory; |
| 38 | |
| 39 | import com.android.internal.util.UserIcons; |
| 40 | |
| 41 | import 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 Chuang | 0808a09 | 2018-07-02 11:08:50 +0800 | [diff] [blame] | 47 | public class EmergencyInfoGroup extends FrameLayout { |
Chihhang Chuang | a218114 | 2018-06-05 15:29:06 +0800 | [diff] [blame] | 48 | private ImageView mEmergencyInfoImage; |
Chihhang Chuang | 0808a09 | 2018-07-02 11:08:50 +0800 | [diff] [blame] | 49 | private TextView mEmergencyInfoName; |
Wesley.CW Wang | 7fce0e8 | 2018-07-17 18:05:09 +0800 | [diff] [blame] | 50 | private TextView mEmergencyInfoHint; |
Chihhang Chuang | a218114 | 2018-06-05 15:29:06 +0800 | [diff] [blame] | 51 | 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 Chuang | a218114 | 2018-06-05 15:29:06 +0800 | [diff] [blame] | 60 | mEmergencyInfoButton = findViewById(R.id.emergency_info_button); |
| 61 | mEmergencyInfoImage = (ImageView) findViewById(R.id.emergency_info_image); |
Chihhang Chuang | 0808a09 | 2018-07-02 11:08:50 +0800 | [diff] [blame] | 62 | mEmergencyInfoName = (TextView) findViewById(R.id.emergency_info_name); |
Wesley.CW Wang | 7fce0e8 | 2018-07-17 18:05:09 +0800 | [diff] [blame] | 63 | mEmergencyInfoHint = (TextView) findViewById(R.id.emergency_info_hint); |
Chihhang Chuang | a218114 | 2018-06-05 15:29:06 +0800 | [diff] [blame] | 64 | } |
| 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 Wang | 7fce0e8 | 2018-07-17 18:05:09 +0800 | [diff] [blame] | 74 | @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 Chuang | a218114 | 2018-06-05 15:29:06 +0800 | [diff] [blame] | 80 | 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 Chuang | 0808a09 | 2018-07-02 11:08:50 +0800 | [diff] [blame] | 97 | |
Chihhang Chuang | a218114 | 2018-06-05 15:29:06 +0800 | [diff] [blame] | 98 | visible = true; |
| 99 | } |
Wesley.CW Wang | a7c20a7 | 2018-07-24 12:20:20 +0800 | [diff] [blame] | 100 | mEmergencyInfoName.setText(getUserName()); |
Chihhang Chuang | a218114 | 2018-06-05 15:29:06 +0800 | [diff] [blame] | 101 | |
| 102 | setVisibility(visible ? View.VISIBLE : View.GONE); |
| 103 | } |
| 104 | |
Chihhang Chuang | 0808a09 | 2018-07-02 11:08:50 +0800 | [diff] [blame] | 105 | /** |
| 106 | * Get user icon. |
| 107 | * |
Wesley.CW Wang | a7c20a7 | 2018-07-24 12:20:20 +0800 | [diff] [blame] | 108 | * @return user icon, or default user icon if user do not set photo. |
Chihhang Chuang | 0808a09 | 2018-07-02 11:08:50 +0800 | [diff] [blame] | 109 | */ |
Chihhang Chuang | a218114 | 2018-06-05 15:29:06 +0800 | [diff] [blame] | 110 | private Drawable getCircularUserIcon() { |
| 111 | final UserManager userManager = (UserManager) getContext().getSystemService( |
| 112 | Context.USER_SERVICE); |
Wesley.CW Wang | a7c20a7 | 2018-07-24 12:20:20 +0800 | [diff] [blame] | 113 | Bitmap bitmapUserIcon = userManager.getUserIcon(UserHandle.getCallingUserId()); |
Chihhang Chuang | a218114 | 2018-06-05 15:29:06 +0800 | [diff] [blame] | 114 | |
| 115 | if (bitmapUserIcon == null) { |
Wesley.CW Wang | a7c20a7 | 2018-07-24 12:20:20 +0800 | [diff] [blame] | 116 | // get default user icon. |
| 117 | final Drawable defaultUserIcon = UserIcons.getDefaultUserIcon( |
| 118 | getContext().getResources(), UserHandle.myUserId(), false); |
| 119 | bitmapUserIcon = UserIcons.convertToBitmap(defaultUserIcon); |
Chihhang Chuang | a218114 | 2018-06-05 15:29:06 +0800 | [diff] [blame] | 120 | } |
Chihhang Chuang | a218114 | 2018-06-05 15:29:06 +0800 | [diff] [blame] | 121 | RoundedBitmapDrawable drawableUserIcon = RoundedBitmapDrawableFactory.create( |
| 122 | getContext().getResources(), bitmapUserIcon); |
| 123 | drawableUserIcon.setCircular(true); |
| 124 | |
| 125 | return drawableUserIcon; |
| 126 | } |
Billy Chi | fef11c4 | 2018-06-15 19:00:15 +0800 | [diff] [blame] | 127 | |
Wesley.CW Wang | a7c20a7 | 2018-07-24 12:20:20 +0800 | [diff] [blame] | 128 | private CharSequence getUserName() { |
| 129 | final UserManager userManager = (UserManager) getContext().getSystemService( |
| 130 | Context.USER_SERVICE); |
| 131 | final String userName = userManager.getUserName(); |
Chihhang Chuang | 0808a09 | 2018-07-02 11:08:50 +0800 | [diff] [blame] | 132 | |
Wesley.CW Wang | a7c20a7 | 2018-07-24 12:20:20 +0800 | [diff] [blame] | 133 | return TextUtils.isEmpty(userName) ? getContext().getText( |
| 134 | R.string.emergency_information_owner_hint) : userName; |
Billy Chi | fef11c4 | 2018-06-15 19:00:15 +0800 | [diff] [blame] | 135 | } |
Wesley.CW Wang | a7c20a7 | 2018-07-24 12:20:20 +0800 | [diff] [blame] | 136 | |
Wesley.CW Wang | 7fce0e8 | 2018-07-17 18:05:09 +0800 | [diff] [blame] | 137 | 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 Cheng | 62afa1c | 2018-08-24 18:33:10 +0800 | [diff] [blame^] | 148 | } |