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.carmodetestapp; 18 19 import android.content.Intent; 20 import android.telecom.Call; 21 import android.telecom.InCallService; 22 import android.util.Log; 23 24 import java.util.ArrayList; 25 import java.util.List; 26 import java.util.concurrent.CountDownLatch; 27 import java.util.concurrent.TimeUnit; 28 29 /** 30 * A simple car-mode InCallService implementation. 31 */ 32 public class CtsCarModeInCallService extends InCallService { 33 private static final String TAG = CtsCarModeInCallService.class.getSimpleName(); 34 private static final long TIMEOUT = 10000L; 35 private static boolean sIsServiceBound = false; 36 private static boolean sIsServiceUnbound = false; 37 private static CtsCarModeInCallService sInstance = null; 38 private int mCallCount = 0; 39 private static CountDownLatch sCallAddedLatch = new CountDownLatch(1); 40 private static CountDownLatch sBoundLatch = new CountDownLatch(1); 41 private static CountDownLatch sUnboundLatch = new CountDownLatch(1); 42 private List<Call> mCalls = new ArrayList<>(); 43 44 @Override onBind(Intent intent)45 public android.os.IBinder onBind(Intent intent) { 46 sIsServiceBound = true; 47 sIsServiceUnbound = false; 48 sInstance = this; 49 sBoundLatch.countDown(); 50 Log.i(TAG, "InCallService on bind"); 51 return super.onBind(intent); 52 } 53 54 @Override onUnbind(Intent intent)55 public boolean onUnbind(Intent intent) { 56 sIsServiceBound = false; 57 sIsServiceUnbound = true; 58 sInstance = null; 59 sUnboundLatch.countDown(); 60 Log.i(TAG, "InCallService on unbind"); 61 return super.onUnbind(intent); 62 } 63 64 @Override onCallAdded(Call call)65 public void onCallAdded(Call call) { 66 Log.i(TAG, "onCallAdded - " + call); 67 mCallCount++; 68 mCalls.add(call); 69 sCallAddedLatch.countDown(); 70 } 71 72 @Override onCallRemoved(Call call)73 public void onCallRemoved(Call call) { 74 Log.i(TAG, "onCallRemoved - " + call); 75 mCallCount--; 76 mCalls.remove(call); 77 sCallAddedLatch = new CountDownLatch(1); 78 } 79 isBound()80 public static boolean isBound() { 81 return sIsServiceBound; 82 } 83 isUnbound()84 public static boolean isUnbound() { 85 return sIsServiceUnbound; 86 } 87 getInstance()88 public static CtsCarModeInCallService getInstance() { 89 return sInstance; 90 } 91 reset()92 public static void reset() { 93 sIsServiceUnbound = false; 94 sIsServiceBound = false; 95 sBoundLatch = new CountDownLatch(1); 96 sUnboundLatch = new CountDownLatch(1); 97 } 98 getCallCount()99 public int getCallCount() { 100 return mCallCount; 101 } 102 disconnectCalls()103 public void disconnectCalls() { 104 for (Call call : mCalls) { 105 call.disconnect(); 106 } 107 } 108 getLastCall()109 public Call getLastCall() { 110 if (!mCalls.isEmpty()) { 111 return mCalls.get(mCalls.size() - 1); 112 } 113 return null; 114 } 115 checkBindStatus(boolean bind)116 public static boolean checkBindStatus(boolean bind) { 117 Log.i(TAG, "checking latch status: service " + (bind ? "bound" : "not bound")); 118 return bind ? checkLatch(sBoundLatch) : checkLatch(sUnboundLatch); 119 } 120 checkCallAddedStatus()121 public static boolean checkCallAddedStatus() { 122 Log.i(TAG, "checking latch status: call added"); 123 return checkLatch(sCallAddedLatch); 124 } 125 checkLatch(CountDownLatch latch)126 private static boolean checkLatch(CountDownLatch latch) { 127 try { 128 return latch.await(TIMEOUT, TimeUnit.MILLISECONDS); 129 } catch (InterruptedException e) { 130 return false; 131 } 132 } 133 } 134