• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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 package android.ambientcontext.cts;
17 
18 import android.app.ambientcontext.AmbientContextEvent;
19 import android.app.ambientcontext.AmbientContextEventRequest;
20 import android.app.ambientcontext.AmbientContextManager;
21 import android.service.ambientcontext.AmbientContextDetectionResult;
22 import android.service.ambientcontext.AmbientContextDetectionService;
23 import android.service.ambientcontext.AmbientContextDetectionServiceStatus;
24 import android.util.Log;
25 
26 import java.util.concurrent.CountDownLatch;
27 import java.util.concurrent.TimeUnit;
28 import java.util.function.Consumer;
29 
30 
31 public class CtsAmbientContextDetectionService extends AmbientContextDetectionService {
32     private static final String TAG = "CtsAmbientContextDetectionService";
33     private static final String FAKE_APP_PACKAGE = "foo.bar.baz";
34 
35     private static Consumer<AmbientContextDetectionResult> sResultConsumer;
36     private static Consumer<AmbientContextDetectionServiceStatus> sQueryConsumer;
37     private static CountDownLatch sRespondLatch = new CountDownLatch(1);
38 
39     @Override
onStartDetection(AmbientContextEventRequest request, String packageName, Consumer<AmbientContextDetectionResult> resultConsumer, Consumer<AmbientContextDetectionServiceStatus> statusConsumer)40     public void onStartDetection(AmbientContextEventRequest request, String packageName,
41             Consumer<AmbientContextDetectionResult> resultConsumer,
42             Consumer<AmbientContextDetectionServiceStatus> statusConsumer) {
43         sResultConsumer = resultConsumer;
44         sQueryConsumer = statusConsumer;
45         sRespondLatch.countDown();
46     }
47 
48     @Override
onStopDetection(String packageName)49     public void onStopDetection(String packageName) {
50         reset();
51         sRespondLatch.countDown();
52     }
53 
54     @Override
onQueryServiceStatus(int[] eventTypes, String packageName, Consumer<AmbientContextDetectionServiceStatus> consumer)55     public void onQueryServiceStatus(int[] eventTypes, String packageName,
56             Consumer<AmbientContextDetectionServiceStatus> consumer) {
57         sQueryConsumer = consumer;
58         sRespondLatch.countDown();
59     }
60 
reset()61     public static void reset() {
62         Log.d(TAG, "reset");
63         sResultConsumer = null;
64         sQueryConsumer = null;
65     }
66 
respondSuccess(AmbientContextEvent event)67     public static void respondSuccess(AmbientContextEvent event) {
68         if (sResultConsumer != null) {
69             AmbientContextDetectionResult result = new AmbientContextDetectionResult
70                     .Builder(FAKE_APP_PACKAGE)
71                     .addEvent(event)
72                     .build();
73             sResultConsumer.accept(result);
74         }
75         if (sQueryConsumer != null) {
76             AmbientContextDetectionServiceStatus serviceStatus =
77                     new AmbientContextDetectionServiceStatus.Builder(FAKE_APP_PACKAGE)
78                             .setStatusCode(AmbientContextManager.STATUS_SUCCESS)
79                             .build();
80             sQueryConsumer.accept(serviceStatus);
81         }
82     }
83 
respondFailure(int status)84     public static void respondFailure(int status) {
85         if (sQueryConsumer != null) {
86             AmbientContextDetectionServiceStatus serviceStatus =
87                     new AmbientContextDetectionServiceStatus.Builder(FAKE_APP_PACKAGE)
88                             .setStatusCode(status)
89                             .build();
90             sQueryConsumer.accept(serviceStatus);
91         }
92     }
93 
hasPendingRequest()94     public static boolean hasPendingRequest() {
95         return sResultConsumer != null;
96     }
97 
hasQueryRequest()98     public static boolean hasQueryRequest() {
99         return sQueryConsumer != null;
100     }
101 
onReceivedResponse()102     public static void onReceivedResponse() {
103         try {
104             if (!sRespondLatch.await(3000, TimeUnit.MILLISECONDS)) {
105                 throw new AssertionError("CtsTestAmbientContextEventProviderService"
106                         + " timed out while expecting a call.");
107             }
108             // reset for next
109             sRespondLatch = new CountDownLatch(1);
110         } catch (InterruptedException e) {
111             Log.e(TAG, e.getMessage());
112             Thread.currentThread().interrupt();
113             throw new AssertionError("Got InterruptedException while waiting for serviceStatus.");
114         }
115     }
116 }
117