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; |
| 31 | import android.widget.ImageView; |
| 32 | import android.widget.LinearLayout; |
| 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 | */ |
| 46 | public class EmergencyInfoGroup extends LinearLayout { |
| 47 | |
| 48 | private ImageView mEmergencyInfoImage; |
Billy Chi | fef11c4 | 2018-06-15 19:00:15 +0800 | [diff] [blame^] | 49 | private TextView mEmergencyInfoNameTextView; |
Chihhang Chuang | a218114 | 2018-06-05 15:29:06 +0800 | [diff] [blame] | 50 | private View mEmergencyInfoTitle; |
| 51 | private View mEmergencyInfoButton; |
Billy Chi | fef11c4 | 2018-06-15 19:00:15 +0800 | [diff] [blame^] | 52 | private String mDefaultEmergencyInfoName; |
Chihhang Chuang | a218114 | 2018-06-05 15:29:06 +0800 | [diff] [blame] | 53 | |
| 54 | public EmergencyInfoGroup(Context context, @Nullable AttributeSet attrs) { |
| 55 | super(context, attrs); |
| 56 | } |
| 57 | |
| 58 | @Override |
| 59 | protected void onFinishInflate() { |
| 60 | super.onFinishInflate(); |
| 61 | mEmergencyInfoTitle = findViewById(R.id.emergency_info_title); |
| 62 | mEmergencyInfoButton = findViewById(R.id.emergency_info_button); |
| 63 | mEmergencyInfoImage = (ImageView) findViewById(R.id.emergency_info_image); |
Billy Chi | fef11c4 | 2018-06-15 19:00:15 +0800 | [diff] [blame^] | 64 | mEmergencyInfoNameTextView = (TextView) findViewById(R.id.emergency_info_name); |
| 65 | mDefaultEmergencyInfoName = getContext().getResources().getString( |
| 66 | R.string.emergency_information_title); |
Chihhang Chuang | a218114 | 2018-06-05 15:29:06 +0800 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | @Override |
| 70 | protected void onWindowVisibilityChanged(int visibility) { |
| 71 | super.onWindowVisibilityChanged(visibility); |
| 72 | if (visibility == View.VISIBLE) { |
| 73 | setupButtonInfo(); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | private void setupButtonInfo() { |
| 78 | List<ResolveInfo> infos; |
| 79 | |
| 80 | if (TelephonyManager.EMERGENCY_ASSISTANCE_ENABLED) { |
| 81 | infos = EmergencyAssistanceHelper.resolveAssistPackageAndQueryActivities(getContext()); |
| 82 | } else { |
| 83 | infos = null; |
| 84 | } |
| 85 | |
| 86 | boolean visible = false; |
| 87 | |
| 88 | if (infos != null && infos.size() > 0) { |
| 89 | final String packageName = infos.get(0).activityInfo.packageName; |
| 90 | final Intent intent = new Intent(TelephonyManager.ACTION_EMERGENCY_ASSISTANCE) |
| 91 | .setPackage(packageName); |
| 92 | mEmergencyInfoButton.setTag(R.id.tag_intent, intent); |
| 93 | mEmergencyInfoImage.setImageDrawable(getCircularUserIcon()); |
Chihhang Chuang | a218114 | 2018-06-05 15:29:06 +0800 | [diff] [blame] | 94 | visible = true; |
| 95 | } |
| 96 | |
| 97 | setVisibility(visible ? View.VISIBLE : View.GONE); |
| 98 | } |
| 99 | |
| 100 | private Drawable getCircularUserIcon() { |
| 101 | final UserManager userManager = (UserManager) getContext().getSystemService( |
| 102 | Context.USER_SERVICE); |
| 103 | Bitmap bitmapUserIcon = userManager.getUserIcon(UserHandle.getCallingUserId()); |
| 104 | |
| 105 | if (bitmapUserIcon == null) { |
| 106 | // get default user icon. |
| 107 | final Drawable defaultUserIcon = UserIcons.getDefaultUserIcon( |
| 108 | getContext().getResources(), UserHandle.getCallingUserId(), false); |
| 109 | bitmapUserIcon = UserIcons.convertToBitmap(defaultUserIcon); |
| 110 | } |
| 111 | |
| 112 | RoundedBitmapDrawable drawableUserIcon = RoundedBitmapDrawableFactory.create( |
| 113 | getContext().getResources(), bitmapUserIcon); |
| 114 | drawableUserIcon.setCircular(true); |
| 115 | |
| 116 | return drawableUserIcon; |
| 117 | } |
Billy Chi | fef11c4 | 2018-06-15 19:00:15 +0800 | [diff] [blame^] | 118 | |
| 119 | void updateEmergencyInfo(String emergencyInfoName) { |
| 120 | String infoNameDescription; |
| 121 | if (TextUtils.isEmpty(emergencyInfoName)) { |
| 122 | mEmergencyInfoTitle.setVisibility(View.INVISIBLE); |
| 123 | mEmergencyInfoNameTextView.setText(mDefaultEmergencyInfoName); |
| 124 | infoNameDescription = mDefaultEmergencyInfoName; |
| 125 | } else { |
| 126 | mEmergencyInfoTitle.setVisibility(View.VISIBLE); |
| 127 | mEmergencyInfoNameTextView.setText(emergencyInfoName); |
| 128 | infoNameDescription = getContext().getString( |
| 129 | R.string.emergency_information_button_content_description, emergencyInfoName); |
| 130 | } |
| 131 | mEmergencyInfoNameTextView.setContentDescription(infoNameDescription); |
| 132 | } |
| 133 | } |