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.cloudmedia
18 
19 import android.content.Intent
20 import android.provider.MediaStore
21 import androidx.compose.runtime.Composable
22 import androidx.compose.ui.Modifier
23 import androidx.compose.ui.platform.LocalContext
24 import androidx.compose.ui.res.stringResource
25 import com.android.photopicker.R
26 import com.android.photopicker.core.configuration.PhotopickerConfiguration
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.Route
36 import com.android.photopicker.features.overflowmenu.OverflowMenuItem
37 
38 /**
39  * Feature class for the Photopicker's cloud media implementation.
40  *
41  * This feature adds the Cloud media preloader for preloading off-device content before the
42  * Photopicker session ends.
43  */
44 class CloudMediaFeature : PhotopickerUiFeature {
45     companion object Registration : FeatureRegistration {
46         override val TAG: String = "PhotopickerCloudMediaFeature"
47 
isEnablednull48         override fun isEnabled(config: PhotopickerConfiguration): Boolean {
49 
50             // Cloud media is not available in permission mode.
51             if (config.action == MediaStore.ACTION_USER_SELECT_IMAGES_FOR_APP) return false
52 
53             return true
54         }
55 
buildnull56         override fun build(featureManager: FeatureManager) = CloudMediaFeature()
57     }
58 
59     override val token = FeatureToken.CLOUD_MEDIA.token
60 
61     /** Events consumed by Cloud Media */
62     override val eventsConsumed = setOf<RegisteredEventClass>()
63 
64     /** Events produced by the Cloud Media */
65     override val eventsProduced = setOf<RegisteredEventClass>()
66 
67     override fun registerLocations(): List<Pair<Location, Int>> {
68         return listOf(
69             Pair(Location.MEDIA_PRELOADER, Priority.HIGH.priority),
70             Pair(Location.OVERFLOW_MENU_ITEMS, Priority.HIGH.priority),
71         )
72     }
73 
registerNavigationRoutesnull74     override fun registerNavigationRoutes(): Set<Route> {
75         return setOf()
76     }
77 
78     @Composable
composenull79     override fun compose(
80         location: Location,
81         modifier: Modifier,
82         params: LocationParams,
83     ) {
84         when (location) {
85             Location.MEDIA_PRELOADER -> MediaPreloader(modifier, params)
86             Location.OVERFLOW_MENU_ITEMS -> {
87                 val context = LocalContext.current
88                 val clickAction = params as? LocationParams.WithClickAction
89                 OverflowMenuItem(
90                     label = stringResource(R.string.photopicker_overflow_cloud_media_app),
91                     onClick = {
92                         clickAction?.onClick()
93                         context.startActivity(Intent(MediaStore.ACTION_PICK_IMAGES_SETTINGS))
94                     }
95                 )
96             }
97             else -> {}
98         }
99     }
100 }
101