1 /* <lambda>null2 * Copyright 2018 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 @file:JvmName("ActivityNavigatorDestinationBuilderKt") 18 @file:JvmMultifileClass 19 20 package androidx.navigation 21 22 import android.app.Activity 23 import android.content.ComponentName 24 import android.content.Context 25 import android.net.Uri 26 import androidx.annotation.IdRes 27 import kotlin.reflect.KClass 28 import kotlin.reflect.KType 29 30 /** Construct a new [ActivityNavigator.Destination] */ 31 @Suppress("Deprecation") 32 @Deprecated( 33 "Use routes to build your ActivityDestination instead", 34 ReplaceWith("activity(route = id.toString()) { builder.invoke() }") 35 ) 36 public inline fun NavGraphBuilder.activity( 37 @IdRes id: Int, 38 builder: ActivityNavigatorDestinationBuilder.() -> Unit 39 ): Unit = 40 destination( 41 ActivityNavigatorDestinationBuilder(provider[ActivityNavigator::class], id).apply(builder) 42 ) 43 44 /** Construct a new [ActivityNavigator.Destination] */ 45 public inline fun NavGraphBuilder.activity( 46 route: String, 47 builder: ActivityNavigatorDestinationBuilder.() -> Unit 48 ): Unit = 49 destination( 50 ActivityNavigatorDestinationBuilder(provider[ActivityNavigator::class], route) 51 .apply(builder) 52 ) 53 54 /** 55 * Construct a new [ActivityNavigator.Destination] 56 * 57 * @param T destination's unique route from a [KClass] 58 * @param typeMap map of destination arguments' kotlin type [KType] to its respective custom 59 * [NavType]. May be empty if [T] does not use custom NavTypes. 60 * @param builder the builder used to construct the fragment destination 61 */ 62 public inline fun <reified T : Any> NavGraphBuilder.activity( 63 typeMap: Map<KType, @JvmSuppressWildcards NavType<*>> = emptyMap(), 64 builder: ActivityNavigatorDestinationBuilder.() -> Unit 65 ): Unit = 66 destination( 67 ActivityNavigatorDestinationBuilder(provider[ActivityNavigator::class], T::class, typeMap) 68 .apply(builder) 69 ) 70 71 /** DSL for constructing a new [ActivityNavigator.Destination] */ 72 @NavDestinationDsl 73 public class ActivityNavigatorDestinationBuilder : 74 NavDestinationBuilder<ActivityNavigator.Destination> { 75 private var context: Context 76 77 @Suppress("Deprecation") 78 @Deprecated( 79 "Use routes to create your ActivityNavigatorDestinationBuilder instead", 80 ReplaceWith("ActivityNavigatorDestinationBuilder(navigator, route = id.toString())") 81 ) 82 public constructor(navigator: ActivityNavigator, @IdRes id: Int) : super(navigator, id) { 83 context = navigator.context 84 } 85 86 public constructor(navigator: ActivityNavigator, route: String) : super(navigator, route) { 87 context = navigator.context 88 } 89 90 /** 91 * DSL for constructing a new [ActivityNavigator.Destination] 92 * 93 * @param navigator navigator used to create the destination 94 * @param route the route from a [KClass] of the destination 95 * @param typeMap map of destination arguments' kotlin type [KType] to its respective custom 96 * [NavType]. May be empty if [route] does not use custom NavTypes. 97 */ 98 public constructor( 99 navigator: ActivityNavigator, 100 route: KClass<out Any>, 101 typeMap: Map<KType, @JvmSuppressWildcards NavType<*>>, 102 ) : super(navigator, route, typeMap) { 103 context = navigator.context 104 } 105 106 public var targetPackage: String? = null 107 108 public var activityClass: KClass<out Activity>? = null 109 110 public var action: String? = null 111 112 public var data: Uri? = null 113 114 public var dataPattern: String? = null 115 116 override fun build(): ActivityNavigator.Destination = 117 super.build().also { destination -> 118 destination.setTargetPackage(targetPackage) 119 activityClass?.let { clazz -> 120 destination.setComponentName(ComponentName(context, clazz.java)) 121 } 122 destination.setAction(action) 123 destination.setData(data) 124 destination.setDataPattern(dataPattern) 125 } 126 } 127