Merge "Telecom Emergency call diagnostics logger" into udc-dev
diff --git a/res/values/config.xml b/res/values/config.xml
index b0e50b0..15f765b 100644
--- a/res/values/config.xml
+++ b/res/values/config.xml
@@ -73,4 +73,8 @@
     <!-- When set, Telecom will attempt to bind to the {@link CallDiagnosticService} implementation
          defined by the app with this package name. -->
     <string name="call_diagnostic_service_package_name"></string>
+
+    <!-- When true, the options in the call blocking settings to block unavailable and unknown
+     callers are combined into a single toggle. -->
+    <bool name="combine_options_to_block_unavailable_and_unknown_callers">true</bool>
 </resources>
diff --git a/src/com/android/server/telecom/Ringer.java b/src/com/android/server/telecom/Ringer.java
index 7f6705f..b6aa4cc 100644
--- a/src/com/android/server/telecom/Ringer.java
+++ b/src/com/android/server/telecom/Ringer.java
@@ -441,7 +441,11 @@
                 }
             };
             deferBlockOnRingingFuture = true;  // Run in vibrationLogic.
-            mRingtonePlayer.play(ringtoneSupplier, vibrationLogic);
+            if (ringtoneSupplier != null) {
+                mRingtonePlayer.play(ringtoneSupplier, vibrationLogic);
+            } else {
+                vibrationLogic.accept(/* ringtone= */ null, /* stopped= */ false);
+            }
 
             // shouldAcquireAudioFocus is meant to be true, but that check is deferred to here
             // because until now is when we actually know if the ringtone loading worked.
diff --git a/src/com/android/server/telecom/TelecomSystem.java b/src/com/android/server/telecom/TelecomSystem.java
index bb25d92..8477d49 100644
--- a/src/com/android/server/telecom/TelecomSystem.java
+++ b/src/com/android/server/telecom/TelecomSystem.java
@@ -220,6 +220,7 @@
             BlockedNumbersAdapter blockedNumbersAdapter) {
         mContext = context.getApplicationContext();
         LogUtils.initLogging(mContext);
+        android.telecom.Log.setLock(mLock);
         AnomalyReporter.initialize(mContext);
         DefaultDialerManagerAdapter defaultDialerAdapter =
                 new DefaultDialerCache.DefaultDialerManagerAdapterImpl();
diff --git a/src/com/android/server/telecom/settings/EnhancedCallBlockingFragment.java b/src/com/android/server/telecom/settings/EnhancedCallBlockingFragment.java
index c0bb56a..b1a1b0e 100644
--- a/src/com/android/server/telecom/settings/EnhancedCallBlockingFragment.java
+++ b/src/com/android/server/telecom/settings/EnhancedCallBlockingFragment.java
@@ -45,6 +45,7 @@
     private static final String BLOCK_UNAVAILABLE_NUMBERS_KEY =
             "block_unavailable_calls_setting";
     private boolean mIsCombiningRestrictedAndUnknownOption = false;
+    private boolean mIsCombiningUnavailableAndUnknownOption = false;
 
     @Override
     public void onCreate(Bundle savedInstanceState) {
@@ -94,11 +95,17 @@
                         R.bool.combine_options_to_block_restricted_and_unknown_callers);
         if (mIsCombiningRestrictedAndUnknownOption) {
             Preference restricted_pref = findPreference(BLOCK_RESTRICTED_NUMBERS_KEY);
-            Preference unavailable_pref = findPreference(BLOCK_UNAVAILABLE_NUMBERS_KEY);
             screen.removePreference(restricted_pref);
-            screen.removePreference(unavailable_pref);
             Log.i(this, "onCreate: removed block restricted preference.");
         }
+
+        mIsCombiningUnavailableAndUnknownOption = getResources().getBoolean(
+                R.bool.combine_options_to_block_unavailable_and_unknown_callers);
+        if (mIsCombiningUnavailableAndUnknownOption) {
+            Preference unavailable_pref = findPreference(BLOCK_UNAVAILABLE_NUMBERS_KEY);
+            screen.removePreference(unavailable_pref);
+            Log.i(this, "onCreate: removed block unavailable preference.");
+        }
     }
 
     /**
@@ -136,14 +143,20 @@
 
     @Override
     public boolean onPreferenceChange(Preference preference, Object objValue) {
-        if (mIsCombiningRestrictedAndUnknownOption
-                && preference.getKey().equals(BLOCK_UNKNOWN_NUMBERS_KEY)) {
-            Log.i(this, "onPreferenceChange: changing %s and %s to %b",
-                    preference.getKey(), BLOCK_RESTRICTED_NUMBERS_KEY, (boolean) objValue);
-            BlockedNumbersUtil.setEnhancedBlockSetting(getActivity(), BLOCK_RESTRICTED_NUMBERS_KEY,
-                    (boolean) objValue);
-            BlockedNumbersUtil.setEnhancedBlockSetting(getActivity(),
-                    BLOCK_UNAVAILABLE_NUMBERS_KEY, (boolean) objValue);
+        if (preference.getKey().equals(BLOCK_UNKNOWN_NUMBERS_KEY)) {
+            if (mIsCombiningRestrictedAndUnknownOption) {
+                Log.i(this, "onPreferenceChange: changing %s and %s to %b",
+                        preference.getKey(), BLOCK_RESTRICTED_NUMBERS_KEY, (boolean) objValue);
+                BlockedNumbersUtil.setEnhancedBlockSetting(getActivity(),
+                        BLOCK_RESTRICTED_NUMBERS_KEY, (boolean) objValue);
+            }
+
+            if (mIsCombiningUnavailableAndUnknownOption) {
+                Log.i(this, "onPreferenceChange: changing %s and %s to %b",
+                        preference.getKey(), BLOCK_UNAVAILABLE_NUMBERS_KEY, (boolean) objValue);
+                BlockedNumbersUtil.setEnhancedBlockSetting(getActivity(),
+                        BLOCK_UNAVAILABLE_NUMBERS_KEY, (boolean) objValue);
+            }
         }
         BlockedNumbersUtil.setEnhancedBlockSetting(getActivity(), preference.getKey(),
                 (boolean) objValue);
diff --git a/tests/src/com/android/server/telecom/tests/RingerTest.java b/tests/src/com/android/server/telecom/tests/RingerTest.java
index a003de3..cf5b791 100644
--- a/tests/src/com/android/server/telecom/tests/RingerTest.java
+++ b/tests/src/com/android/server/telecom/tests/RingerTest.java
@@ -100,6 +100,7 @@
             new PhoneAccountHandle(new ComponentName("pa_pkg", "pa_cls"),
                     "pa_id");
 
+    boolean mIsHapticPlaybackSupported = true;  // Note: initializeRinger() after changes.
     AsyncRingtonePlayer asyncRingtonePlayer = new AsyncRingtonePlayer();
     Ringer mRingerUnderTest;
     AudioManager mockAudioManager;
@@ -114,18 +115,28 @@
         when(mockPlayerFactory.createPlayer(anyInt())).thenReturn(mockTonePlayer);
         mockAudioManager = mContext.getSystemService(AudioManager.class);
         when(mockAudioManager.getRingerMode()).thenReturn(AudioManager.RINGER_MODE_NORMAL);
-        when(mockSystemSettingsUtil.isHapticPlaybackSupported(any(Context.class))).thenReturn(true);
+        when(mockSystemSettingsUtil.isHapticPlaybackSupported(any(Context.class)))
+                .thenAnswer((invocation) -> mIsHapticPlaybackSupported);
         mockNotificationManager =mContext.getSystemService(NotificationManager.class);
         when(mockTonePlayer.startTone()).thenReturn(true);
         when(mockNotificationManager.matchesCallFilter(any(Bundle.class))).thenReturn(true);
         when(mockRingtoneFactory.hasHapticChannels(any(Ringtone.class))).thenReturn(false);
-        mRingerUnderTest = new Ringer(mockPlayerFactory, mContext, mockSystemSettingsUtil,
-                asyncRingtonePlayer, mockRingtoneFactory, mockVibrator, spyVibrationEffectProxy,
-                mockInCallController, mockNotificationManager, mockAccessibilityManagerAdapter);
         when(mockCall1.getState()).thenReturn(CallState.RINGING);
         when(mockCall2.getState()).thenReturn(CallState.RINGING);
         when(mockCall1.getUserHandleFromTargetPhoneAccount()).thenReturn(PA_HANDLE.getUserHandle());
         when(mockCall2.getUserHandleFromTargetPhoneAccount()).thenReturn(PA_HANDLE.getUserHandle());
+
+        createRingerUnderTest();
+    }
+
+    /**
+     * (Re-)Creates the Ringer for the test. This needs to be called if changing final properties,
+     * like mIsHapticPlaybackSupported.
+     */
+    private void createRingerUnderTest() {
+        mRingerUnderTest = new Ringer(mockPlayerFactory, mContext, mockSystemSettingsUtil,
+                asyncRingtonePlayer, mockRingtoneFactory, mockVibrator, spyVibrationEffectProxy,
+                mockInCallController, mockNotificationManager, mockAccessibilityManagerAdapter);
         // This future is used to wait for AsyncRingtonePlayer to finish its part.
         mRingerUnderTest.setBlockOnRingingFuture(mRingCompletionFuture);
     }
@@ -300,6 +311,24 @@
 
     @SmallTest
     @Test
+    public void testVibrateButNoRingForSilentRingtoneWithoutAudioHapticSupport() throws Exception {
+        mIsHapticPlaybackSupported = false;
+        createRingerUnderTest();  // Needed after changing haptic playback support.
+        mRingerUnderTest.startCallWaiting(mockCall1);
+        when(mockAudioManager.getRingerMode()).thenReturn(AudioManager.RINGER_MODE_VIBRATE);
+        when(mockAudioManager.getStreamVolume(AudioManager.STREAM_RING)).thenReturn(0);
+        enableVibrationWhenRinging();
+        assertFalse(startRingingAndWaitForAsync(mockCall2, false));
+        verify(mockTonePlayer).stopTone();
+        verifyZeroInteractions(mockRingtoneFactory);
+
+        // Play default vibration when future completes with no audio coupled haptics
+        verify(mockVibrator).vibrate(eq(mRingerUnderTest.mDefaultVibrationEffect),
+                any(VibrationAttributes.class));
+    }
+
+    @SmallTest
+    @Test
     public void testAudioCoupledHapticsForSilentRingtone() throws Exception {
         Ringtone mockRingtone = ensureRingtoneMocked();
 
@@ -543,8 +572,8 @@
     }
 
     private void setIsUsingHaptics(Ringtone mockRingtone, boolean useHaptics) {
-        when(mockSystemSettingsUtil.isHapticPlaybackSupported(any(Context.class)))
-            .thenReturn(useHaptics);
+        // Note: using haptics can also depend on mIsHapticPlaybackSupported. If changing
+        // that, the ringerUnderTest needs to be re-created.
         when(mockSystemSettingsUtil.isAudioCoupledVibrationForRampingRingerEnabled())
             .thenReturn(useHaptics);
         when(mockRingtone.hasHapticChannels()).thenReturn(useHaptics);