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 import android.annotation.SuppressLint 20 import android.os.CancellationSignal 21 import android.os.OutcomeReceiver 22 import androidx.annotation.RestrictTo 23 import androidx.appfunctions.internal.Dependencies 24 import androidx.appfunctions.internal.Dispatchers 25 import com.android.extensions.appfunctions.AppFunctionException as ExtensionAppFunctionException 26 import com.android.extensions.appfunctions.AppFunctionService 27 import com.android.extensions.appfunctions.ExecuteAppFunctionRequest as ExtensionExecuteAppFunctionRequest 28 import com.android.extensions.appfunctions.ExecuteAppFunctionResponse as ExtensionExecuteAppFunctionResponse 29 30 /** The implementation of [AppFunctionService] from extension library. */ 31 @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) 32 @SuppressLint("NewApi") 33 public class ExtensionAppFunctionService : AppFunctionService() { 34 35 private lateinit var delegate: AppFunctionServiceDelegate 36 onCreatenull37 override fun onCreate() { 38 super.onCreate() 39 delegate = 40 AppFunctionServiceDelegate( 41 this@ExtensionAppFunctionService, 42 Dispatchers.Worker, 43 Dispatchers.Main, 44 Dependencies.aggregatedAppFunctionInventory, 45 Dependencies.aggregatedAppFunctionInvoker, 46 Dependencies.translatorSelector 47 ) 48 } 49 onExecuteFunctionnull50 override fun onExecuteFunction( 51 request: ExtensionExecuteAppFunctionRequest, 52 callingPackage: String, 53 cancellationSignal: CancellationSignal, 54 callback: 55 OutcomeReceiver<ExtensionExecuteAppFunctionResponse, ExtensionAppFunctionException> 56 ) { 57 val executionJob = 58 delegate.onExecuteFunction( 59 androidx.appfunctions.ExecuteAppFunctionRequest.fromPlatformExtensionClass(request), 60 callingPackage, 61 object : 62 OutcomeReceiver< 63 ExecuteAppFunctionResponse, 64 AppFunctionException, 65 > { 66 override fun onResult(result: ExecuteAppFunctionResponse) { 67 when (result) { 68 is ExecuteAppFunctionResponse.Success -> { 69 callback.onResult(result.toPlatformExtensionClass()) 70 } 71 is ExecuteAppFunctionResponse.Error -> { 72 callback.onError(result.error.toPlatformExtensionsClass()) 73 } 74 } 75 } 76 77 override fun onError(error: AppFunctionException) { 78 callback.onError(error.toPlatformExtensionsClass()) 79 } 80 }, 81 ) 82 cancellationSignal.setOnCancelListener { executionJob.cancel() } 83 } 84 onDestroynull85 override fun onDestroy() { 86 super.onDestroy() 87 delegate.onDestroy() 88 } 89 } 90