1 /* 2 * Copyright 2024 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.photopicker.core.navigation.utils 18 19 import android.app.Activity 20 import android.content.Context 21 import android.content.ContextWrapper 22 import android.view.View 23 import android.view.Window 24 import android.view.WindowManager 25 import android.widget.FrameLayout 26 import androidx.compose.runtime.Composable 27 import androidx.compose.runtime.SideEffect 28 import androidx.compose.runtime.getValue 29 import androidx.compose.runtime.setValue 30 import androidx.compose.ui.platform.LocalView 31 import androidx.compose.ui.window.DialogWindowProvider 32 33 /** Private to limit the visibility as this is a temporary function until b/281081905 is fixed */ getActivityWindownull34@Composable private fun getActivityWindow(): Window? = LocalView.current.context.getActivityWindow() 35 36 /** Private to limit the visibility as this is a temporary function until b/281081905 is fixed */ 37 private tailrec fun Context.getActivityWindow(): Window? = 38 when (this) { 39 is Activity -> window 40 is ContextWrapper -> baseContext.getActivityWindow() 41 else -> null 42 } 43 44 /** 45 * Workaround to set the Dialog's window parameters as the same as the parent activities window 46 * attributes. This should be removed when b/281081905 is fixed and DialogProperties allows dialogs 47 * to handle system windows correctly. 48 * 49 * In the event that Photopicker is not currently running in an activity, this has no effect. 50 */ 51 @Composable SetDialogDestinationToEdgeToEdgenull52fun SetDialogDestinationToEdgeToEdge() { 53 val activityWindow = getActivityWindow() 54 val dialogWindow = (LocalView.current.parent as? DialogWindowProvider)?.window 55 val parentView = LocalView.current.parent as View 56 SideEffect { 57 if (activityWindow != null && dialogWindow != null) { 58 val attributes = WindowManager.LayoutParams() 59 attributes.copyFrom(activityWindow.attributes) 60 attributes.type = dialogWindow.attributes.type 61 dialogWindow.attributes = attributes 62 parentView.layoutParams = 63 FrameLayout.LayoutParams( 64 activityWindow.decorView.width, 65 activityWindow.decorView.height 66 ) 67 } 68 } 69 } 70