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; |
| 28 | import android.util.AttributeSet; |
| 29 | import android.view.View; |
| 30 | import android.widget.ImageView; |
| 31 | import android.widget.LinearLayout; |
| 32 | import android.widget.TextView; |
| 33 | |
| 34 | import androidx.core.graphics.drawable.RoundedBitmapDrawable; |
| 35 | import androidx.core.graphics.drawable.RoundedBitmapDrawableFactory; |
| 36 | |
| 37 | import com.android.internal.util.UserIcons; |
| 38 | |
| 39 | import java.util.List; |
| 40 | |
| 41 | /** |
| 42 | * EmergencyInfoGroup display user icon and user name. And it is an entry point to |
| 43 | * Emergency Information. |
| 44 | */ |
| 45 | public class EmergencyInfoGroup extends LinearLayout { |
| 46 | |
| 47 | private ImageView mEmergencyInfoImage; |
| 48 | private TextView mEmergencyInfoName; |
| 49 | private View mEmergencyInfoTitle; |
| 50 | private View mEmergencyInfoButton; |
| 51 | |
| 52 | public EmergencyInfoGroup(Context context, @Nullable AttributeSet attrs) { |
| 53 | super(context, attrs); |
| 54 | } |
| 55 | |
| 56 | @Override |
| 57 | protected void onFinishInflate() { |
| 58 | super.onFinishInflate(); |
| 59 | mEmergencyInfoTitle = findViewById(R.id.emergency_info_title); |
| 60 | mEmergencyInfoButton = findViewById(R.id.emergency_info_button); |
| 61 | mEmergencyInfoImage = (ImageView) findViewById(R.id.emergency_info_image); |
| 62 | mEmergencyInfoName = (TextView) findViewById(R.id.emergency_info_name); |
| 63 | } |
| 64 | |
| 65 | @Override |
| 66 | protected void onWindowVisibilityChanged(int visibility) { |
| 67 | super.onWindowVisibilityChanged(visibility); |
| 68 | if (visibility == View.VISIBLE) { |
| 69 | setupButtonInfo(); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | private void setupButtonInfo() { |
| 74 | List<ResolveInfo> infos; |
| 75 | |
| 76 | if (TelephonyManager.EMERGENCY_ASSISTANCE_ENABLED) { |
| 77 | infos = EmergencyAssistanceHelper.resolveAssistPackageAndQueryActivities(getContext()); |
| 78 | } else { |
| 79 | infos = null; |
| 80 | } |
| 81 | |
| 82 | boolean visible = false; |
| 83 | |
| 84 | if (infos != null && infos.size() > 0) { |
| 85 | final String packageName = infos.get(0).activityInfo.packageName; |
| 86 | final Intent intent = new Intent(TelephonyManager.ACTION_EMERGENCY_ASSISTANCE) |
| 87 | .setPackage(packageName); |
| 88 | mEmergencyInfoButton.setTag(R.id.tag_intent, intent); |
| 89 | mEmergencyInfoImage.setImageDrawable(getCircularUserIcon()); |
| 90 | |
| 91 | /* TODO: Get user name. |
| 92 | if user name exist: |
| 93 | 1. mEmergencyInfoTitle show title. |
| 94 | 2. mEmergencyInfoName show user name. |
| 95 | if user name does not exist: |
| 96 | 1. mEmergencyInfoTitle hide. |
| 97 | 2. mEmergencyInfoName show app label. */ |
| 98 | mEmergencyInfoTitle.setVisibility(View.INVISIBLE); |
| 99 | mEmergencyInfoName.setText(getContext().getResources().getString( |
| 100 | R.string.emergency_information_title)); |
| 101 | |
| 102 | visible = true; |
| 103 | } |
| 104 | |
| 105 | setVisibility(visible ? View.VISIBLE : View.GONE); |
| 106 | } |
| 107 | |
| 108 | private Drawable getCircularUserIcon() { |
| 109 | final UserManager userManager = (UserManager) getContext().getSystemService( |
| 110 | Context.USER_SERVICE); |
| 111 | Bitmap bitmapUserIcon = userManager.getUserIcon(UserHandle.getCallingUserId()); |
| 112 | |
| 113 | if (bitmapUserIcon == null) { |
| 114 | // get default user icon. |
| 115 | final Drawable defaultUserIcon = UserIcons.getDefaultUserIcon( |
| 116 | getContext().getResources(), UserHandle.getCallingUserId(), false); |
| 117 | bitmapUserIcon = UserIcons.convertToBitmap(defaultUserIcon); |
| 118 | } |
| 119 | |
| 120 | RoundedBitmapDrawable drawableUserIcon = RoundedBitmapDrawableFactory.create( |
| 121 | getContext().getResources(), bitmapUserIcon); |
| 122 | drawableUserIcon.setCircular(true); |
| 123 | |
| 124 | return drawableUserIcon; |
| 125 | } |
| 126 | } |