private boolean isPackageDefaultDialer(String packageName) { TelecomManager telecomManager = (TelecomManager) context.getSystemService(Context.TELECOM_SERVICE); return packageName.equals(telecomManager.getDefaultDialerPackage()); }
@Test @Config(minSdk = M) public void getCallCapablePhoneAccounts() { PhoneAccountHandle callCapableHandle = createHandle("id1"); telecomService.registerPhoneAccount(PhoneAccount.builder(callCapableHandle, "enabled") .setIsEnabled(true) .build()); PhoneAccountHandle notCallCapableHandler = createHandle("id2"); telecomService.registerPhoneAccount(PhoneAccount.builder(notCallCapableHandler, "disabled") .setIsEnabled(false) .build()); List<PhoneAccountHandle> callCapablePhoneAccounts = telecomService.getCallCapablePhoneAccounts(); assertThat(callCapablePhoneAccounts).contains(callCapableHandle); assertThat(callCapablePhoneAccounts).doesNotContain(notCallCapableHandler); }
@Test public void registerAndUnRegister() { assertThat(shadowOf(telecomService).getAllPhoneAccountsCount()).isEqualTo(0); assertThat(shadowOf(telecomService).getAllPhoneAccounts()).hasSize(0); PhoneAccountHandle handler = createHandle("id"); PhoneAccount phoneAccount = PhoneAccount.builder(handler, "main_account").build(); telecomService.registerPhoneAccount(phoneAccount); assertThat(shadowOf(telecomService).getAllPhoneAccountsCount()).isEqualTo(1); assertThat(shadowOf(telecomService).getAllPhoneAccounts()).hasSize(1); assertThat(telecomService.getAllPhoneAccountHandles()).hasSize(1); assertThat(telecomService.getAllPhoneAccountHandles()).contains(handler); assertThat(telecomService.getPhoneAccount(handler).getLabel()).isEqualTo(phoneAccount.getLabel()); telecomService.unregisterPhoneAccount(handler); assertThat(shadowOf(telecomService).getAllPhoneAccountsCount()).isEqualTo(0); assertThat(shadowOf(telecomService).getAllPhoneAccounts()).hasSize(0); assertThat(telecomService.getAllPhoneAccountHandles()).hasSize(0); }
@Test @Config(minSdk = LOLLIPOP_MR1) public void clearAccountsForPackage() { PhoneAccountHandle accountHandle1 = createHandle("a.package", "id1"); telecomService.registerPhoneAccount(PhoneAccount.builder(accountHandle1, "another_package") .build()); PhoneAccountHandle accountHandle2 = createHandle("some.other.package", "id2"); telecomService.registerPhoneAccount(PhoneAccount.builder(accountHandle2, "another_package") .build()); telecomService.clearAccountsForPackage(accountHandle1.getComponentName().getPackageName()); assertThat(telecomService.getPhoneAccount(accountHandle1)).isNull(); assertThat(telecomService.getPhoneAccount(accountHandle2)).isNotNull(); }
@Test public void testIsRinging_incomingCallAdded_thenRingerSilenced_shouldBeFalse() { telecomService.addNewIncomingCall(createHandle("id"), null); telecomService.silenceRinger(); assertThat(shadowOf(telecomService).isRinging()).isFalse(); }
/** * Determines if one of the call capable phone accounts defined supports calling with a subject * specified. * * @param context The context. * @return {@code true} if one of the call capable phone accounts supports calling with a * subject specified, {@code false} otherwise. */ public static boolean isCallWithSubjectSupported(Context context) { if (!PermissionsUtil.hasPermission(context, android.Manifest.permission.READ_PHONE_STATE) || !CompatUtils.isCallSubjectCompatible()) { return false; } TelecomManager telecommMgr = (TelecomManager) context.getSystemService(Context.TELECOM_SERVICE); if (telecommMgr == null) { return false; } List<PhoneAccountHandle> accountHandles = telecommMgr.getCallCapablePhoneAccounts(); for (PhoneAccountHandle accountHandle : accountHandles) { PhoneAccount account = telecommMgr.getPhoneAccount(accountHandle); if (account != null && account.hasCapabilities(PhoneAccount.CAPABILITY_CALL_SUBJECT)) { return true; } } return false; } }
@Test public void testIsRinging_ringerSilenced_thenUnknownCallAdded_shouldBeTrue() { telecomService.silenceRinger(); shadowOf(telecomService).addNewUnknownCall(createHandle("id"), null); assertThat(shadowOf(telecomService).isRinging()).isTrue(); }
@Test public void isInCall_setIsInCallNotCalled_shouldReturnFalse() throws Exception { assertThat(telecomService.isInCall()).isFalse(); }
/** * Return the {@link PhoneAccount} for a specified {@link PhoneAccountHandle}. Object includes * resources which can be used in a user interface. * * @param telecomManager the {@link TelecomManager} used for method calls, if possible. * @param account The {@link PhoneAccountHandle}. * @return The {@link PhoneAccount} object or null if it doesn't exist. */ @Nullable public static PhoneAccount getPhoneAccount(@Nullable TelecomManager telecomManager, @Nullable PhoneAccountHandle accountHandle) { if (telecomManager != null && (CompatUtils.isMethodAvailable( TELECOM_MANAGER_CLASS, "getPhoneAccount", PhoneAccountHandle.class))) { return telecomManager.getPhoneAccount(accountHandle); } return null; }
/** * Returns a list of {@link PhoneAccountHandle}s which can be used to make and receive phone * calls. The returned list includes only those accounts which have been explicitly enabled * by the user. * * @param telecomManager the {@link TelecomManager} used for method calls, if possible. * @return A list of PhoneAccountHandle objects. */ public static List<PhoneAccountHandle> getCallCapablePhoneAccounts( @Nullable TelecomManager telecomManager) { if (telecomManager != null && (CompatUtils.isMarshmallowCompatible() || CompatUtils.isMethodAvailable(TELECOM_MANAGER_CLASS, "getCallCapablePhoneAccounts"))) { return telecomManager.getCallCapablePhoneAccounts(); } return new ArrayList<>(); }
@Test public void testAddNewIncomingCall() { telecomService.addNewIncomingCall(createHandle("id"), null); assertThat(shadowOf(telecomService).getAllIncomingCalls()).hasSize(1); }
@Test public void testAddUnknownCall() { telecomService.addNewUnknownCall(createHandle("id"), null); assertThat(shadowOf(telecomService).getAllUnknownCalls()).hasSize(1); }
List<PhoneAccountHandle> accountHandles = telecommMgr.getCallCapablePhoneAccounts(); for (PhoneAccountHandle accountHandle : accountHandles) { PhoneAccount account = telecommMgr.getPhoneAccount(accountHandle); if (account != null) { if (account.hasCapabilities(PhoneAccount.CAPABILITY_VIDEO_CALLING)) {
@Test public void testIsRinging_ringerSilenced_thenIncomingCallAdded_shouldBeTrue() { telecomService.silenceRinger(); telecomService.addNewIncomingCall(createHandle("id"), null); assertThat(shadowOf(telecomService).isRinging()).isTrue(); }
@Test public void testIsRinging_unknownCallAdded_thenRingerSilenced_shouldBeFalse() { shadowOf(telecomService).addNewUnknownCall(createHandle("id"), null); telecomService.silenceRinger(); assertThat(shadowOf(telecomService).isRinging()).isFalse(); }
@Test public void canSetAndGetIsInCall() throws Exception { shadowOf(telecomService).setIsInCall(true); assertThat(telecomService.isInCall()).isTrue(); }
final PhoneAccount account = telecomManager.getPhoneAccount(mPhoneAccountHandle);
@Test public void testIsRinging_incomingCallAdded_shouldBeTrue() { telecomService.addNewIncomingCall(createHandle("id"), null); assertThat(shadowOf(telecomService).isRinging()).isTrue(); }
@Test @Config(minSdk = M) public void setDefaultDialerPackage() { shadowOf(telecomService).setDefaultDialer("some.package"); assertThat(telecomService.getDefaultDialerPackage()).isEqualTo("some.package"); }
/** * Silences the ringer if a ringing call exists. Noop if {@link TelecomManager#silenceRinger()} * is unavailable. * * @param telecomManager the TelecomManager to use to silence the ringer. */ public static void silenceRinger(@Nullable TelecomManager telecomManager) { if (telecomManager != null && (CompatUtils.isMarshmallowCompatible() || CompatUtils .isMethodAvailable(TELECOM_MANAGER_CLASS, "silenceRinger"))) { telecomManager.silenceRinger(); } }