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.preparemedia 18 19 import android.provider.MediaStore 20 import androidx.compose.runtime.Composable 21 import androidx.compose.ui.Modifier 22 import com.android.photopicker.core.configuration.PhotopickerConfiguration 23 import com.android.photopicker.core.configuration.PhotopickerRuntimeEnv 24 import com.android.photopicker.core.events.Event 25 import com.android.photopicker.core.events.RegisteredEventClass 26 import com.android.photopicker.core.features.FeatureManager 27 import com.android.photopicker.core.features.FeatureRegistration 28 import com.android.photopicker.core.features.FeatureToken 29 import com.android.photopicker.core.features.Location 30 import com.android.photopicker.core.features.LocationParams 31 import com.android.photopicker.core.features.PhotopickerUiFeature 32 import com.android.photopicker.core.features.PrefetchResultKey 33 import com.android.photopicker.core.features.Priority 34 import com.android.photopicker.core.navigation.Route 35 import com.android.photopicker.features.cloudmedia.CloudMediaFeature 36 import kotlinx.coroutines.Deferred 37 38 /** 39 * Feature class for the Photopicker's media preparation implementation. 40 * 41 * This feature adds the preparer for preparing media content before the Photopicker session ends. 42 */ 43 class PrepareMediaFeature : PhotopickerUiFeature { 44 companion object Registration : FeatureRegistration { 45 override val TAG: String = "PrepareMediaFeature" 46 isEnablednull47 override fun isEnabled( 48 config: PhotopickerConfiguration, 49 deferredPrefetchResultsMap: Map<PrefetchResultKey, Deferred<Any?>>, 50 ): Boolean { 51 with(config) { 52 if (runtimeEnv != PhotopickerRuntimeEnv.ACTIVITY) { 53 return false 54 } 55 56 // Media Prepare is not available in permission mode. 57 if (action == MediaStore.ACTION_USER_SELECT_IMAGES_FOR_APP) { 58 return false 59 } 60 61 val pickerTranscodingEnabled = 62 flags.PICKER_TRANSCODING_ENABLED && callingPackageMediaCapabilities != null 63 return pickerTranscodingEnabled || CloudMediaFeature.isEnabled(this) 64 } 65 } 66 buildnull67 override fun build(featureManager: FeatureManager) = PrepareMediaFeature() 68 } 69 70 override val token = FeatureToken.MEDIA_PREPARE.token 71 72 override val eventsConsumed = setOf<RegisteredEventClass>() 73 74 override val eventsProduced = setOf(Event.LogPhotopickerUIEvent::class.java) 75 76 override fun registerLocations(): List<Pair<Location, Int>> { 77 return listOf(Pair(Location.MEDIA_PREPARER, Priority.HIGH.priority)) 78 } 79 registerNavigationRoutesnull80 override fun registerNavigationRoutes(): Set<Route> { 81 return setOf() 82 } 83 84 @Composable composenull85 override fun compose(location: Location, modifier: Modifier, params: LocationParams) { 86 when (location) { 87 Location.MEDIA_PREPARER -> MediaPreparer(modifier, params) 88 else -> {} 89 } 90 } 91 } 92