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 static com.android.car.portraitlauncher.homeactivities.CarUiPortraitHomeScreen.sendVirtualBackPress;
20 
21 import android.annotation.NonNull;
22 import android.annotation.Nullable;
23 import android.car.Car;
24 import android.car.content.pm.CarPackageManager;
25 import android.content.ComponentName;
26 import android.content.Context;
27 import android.content.pm.PackageManager.NameNotFoundException;
28 import android.content.res.Resources;
29 import android.graphics.drawable.Drawable;
30 import android.hardware.input.InputManager;
31 import android.util.AttributeSet;
32 import android.util.Log;
33 import android.view.View;
34 import android.widget.ImageButton;
35 import android.widget.RelativeLayout;
36 
37 import com.android.car.portraitlauncher.R;
38 
39 /** The grip bar used to drag a TaskViewPanel */
40 public class GripBarView extends RelativeLayout {
41 
42     private static final String TAG = GripBarView.class.getName();
43 
44     @Nullable
45     private CarPackageManager mCarPackageManager = null;
46     @Nullable
47     private InputManager mInputManager;
48 
49     @Nullable
50     private View mBackButton = null;
51     @Nullable
52     private View mDisplayCompatToolbar = null;
53 
GripBarView(Context context)54     public GripBarView(Context context) {
55         this(context, null);
56     }
57 
GripBarView(Context context, AttributeSet attrs)58     public GripBarView(Context context, AttributeSet attrs) {
59         this(context, attrs, 0);
60     }
61 
GripBarView(Context context, AttributeSet attrs, int defStyleAttr)62     public GripBarView(Context context, AttributeSet attrs, int defStyleAttr) {
63         this(context, attrs, defStyleAttr, 0);
64     }
65 
GripBarView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)66     public GripBarView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
67         super(context, attrs, defStyleAttr, defStyleRes);
68         initView();
69     }
70 
initView()71     private void initView() {
72         // TODO: (b/307556334) move this to a central place.
73         Car.createCar(getContext(), /* handler= */ null,
74                 Car.CAR_WAIT_TIMEOUT_WAIT_FOREVER,
75                 (car, ready) -> {
76                     if (!ready) {
77                         Log.w(TAG, "CarService is not ready.");
78                         return;
79                     }
80 
81                     mCarPackageManager = car.getCarManager(CarPackageManager.class);
82                     mInputManager = getContext().getSystemService(InputManager.class);
83 
84                     inflate(getContext(), R.layout.car_ui_portrait_grip_bar_layout, this);
85                     mDisplayCompatToolbar = findViewById(R.id.displaycompat_toolbar);
86                     mBackButton = findViewById(R.id.back_button);
87                     mBackButton.setOnClickListener(v -> {
88                         sendVirtualBackPress();
89                     });
90                 });
91     }
92 
93     /** Refreshes the view according to the given {@link Resources.Theme}. */
refresh(Resources.Theme theme)94     public void refresh(Resources.Theme theme) {
95         Drawable background = getResources().getDrawable(R.drawable.grip_bar_background, theme);
96         findViewById(R.id.grip_bar_handle).setBackground(background);
97 
98         int displayCompatToolbarBackground = getResources().getColor(R.color.car_background, theme);
99         findViewById(R.id.displaycompat_toolbar)
100                 .setBackgroundColor(displayCompatToolbarBackground);
101 
102         ImageButton backButton = findViewById(R.id.back_button);
103         ImageButton fullscreenButton = findViewById(R.id.fullscreen_btn);
104 
105         Drawable roundBackground = getResources().getDrawable(R.drawable.ic_round_bg, theme);
106         backButton.setBackground(roundBackground);
107         fullscreenButton.setBackground(roundBackground);
108 
109         Drawable backArrowIcon = getResources().getDrawable(R.drawable.ic_arrow_back_32, theme);
110         backButton.setImageDrawable(backArrowIcon);
111 
112         Drawable fullscreenIcon = getResources().getDrawable(R.drawable.ic_fullscreen_32, theme);
113         fullscreenButton.setImageDrawable(fullscreenIcon);
114     }
115 
116     /** Update the view according to the given {@link ComponentName} */
update(@onNull ComponentName componentName)117     public void update(@NonNull ComponentName componentName) {
118         try {
119             if (mCarPackageManager != null
120                     && mCarPackageManager.requiresDisplayCompat(componentName.getPackageName())) {
121                 showToolbar();
122                 return;
123             }
124         } catch (NameNotFoundException e) { /* ignore */}
125         hideToolbar();
126     }
127 
showToolbar()128     private void showToolbar() {
129         mDisplayCompatToolbar.setVisibility(View.VISIBLE);
130     }
131 
hideToolbar()132     private void hideToolbar() {
133         mDisplayCompatToolbar.setVisibility(View.GONE);
134     }
135 }
136