1 /* 2 * Copyright (C) 2019 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.attentionservice.cts; 18 19 import android.service.attention.AttentionService; 20 import android.util.Log; 21 22 import java.util.concurrent.CountDownLatch; 23 import java.util.concurrent.TimeUnit; 24 25 public class CtsTestAttentionService extends AttentionService { 26 private static final String TAG = "CtsTestAttentionService"; 27 private static AttentionCallback sCurrentAttentionCallback; 28 private static ProximityUpdateCallback sCurrentProximityUpdateCallback; 29 private static CountDownLatch sRespondLatch = new CountDownLatch(1); 30 31 @Override onCheckAttention(AttentionCallback callback)32 public void onCheckAttention(AttentionCallback callback) { 33 sCurrentAttentionCallback = callback; 34 sRespondLatch.countDown(); 35 } 36 37 @Override onCancelAttentionCheck(AttentionCallback callback)38 public void onCancelAttentionCheck(AttentionCallback callback) { 39 callback.onFailure(ATTENTION_FAILURE_CANCELLED); 40 resetAttentionCheck(); 41 sRespondLatch.countDown(); 42 } 43 44 @Override onStartProximityUpdates(ProximityUpdateCallback callback)45 public void onStartProximityUpdates(ProximityUpdateCallback callback) { 46 sCurrentProximityUpdateCallback = callback; 47 sRespondLatch.countDown(); 48 } 49 50 @Override onStopProximityUpdates()51 public void onStopProximityUpdates() { 52 resetProximity(); 53 sRespondLatch.countDown(); 54 } 55 reset()56 public static void reset() { 57 resetAttentionCheck(); 58 resetProximity(); 59 } 60 resetAttentionCheck()61 public static void resetAttentionCheck() { 62 sCurrentAttentionCallback = null; 63 } 64 resetProximity()65 public static void resetProximity() { 66 sCurrentProximityUpdateCallback = null; 67 } 68 respondSuccess(int code)69 public static void respondSuccess(int code) { 70 if (sCurrentAttentionCallback != null) { 71 sCurrentAttentionCallback.onSuccess(code, 0); 72 } 73 resetAttentionCheck(); 74 } 75 respondFailure(int code)76 public static void respondFailure(int code) { 77 if (sCurrentAttentionCallback != null) { 78 sCurrentAttentionCallback.onFailure(code); 79 } 80 resetAttentionCheck(); 81 } 82 respondProximity(double distance)83 public static void respondProximity(double distance) { 84 if (sCurrentProximityUpdateCallback != null) { 85 sCurrentProximityUpdateCallback.onProximityUpdate(distance); 86 } 87 } 88 hasPendingChecks()89 public static boolean hasPendingChecks() { 90 return sCurrentAttentionCallback != null; 91 } 92 hasCurrentProximityUpdates()93 public static boolean hasCurrentProximityUpdates() { 94 return sCurrentProximityUpdateCallback != null; 95 } 96 onReceivedResponse()97 public static void onReceivedResponse() { 98 try { 99 if (!sRespondLatch.await(500, TimeUnit.MILLISECONDS)) { 100 throw new AssertionError("CtsTestAttentionService timed out while expecting a call."); 101 } 102 //reset for next 103 sRespondLatch = new CountDownLatch(1); 104 } catch (InterruptedException e) { 105 Log.e(TAG, e.getMessage()); 106 Thread.currentThread().interrupt(); 107 throw new AssertionError("Got InterruptedException while waiting for response."); 108 } 109 } 110 } 111