1 /*
2  * Copyright 2021 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.glance.action
18 
19 import android.app.Activity
20 import android.content.ComponentName
21 import android.os.Bundle
22 import androidx.annotation.RestrictTo
23 import androidx.glance.ExperimentalGlanceApi
24 
25 @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
26 interface StartActivityAction : Action {
27     val parameters: ActionParameters
28     val activityOptions: Bundle?
29 }
30 
31 @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
32 class StartActivityComponentAction(
33     val componentName: ComponentName,
34     override val parameters: ActionParameters,
35     override val activityOptions: Bundle?,
36 ) : StartActivityAction
37 
38 @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
39 class StartActivityClassAction(
40     val activityClass: Class<out Activity>,
41     override val parameters: ActionParameters,
42     override val activityOptions: Bundle?,
43 ) : StartActivityAction
44 
45 /**
46  * Creates an [Action] that launches the [Activity] specified by the given [ComponentName].
47  *
48  * @param componentName component of the activity to launch
49  * @param parameters the parameters associated with the action. Parameter values will be added to
50  *   the activity intent, keyed by the parameter key name string.
51  */
actionStartActivitynull52 fun actionStartActivity(
53     componentName: ComponentName,
54     parameters: ActionParameters = actionParametersOf(),
55 ): Action = StartActivityComponentAction(componentName, parameters, null)
56 
57 /**
58  * Creates an [Action] that launches the [Activity] specified by the given [ComponentName].
59  *
60  * @param componentName component of the activity to launch
61  * @param parameters the parameters associated with the action. Parameter values will be added to
62  *   the activity intent, keyed by the parameter key name string.
63  * @param activityOptions Additional options built from an [android.app.ActivityOptions] to apply to
64  *   an activity start.
65  */
66 @ExperimentalGlanceApi
67 fun actionStartActivity(
68     componentName: ComponentName,
69     parameters: ActionParameters = actionParametersOf(),
70     activityOptions: Bundle? = null,
71 ): Action = StartActivityComponentAction(componentName, parameters, activityOptions)
72 
73 /**
74  * Creates an [Action] that launches the specified [Activity] when triggered.
75  *
76  * @param activity class of the activity to launch
77  * @param parameters the parameters associated with the action. Parameter values will be added to
78  *   the activity intent, keyed by the parameter key name string.
79  */
80 fun <T : Activity> actionStartActivity(
81     activity: Class<T>,
82     parameters: ActionParameters = actionParametersOf(),
83 ): Action = StartActivityClassAction(activity, parameters, null)
84 
85 /**
86  * Creates an [Action] that launches the specified [Activity] when triggered.
87  *
88  * @param activity class of the activity to launch
89  * @param parameters the parameters associated with the action. Parameter values will be added to
90  *   the activity intent, keyed by the parameter key name string.
91  * @param activityOptions Additional options built from an [android.app.ActivityOptions] to apply to
92  *   an activity start.
93  */
94 @ExperimentalGlanceApi
95 fun <T : Activity> actionStartActivity(
96     activity: Class<T>,
97     parameters: ActionParameters = actionParametersOf(),
98     activityOptions: Bundle? = null,
99 ): Action = StartActivityClassAction(activity, parameters, activityOptions)
100 
101 @Suppress("MissingNullability")
102 /* Shouldn't need to specify @NonNull. b/199284086 */
103 /**
104  * Creates an [Action] that launches the specified [Activity] when triggered.
105  *
106  * @param parameters the parameters associated with the action. Parameter values will be added to
107  *   the activity intent, keyed by the parameter key name string.
108  */
109 inline fun <reified T : Activity> actionStartActivity(
110     parameters: ActionParameters = actionParametersOf(),
111 ): Action = actionStartActivity(T::class.java, parameters)
112 
113 @Suppress("MissingNullability")
114 /* Shouldn't need to specify @NonNull. b/199284086 */
115 /**
116  * Creates an [Action] that launches the specified [Activity] when triggered.
117  *
118  * @param parameters the parameters associated with the action. Parameter values will be added to
119  *   the activity intent, keyed by the parameter key name string.
120  * @param activityOptions Additional options built from an [android.app.ActivityOptions] to apply to
121  *   an activity start.
122  */
123 @ExperimentalGlanceApi
124 inline fun <reified T : Activity> actionStartActivity(
125     parameters: ActionParameters = actionParametersOf(),
126     activityOptions: Bundle? = null,
127 ): Action = actionStartActivity(T::class.java, parameters, activityOptions)
128