1 /*
2  * Copyright (C) 2020 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.notification.headsup;
18 
19 import static android.view.accessibility.AccessibilityNodeInfo.ACTION_FOCUS;
20 
21 import static com.android.car.ui.utils.RotaryConstants.ROTARY_FOCUS_DELEGATING_CONTAINER;
22 
23 import android.content.Context;
24 import android.graphics.Rect;
25 import android.os.Handler;
26 import android.os.Looper;
27 import android.util.AttributeSet;
28 import android.view.View;
29 import android.widget.FrameLayout;
30 
31 import androidx.annotation.NonNull;
32 import androidx.annotation.Nullable;
33 import androidx.annotation.VisibleForTesting;
34 
35 import com.android.car.notification.R;
36 import com.android.car.ui.FocusArea;
37 
38 /**
39  * Container that is used to present heads-up notifications. It is responsible for delegating the
40  * focus to the topmost notification and ensuring that new HUNs gains focus automatically when
41  * one of the existing HUNs already has focus.
42  */
43 public class HeadsUpContainerView extends FrameLayout {
44     private final boolean mFocusHUNWhenShown;
45     private final int mEnterAnimationDuration;
46     private final int mExitAnimationDuration;
47     private Handler mHandler;
48 
HeadsUpContainerView(@onNull Context context)49     public HeadsUpContainerView(@NonNull Context context) {
50         super(context);
51     }
52 
HeadsUpContainerView(@onNull Context context, @Nullable AttributeSet attrs)53     public HeadsUpContainerView(@NonNull Context context, @Nullable AttributeSet attrs) {
54         super(context, attrs);
55     }
56 
HeadsUpContainerView(@onNull Context context, @Nullable AttributeSet attrs, int defStyleAttr)57     public HeadsUpContainerView(@NonNull Context context, @Nullable AttributeSet attrs,
58             int defStyleAttr) {
59         super(context, attrs, defStyleAttr);
60     }
61 
HeadsUpContainerView(@onNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes)62     public HeadsUpContainerView(@NonNull Context context, @Nullable AttributeSet attrs,
63             int defStyleAttr, int defStyleRes) {
64         super(context, attrs, defStyleAttr, defStyleRes);
65     }
66 
67     {
68         mHandler = new Handler(Looper.getMainLooper());
69         mEnterAnimationDuration = getResources()
70                 .getInteger(R.integer.headsup_total_enter_duration_ms);
71         mExitAnimationDuration = getResources().getInteger(R.integer.headsup_exit_duration_ms);
72         mFocusHUNWhenShown = getResources().getBoolean(R.bool.config_focusHUNWhenShown);
73 
74         // This tag is required to make this container receive the focus request in order to
75         // delegate focus to its children, even though the container itself isn't focusable.
76         setContentDescription(ROTARY_FOCUS_DELEGATING_CONTAINER);
77         setClickable(false);
78     }
79 
80     @Override
onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect)81     protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
82         if (isInTouchMode()) {
83             return super.onRequestFocusInDescendants(direction, previouslyFocusedRect);
84         }
85         return focusTopmostChild();
86     }
87 
88     @Override
addView(View child)89     public void addView(View child) {
90         super.addView(child);
91 
92         if (!isInTouchMode() && (getFocusedChild() != null || mFocusHUNWhenShown)
93                 && !topmostChildHasFocus()) {
94             // Wait for the duration of the heads-up enter animation for a smoother UI experience.
95             mHandler.postDelayed(() -> focusTopmostChild(), mEnterAnimationDuration);
96         }
97     }
98 
99     @Override
removeViewAt(int index)100     public void removeViewAt(int index) {
101         super.removeViewAt(index);
102 
103         if (!isInTouchMode() && mFocusHUNWhenShown && index == getChildCount()
104                 && getChildCount() > 0 && !topmostChildHasFocus()) {
105             // Wait for the duration of the heads-up exit animation for a smoother UI experience.
106             mHandler.postDelayed(() -> focusTopmostChild(), mExitAnimationDuration);
107         }
108     }
109 
topmostChildHasFocus()110     private boolean topmostChildHasFocus() {
111         int childCount = getChildCount();
112         if (childCount <= 0) {
113             return false;
114         }
115 
116         View topmostChild = getChildAt(childCount - 1);
117         if (!(topmostChild instanceof FocusArea)) {
118             return false;
119         }
120 
121         return topmostChild.hasFocus();
122     }
123 
focusTopmostChild()124     private boolean focusTopmostChild() {
125         int childCount = getChildCount();
126         if (childCount <= 0) {
127             return false;
128         }
129 
130         View topmostChild = getChildAt(childCount - 1);
131         if (!(topmostChild instanceof FocusArea)) {
132             return false;
133         }
134 
135         FocusArea focusArea = (FocusArea) topmostChild;
136         View view = focusArea.findViewById(R.id.action_1);
137         if (view != null) {
138             focusArea.setDefaultFocus(view);
139         }
140 
141         return topmostChild.performAccessibilityAction(ACTION_FOCUS, /* arguments= */ null);
142     }
143 
144     @VisibleForTesting
setHandler(Handler handler)145     void setHandler(Handler handler) {
146         mHandler = handler;
147     }
148 }
149