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.features.preview 18 19 import androidx.compose.runtime.Composable 20 import androidx.compose.ui.Modifier 21 import androidx.compose.ui.window.DialogProperties 22 import androidx.navigation.NamedNavArgument 23 import androidx.navigation.NavBackStackEntry 24 import androidx.navigation.NavDeepLink 25 import com.android.photopicker.core.configuration.PhotopickerConfiguration 26 import com.android.photopicker.core.events.Event 27 import com.android.photopicker.core.events.RegisteredEventClass 28 import com.android.photopicker.core.features.FeatureManager 29 import com.android.photopicker.core.features.FeatureRegistration 30 import com.android.photopicker.core.features.FeatureToken 31 import com.android.photopicker.core.features.Location 32 import com.android.photopicker.core.features.LocationParams 33 import com.android.photopicker.core.features.PhotopickerUiFeature 34 import com.android.photopicker.core.features.Priority 35 import com.android.photopicker.core.navigation.PhotopickerDestinations 36 import com.android.photopicker.core.navigation.Route 37 import com.android.photopicker.core.navigation.utils.SetDialogDestinationToEdgeToEdge 38 import com.android.photopicker.data.model.Media 39 40 /** 41 * Feature class for the Photopicker's media preview. 42 * 43 * This feature adds the [PREVIEW_MEDIA] and [PREVIEW_SELECTION] routes to the application. 44 */ 45 class PreviewFeature : PhotopickerUiFeature { 46 47 companion object Registration : FeatureRegistration { 48 override val TAG: String = "PhotopickerPreviewFeature" 49 isEnablednull50 override fun isEnabled(config: PhotopickerConfiguration) = true 51 52 override fun build(featureManager: FeatureManager) = PreviewFeature() 53 54 val PREVIEW_MEDIA_KEY = "preview_media" 55 } 56 57 override val token = FeatureToken.PREVIEW.token 58 59 /** Events consumed by the Preview page */ 60 override val eventsConsumed = emptySet<RegisteredEventClass>() 61 62 /** Events produced by the Preview page */ 63 override val eventsProduced = 64 setOf<RegisteredEventClass>(Event.MediaSelectionConfirmed::class.java) 65 66 override fun registerLocations(): List<Pair<Location, Int>> { 67 return listOf( 68 Pair(Location.SELECTION_BAR_SECONDARY_ACTION, Priority.HIGH.priority), 69 ) 70 } 71 registerNavigationRoutesnull72 override fun registerNavigationRoutes(): Set<Route> { 73 return setOf( 74 object : Route { 75 override val route = PhotopickerDestinations.PREVIEW_SELECTION.route 76 override val initialRoutePriority = Priority.LAST.priority 77 override val arguments = emptyList<NamedNavArgument>() 78 override val deepLinks = emptyList<NavDeepLink>() 79 override val isDialog = true 80 override val dialogProperties = 81 DialogProperties( 82 dismissOnBackPress = true, 83 dismissOnClickOutside = true, 84 // decorFitsSystemWindows = true doesn't currently allow dialogs to 85 // go full edge-to-edge. Until b/281081905 is fixed, use a workaround that 86 // involves setting usePlatformDefaultWidth = true and copying the 87 // attributes in the parent window. 88 usePlatformDefaultWidth = true, // is true to get the hack to work. 89 decorFitsSystemWindows = false, 90 ) 91 92 override val enterTransition = null 93 override val exitTransition = null 94 override val popEnterTransition = null 95 override val popExitTransition = null 96 97 @Composable 98 override fun composable( 99 navBackStackEntry: NavBackStackEntry?, 100 ) { 101 // Until b/281081905 is fixed, use a workaround to enable edge-to-edge in the 102 // dialog 103 SetDialogDestinationToEdgeToEdge() 104 PreviewSelection() 105 } 106 }, 107 object : Route { 108 override val route = PhotopickerDestinations.PREVIEW_MEDIA.route 109 override val initialRoutePriority = Priority.LAST.priority 110 override val arguments = emptyList<NamedNavArgument>() 111 override val deepLinks = emptyList<NavDeepLink>() 112 override val isDialog = true 113 override val dialogProperties = 114 DialogProperties( 115 dismissOnBackPress = true, 116 dismissOnClickOutside = true, 117 // decorFitsSystemWindows = true doesn't currently allow dialogs to 118 // go full edge-to-edge. Until b/281081905 is fixed, use a workaround that 119 // involves setting usePlatformDefaultWidth = true and copying the 120 // attributes in the parent window. 121 usePlatformDefaultWidth = true, // is true to get the hack to work. 122 decorFitsSystemWindows = false, 123 ) 124 125 override val enterTransition = null 126 override val exitTransition = null 127 override val popEnterTransition = null 128 override val popExitTransition = null 129 130 @Composable 131 override fun composable( 132 navBackStackEntry: NavBackStackEntry?, 133 ) { 134 val flow = 135 checkNotNull( 136 navBackStackEntry 137 ?.savedStateHandle 138 ?.getStateFlow<Media?>(PREVIEW_MEDIA_KEY, null) 139 ) { 140 "Unable to get a savedStateHandle for preview media" 141 } 142 // Until b/281081905 is fixed, use a workaround to enable edge-to-edge in the 143 // dialog 144 SetDialogDestinationToEdgeToEdge() 145 PreviewMedia(flow) 146 } 147 }, 148 ) 149 } 150 151 @Composable composenull152 override fun compose( 153 location: Location, 154 modifier: Modifier, 155 params: LocationParams, 156 ) { 157 when (location) { 158 Location.SELECTION_BAR_SECONDARY_ACTION -> PreviewSelectionButton(modifier) 159 else -> {} 160 } 161 } 162 } 163