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; |
Chihhang Chuang | a218114 | 2018-06-05 15:29:06 +0800 | [diff] [blame] | 33 | import android.widget.TextView; |
| 34 | |
| 35 | import androidx.core.graphics.drawable.RoundedBitmapDrawable; |
| 36 | import androidx.core.graphics.drawable.RoundedBitmapDrawableFactory; |
| 37 | |
| 38 | import com.android.internal.util.UserIcons; |
| 39 | |
| 40 | import 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 Chuang | 0808a09 | 2018-07-02 11:08:50 +0800 | [diff] [blame^] | 46 | public class EmergencyInfoGroup extends FrameLayout { |
Chihhang Chuang | a218114 | 2018-06-05 15:29:06 +0800 | [diff] [blame] | 47 | private ImageView mEmergencyInfoImage; |
Chihhang Chuang | 0808a09 | 2018-07-02 11:08:50 +0800 | [diff] [blame^] | 48 | private TextView mEmergencyInfoName; |
Chihhang Chuang | a218114 | 2018-06-05 15:29:06 +0800 | [diff] [blame] | 49 | 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 Chuang | a218114 | 2018-06-05 15:29:06 +0800 | [diff] [blame] | 58 | mEmergencyInfoButton = findViewById(R.id.emergency_info_button); |
| 59 | mEmergencyInfoImage = (ImageView) findViewById(R.id.emergency_info_image); |
Chihhang Chuang | 0808a09 | 2018-07-02 11:08:50 +0800 | [diff] [blame^] | 60 | mEmergencyInfoName = (TextView) findViewById(R.id.emergency_info_name); |
Chihhang Chuang | a218114 | 2018-06-05 15:29:06 +0800 | [diff] [blame] | 61 | } |
| 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 Chuang | 0808a09 | 2018-07-02 11:08:50 +0800 | [diff] [blame^] | 88 | |
Chihhang Chuang | a218114 | 2018-06-05 15:29:06 +0800 | [diff] [blame] | 89 | visible = true; |
| 90 | } |
| 91 | |
| 92 | setVisibility(visible ? View.VISIBLE : View.GONE); |
| 93 | } |
| 94 | |
Chihhang Chuang | 0808a09 | 2018-07-02 11:08:50 +0800 | [diff] [blame^] | 95 | /** |
| 96 | * Get user icon. |
| 97 | * |
| 98 | * @return user icon, or anonymous avatar if user do not set photo. |
| 99 | */ |
Chihhang Chuang | a218114 | 2018-06-05 15:29:06 +0800 | [diff] [blame] | 100 | private Drawable getCircularUserIcon() { |
Chihhang Chuang | 0808a09 | 2018-07-02 11:08:50 +0800 | [diff] [blame^] | 101 | final int userId = UserHandle.getCallingUserId(); |
| 102 | |
Chihhang Chuang | a218114 | 2018-06-05 15:29:06 +0800 | [diff] [blame] | 103 | final UserManager userManager = (UserManager) getContext().getSystemService( |
| 104 | Context.USER_SERVICE); |
Chihhang Chuang | 0808a09 | 2018-07-02 11:08:50 +0800 | [diff] [blame^] | 105 | |
| 106 | // get user icon. |
| 107 | Bitmap bitmapUserIcon = userManager.getUserIcon(userId); |
Chihhang Chuang | a218114 | 2018-06-05 15:29:06 +0800 | [diff] [blame] | 108 | |
| 109 | if (bitmapUserIcon == null) { |
Chihhang Chuang | 0808a09 | 2018-07-02 11:08:50 +0800 | [diff] [blame^] | 110 | // use anonymous avatar. |
| 111 | return getContext().getDrawable(R.drawable.logo_avatar_anonymous_120); |
Chihhang Chuang | a218114 | 2018-06-05 15:29:06 +0800 | [diff] [blame] | 112 | } |
| 113 | |
Chihhang Chuang | 0808a09 | 2018-07-02 11:08:50 +0800 | [diff] [blame^] | 114 | // 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 Chuang | a218114 | 2018-06-05 15:29:06 +0800 | [diff] [blame] | 126 | RoundedBitmapDrawable drawableUserIcon = RoundedBitmapDrawableFactory.create( |
| 127 | getContext().getResources(), bitmapUserIcon); |
| 128 | drawableUserIcon.setCircular(true); |
| 129 | |
| 130 | return drawableUserIcon; |
| 131 | } |
Billy Chi | fef11c4 | 2018-06-15 19:00:15 +0800 | [diff] [blame] | 132 | |
| 133 | void updateEmergencyInfo(String emergencyInfoName) { |
Billy Chi | fef11c4 | 2018-06-15 19:00:15 +0800 | [diff] [blame] | 134 | if (TextUtils.isEmpty(emergencyInfoName)) { |
Chihhang Chuang | 0808a09 | 2018-07-02 11:08:50 +0800 | [diff] [blame^] | 135 | emergencyInfoName = getContext().getString(R.string.emergency_information_owner_hint); |
Billy Chi | fef11c4 | 2018-06-15 19:00:15 +0800 | [diff] [blame] | 136 | } |
Chihhang Chuang | 0808a09 | 2018-07-02 11:08:50 +0800 | [diff] [blame^] | 137 | mEmergencyInfoName.setText(emergencyInfoName); |
| 138 | |
| 139 | final String infoNameDescription = getContext().getString( |
| 140 | R.string.emergency_information_owner_content_description, emergencyInfoName); |
| 141 | mEmergencyInfoName.setContentDescription(infoNameDescription); |
Billy Chi | fef11c4 | 2018-06-15 19:00:15 +0800 | [diff] [blame] | 142 | } |
| 143 | } |