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.wearable.cts;
18 
19 import android.app.wearable.WearableSensingDataRequest;
20 import android.app.wearable.WearableSensingManager;
21 import android.content.BroadcastReceiver;
22 import android.content.Context;
23 import android.content.Intent;
24 import android.util.Log;
25 
26 import java.util.concurrent.CountDownLatch;
27 import java.util.concurrent.TimeUnit;
28 
29 /** A broadcast receiver to receive wearable sensing data requests in CTS. */
30 public final class CtsWearableSensingDataRequestBroadcastReceiver extends BroadcastReceiver {
31 
32     private static final String TAG = "CtsWearableSensingDataRequestBroadcastReceiver";
33     private static WearableSensingDataRequest sLatestDataRequest;
34     private static CountDownLatch sRespondLatch = new CountDownLatch(1);
35 
36     @Override
onReceive(Context context, Intent intent)37     public void onReceive(Context context, Intent intent) {
38         sLatestDataRequest = WearableSensingManager.getDataRequestFromIntent(intent);
39         sRespondLatch.countDown();
40     }
41 
42     /** Resets all states. */
reset()43     public static void reset() {
44         sLatestDataRequest = null;
45         sRespondLatch = new CountDownLatch(1);
46     }
47 
48     /** Gets the last data request received. */
getLatestDataRequest()49     public static WearableSensingDataRequest getLatestDataRequest() {
50         return sLatestDataRequest;
51     }
52 
53     /**
54      * Sets the number of results {@link #awaitResult()} will wait before returning. If not set, it
55      * defaults to 1.
56      */
setResultCountToAwait(int count)57     public static void setResultCountToAwait(int count) {
58         sRespondLatch = new CountDownLatch(count);
59     }
60 
61     /** Waits for a result to arrive. */
awaitResult()62     public static void awaitResult() {
63         try {
64             if (!sRespondLatch.await(3, TimeUnit.SECONDS)) {
65                 throw new AssertionError(
66                         "CtsWearableSensingDataRequestBroadcastReceiver"
67                                 + " timed out while expecting a call.");
68             }
69         } catch (InterruptedException ex) {
70             Log.e(TAG, "Interrupted in test.", ex);
71             Thread.currentThread().interrupt();
72             throw new AssertionError("Got InterruptedException while waiting for result.");
73         }
74         // reset for next
75         sRespondLatch = new CountDownLatch(1);
76     }
77 }
78