1 /* 2 * Copyright (C) 2015 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; 18 19 import static org.junit.Assert.assertFalse; 20 21 import android.content.ComponentName; 22 import android.content.Context; 23 import android.content.Intent; 24 import android.content.pm.PackageManager; 25 import android.telecom.Call; 26 import android.telecom.CallScreeningService; 27 import android.util.Log; 28 29 import java.util.concurrent.Semaphore; 30 31 public class MockCallScreeningService extends CallScreeningService { 32 private static String LOG_TAG = "MockCallScreeningSvc"; 33 34 private static boolean mIsServiceUnbound; 35 private static CallScreeningServiceCallbacks sCallbacks; 36 37 public static abstract class CallScreeningServiceCallbacks { 38 public Semaphore lock = new Semaphore(0); 39 private MockCallScreeningService mService; 40 onScreenCall(Call.Details callDetails)41 public void onScreenCall(Call.Details callDetails) {}; 42 getService()43 final public MockCallScreeningService getService() { 44 return mService; 45 } 46 setService(MockCallScreeningService service)47 final public void setService(MockCallScreeningService service) { 48 mService = service; 49 } 50 } 51 52 @Override onBind(android.content.Intent intent)53 public android.os.IBinder onBind(android.content.Intent intent) { 54 Log.i(LOG_TAG, "Service bounded"); 55 if (getCallbacks() != null) { 56 getCallbacks().setService(this); 57 } 58 mIsServiceUnbound = false; 59 return super.onBind(intent); 60 } 61 62 @Override onScreenCall(Call.Details callDetails)63 public void onScreenCall(Call.Details callDetails) { 64 if (getCallbacks() != null) { 65 getCallbacks().onScreenCall(callDetails); 66 } 67 } 68 setCallbacks(CallScreeningServiceCallbacks callbacks)69 public static void setCallbacks(CallScreeningServiceCallbacks callbacks) { 70 sCallbacks = callbacks; 71 } 72 getCallbacks()73 private CallScreeningServiceCallbacks getCallbacks() { 74 if (sCallbacks != null) { 75 sCallbacks.setService(this); 76 } 77 return sCallbacks; 78 } 79 80 @Override onUnbind(Intent intent)81 public boolean onUnbind(Intent intent) { 82 Log.i(LOG_TAG, "Service unbounded"); 83 assertFalse(mIsServiceUnbound); 84 mIsServiceUnbound = true; 85 return super.onUnbind(intent); 86 } 87 isServiceUnbound()88 public static boolean isServiceUnbound() { 89 return mIsServiceUnbound; 90 } 91 enableService(Context context)92 public static void enableService(Context context) { 93 context.getPackageManager().setComponentEnabledSetting(getComponentName(context), 94 PackageManager.COMPONENT_ENABLED_STATE_ENABLED, 95 PackageManager.DONT_KILL_APP); 96 } 97 disableService(Context context)98 public static void disableService(Context context) { 99 context.getPackageManager().setComponentEnabledSetting(getComponentName(context), 100 PackageManager.COMPONENT_ENABLED_STATE_DISABLED, 101 PackageManager.DONT_KILL_APP); 102 } 103 getComponentName(Context context)104 private static ComponentName getComponentName(Context context) { 105 return new ComponentName(context, MockCallScreeningService.class); 106 } 107 } 108