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.telecom.cts.api29incallservice; 18 19 import android.content.Intent; 20 import android.os.Handler; 21 import android.os.HandlerThread; 22 import android.os.IBinder; 23 import android.telecom.Call; 24 import android.telecom.cts.MockInCallService; 25 import android.util.Log; 26 27 import java.util.HashSet; 28 import java.util.Set; 29 import java.util.concurrent.CompletableFuture; 30 import java.util.concurrent.TimeUnit; 31 import java.util.concurrent.TimeoutException; 32 33 public class CtsApi29InCallService extends MockInCallService { 34 35 public static final String PACKAGE_NAME = CtsApi29InCallService.class.getPackage().getName(); 36 private static final String TAG = CtsApi29InCallService.class.getSimpleName(); 37 protected static final int TIMEOUT = 10000; 38 39 static Set<Call> sCalls = new HashSet<>(); 40 static int sHistoricalCallCount = 0; 41 static boolean sShouldReturnNullBinding = false; 42 static CompletableFuture<Boolean> sBindRequestFuture = new CompletableFuture<>(); 43 44 @Override onCallAdded(Call call)45 public void onCallAdded(Call call) { 46 super.onCallAdded(call); 47 if (!sCalls.contains(call)) { 48 sHistoricalCallCount++; 49 } 50 sCalls.add(call); 51 Log.i(TAG, "onCallAdded, size=" + sCalls.size()); 52 } 53 54 @Override onCallRemoved(Call call)55 public void onCallRemoved(Call call) { 56 super.onCallRemoved(call); 57 sCalls.remove(call); 58 Log.i(TAG, "onCallRemoved, size=" + sCalls.size()); 59 } 60 61 @Override onBind(Intent intent)62 public IBinder onBind(Intent intent) { 63 Log.i(TAG, "onBind future=" + sBindRequestFuture); 64 sBindRequestFuture.complete(true); 65 // Sets mIsServiceBound 66 IBinder result = super.onBind(intent); 67 if (!sShouldReturnNullBinding) { 68 return result; 69 } 70 return null; 71 } 72 73 @Override onUnbind(Intent intent)74 public boolean onUnbind(Intent intent) { 75 reset(); 76 return super.onUnbind(intent); 77 } 78 waitForBindRequest()79 public static boolean waitForBindRequest() { 80 try { 81 if (isServiceBound()) return true; 82 return sBindRequestFuture.get(TIMEOUT, TimeUnit.MILLISECONDS); 83 } catch (TimeoutException te) { 84 Log.e(TAG, "Waited too long for bind request. future=" + sBindRequestFuture); 85 return false; 86 } catch (Exception e) { 87 return false; 88 } 89 } 90 getLocalCallCount()91 public static int getLocalCallCount() { 92 synchronized (sLock) { 93 return sCalls.size(); 94 } 95 } 96 reset()97 static void reset() { 98 sCalls.clear(); 99 sHistoricalCallCount = 0; 100 sShouldReturnNullBinding = false; 101 sBindRequestFuture = new CompletableFuture<>(); 102 } 103 } 104