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.ComponentName;
20 import android.content.Context;
21 import android.content.pm.PackageManager;
22 import android.graphics.drawable.Drawable;
23 import android.util.AttributeSet;
24 import android.util.Log;
25 import android.widget.ImageView;
26 
27 import androidx.annotation.NonNull;
28 import androidx.annotation.Nullable;
29 import androidx.constraintlayout.widget.ConstraintLayout;
30 
31 import com.android.car.portraitlauncher.R;
32 
33 /**
34  * A view that is shown on top of the task view in {@link TaskViewPanel} to improve visual effects.
35  */
36 public class TaskViewPanelOverlay extends ConstraintLayout {
37     private static final String TAG = TaskViewPanelOverlay.class.getSimpleName();
38 
39     private ImageView mBackground;
40     private ImageView mIcon;
41     private ComponentName mComponentName;
42 
TaskViewPanelOverlay(@onNull Context context)43     public TaskViewPanelOverlay(@NonNull Context context) {
44         this(context, /* attrs= */ null);
45     }
46 
TaskViewPanelOverlay(@onNull Context context, @Nullable AttributeSet attrs)47     public TaskViewPanelOverlay(@NonNull Context context, @Nullable AttributeSet attrs) {
48         this(context, attrs, /* defStyleAttr= */ 0);
49     }
50 
TaskViewPanelOverlay(@onNull Context context, @Nullable AttributeSet attrs, int defStyleAttr)51     public TaskViewPanelOverlay(@NonNull Context context, @Nullable AttributeSet attrs,
52             int defStyleAttr) {
53         this(context, attrs, defStyleAttr, /* defStyleRes= */ 0);
54     }
55 
TaskViewPanelOverlay(@onNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes)56     public TaskViewPanelOverlay(@NonNull Context context, @Nullable AttributeSet attrs,
57             int defStyleAttr, int defStyleRes) {
58         super(context, attrs, defStyleAttr, defStyleRes);
59     }
60 
61     @Override
onFinishInflate()62     protected void onFinishInflate() {
63         super.onFinishInflate();
64         mBackground = findViewById(R.id.task_view_overlay_icon_background);
65         mIcon = findViewById(R.id.task_view_overlay_icon);
66     }
67 
68     /**
69      * Sets the visibility of the overlay to visible and set the centered icon state according to
70      * {@code withIcon}.
71      */
show(boolean withIcon)72     public void show(boolean withIcon) {
73         setVisibility(VISIBLE);
74         setIconEnabled(withIcon);
75     }
76 
77     /** Sets the visibility of the overlay to gone and disable the centered icon. */
hide()78     public void hide() {
79         setVisibility(GONE);
80         setIconEnabled(/* enableIcon= */ false);
81     }
82 
83     /** Sets {@code componentName} that used to retrieve the centered icon. */
setComponentName(ComponentName componentName)84     void setComponentName(ComponentName componentName) {
85         mComponentName = componentName;
86     }
87 
88     /** Refreshes the overlay according to current theme. */
refresh()89     void refresh() {
90         int backgroundColor = getResources().getColor(R.color.car_background, mContext.getTheme());
91         setBackgroundColor(backgroundColor);
92         Drawable iconBackgroundDrawable = getResources().getDrawable(R.drawable.app_icon_background,
93                 mContext.getTheme());
94         mBackground.setBackground(iconBackgroundDrawable);
95     }
96 
97     /**
98      * Sets the state of the centered icon.
99      *
100      * @param enabled True if the icon is enabled, false otherwise.
101      */
setIconEnabled(boolean enabled)102     private void setIconEnabled(boolean enabled) {
103         mIcon.setBackground(enabled ? getApplicationIcon(mComponentName) : null);
104         mIcon.setVisibility(enabled ? VISIBLE : GONE);
105         mBackground.setVisibility(enabled ? VISIBLE : GONE);
106     }
107 
108     /** Retrieve the icon associated with the given {@code componentName}. */
getApplicationIcon(ComponentName componentName)109     private Drawable getApplicationIcon(ComponentName componentName) {
110         if (componentName == null) {
111             return null;
112         }
113 
114         Drawable icon = null;
115         try {
116             icon = mContext.getPackageManager().getApplicationIcon(componentName.getPackageName());
117         } catch (PackageManager.NameNotFoundException e) {
118             Log.e(TAG, "Fail to get application icon. ", e);
119         }
120         return icon;
121     }
122 }
123