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.carlauncher.recents.view;
18 
19 import android.graphics.Bitmap;
20 import android.graphics.drawable.Drawable;
21 import android.view.View;
22 import android.view.ViewTreeObserver;
23 import android.widget.ImageView;
24 
25 import androidx.annotation.NonNull;
26 import androidx.annotation.Nullable;
27 
28 import com.android.car.carlauncher.R;
29 
30 /**
31  * Builds onto BaseViewHolder by adding functionality of disabled state and adding click and touch
32  * listeners.
33  */
34 public class TaskViewHolder extends BaseTaskViewHolder {
35     private final ImageView mThumbnailImageView;
36     private final ImageView mIconImageView;
37     private final ImageView mDismissButton;
38     private final float mDisabledStateAlpha;
39     private ViewTreeObserver.OnTouchModeChangeListener mOnTouchModeChangeListener;
40 
TaskViewHolder(@onNull View itemView)41     public TaskViewHolder(@NonNull View itemView) {
42         super(itemView);
43         mThumbnailImageView = itemView.findViewById(R.id.task_thumbnail);
44         mIconImageView = itemView.findViewById(R.id.task_icon);
45         mDismissButton = itemView.findViewById(R.id.task_dismiss_button);
46         mDisabledStateAlpha = itemView.getResources().getFloat(
47                 R.dimen.disabled_recent_task_alpha);
48     }
49 
bind(@ullable Drawable icon, @Nullable Bitmap thumbnail, boolean isDisabled, @Nullable View.OnClickListener openTaskClickListener, @Nullable View.OnClickListener dismissTaskClickListener, @Nullable View.OnTouchListener taskTouchListener)50     public void bind(@Nullable Drawable icon, @Nullable Bitmap thumbnail, boolean isDisabled,
51             @Nullable View.OnClickListener openTaskClickListener,
52             @Nullable View.OnClickListener dismissTaskClickListener,
53             @Nullable View.OnTouchListener taskTouchListener) {
54         super.bind(icon, thumbnail);
55 
56         setDisabledAlpha(mThumbnailImageView, isDisabled);
57         setDisabledAlpha(mIconImageView, isDisabled);
58 
59         setListeners(mThumbnailImageView, taskTouchListener, openTaskClickListener);
60         setListeners(mIconImageView, taskTouchListener, openTaskClickListener);
61 
62         mDismissButton.setOnClickListener(dismissTaskClickListener);
63     }
64 
65     /**
66      * Callback for when the vh is attached to the window.
67      * Used to set the state for Dismiss Button depending on the touch mode. This should be done
68      * after the view is attached to the window else {@link View#isInTouchMode} would always return
69      * the default value.
70      */
attachedToWindow()71     public void attachedToWindow() {
72         setDismissButtonVisibility(itemView.isInTouchMode());
73         mOnTouchModeChangeListener = this::setDismissButtonVisibility;
74         itemView.getViewTreeObserver().addOnTouchModeChangeListener(mOnTouchModeChangeListener);
75     }
76 
77     /**
78      * Callback for when the vh is detached from the window.
79      */
detachedFromWindow()80     public void detachedFromWindow() {
81         itemView.getViewTreeObserver().removeOnTouchModeChangeListener(mOnTouchModeChangeListener);
82     }
83 
setListeners(View view, View.OnTouchListener onTouchListener, View.OnClickListener onClickListener)84     private void setListeners(View view, View.OnTouchListener onTouchListener,
85             View.OnClickListener onClickListener) {
86         view.setOnTouchListener(onTouchListener);
87         view.setOnClickListener(onClickListener);
88     }
89 
setDisabledAlpha(View view, boolean isDisabled)90     private void setDisabledAlpha(View view, boolean isDisabled) {
91         if (isDisabled) {
92             view.setAlpha(mDisabledStateAlpha);
93         } else {
94             view.setAlpha(1f);
95         }
96     }
97 
setDismissButtonVisibility(boolean isInTouchMode)98     private void setDismissButtonVisibility(boolean isInTouchMode) {
99         if (mDismissButton == null) {
100             return;
101         }
102         mDismissButton.setVisibility(isInTouchMode ? View.GONE : View.VISIBLE);
103     }
104 }
105