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 45 private Handler mHandler; 46 private int mAnimationDuration; 47 HeadsUpContainerView(@onNull Context context)48 public HeadsUpContainerView(@NonNull Context context) { 49 super(context); 50 init(); 51 } 52 HeadsUpContainerView(@onNull Context context, @Nullable AttributeSet attrs)53 public HeadsUpContainerView(@NonNull Context context, @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, int defStyleRes) { 66 super(context, attrs, defStyleAttr, defStyleRes); 67 init(); 68 } 69 init()70 private void init() { 71 mHandler = new Handler(Looper.getMainLooper()); 72 mAnimationDuration = getResources().getInteger(R.integer.headsup_total_enter_duration_ms); 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) { 93 // Request focus for the topmost child if one of the children is already focused. 94 // Wait for the duration of the heads-up animation for a smoother UI experience. 95 mHandler.postDelayed(() -> focusTopmostChild(), mAnimationDuration); 96 } 97 } 98 focusTopmostChild()99 private boolean focusTopmostChild() { 100 int childCount = getChildCount(); 101 if (childCount <= 0) { 102 return false; 103 } 104 105 View topmostChild = getChildAt(childCount - 1); 106 if (!(topmostChild instanceof FocusArea)) { 107 return false; 108 } 109 110 FocusArea focusArea = (FocusArea) topmostChild; 111 View view = focusArea.findViewById(R.id.action_1); 112 if (view != null) { 113 focusArea.setDefaultFocus(view); 114 } 115 116 return topmostChild.performAccessibilityAction(ACTION_FOCUS, /* arguments= */ null); 117 } 118 119 @VisibleForTesting setHandler(Handler handler)120 void setHandler(Handler handler) { 121 mHandler = handler; 122 } 123 } 124