• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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 android.app.appsearch.testutil.functions;
18 
19 import static android.app.appsearch.AppSearchResult.RESULT_NOT_FOUND;
20 import static android.app.appsearch.AppSearchResult.newFailedResult;
21 import static android.app.appsearch.AppSearchResult.newSuccessfulResult;
22 import static android.app.appsearch.functions.ExecuteAppFunctionResponse.PROPERTY_RESULT;
23 
24 import android.app.appsearch.AppSearchResult;
25 import android.app.appsearch.GenericDocument;
26 import android.app.appsearch.functions.AppFunctionService;
27 import android.app.appsearch.functions.ExecuteAppFunctionRequest;
28 import android.app.appsearch.functions.ExecuteAppFunctionResponse;
29 import android.content.Intent;
30 
31 import androidx.annotation.NonNull;
32 
33 import java.util.concurrent.Executor;
34 import java.util.concurrent.Executors;
35 import java.util.function.Consumer;
36 
37 /**
38  * An implementation of {@link android.app.appsearch.functions.AppFunctionService} that provides
39  * some simple functions for testing purposes.
40  */
41 public class TestAppFunctionService extends AppFunctionService {
42     private final Executor mExecutor = Executors.newSingleThreadExecutor();
43 
44     @Override
onCreate()45     public void onCreate() {
46         super.onCreate();
47         TestAppFunctionServiceLifecycleReceiver.notifyOnCreateInvoked(this);
48     }
49 
50     @Override
onExecuteFunction( @onNull ExecuteAppFunctionRequest request, @NonNull Consumer<AppSearchResult<ExecuteAppFunctionResponse>> callback)51     public void onExecuteFunction(
52             @NonNull ExecuteAppFunctionRequest request,
53             @NonNull Consumer<AppSearchResult<ExecuteAppFunctionResponse>> callback) {
54         switch (request.getFunctionIdentifier()) {
55             case "add": {
56                 ExecuteAppFunctionResponse result = add(request);
57                 callback.accept(newSuccessfulResult(result));
58                 break;
59             }
60             case "add_invokeCallbackTwice": {
61                 ExecuteAppFunctionResponse result = add(request);
62                 callback.accept(newSuccessfulResult(result));
63                 callback.accept(newSuccessfulResult(result));
64                 break;
65             }
66             case "startActivity": {
67                 ExecuteAppFunctionResponse result = startActivity(request);
68                 callback.accept(newSuccessfulResult(result));
69                 break;
70             }
71             case "throwException": {
72                 throw new RuntimeException();
73             }
74             case "kill": {
75                 System.exit(0);
76                 break;
77             }
78             case "notInvokeCallback": {
79                 break;
80             }
81             case "addAsync": {
82                 mExecutor.execute(() -> {
83                     ExecuteAppFunctionResponse result = add(request);
84                     callback.accept(newSuccessfulResult(result));
85                 });
86                 break;
87             }
88             case "noOp": {
89                 callback.accept(
90                         newSuccessfulResult(new ExecuteAppFunctionResponse.Builder().build()));
91                 break;
92             }
93             default:
94                 callback.accept(newFailedResult(RESULT_NOT_FOUND, "no such method"));
95         }
96     }
97 
add(ExecuteAppFunctionRequest request)98     private ExecuteAppFunctionResponse add(ExecuteAppFunctionRequest request) {
99         long a = request.getParameters().getPropertyLong("a");
100         long b = request.getParameters().getPropertyLong("b");
101         GenericDocument result = new GenericDocument.Builder<>("", "", "")
102                 .setPropertyLong(PROPERTY_RESULT, a + b)
103                 .build();
104         return new ExecuteAppFunctionResponse.Builder().setResult(result).build();
105     }
106 
startActivity(ExecuteAppFunctionRequest request)107     private ExecuteAppFunctionResponse startActivity(ExecuteAppFunctionRequest request) {
108         Intent intent = new Intent(this, ActivityCreationSynchronizer.class);
109         intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
110         startActivity(intent);
111 
112         return new ExecuteAppFunctionResponse.Builder().build();
113     }
114 
115     @Override
onDestroy()116     public void onDestroy() {
117         super.onDestroy();
118         TestAppFunctionServiceLifecycleReceiver.notifyOnDestroyInvoked(this);
119     }
120 }
121