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 34 import com.android.car.notification.R; 35 import com.android.car.ui.FocusArea; 36 37 /** 38 * Container that is used to present heads-up notifications. It is responsible for delegating the 39 * focus to the topmost notification and ensuring that new HUNs gains focus automatically when 40 * one of the existing HUNs already has focus. 41 */ 42 public class HeadsUpContainerView extends FrameLayout { 43 44 private Handler mHandler; 45 private int mAnimationDuration; 46 HeadsUpContainerView(@onNull Context context)47 public HeadsUpContainerView(@NonNull Context context) { 48 super(context); 49 init(); 50 } 51 HeadsUpContainerView(@onNull Context context, @Nullable AttributeSet attrs)52 public HeadsUpContainerView(@NonNull Context context, 53 @Nullable AttributeSet attrs) { 54 super(context, attrs); 55 init(); 56 } 57 HeadsUpContainerView(@onNull Context context, @Nullable AttributeSet attrs, int defStyleAttr)58 public HeadsUpContainerView(@NonNull Context context, @Nullable AttributeSet attrs, 59 int defStyleAttr) { 60 super(context, attrs, defStyleAttr); 61 init(); 62 } 63 HeadsUpContainerView(@onNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes)64 public HeadsUpContainerView(@NonNull Context context, @Nullable AttributeSet attrs, 65 int defStyleAttr, 66 int defStyleRes) { 67 super(context, attrs, defStyleAttr, defStyleRes); 68 init(); 69 } 70 init()71 private void init() { 72 mHandler = new Handler(Looper.getMainLooper()); 73 mAnimationDuration = getResources().getInteger(R.integer.headsup_total_enter_duration_ms); 74 75 // This tag is required to make this container receive the focus request in order to 76 // delegate focus to its children, even though the container itself isn't focusable. 77 setContentDescription(ROTARY_FOCUS_DELEGATING_CONTAINER); 78 setClickable(false); 79 } 80 81 @Override onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect)82 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) { 83 if (isInTouchMode()) { 84 return super.onRequestFocusInDescendants(direction, previouslyFocusedRect); 85 } 86 return focusTopmostChild(); 87 } 88 89 @Override addView(View child)90 public void addView(View child) { 91 super.addView(child); 92 93 if (!isInTouchMode() && getFocusedChild() != null) { 94 // Request focus for the topmost child if one of the children is already focused. 95 // Wait for the duration of the heads-up animation for a smoother UI experience. 96 mHandler.postDelayed(() -> focusTopmostChild(), mAnimationDuration); 97 } 98 } 99 focusTopmostChild()100 private boolean focusTopmostChild() { 101 int childCount = getChildCount(); 102 if (childCount <= 0) { 103 return false; 104 } 105 106 View topmostChild = getChildAt(childCount - 1); 107 if (!(topmostChild instanceof FocusArea)) { 108 return false; 109 } 110 111 FocusArea focusArea = (FocusArea) topmostChild; 112 View view = focusArea.findViewById(R.id.action_1); 113 if (view != null) { 114 focusArea.setDefaultFocus(view); 115 } 116 117 return topmostChild.performAccessibilityAction(ACTION_FOCUS, /* arguments= */ null); 118 } 119 } 120