• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.configuration
18 
19 import android.content.Intent
20 import android.media.ApplicationMediaCapabilities
21 import com.android.photopicker.core.events.generatePickerSessionId
22 import kotlinx.coroutines.CoroutineScope
23 import kotlinx.coroutines.flow.SharingStarted
24 import kotlinx.coroutines.flow.StateFlow
25 import kotlinx.coroutines.flow.flow
26 import kotlinx.coroutines.flow.stateIn
27 
28 /**
29  * Helper function to generate a [StateFlow] that mimics the flow emitted by the
30  * [ConfigurationManager]. This flow immediately emits the provided [PhotopickerConfiguration] or a
31  * default test configuration if none is provided.
32  */
provideTestConfigurationFlownull33 fun provideTestConfigurationFlow(
34     scope: CoroutineScope,
35     defaultConfiguration: PhotopickerConfiguration = TestPhotopickerConfiguration.default(),
36 ): StateFlow<PhotopickerConfiguration> {
37 
38     return flow { emit(defaultConfiguration) }
39         .stateIn(scope, SharingStarted.Eagerly, initialValue = defaultConfiguration)
40 }
41 
42 /** Builder for a [PhotopickerConfiguration] to use in Tests. */
43 class TestPhotopickerConfiguration {
44     companion object {
45         /**
46          * Create a new [PhotopickerConfiguration]
47          *
48          * @return [PhotopickerConfiguration] with the applied properties.
49          */
buildnull50         inline fun build(block: Builder.() -> Unit) = Builder().apply(block).build()
51 
52         /**
53          * Create a new [PhotopickerConfiguration].
54          *
55          * @return [PhotopickerConfiguration] with default properties.
56          */
57         fun default() = Builder().build()
58     }
59 
60     /** Internal Builder implementation. Callers should use [TestPhotopickerConfiguration.build]. */
61     class Builder {
62         private var action: String = ""
63         private var intent: Intent? = null
64         private var selectionLimit: Int = DEFAULT_SELECTION_LIMIT
65         private var pickImagesInOrder: Boolean = false
66         private var callingPackage: String? = null
67         private var callingPackageUid: Int? = null
68         private var callingPackageLabel: String? = null
69         private var runtimeEnv: PhotopickerRuntimeEnv = PhotopickerRuntimeEnv.ACTIVITY
70         private var sessionId: Int = generatePickerSessionId()
71         private var flags: PhotopickerFlags = PhotopickerFlags()
72         private var mimeTypes: ArrayList<String> = arrayListOf("image/*", "video/*")
73         private var appMediaCapabilities: ApplicationMediaCapabilities? = null
74 
75         fun action(value: String) = apply { this.action = value }
76 
77         fun intent(value: Intent?) = apply { this.intent = value }
78 
79         fun selectionLimit(value: Int) = apply { this.selectionLimit = value }
80 
81         fun pickImagesInOrder(value: Boolean) = apply { this.pickImagesInOrder = value }
82 
83         fun callingPackage(value: String) = apply { this.callingPackage = value }
84 
85         fun callingPackageUid(value: Int) = apply { this.callingPackageUid = value }
86 
87         fun callingPackageLabel(value: String) = apply { this.callingPackageLabel = value }
88 
89         fun runtimeEnv(value: PhotopickerRuntimeEnv) = apply { this.runtimeEnv = value }
90 
91         fun sessionId(value: Int) = apply { this.sessionId = value }
92 
93         fun flags(value: PhotopickerFlags) = apply { this.flags = value }
94 
95         fun mimeTypes(value: ArrayList<String>) = apply { this.mimeTypes = value }
96 
97         fun appMediaCapabilities(value: ApplicationMediaCapabilities) = apply {
98             this.appMediaCapabilities = value
99         }
100 
101         fun build(): PhotopickerConfiguration {
102             return PhotopickerConfiguration(
103                 action = action,
104                 intent = intent,
105                 selectionLimit = selectionLimit,
106                 pickImagesInOrder = pickImagesInOrder,
107                 callingPackage = callingPackage,
108                 callingPackageUid = callingPackageUid,
109                 callingPackageLabel = callingPackageLabel,
110                 runtimeEnv = runtimeEnv,
111                 sessionId = sessionId,
112                 flags = flags,
113                 mimeTypes = mimeTypes,
114                 callingPackageMediaCapabilities = appMediaCapabilities,
115             )
116         }
117     }
118 }
119