Chihhang Chuang | f264cfb | 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 | 17ec228 | 2018-06-15 19:00:15 +0800 | [diff] [blame] | 28 | import android.text.TextUtils; |
Chihhang Chuang | f264cfb | 2018-06-05 15:29:06 +0800 | [diff] [blame] | 29 | import android.util.AttributeSet; |
| 30 | import android.view.View; |
Chihhang Chuang | f8d3300 | 2018-07-02 11:08:50 +0800 | [diff] [blame] | 31 | import android.widget.FrameLayout; |
Chihhang Chuang | f264cfb | 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 | f264cfb | 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 | f8d3300 | 2018-07-02 11:08:50 +0800 | [diff] [blame] | 47 | public class EmergencyInfoGroup extends FrameLayout { |
Chihhang Chuang | f264cfb | 2018-06-05 15:29:06 +0800 | [diff] [blame] | 48 | private ImageView mEmergencyInfoImage; |
Chihhang Chuang | f8d3300 | 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 | f264cfb | 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 | f264cfb | 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 | f8d3300 | 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 | f264cfb | 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 | f264cfb | 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 | f8d3300 | 2018-07-02 11:08:50 +0800 | [diff] [blame] | 97 | |
Chihhang Chuang | f264cfb | 2018-06-05 15:29:06 +0800 | [diff] [blame] | 98 | visible = true; |
| 99 | } |
| 100 | |
| 101 | setVisibility(visible ? View.VISIBLE : View.GONE); |
| 102 | } |
| 103 | |
Chihhang Chuang | f8d3300 | 2018-07-02 11:08:50 +0800 | [diff] [blame] | 104 | /** |
| 105 | * Get user icon. |
| 106 | * |
| 107 | * @return user icon, or anonymous avatar if user do not set photo. |
| 108 | */ |
Chihhang Chuang | f264cfb | 2018-06-05 15:29:06 +0800 | [diff] [blame] | 109 | private Drawable getCircularUserIcon() { |
Chihhang Chuang | f8d3300 | 2018-07-02 11:08:50 +0800 | [diff] [blame] | 110 | final int userId = UserHandle.getCallingUserId(); |
| 111 | |
Chihhang Chuang | f264cfb | 2018-06-05 15:29:06 +0800 | [diff] [blame] | 112 | final UserManager userManager = (UserManager) getContext().getSystemService( |
| 113 | Context.USER_SERVICE); |
Chihhang Chuang | f8d3300 | 2018-07-02 11:08:50 +0800 | [diff] [blame] | 114 | |
| 115 | // get user icon. |
| 116 | Bitmap bitmapUserIcon = userManager.getUserIcon(userId); |
Chihhang Chuang | f264cfb | 2018-06-05 15:29:06 +0800 | [diff] [blame] | 117 | |
| 118 | if (bitmapUserIcon == null) { |
Chihhang Chuang | f8d3300 | 2018-07-02 11:08:50 +0800 | [diff] [blame] | 119 | // use anonymous avatar. |
| 120 | return getContext().getDrawable(R.drawable.logo_avatar_anonymous_120); |
Chihhang Chuang | f264cfb | 2018-06-05 15:29:06 +0800 | [diff] [blame] | 121 | } |
| 122 | |
Chihhang Chuang | f8d3300 | 2018-07-02 11:08:50 +0800 | [diff] [blame] | 123 | // 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 Chuang | f264cfb | 2018-06-05 15:29:06 +0800 | [diff] [blame] | 135 | RoundedBitmapDrawable drawableUserIcon = RoundedBitmapDrawableFactory.create( |
| 136 | getContext().getResources(), bitmapUserIcon); |
| 137 | drawableUserIcon.setCircular(true); |
| 138 | |
| 139 | return drawableUserIcon; |
| 140 | } |
Billy Chi | 17ec228 | 2018-06-15 19:00:15 +0800 | [diff] [blame] | 141 | |
Wesley.CW Wang | 7fce0e8 | 2018-07-17 18:05:09 +0800 | [diff] [blame^] | 142 | 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 Chi | 17ec228 | 2018-06-15 19:00:15 +0800 | [diff] [blame] | 154 | void updateEmergencyInfo(String emergencyInfoName) { |
Billy Chi | 17ec228 | 2018-06-15 19:00:15 +0800 | [diff] [blame] | 155 | if (TextUtils.isEmpty(emergencyInfoName)) { |
Chihhang Chuang | f8d3300 | 2018-07-02 11:08:50 +0800 | [diff] [blame] | 156 | emergencyInfoName = getContext().getString(R.string.emergency_information_owner_hint); |
Billy Chi | 17ec228 | 2018-06-15 19:00:15 +0800 | [diff] [blame] | 157 | } |
Chihhang Chuang | f8d3300 | 2018-07-02 11:08:50 +0800 | [diff] [blame] | 158 | mEmergencyInfoName.setText(emergencyInfoName); |
Billy Chi | 17ec228 | 2018-06-15 19:00:15 +0800 | [diff] [blame] | 159 | } |
| 160 | } |