DO NOT MERGE - qt-qpr1-dev-plus-aosp-without-vendor@5915889 into stage-aosp-master
am: 3b0be8a3b2
Change-Id: I8c7221d336d35592cc4fc4c17e4450209f48c7d2
diff --git a/src/com/android/server/telecom/AsyncRingtonePlayer.java b/src/com/android/server/telecom/AsyncRingtonePlayer.java
index 8f11882..93041c8 100644
--- a/src/com/android/server/telecom/AsyncRingtonePlayer.java
+++ b/src/com/android/server/telecom/AsyncRingtonePlayer.java
@@ -224,10 +224,11 @@
// can know whether to trigger the vibrator.
if (mHapticsFuture != null && !mHapticsFuture.isDone()) {
boolean hasHaptics = factory.hasHapticChannels(mRingtone);
-
Log.i(this, "handlePlay: hasHaptics=%b, isVibrationEnabled=%b", hasHaptics,
isVibrationEnabled);
- if (hasHaptics) {
+ SystemSettingsUtil systemSettingsUtil = new SystemSettingsUtil();
+ if (hasHaptics && (volumeShaperConfig == null
+ || systemSettingsUtil.enableAudioCoupledVibrationForRampingRinger())) {
AudioAttributes attributes = mRingtone.getAudioAttributes();
Log.d(this, "handlePlay: %s haptic channel",
(isVibrationEnabled ? "unmuting" : "muting"));
diff --git a/src/com/android/server/telecom/CallsManager.java b/src/com/android/server/telecom/CallsManager.java
index 2d42625..36ae22d 100644
--- a/src/com/android/server/telecom/CallsManager.java
+++ b/src/com/android/server/telecom/CallsManager.java
@@ -2040,7 +2040,7 @@
}
Call activeCall = getActiveCall();
- if (activeCall != call) {
+ if (activeCall != null && activeCall != call) {
Log.w(this, "Ignoring enter audio processing because there's already a call active");
return;
}
diff --git a/src/com/android/server/telecom/Ringer.java b/src/com/android/server/telecom/Ringer.java
index 55f2b0d..49cf7e5 100644
--- a/src/com/android/server/telecom/Ringer.java
+++ b/src/com/android/server/telecom/Ringer.java
@@ -346,6 +346,12 @@
isUsingAudioCoupledHaptics, mIsHapticPlaybackSupportedByDevice);
maybeStartVibration(foregroundCall, shouldRingForContact, effect,
isVibratorEnabled, isRingerAudible);
+ } else if (mSystemSettingsUtil.applyRampingRinger(mContext)
+ && mSystemSettingsUtil.enableRampingRingerFromDeviceConfig()
+ && !mSystemSettingsUtil.enableAudioCoupledVibrationForRampingRinger()) {
+ Log.i(this, "startRinging: apply ramping ringer vibration");
+ maybeStartVibration(foregroundCall, shouldRingForContact, effect,
+ isVibratorEnabled, isRingerAudible);
} else {
Log.addEvent(foregroundCall, LogUtils.Events.SKIP_VIBRATION,
"using audio-coupled haptics");
@@ -368,7 +374,6 @@
private void maybeStartVibration(Call foregroundCall, boolean shouldRingForContact,
VibrationEffect effect, boolean isVibrationEnabled, boolean isRingerAudible) {
-
if (isVibrationEnabled
&& !mIsVibrating && shouldRingForContact) {
if (mSystemSettingsUtil.applyRampingRinger(mContext)
diff --git a/src/com/android/server/telecom/callfiltering/BlockCheckerFilter.java b/src/com/android/server/telecom/callfiltering/BlockCheckerFilter.java
new file mode 100644
index 0000000..2cadb91
--- /dev/null
+++ b/src/com/android/server/telecom/callfiltering/BlockCheckerFilter.java
@@ -0,0 +1,175 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.server.telecom.callfiltering;
+
+import android.content.Context;
+import android.net.Uri;
+import android.os.Bundle;
+import android.os.Handler;
+import android.os.HandlerThread;
+import android.provider.BlockedNumberContract;
+import android.provider.CallLog;
+import android.telecom.Log;
+import android.telecom.TelecomManager;
+import android.telephony.CallerInfo;
+
+import com.android.server.telecom.Call;
+import com.android.server.telecom.CallerInfoLookupHelper;
+import com.android.server.telecom.LogUtils;
+import com.android.server.telecom.LoggedHandlerExecutor;
+import com.android.server.telecom.settings.BlockedNumbersUtil;
+
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.CompletionStage;
+
+public class BlockCheckerFilter extends CallFilter {
+ private final Call mCall;
+ private final Context mContext;
+ private final CallerInfoLookupHelper mCallerInfoLookupHelper;
+ private final BlockCheckerAdapter mBlockCheckerAdapter;
+ private final String TAG = "BlockCheckerFilter";
+ private boolean mContactExists;
+ private HandlerThread mHandlerThread;
+ private Handler mHandler;
+
+ public static final long CALLER_INFO_QUERY_TIMEOUT = 5000;
+
+ public BlockCheckerFilter(Context context, Call call,
+ CallerInfoLookupHelper callerInfoLookupHelper,
+ BlockCheckerAdapter blockCheckerAdapter) {
+ mCall = call;
+ mContext = context;
+ mCallerInfoLookupHelper = callerInfoLookupHelper;
+ mBlockCheckerAdapter = blockCheckerAdapter;
+ mContactExists = false;
+ mHandlerThread = new HandlerThread(TAG);
+ mHandlerThread.start();
+ mHandler = new Handler(mHandlerThread.getLooper());
+ }
+
+ @Override
+ public CompletionStage<CallFilteringResult> startFilterLookup(CallFilteringResult result) {
+ Log.addEvent(mCall, LogUtils.Events.BLOCK_CHECK_INITIATED);
+ CompletableFuture<CallFilteringResult> resultFuture = new CompletableFuture<>();
+ Bundle extras = new Bundle();
+ if (BlockedNumbersUtil.isEnhancedCallBlockingEnabledByPlatform(mContext)) {
+ int presentation = mCall.getHandlePresentation();
+ extras.putInt(BlockedNumberContract.EXTRA_CALL_PRESENTATION, presentation);
+ if (presentation == TelecomManager.PRESENTATION_ALLOWED) {
+ mCallerInfoLookupHelper.startLookup(mCall.getHandle(),
+ new CallerInfoLookupHelper.OnQueryCompleteListener() {
+ @Override
+ public void onCallerInfoQueryComplete(Uri handle, CallerInfo info) {
+ if (info != null && info.contactExists) {
+ mContactExists = true;
+ }
+ getBlockStatus(resultFuture);
+ }
+
+ @Override
+ public void onContactPhotoQueryComplete(Uri handle, CallerInfo info) {
+ // Ignore
+ }
+ });
+ } else {
+ getBlockStatus(resultFuture);
+ }
+ } else {
+ getBlockStatus(resultFuture);
+ }
+ return resultFuture;
+ }
+
+ private void getBlockStatus(
+ CompletableFuture<CallFilteringResult> resultFuture) {
+ // Set extras
+ Bundle extras = new Bundle();
+ if (BlockedNumbersUtil.isEnhancedCallBlockingEnabledByPlatform(mContext)) {
+ int presentation = mCall.getHandlePresentation();
+ extras.putInt(BlockedNumberContract.EXTRA_CALL_PRESENTATION, presentation);
+ if (presentation == TelecomManager.PRESENTATION_ALLOWED) {
+ extras.putBoolean(BlockedNumberContract.EXTRA_CONTACT_EXIST, mContactExists);
+ }
+ }
+
+ // Set number
+ final String number = mCall.getHandle() == null ? null :
+ mCall.getHandle().getSchemeSpecificPart();
+
+ CompletableFuture.supplyAsync(
+ () -> mBlockCheckerAdapter.getBlockStatus(mContext, number, extras),
+ new LoggedHandlerExecutor(mHandler, "BCF.gBS", null))
+ .thenApplyAsync((x) -> completeResult(resultFuture, x),
+ new LoggedHandlerExecutor(mHandler, "BCF.gBS", null));
+ }
+
+ private int completeResult(CompletableFuture<CallFilteringResult> resultFuture,
+ int blockStatus) {
+ CallFilteringResult result;
+ if (blockStatus != BlockedNumberContract.STATUS_NOT_BLOCKED) {
+ result = new CallFilteringResult.Builder()
+ .setShouldAllowCall(false)
+ .setShouldReject(true)
+ .setShouldAddToCallLog(true)
+ .setShouldShowNotification(false)
+ .setCallBlockReason(getBlockReason(blockStatus))
+ .setCallScreeningAppName(null)
+ .setCallScreeningComponentName(null)
+ .setContactExists(mContactExists)
+ .build();
+ } else {
+ result = new CallFilteringResult.Builder()
+ .setShouldAllowCall(true)
+ .setShouldReject(false)
+ .setShouldSilence(false)
+ .setShouldAddToCallLog(true)
+ .setShouldShowNotification(true)
+ .setContactExists(mContactExists)
+ .build();
+ }
+ Log.addEvent(mCall, LogUtils.Events.BLOCK_CHECK_FINISHED,
+ BlockedNumberContract.SystemContract.blockStatusToString(blockStatus) + " "
+ + result);
+ resultFuture.complete(result);
+ mHandlerThread.quitSafely();
+ return blockStatus;
+ }
+
+ private int getBlockReason(int blockStatus) {
+ switch (blockStatus) {
+ case BlockedNumberContract.STATUS_BLOCKED_IN_LIST:
+ return CallLog.Calls.BLOCK_REASON_BLOCKED_NUMBER;
+
+ case BlockedNumberContract.STATUS_BLOCKED_UNKNOWN_NUMBER:
+ return CallLog.Calls.BLOCK_REASON_UNKNOWN_NUMBER;
+
+ case BlockedNumberContract.STATUS_BLOCKED_RESTRICTED:
+ return CallLog.Calls.BLOCK_REASON_RESTRICTED_NUMBER;
+
+ case BlockedNumberContract.STATUS_BLOCKED_PAYPHONE:
+ return CallLog.Calls.BLOCK_REASON_PAY_PHONE;
+
+ case BlockedNumberContract.STATUS_BLOCKED_NOT_IN_CONTACTS:
+ return CallLog.Calls.BLOCK_REASON_NOT_IN_CONTACTS;
+
+ default:
+ Log.w(this,
+ "There's no call log block reason can be converted");
+ return CallLog.Calls.BLOCK_REASON_BLOCKED_NUMBER;
+ }
+ }
+}
diff --git a/src/com/android/server/telecom/callfiltering/DirectToVoicemailFilter.java b/src/com/android/server/telecom/callfiltering/DirectToVoicemailFilter.java
new file mode 100644
index 0000000..a361f2f
--- /dev/null
+++ b/src/com/android/server/telecom/callfiltering/DirectToVoicemailFilter.java
@@ -0,0 +1,82 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.server.telecom.callfiltering;
+
+import android.net.Uri;
+import android.provider.CallLog;
+import android.telecom.Log;
+import android.telephony.CallerInfo;
+
+import com.android.server.telecom.Call;
+import com.android.server.telecom.CallerInfoLookupHelper;
+import com.android.server.telecom.LogUtils;
+
+import java.util.Objects;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.CompletionStage;
+
+public class DirectToVoicemailFilter extends CallFilter {
+ private final Call mCall;
+ private final CallerInfoLookupHelper mCallerInfoLookupHelper;
+
+ public DirectToVoicemailFilter(Call call, CallerInfoLookupHelper callerInfoLookupHelper) {
+ mCall = call;
+ mCallerInfoLookupHelper = callerInfoLookupHelper;
+ }
+
+ @Override
+ public CompletionStage<CallFilteringResult> startFilterLookup(CallFilteringResult result) {
+ Log.addEvent(mCall, LogUtils.Events.DIRECT_TO_VM_INITIATED);
+ CompletableFuture<CallFilteringResult> resultFuture = new CompletableFuture<>();
+ mCallerInfoLookupHelper.startLookup(mCall.getHandle(),
+ new CallerInfoLookupHelper.OnQueryCompleteListener() {
+ @Override
+ public void onCallerInfoQueryComplete(Uri handle, CallerInfo info) {
+ if ((handle != null) && Objects.equals(mCall.getHandle(), handle)
+ && (info != null)) {
+ resultFuture.complete(new CallFilteringResult.Builder()
+ .setShouldAllowCall(!info.shouldSendToVoicemail)
+ .setShouldReject(info.shouldSendToVoicemail)
+ .setShouldAddToCallLog(true)
+ .setShouldShowNotification(true)
+ .setCallBlockReason(info.shouldSendToVoicemail ?
+ CallLog.Calls.BLOCK_REASON_DIRECT_TO_VOICEMAIL
+ : CallLog.Calls.BLOCK_REASON_NOT_BLOCKED)
+ .setContactExists(info.contactExists)
+ .build());
+ } else {
+ if (info == null) {
+ Log.w(this, "CallerInfo lookup returned a null " +
+ "CallerInfo.");
+ } else {
+ Log.w(this, "CallerInfo lookup returned with "
+ + "handle %s, should be %s", handle,
+ mCall.getHandle());
+ }
+ resultFuture.complete(IncomingCallFilterGraph.DEFAULT_RESULT);
+ }
+ Log.addEvent(mCall, LogUtils.Events.DIRECT_TO_VM_FINISHED);
+ }
+
+ @Override
+ public void onContactPhotoQueryComplete(Uri handle, CallerInfo info) {
+ // Ignore
+ }
+ });
+ return resultFuture;
+ }
+}
diff --git a/testapps/src/com/android/server/telecom/testapps/TestConnectionService.java b/testapps/src/com/android/server/telecom/testapps/TestConnectionService.java
index 0ada7ae..90509f6 100644
--- a/testapps/src/com/android/server/telecom/testapps/TestConnectionService.java
+++ b/testapps/src/com/android/server/telecom/testapps/TestConnectionService.java
@@ -79,17 +79,6 @@
private final class TestConference extends Conference {
- private final Connection.Listener mConnectionListener = new Connection.Listener() {
- @Override
- public void onDestroyed(Connection c) {
- removeConnection(c);
- if (getConnections().size() == 0) {
- setDisconnected(new DisconnectCause(DisconnectCause.REMOTE));
- destroy();
- }
- }
- };
-
public TestConference(Connection a, Connection b) {
super(null);
setConnectionCapabilities(
@@ -100,9 +89,6 @@
addConnection(a);
addConnection(b);
- a.addConnectionListener(mConnectionListener);
- b.addConnectionListener(mConnectionListener);
-
a.setConference(this);
b.setConference(this);
@@ -122,7 +108,6 @@
if (getConnections().contains(connection)) {
connection.setConference(null);
removeConnection(connection);
- connection.removeConnectionListener(mConnectionListener);
}
}
diff --git a/tests/src/com/android/server/telecom/tests/BlockCheckerFilterTest.java b/tests/src/com/android/server/telecom/tests/BlockCheckerFilterTest.java
new file mode 100644
index 0000000..c9a4cef
--- /dev/null
+++ b/tests/src/com/android/server/telecom/tests/BlockCheckerFilterTest.java
@@ -0,0 +1,163 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.server.telecom.tests;
+
+import static android.provider.BlockedNumberContract.STATUS_BLOCKED_IN_LIST;
+import static android.provider.BlockedNumberContract.STATUS_NOT_BLOCKED;
+
+import static junit.framework.TestCase.assertEquals;
+
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.timeout;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import android.content.Context;
+import android.net.Uri;
+import android.os.Bundle;
+import android.os.PersistableBundle;
+import android.provider.CallLog;
+import android.telephony.CallerInfo;
+import android.telecom.TelecomManager;
+import android.telephony.CarrierConfigManager;
+import android.test.suitebuilder.annotation.SmallTest;
+import android.util.Pair;
+
+import com.android.server.telecom.Call;
+import com.android.server.telecom.CallerInfoLookupHelper;
+import com.android.server.telecom.callfiltering.BlockCheckerAdapter;
+import com.android.server.telecom.callfiltering.BlockCheckerFilter;
+import com.android.server.telecom.callfiltering.CallFilteringResult;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+import org.mockito.ArgumentCaptor;
+import org.mockito.Mock;
+
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.CompletionStage;
+import java.util.concurrent.TimeUnit;
+
+@RunWith(JUnit4.class)
+public class BlockCheckerFilterTest extends TelecomTestCase {
+ @Mock Call mCall;
+ @Mock private BlockCheckerAdapter mBlockCheckerAdapter;
+ @Mock private CallerInfoLookupHelper mCallerInfoLookupHelper;
+ @Mock private CarrierConfigManager mCarrierConfigManager;
+
+ private BlockCheckerFilter mFilter;
+ private static final CallFilteringResult PASS_RESULT = new CallFilteringResult.Builder()
+ .setShouldAllowCall(true)
+ .setShouldReject(false)
+ .setShouldAddToCallLog(true)
+ .setShouldShowNotification(true)
+ .build();
+ private static final CallFilteringResult BLOCK_RESULT = new CallFilteringResult.Builder()
+ .setShouldAllowCall(false)
+ .setShouldReject(true)
+ .setShouldAddToCallLog(true)
+ .setShouldShowNotification(false)
+ .setCallBlockReason(CallLog.Calls.BLOCK_REASON_BLOCKED_NUMBER)
+ .setCallScreeningAppName(null)
+ .setCallScreeningComponentName(null)
+ .build();
+
+ private static final Uri TEST_HANDLE = Uri.parse("tel:1235551234");
+
+ @Override
+ @Before
+ public void setUp() throws Exception {
+ super.setUp();
+ when(mCall.getHandle()).thenReturn(TEST_HANDLE);
+ mFilter = new BlockCheckerFilter(mContext, mCall, mCallerInfoLookupHelper,
+ mBlockCheckerAdapter);
+ }
+
+ @SmallTest
+ @Test
+ public void testBlockNumber() throws Exception {
+ when(mBlockCheckerAdapter.getBlockStatus(any(Context.class),
+ eq(TEST_HANDLE.getSchemeSpecificPart()), any(Bundle.class)))
+ .thenReturn(STATUS_BLOCKED_IN_LIST);
+
+ setEnhancedBlockingEnabled(false);
+ assertEquals(BLOCK_RESULT, mFilter.startFilterLookup(PASS_RESULT).toCompletableFuture()
+ .get(BlockCheckerFilter.CALLER_INFO_QUERY_TIMEOUT, TimeUnit.MILLISECONDS));
+ }
+
+ @SmallTest
+ @Test
+ public void testBlockNumberWhenEnhancedBlockingEnabled() throws Exception {
+ when(mBlockCheckerAdapter.getBlockStatus(any(Context.class),
+ eq(TEST_HANDLE.getSchemeSpecificPart()), any(Bundle.class)))
+ .thenReturn(STATUS_BLOCKED_IN_LIST);
+
+ setEnhancedBlockingEnabled(true);
+ CompletionStage<CallFilteringResult> resultFuture = mFilter.startFilterLookup(PASS_RESULT);
+ verifyEnhancedLookupStart();
+ assertEquals(BLOCK_RESULT, resultFuture.toCompletableFuture()
+ .get(BlockCheckerFilter.CALLER_INFO_QUERY_TIMEOUT, TimeUnit.MILLISECONDS));
+ }
+
+ @SmallTest
+ @Test
+ public void testDontBlockNumber() throws Exception {
+ when(mBlockCheckerAdapter.getBlockStatus(any(Context.class),
+ eq(TEST_HANDLE.getSchemeSpecificPart()), any(Bundle.class)))
+ .thenReturn(STATUS_NOT_BLOCKED);
+
+ setEnhancedBlockingEnabled(false);
+ assertEquals(PASS_RESULT, mFilter.startFilterLookup(PASS_RESULT).toCompletableFuture()
+ .get(BlockCheckerFilter.CALLER_INFO_QUERY_TIMEOUT, TimeUnit.MILLISECONDS));
+ }
+
+ @SmallTest
+ @Test
+ public void testDontBlockNumberWhenEnhancedBlockingEnabled() throws Exception {
+ when(mBlockCheckerAdapter.getBlockStatus(any(Context.class),
+ eq(TEST_HANDLE.getSchemeSpecificPart()), any(Bundle.class)))
+ .thenReturn(STATUS_NOT_BLOCKED);
+
+ setEnhancedBlockingEnabled(true);
+ CompletionStage<CallFilteringResult> resultFuture = mFilter.startFilterLookup(PASS_RESULT);
+ verifyEnhancedLookupStart();
+ assertEquals(PASS_RESULT, resultFuture.toCompletableFuture()
+ .get(BlockCheckerFilter.CALLER_INFO_QUERY_TIMEOUT, TimeUnit.MILLISECONDS));
+ }
+
+ private void setEnhancedBlockingEnabled(boolean value) {
+ PersistableBundle bundle = new PersistableBundle();
+ bundle.putBoolean(CarrierConfigManager.KEY_SUPPORT_ENHANCED_CALL_BLOCKING_BOOL, value);
+ when(mContext.getSystemService(Context.CARRIER_CONFIG_SERVICE))
+ .thenReturn(mCarrierConfigManager);
+ when(mCarrierConfigManager.getConfig()).thenReturn(bundle);
+ if (value) {
+ when(mCall.getHandlePresentation()).thenReturn(TelecomManager.PRESENTATION_ALLOWED);
+ }
+ }
+
+ private void verifyEnhancedLookupStart() {
+ ArgumentCaptor<CallerInfoLookupHelper.OnQueryCompleteListener> captor =
+ ArgumentCaptor.forClass(CallerInfoLookupHelper.OnQueryCompleteListener.class);
+ verify(mCallerInfoLookupHelper, timeout(BlockCheckerFilter.CALLER_INFO_QUERY_TIMEOUT))
+ .startLookup(eq(TEST_HANDLE), captor.capture());
+ captor.getValue().onCallerInfoQueryComplete(TEST_HANDLE, null);
+ }
+}
diff --git a/tests/src/com/android/server/telecom/tests/DirectToVoicemailFilterTest.java b/tests/src/com/android/server/telecom/tests/DirectToVoicemailFilterTest.java
new file mode 100644
index 0000000..43aa8e2
--- /dev/null
+++ b/tests/src/com/android/server/telecom/tests/DirectToVoicemailFilterTest.java
@@ -0,0 +1,137 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.server.telecom.tests;
+
+import static junit.framework.Assert.assertEquals;
+
+import static org.mockito.ArgumentMatchers.nullable;
+import static org.mockito.Mockito.timeout;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import android.telephony.CallerInfo;
+import android.net.Uri;
+import android.provider.CallLog;
+import android.test.suitebuilder.annotation.SmallTest;
+
+import com.android.server.telecom.Call;
+import com.android.server.telecom.CallerInfoLookupHelper;
+import com.android.server.telecom.callfiltering.CallFilter;
+import com.android.server.telecom.callfiltering.CallFilteringResult;
+import com.android.server.telecom.callfiltering.DirectToVoicemailFilter;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+import org.mockito.ArgumentCaptor;
+import org.mockito.Mock;
+
+import java.util.concurrent.CompletionStage;
+import java.util.concurrent.TimeUnit;
+
+
+@RunWith(JUnit4.class)
+public class DirectToVoicemailFilterTest extends TelecomTestCase {
+ @Mock private CallerInfoLookupHelper mCallerInfoLookupHelper;
+ @Mock private Call mCall;
+
+ private final CallFilteringResult PASS_RESULT = new CallFilteringResult.Builder()
+ .setShouldAllowCall(true)
+ .setShouldReject(false)
+ .setShouldAddToCallLog(true)
+ .setShouldShowNotification(true)
+ .build();
+ private final CallFilteringResult DIRECT_TO_VOICEMAIL_RESULT = new CallFilteringResult.Builder()
+ .setShouldAllowCall(false)
+ .setShouldReject(true)
+ .setShouldAddToCallLog(true)
+ .setShouldShowNotification(true)
+ .setCallBlockReason(CallLog.Calls.BLOCK_REASON_DIRECT_TO_VOICEMAIL)
+ .setCallScreeningAppName(null)
+ .setCallScreeningComponentName(null)
+ .build();
+ private final long CALLER_INFO_TIMEOUT = 2000;
+
+ private Uri mTestHandle;
+ private DirectToVoicemailFilter mFilter;
+ private CallerInfo mCallerInfo;
+
+ @Override
+ @Before
+ public void setUp() throws Exception {
+ super.setUp();
+ mTestHandle = Uri.parse("tel:1235551234");
+ mFilter = new DirectToVoicemailFilter(mCall, mCallerInfoLookupHelper);
+ mCallerInfo = new CallerInfo();
+ when(mCall.getHandle()).thenReturn(mTestHandle);
+ }
+
+ @SmallTest
+ @Test
+ public void testSendToVoicemail() throws Exception {
+ mCallerInfo.shouldSendToVoicemail = true;
+
+ CompletionStage<CallFilteringResult> resultFuture = mFilter.startFilterLookup(PASS_RESULT);
+ CallerInfoLookupHelper.OnQueryCompleteListener listener = verifyLookupStart();
+ listener.onCallerInfoQueryComplete(mTestHandle, mCallerInfo);
+ assertEquals(DIRECT_TO_VOICEMAIL_RESULT, resultFuture.toCompletableFuture()
+ .get(CALLER_INFO_TIMEOUT, TimeUnit.MILLISECONDS));
+ }
+
+ @SmallTest
+ @Test
+ public void testDontSendToVoicemail() throws Exception {
+ mCallerInfo.shouldSendToVoicemail = false;
+
+ CompletionStage<CallFilteringResult> resultFuture = mFilter.startFilterLookup(PASS_RESULT);
+ CallerInfoLookupHelper.OnQueryCompleteListener listener = verifyLookupStart();
+ listener.onCallerInfoQueryComplete(mTestHandle, mCallerInfo);
+ assertEquals(PASS_RESULT, resultFuture.toCompletableFuture()
+ .get(CALLER_INFO_TIMEOUT, TimeUnit.MILLISECONDS));
+ }
+
+ @SmallTest
+ @Test
+ public void testNullResponseAndDifferentHandleFromLookupHelper() throws Exception {
+ mTestHandle = null;
+
+ CompletionStage<CallFilteringResult> resultFuture = mFilter.startFilterLookup(PASS_RESULT);
+ CallerInfoLookupHelper.OnQueryCompleteListener listener = verifyLookupStart();
+ listener.onCallerInfoQueryComplete(null, null);
+ assertEquals(PASS_RESULT, resultFuture.toCompletableFuture()
+ .get(CALLER_INFO_TIMEOUT, TimeUnit.MILLISECONDS));
+ }
+
+ @SmallTest
+ @Test
+ public void testNullResponseAndSameHandleFromLookupHelper() throws Exception {
+ CompletionStage<CallFilteringResult> resultFuture = mFilter.startFilterLookup(PASS_RESULT);
+ CallerInfoLookupHelper.OnQueryCompleteListener listener = verifyLookupStart();
+ listener.onCallerInfoQueryComplete(mTestHandle, null);
+ assertEquals(PASS_RESULT, resultFuture.toCompletableFuture()
+ .get(CALLER_INFO_TIMEOUT, TimeUnit.MILLISECONDS));
+ }
+
+ private CallerInfoLookupHelper.OnQueryCompleteListener verifyLookupStart() {
+ ArgumentCaptor<CallerInfoLookupHelper.OnQueryCompleteListener> captor =
+ ArgumentCaptor.forClass(CallerInfoLookupHelper.OnQueryCompleteListener.class);
+ verify(mCallerInfoLookupHelper, timeout(CALLER_INFO_TIMEOUT))
+ .startLookup(nullable(Uri.class), captor.capture());
+ return captor.getValue();
+ }
+}