1 /* 2 * Copyright 2025 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 androidx.appfunctions 18 19 /** The configuration object used to customize AppFunction setup. */ 20 public class AppFunctionConfiguration 21 internal constructor( 22 /** 23 * A map of factories used to construct the enclosing classes of AppFunctions. 24 * 25 * The keys in this map are the enclosing classes of the AppFunctions to be constructed, and the 26 * values are the corresponding factory. If not provided in the map, the default no-argument 27 * constructors will be used to construct the classes. 28 */ 29 public val enclosingClassFactories: Map<Class<*>, (() -> Any)> 30 ) { 31 /** 32 * A class to provide customized [AppFunctionConfiguration] object. 33 * 34 * To provide the configuration, implements the [AppFunctionConfiguration.Provider] interface on 35 * your [android.app.Application] class. 36 */ 37 public interface Provider { 38 /** The [AppFunctionConfiguration] used to customize AppFunction setup. */ 39 public val appFunctionConfiguration: AppFunctionConfiguration 40 } 41 42 /** A builder for [AppFunctionConfiguration]. */ 43 public class Builder { 44 45 private val enclosingClassFactories = mutableMapOf<Class<*>, (() -> Any)>() 46 47 /** 48 * Adds a [factory] for creating an [enclosingClass]. 49 * 50 * If there is already a factory instance set for [enclosingClass], it will be overridden. 51 * 52 * @param enclosingClass The [Class] object representing the enclosing class to be 53 * instantiated. 54 * @param factory The factory to create the instance of [enclosingClass]. This is called by 55 * the AppFunctions framework to instantiate the class whenever an instance of 56 * [enclosingClass] is needed. 57 * @see AppFunction 58 */ addEnclosingClassFactorynull59 public fun <T : Any> addEnclosingClassFactory( 60 enclosingClass: Class<T>, 61 factory: () -> T 62 ): Builder { 63 @Suppress("UNCHECKED_CAST") 64 enclosingClassFactories[enclosingClass] = factory as () -> Any 65 return this 66 } 67 68 /** Builds the [AppFunctionConfiguration]. */ buildnull69 public fun build(): AppFunctionConfiguration { 70 return AppFunctionConfiguration(enclosingClassFactories.toMap()) 71 } 72 } 73 } 74