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.integration.tests
18 
19 import android.app.PendingIntent
20 import android.content.Intent
21 import android.util.Log
22 import androidx.appfunctions.AppFunction
23 import androidx.appfunctions.AppFunctionContext
24 import androidx.appfunctions.AppFunctionInvalidArgumentException
25 import androidx.appfunctions.AppFunctionOpenable
26 import androidx.appfunctions.AppFunctionSerializable
27 import java.time.LocalDateTime
28 
29 @AppFunctionSerializable data class SetField<T>(val value: T)
30 
31 @AppFunctionSerializable
32 data class CreateNoteParams(
33     val title: String,
34     val content: List<String>,
35     val owner: Owner,
36     val attachments: List<Attachment>,
37 )
38 
39 // TODO(b/401517540): Test AppFunctionSerializable
40 @AppFunctionSerializable
41 data class UpdateNoteParams(
42     val title: SetField<String>? = null,
43     val nullableTitle: SetField<String?>? = null,
44     val content: SetField<List<String>>? = null,
45     val nullableContent: SetField<List<String>?>? = null,
46 )
47 
48 @AppFunctionSerializable
49 data class Owner(
50     val name: String,
51 )
52 
53 @AppFunctionSerializable
54 data class Attachment(
55     val uri: String,
56     val nested: Attachment? = null,
57 )
58 
59 @AppFunctionSerializable
60 data class Note(
61     val title: String,
62     val content: List<String>,
63     val owner: Owner,
64     val attachments: List<Attachment>,
65 )
66 
67 @AppFunctionSerializable
68 data class OpenableNote(
69     val title: String,
70     val content: List<String>,
71     val owner: Owner,
72     val attachments: List<Attachment>,
73     override val intentToOpen: PendingIntent
74 ) : AppFunctionOpenable
75 
76 @AppFunctionSerializable data class DateTime(val localDateTime: LocalDateTime)
77 
78 @Suppress("UNUSED_PARAMETER")
79 class TestFunctions {
80     @AppFunction
addnull81     fun add(appFunctionContext: AppFunctionContext, num1: Long, num2: Long) = num1 + num2
82 
83     @AppFunction
84     fun logLocalDateTime(appFunctionContext: AppFunctionContext, dateTime: DateTime) {
85         Log.d("TestFunctions", "LocalDateTime: ${dateTime.localDateTime}")
86     }
87 
88     @AppFunction
getLocalDatenull89     fun getLocalDate(appFunctionContext: AppFunctionContext): DateTime {
90         return DateTime(localDateTime = LocalDateTime.now())
91     }
92 
93     @AppFunction
doThrownull94     fun doThrow(appFunctionContext: AppFunctionContext) {
95         throw AppFunctionInvalidArgumentException("invalid")
96     }
97 
voidFunctionnull98     @AppFunction fun voidFunction(appFunctionContext: AppFunctionContext) {}
99 
100     @AppFunction
createNotenull101     fun createNote(
102         appFunctionContext: AppFunctionContext,
103         createNoteParams: CreateNoteParams
104     ): Note {
105         return Note(
106             title = createNoteParams.title,
107             content = createNoteParams.content,
108             owner = createNoteParams.owner,
109             attachments = createNoteParams.attachments
110         )
111     }
112 
113     @AppFunction
updateNotenull114     fun updateNote(
115         appFunctionContext: AppFunctionContext,
116         updateNoteParams: UpdateNoteParams,
117     ): Note {
118         return Note(
119             title =
120                 (updateNoteParams.title?.value ?: "DefaultTitle") +
121                     "_" +
122                     (updateNoteParams.nullableTitle?.value ?: "DefaultTitle"),
123             content =
124                 (updateNoteParams.content?.value ?: listOf("DefaultContent")) +
125                     (updateNoteParams.nullableContent?.value ?: listOf("DefaultContent")),
126             owner = Owner("test"),
127             attachments = listOf(),
128         )
129     }
130 
131     @AppFunction
getOpenableNotenull132     fun getOpenableNote(
133         appFunctionContext: AppFunctionContext,
134         createNoteParams: CreateNoteParams
135     ): OpenableNote {
136         return OpenableNote(
137             title = createNoteParams.title,
138             content = createNoteParams.content,
139             owner = createNoteParams.owner,
140             attachments = createNoteParams.attachments,
141             intentToOpen =
142                 PendingIntent.getActivity(
143                     appFunctionContext.context,
144                     0,
145                     Intent(),
146                     PendingIntent.FLAG_IMMUTABLE
147                 )
148         )
149     }
150 }
151 
152 @Suppress("UNUSED_PARAMETER")
153 class TestFactory {
154     private val createdByFactory: Boolean
155 
156     constructor() : this(false)
157 
158     constructor(createdByFactory: Boolean) {
159         this.createdByFactory = createdByFactory
160     }
161 
162     @AppFunction
isCreatedByFactorynull163     fun isCreatedByFactory(appFunctionContext: AppFunctionContext): Boolean = createdByFactory
164 }
165