1 /* 2 * Copyright (C) 2023 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.car.portraitlauncher.panel; 18 19 import android.content.Context; 20 import android.content.res.Resources; 21 import android.graphics.Canvas; 22 import android.graphics.Paint; 23 import android.util.AttributeSet; 24 import android.view.SurfaceHolder; 25 import android.view.SurfaceView; 26 27 import androidx.annotation.NonNull; 28 import androidx.annotation.Nullable; 29 30 import com.android.car.portraitlauncher.R; 31 32 /** 33 * A {@link SurfaceView} that acts as a solid black background of {@link TaskViewPanel}. It 34 * avoids user to see the background app when an activity in RootTaskView fades out. 35 * TODO(b/336858361): improve the UI for this surfaceview. 36 */ 37 public class BackgroundSurfaceView extends SurfaceView { 38 39 // Whether the panel should use a fixed color. 40 private boolean mUseFixedColor; 41 42 // The color used in the surface view. 43 private int mColor; 44 private int mTextColor; 45 46 // The Text at the center of the surface view. 47 private String mText; 48 BackgroundSurfaceView(Context context)49 public BackgroundSurfaceView(Context context) { 50 this(context, null); 51 } 52 BackgroundSurfaceView(Context context, AttributeSet attrs)53 public BackgroundSurfaceView(Context context, AttributeSet attrs) { 54 this(context, attrs, 0); 55 } 56 BackgroundSurfaceView(Context context, AttributeSet attrs, int defStyleAttr)57 public BackgroundSurfaceView(Context context, AttributeSet attrs, int defStyleAttr) { 58 this(context, attrs, defStyleAttr, 0); 59 } 60 BackgroundSurfaceView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)61 public BackgroundSurfaceView(Context context, AttributeSet attrs, int defStyleAttr, 62 int defStyleRes) { 63 this(context, attrs, defStyleAttr, defStyleRes, false); 64 } 65 BackgroundSurfaceView(@onNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes, boolean disableBackgroundLayer)66 public BackgroundSurfaceView(@NonNull Context context, 67 @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes, 68 boolean disableBackgroundLayer) { 69 super(context, attrs, defStyleAttr, defStyleRes, disableBackgroundLayer); 70 setZOrderMediaOverlay(true); 71 setupSurfaceView(); 72 } 73 setupSurfaceView()74 private void setupSurfaceView() { 75 mColor = getResources().getColor(R.color.car_background, getContext().getTheme()); 76 mTextColor = getResources().getColor(R.color.car_on_background, getContext().getTheme()); 77 78 getHolder().addCallback(new SurfaceHolder.Callback() { 79 @Override 80 public void surfaceCreated(@NonNull SurfaceHolder holder) { 81 drawSurface(holder); 82 } 83 84 @Override 85 public void surfaceChanged(@NonNull SurfaceHolder holder, int format, int width, 86 int height) { 87 drawSurface(holder); 88 } 89 90 @Override 91 public void surfaceDestroyed(@NonNull SurfaceHolder holder) { 92 93 } 94 }); 95 } 96 drawSurface(SurfaceHolder holder)97 private void drawSurface(SurfaceHolder holder) { 98 Canvas canvas = holder.lockCanvas(); 99 if (canvas == null) { 100 return; 101 } 102 canvas.drawColor(mColor); 103 104 if (mText != null) { 105 Paint paint = new Paint(); 106 paint.setColor(mTextColor); 107 paint.setTextSize(20); 108 paint.setTextAlign(Paint.Align.CENTER); 109 float xPos = (canvas.getWidth() / 2f); 110 float yPos = (canvas.getHeight() / 2f - ((paint.descent() + paint.ascent()) / 2)); 111 canvas.drawText(mText, xPos, yPos, paint); 112 } 113 114 holder.unlockCanvasAndPost(canvas); 115 } 116 117 /** Sets the fixed color on the surface view */ setFixedColor(int color)118 public void setFixedColor(int color) { 119 mColor = color; 120 mUseFixedColor = true; 121 drawSurface(getHolder()); 122 } 123 124 /** Sets the text that shows on the surface view */ setText(String text)125 public void setText(String text) { 126 mText = text; 127 drawSurface(getHolder()); 128 } 129 130 /** Sets the fixed color and centered text on the surface view */ setFixedColorAndText(int color, String text)131 public void setFixedColorAndText(int color, String text) { 132 mText = text; 133 setFixedColor(color); 134 } 135 136 /** refreshes the color of the surface view if needed. */ refresh(Resources.Theme theme)137 public void refresh(Resources.Theme theme) { 138 if (!mUseFixedColor) { 139 mColor = getResources().getColor(R.color.car_background, theme); 140 mTextColor = getResources().getColor(R.color.car_on_background, 141 getContext().getTheme()); 142 drawSurface(getHolder()); 143 } 144 } 145 } 146