1 /* 2 * Copyright (C) 2021 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.systemui.screenshot; 18 19 import static android.view.ViewGroup.LayoutParams.MATCH_PARENT; 20 import static android.view.WindowManager.LayoutParams.TYPE_SCREENSHOT; 21 22 import android.content.Context; 23 import android.graphics.PixelFormat; 24 import android.util.DisplayMetrics; 25 import android.view.Window; 26 import android.view.WindowManager; 27 28 import com.android.internal.policy.PhoneWindow; 29 30 /** 31 * Utility methods for setting up a floating window 32 */ 33 public class FloatingWindowUtil { 34 35 /** 36 * Convert input dp to pixels given DisplayMetrics 37 */ dpToPx(DisplayMetrics metrics, float dp)38 public static float dpToPx(DisplayMetrics metrics, float dp) { 39 return dp * metrics.densityDpi / (float) DisplayMetrics.DENSITY_DEFAULT; 40 } 41 42 /** 43 * Sets up window params for a floating window 44 */ getFloatingWindowParams()45 public static WindowManager.LayoutParams getFloatingWindowParams() { 46 WindowManager.LayoutParams params = new WindowManager.LayoutParams( 47 MATCH_PARENT, MATCH_PARENT, /* xpos */ 0, /* ypos */ 0, TYPE_SCREENSHOT, 48 WindowManager.LayoutParams.FLAG_FULLSCREEN 49 | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN 50 | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL 51 | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH 52 | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED 53 | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM, 54 PixelFormat.TRANSLUCENT); 55 params.layoutInDisplayCutoutMode = 56 WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS; 57 params.setFitInsetsTypes(0); 58 // This is needed to let touches pass through outside the touchable areas 59 params.privateFlags |= WindowManager.LayoutParams.PRIVATE_FLAG_TRUSTED_OVERLAY; 60 return params; 61 } 62 63 /** 64 * Constructs a transparent floating window 65 */ getFloatingWindow(Context context)66 public static PhoneWindow getFloatingWindow(Context context) { 67 PhoneWindow window = new PhoneWindow(context); 68 window.requestFeature(Window.FEATURE_NO_TITLE); 69 window.requestFeature(Window.FEATURE_ACTIVITY_TRANSITIONS); 70 window.setBackgroundDrawableResource(android.R.color.transparent); 71 return window; 72 } 73 74 75 } 76