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.server.policy; 18 19 import android.annotation.NonNull; 20 import android.app.Dialog; 21 import android.content.Context; 22 import android.os.Bundle; 23 import android.view.Gravity; 24 import android.view.View; 25 import android.view.Window; 26 import android.view.WindowManager; 27 import android.view.accessibility.AccessibilityEvent; 28 import android.widget.Button; 29 30 import com.android.internal.R; 31 32 /** 33 * Toast for side fps. This is typically shown during enrollment 34 * when a user presses the power button. 35 * 36 * This dialog is used by {@link SideFpsEventHandler} 37 */ 38 public class SideFpsToast extends Dialog { SideFpsToast(Context context)39 SideFpsToast(Context context) { 40 super(context); 41 } 42 43 @Override onCreate(Bundle savedInstanceState)44 protected void onCreate(Bundle savedInstanceState) { 45 super.onCreate(savedInstanceState); 46 setContentView(R.layout.side_fps_toast); 47 } 48 49 @Override onStart()50 protected void onStart() { 51 super.onStart(); 52 final Window window = this.getWindow(); 53 WindowManager.LayoutParams windowParams = window.getAttributes(); 54 windowParams.dimAmount = 0; 55 windowParams.flags |= WindowManager.LayoutParams.FLAG_DIM_BEHIND; 56 windowParams.gravity = Gravity.BOTTOM; 57 window.setAttributes(windowParams); 58 } 59 60 /** 61 * Sets the onClickListener for the toast dialog. 62 * @param listener 63 */ setOnClickListener(View.OnClickListener listener)64 public void setOnClickListener(View.OnClickListener listener) { 65 final Button turnOffScreen = findViewById(R.id.turn_off_screen); 66 if (turnOffScreen != null) { 67 turnOffScreen.setOnClickListener(listener); 68 } 69 } 70 71 /** 72 * When accessibility mode is on, add AccessibilityDelegate to dismiss dialog when focus is 73 * moved away from the dialog. 74 */ addAccessibilityDelegate()75 public void addAccessibilityDelegate() { 76 final Button turnOffScreen = findViewById(R.id.turn_off_screen); 77 if (turnOffScreen != null) { 78 turnOffScreen.setAccessibilityDelegate(new View.AccessibilityDelegate() { 79 @Override 80 public void onInitializeAccessibilityEvent(@NonNull View host, 81 @NonNull AccessibilityEvent event) { 82 if (event.getEventType() 83 == AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED 84 && isShowing()) { 85 dismiss(); 86 } 87 super.onInitializeAccessibilityEvent(host, event); 88 } 89 }); 90 91 } 92 } 93 } 94