• 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.provider.DeviceConfig
20 import java.util.concurrent.Executor
21 
22 /**
23  * A supplier interface for [DeviceConfig]. This allows for isolating the device state during tests
24  * by providing an alternate implementation to the [ConfigurationManager].
25  *
26  * For production / runtime usage that wraps the real [DeviceConfig], see [DeviceConfigProxyImpl]
27  */
28 interface DeviceConfigProxy {
29 
30     /**
31      * Register a listener to receive callbacks when flags in the backing [DeviceConfig] supplier
32      * change.
33      *
34      * @param namespace The namespace to attach this listener to.
35      * @param executor An executor for the listener to be run on. If being called from kotlin,
36      *   consider providing a `[CoroutineDispatcher].asExecutor`
37      * @param listener the implementation of [DeviceConfig.OnPropertiesChangedListener] to register.
38      */
addOnPropertiesChangedListenernull39     fun addOnPropertiesChangedListener(
40         namespace: String,
41         executor: Executor,
42         listener: DeviceConfig.OnPropertiesChangedListener,
43     )
44 
45     /** Remove the provided listener from receiving future device config flag updates. */
46     fun removeOnPropertiesChangedListener(listener: DeviceConfig.OnPropertiesChangedListener)
47 
48     /**
49      * Fetch a flag from the backing [DeviceConfig] supplier.
50      *
51      * This method assumes the caller knows the correct type of the flag they are fetching, and will
52      * expect a flag type that matches the same type as the defaultValue.
53      *
54      * This wraps the various "getFlagOfType" methods that exist in device config into one method.
55      *
56      * In the event the flag is unset, the provided defaultValue will be returned. If the flag type
57      * is incorrect, the provided defaultValue will be returned.
58      */
59     fun <T> getFlag(namespace: String, key: String, defaultValue: T): T
60 }
61