Aarthi Balachander | cf6ca0c | 2018-04-10 19:26:45 -0700 | [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.server.am; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.content.pm.UserInfo; |
| 21 | import android.content.res.Resources; |
| 22 | import android.graphics.Bitmap; |
| 23 | import android.graphics.Canvas; |
| 24 | import android.graphics.Color; |
| 25 | import android.graphics.Paint; |
| 26 | import android.graphics.Paint.Align; |
| 27 | import android.graphics.drawable.ColorDrawable; |
| 28 | import android.graphics.drawable.GradientDrawable; |
| 29 | import android.view.LayoutInflater; |
| 30 | import android.view.View; |
| 31 | import android.widget.ImageView; |
| 32 | import android.widget.TextView; |
| 33 | import com.android.internal.R; |
| 34 | |
| 35 | /** |
| 36 | * Dialog to show when a user switch it about to happen for the car. The intent is to snapshot the |
| 37 | * screen immediately after the dialog shows so that the user is informed that something is |
| 38 | * happening in the background rather than just freeze the screen and not know if the user-switch |
| 39 | * affordance was being handled. |
| 40 | * |
| 41 | */ |
| 42 | final class CarUserSwitchingDialog extends UserSwitchingDialog { |
| 43 | private static final String TAG = "ActivityManagerCarUserSwitchingDialog"; |
| 44 | |
| 45 | public CarUserSwitchingDialog(ActivityManagerService service, Context context, UserInfo oldUser, |
| 46 | UserInfo newUser, boolean aboveSystem, String switchingFromSystemUserMessage, |
| 47 | String switchingToSystemUserMessage) { |
| 48 | super(service, context, oldUser, newUser, aboveSystem, switchingFromSystemUserMessage, |
| 49 | switchingToSystemUserMessage); |
| 50 | |
| 51 | getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); |
| 52 | } |
| 53 | |
| 54 | @Override |
| 55 | void inflateContent() { |
| 56 | // Set up the dialog contents |
| 57 | setCancelable(false); |
| 58 | Resources res = getContext().getResources(); |
| 59 | // Custom view due to alignment and font size requirements |
| 60 | View view = LayoutInflater.from(getContext()).inflate(R.layout.car_user_switching_dialog, |
| 61 | null); |
| 62 | |
| 63 | ((ImageView) view.findViewById(R.id.user_loading_avatar)) |
| 64 | .setImageBitmap(getDefaultUserIcon(mNewUser)); |
| 65 | ((TextView) view.findViewById(R.id.user_loading)) |
| 66 | .setText(res.getString(R.string.car_loading_profile)); |
| 67 | setView(view); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Returns the default user icon. This icon is a circle with a letter in it. The letter is |
| 72 | * the first character in the username. |
| 73 | * |
| 74 | * @param userInfo the profile of the user for which the icon should be created |
| 75 | */ |
| 76 | private Bitmap getDefaultUserIcon(UserInfo userInfo) { |
| 77 | Resources res = mContext.getResources(); |
| 78 | int mPodImageAvatarWidth = res.getDimensionPixelSize( |
| 79 | R.dimen.car_fullscreen_user_pod_image_avatar_width); |
| 80 | int mPodImageAvatarHeight = res.getDimensionPixelSize( |
| 81 | R.dimen.car_fullscreen_user_pod_image_avatar_height); |
| 82 | CharSequence displayText = userInfo.name.subSequence(0, 1); |
| 83 | Bitmap out = Bitmap.createBitmap(mPodImageAvatarWidth, mPodImageAvatarHeight, |
| 84 | Bitmap.Config.ARGB_8888); |
| 85 | Canvas canvas = new Canvas(out); |
| 86 | |
| 87 | // Draw the circle background. |
| 88 | GradientDrawable shape = new GradientDrawable(); |
| 89 | shape.setShape(GradientDrawable.RADIAL_GRADIENT); |
| 90 | shape.setGradientRadius(1.0f); |
| 91 | shape.setColor(mContext.getColor(R.color.car_user_switcher_user_image_bgcolor)); |
| 92 | shape.setBounds(0, 0, mPodImageAvatarWidth, mPodImageAvatarHeight); |
| 93 | shape.draw(canvas); |
| 94 | |
| 95 | // Draw the letter in the center. |
| 96 | Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); |
| 97 | paint.setColor(mContext.getColor(R.color.car_user_switcher_user_image_fgcolor)); |
| 98 | paint.setTextAlign(Align.CENTER); |
| 99 | paint.setTextSize(res.getDimensionPixelSize( |
| 100 | R.dimen.car_fullscreen_user_pod_icon_text_size)); |
| 101 | |
| 102 | Paint.FontMetricsInt metrics = paint.getFontMetricsInt(); |
| 103 | // The Y coordinate is measured by taking half the height of the pod, but that would |
| 104 | // draw the character putting the bottom of the font in the middle of the pod. To |
| 105 | // correct this, half the difference between the top and bottom distance metrics of the |
| 106 | // font gives the offset of the font. Bottom is a positive value, top is negative, so |
| 107 | // the different is actually a sum. The "half" operation is then factored out. |
| 108 | canvas.drawText(displayText.toString(), mPodImageAvatarWidth / 2, |
| 109 | (mPodImageAvatarHeight - (metrics.bottom + metrics.top)) / 2, paint); |
| 110 | |
| 111 | return out; |
| 112 | } |
| 113 | } |