• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
<lambda>null2  * Copyright (C) 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.search
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.events.Event
24 import com.android.photopicker.core.events.RegisteredEventClass
25 import com.android.photopicker.core.features.FeatureManager
26 import com.android.photopicker.core.features.FeatureRegistration
27 import com.android.photopicker.core.features.FeatureToken
28 import com.android.photopicker.core.features.Location
29 import com.android.photopicker.core.features.LocationParams
30 import com.android.photopicker.core.features.PhotopickerUiFeature
31 import com.android.photopicker.core.features.PrefetchResultKey
32 import com.android.photopicker.core.features.Priority
33 import com.android.photopicker.data.PrefetchDataService
34 import com.android.photopicker.features.search.model.GlobalSearchState
35 import kotlinx.coroutines.Deferred
36 import kotlinx.coroutines.runBlocking
37 
38 /** Feature class for the Photopicker's search functionality. */
39 class SearchFeature : PhotopickerUiFeature {
40 
41     companion object Registration : FeatureRegistration {
42         override val TAG: String = "SearchFeature"
43 
44         override fun getPrefetchRequest(
45             config: PhotopickerConfiguration
46         ): Map<PrefetchResultKey, suspend (PrefetchDataService) -> Any?>? {
47             return if (
48                 config.flags.PICKER_SEARCH_ENABLED &&
49                     config.action != MediaStore.ACTION_USER_SELECT_IMAGES_FOR_APP
50             ) {
51                 mapOf(
52                     PrefetchResultKey.SEARCH_STATE to
53                         { prefetchDataService ->
54                             prefetchDataService.getGlobalSearchState()
55                         }
56                 )
57             } else {
58                 null
59             }
60         }
61 
62         override fun isEnabled(
63             config: PhotopickerConfiguration,
64             deferredPrefetchResultsMap: Map<PrefetchResultKey, Deferred<Any?>>,
65         ): Boolean {
66             // Search feature is not enabled in permission mode.
67             if (config.action == MediaStore.ACTION_USER_SELECT_IMAGES_FOR_APP) return false
68 
69             if (!config.flags.PICKER_SEARCH_ENABLED) return false
70 
71             return runBlocking {
72                 val searchStatus: Any? =
73                     deferredPrefetchResultsMap[PrefetchResultKey.SEARCH_STATE]?.await()
74                 when (searchStatus) {
75                     is GlobalSearchState ->
76                         searchStatus == GlobalSearchState.ENABLED ||
77                             searchStatus == GlobalSearchState.ENABLED_IN_OTHER_PROFILES_ONLY
78                     else -> false // prefetch may have timed out
79                 }
80             }
81         }
82 
83         override fun build(featureManager: FeatureManager) = SearchFeature()
84     }
85 
86     override fun registerLocations(): List<Pair<Location, Int>> {
87         return listOf(Pair(Location.SEARCH_BAR, Priority.HIGH.priority))
88     }
89 
90     @Composable
91     override fun compose(location: Location, modifier: Modifier, params: LocationParams) {
92         when (location) {
93             Location.SEARCH_BAR -> Search(modifier, params)
94             else -> {}
95         }
96     }
97 
98     override val token = FeatureToken.SEARCH.token
99 
100     override val eventsConsumed = setOf<RegisteredEventClass>()
101 
102     /** Events produced by the search feature */
103     override val eventsProduced =
104         setOf(
105             Event.ShowSnackbarMessage::class.java,
106             Event.LogPhotopickerUIEvent::class.java,
107             Event.ReportPhotopickerSearchInfo::class.java,
108             Event.LogPhotopickerPageInfo::class.java,
109         )
110 }
111