1 /* 2 * Copyright (C) 2022 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.settings.accessibility; 18 19 import android.content.Context; 20 import android.content.res.Resources; 21 import android.graphics.drawable.ColorDrawable; 22 import android.os.Bundle; 23 import android.os.Handler; 24 import android.view.Gravity; 25 import android.view.LayoutInflater; 26 import android.view.View; 27 import android.view.View.AccessibilityDelegate; 28 import android.view.accessibility.AccessibilityNodeInfo; 29 import android.view.accessibility.AccessibilityNodeInfo.AccessibilityAction; 30 import android.widget.ImageView; 31 import android.widget.LinearLayout; 32 import android.widget.PopupWindow; 33 import android.widget.TextView; 34 35 import androidx.annotation.DrawableRes; 36 import androidx.annotation.VisibleForTesting; 37 38 import com.android.settings.R; 39 40 /** 41 * UI container for the accessibility quick settings tooltip. 42 * 43 * <p> The popup window shows the information about the operation of the quick settings. In 44 * addition, the arrow is pointing to the top center of the device to display one-off menu within 45 * {@code mCloseDelayTimeMillis} time.</p> 46 */ 47 public class AccessibilityQuickSettingsTooltipWindow extends PopupWindow { 48 49 private final Context mContext; 50 private Handler mHandler; 51 private long mCloseDelayTimeMillis; 52 AccessibilityQuickSettingsTooltipWindow(Context context)53 public AccessibilityQuickSettingsTooltipWindow(Context context) { 54 super(context); 55 this.mContext = context; 56 } 57 58 private final AccessibilityDelegate mAccessibilityDelegate = new AccessibilityDelegate() { 59 @Override 60 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) { 61 super.onInitializeAccessibilityNodeInfo(host, info); 62 final AccessibilityAction clickAction = new AccessibilityAction( 63 AccessibilityNodeInfo.ACTION_CLICK, 64 mContext.getString(R.string.accessibility_quick_settings_tooltip_dismiss)); 65 info.addAction(clickAction); 66 } 67 68 @Override 69 public boolean performAccessibilityAction(View host, int action, Bundle args) { 70 if (action == AccessibilityNodeInfo.ACTION_CLICK) { 71 dismiss(); 72 return true; 73 } 74 return super.performAccessibilityAction(host, action, args); 75 } 76 }; 77 /** 78 * Sets up {@link #AccessibilityQuickSettingsTooltipWindow}'s layout and content. 79 * 80 * @param text text to be displayed 81 * @param imageResId the resource ID of the image drawable 82 */ setup(CharSequence text, @DrawableRes int imageResId)83 public void setup(CharSequence text, @DrawableRes int imageResId) { 84 this.setup(text, imageResId, /* closeDelayTimeMillis= */ 0); 85 } 86 87 /** 88 * Sets up {@link #AccessibilityQuickSettingsTooltipWindow}'s layout and content. 89 * 90 * <p> The system will attempt to close popup window to the target duration of the threads if 91 * close delay time is positive number. </p> 92 * 93 * @param text text to be displayed 94 * @param imageResId the resource ID of the image drawable 95 * @param closeDelayTimeMillis how long the popup window be auto-closed 96 */ setup(CharSequence text, @DrawableRes int imageResId, long closeDelayTimeMillis)97 public void setup(CharSequence text, @DrawableRes int imageResId, long closeDelayTimeMillis) { 98 this.mCloseDelayTimeMillis = closeDelayTimeMillis; 99 100 setBackgroundDrawable(new ColorDrawable(mContext.getColor(android.R.color.transparent))); 101 final LayoutInflater inflater = mContext.getSystemService(LayoutInflater.class); 102 final View popupView = 103 inflater.inflate(R.layout.accessibility_qs_tooltip, /* root= */ null); 104 popupView.setFocusable(/* focusable= */ true); 105 popupView.setAccessibilityDelegate(mAccessibilityDelegate); 106 setContentView(popupView); 107 108 final ImageView imageView = getContentView().findViewById(R.id.qs_illustration); 109 imageView.setImageResource(imageResId); 110 final TextView textView = getContentView().findViewById(R.id.qs_content); 111 textView.setText(text); 112 setWidth(getWindowWidthWith(textView)); 113 setHeight(LinearLayout.LayoutParams.WRAP_CONTENT); 114 setFocusable(/* focusable= */ true); 115 setOutsideTouchable(/* touchable= */ true); 116 } 117 118 /** 119 * Displays the content view in a popup window at the top and center position. 120 * 121 * @param targetView a target view to get the {@link View#getWindowToken()} token from. 122 */ showAtTopCenter(View targetView)123 public void showAtTopCenter(View targetView) { 124 showAtLocation(targetView, Gravity.TOP | Gravity.CENTER_HORIZONTAL, 0, 0); 125 } 126 127 /** 128 * Disposes of the popup window. 129 * 130 * <p> Remove any pending posts of callbacks and sent messages for closing popup window. </p> 131 */ 132 @Override dismiss()133 public void dismiss() { 134 super.dismiss(); 135 if (mHandler != null) { 136 mHandler.removeCallbacksAndMessages(/* token= */ null); 137 } 138 } 139 140 /** 141 * Displays the content view in a popup window at the specified location. 142 * 143 * <p> The system will attempt to close popup window to the target duration of the threads if 144 * close delay time is positive number. </p> 145 * 146 * @param parent a parent view to get the {@link android.view.View#getWindowToken()} token from 147 * @param gravity the gravity which controls the placement of the popup window 148 * @param x the popup's x location offset 149 * @param y the popup's y location offset 150 */ 151 @Override showAtLocation(View parent, int gravity, int x, int y)152 public void showAtLocation(View parent, int gravity, int x, int y) { 153 super.showAtLocation(parent, gravity, x, y); 154 scheduleAutoCloseAction(); 155 } 156 scheduleAutoCloseAction()157 private void scheduleAutoCloseAction() { 158 if (mCloseDelayTimeMillis <= 0) { 159 return; 160 } 161 162 if (mHandler == null) { 163 mHandler = new Handler(mContext.getMainLooper()); 164 } 165 mHandler.removeCallbacksAndMessages(/* token= */ null); 166 mHandler.postDelayed(this::dismiss, mCloseDelayTimeMillis); 167 } 168 getWindowWidthWith(TextView textView)169 private int getWindowWidthWith(TextView textView) { 170 final int availableWindowWidth = getAvailableWindowWidth(); 171 final int widthSpec = 172 View.MeasureSpec.makeMeasureSpec(availableWindowWidth, View.MeasureSpec.AT_MOST); 173 final int heightSpec = 174 View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); 175 textView.measure(widthSpec, heightSpec); 176 return textView.getMeasuredWidth(); 177 } 178 179 @VisibleForTesting getAvailableWindowWidth()180 int getAvailableWindowWidth() { 181 final Resources res = mContext.getResources(); 182 final int padding = res.getDimensionPixelSize(R.dimen.accessibility_qs_tooltip_margin); 183 final int screenWidth = res.getDisplayMetrics().widthPixels; 184 return screenWidth - padding * 2; 185 } 186 } 187