Cache setDefaultDialer value to fix CTS failures

When CTS tests call setDefaultDialer, they expect the changes to be made
synchronously, because they require these permissions later in the test.
The new DefaultDialerCache didn't update the cache synchronously, instead
it would update the cache when the ContentProvider updated, after a short
delay. This caused CTS tests to be flaky or fail.

I also fixed some of the Telecom Unit Test failures that were occuring.

Test: Run CTS tests.
Bug: 34094508
Change-Id: I6324c84a15c4087ff8acccaf8aae93ea586823d3
diff --git a/src/com/android/server/telecom/DefaultDialerCache.java b/src/com/android/server/telecom/DefaultDialerCache.java
index d511332..9f7be16 100644
--- a/src/com/android/server/telecom/DefaultDialerCache.java
+++ b/src/com/android/server/telecom/DefaultDialerCache.java
@@ -175,9 +175,15 @@
     }
 
     public boolean setDefaultDialer(String packageName, int userId) {
-        // No need to update cache -- this'll trigger the content observer.
-        return mDefaultDialerManagerAdapter.setDefaultDialerApplication(
+        boolean isChanged = mDefaultDialerManagerAdapter.setDefaultDialerApplication(
                 mContext, packageName, userId);
+        if(isChanged) {
+            synchronized (mLock) {
+                // Update the cache synchronously so that there is no delay in cache update.
+                mCurrentDefaultDialerPerUser.put(userId, packageName);
+            }
+        }
+        return isChanged;
     }
 
     private String refreshCacheForUser(int userId) {
diff --git a/tests/src/com/android/server/telecom/tests/BluetoothPhoneServiceTest.java b/tests/src/com/android/server/telecom/tests/BluetoothPhoneServiceTest.java
index 9052134..b4a0a07 100644
--- a/tests/src/com/android/server/telecom/tests/BluetoothPhoneServiceTest.java
+++ b/tests/src/com/android/server/telecom/tests/BluetoothPhoneServiceTest.java
@@ -246,7 +246,7 @@
         mBluetoothPhoneService.mBinder.listCurrentCalls();
 
         verify(mMockBluetoothHeadset).clccResponse(eq(1), eq(0), eq(0), eq(0), eq(false),
-                eq("555-000"), eq(PhoneNumberUtils.TOA_Unknown));
+                eq("555000"), eq(PhoneNumberUtils.TOA_Unknown));
         verify(mMockBluetoothHeadset).clccResponse(0, 0, 0, 0, false, null, 0);
     }
 
@@ -335,9 +335,9 @@
         mBluetoothPhoneService.mBinder.listCurrentCalls();
 
         verify(mMockBluetoothHeadset).clccResponse(eq(1), eq(0), eq(CALL_STATE_ACTIVE), eq(0),
-                eq(false), eq("555-0000"), eq(PhoneNumberUtils.TOA_Unknown));
+                eq(false), eq("5550000"), eq(PhoneNumberUtils.TOA_Unknown));
         verify(mMockBluetoothHeadset).clccResponse(eq(2), eq(1), eq(CALL_STATE_HELD), eq(0),
-                eq(false), eq("555-0001"), eq(PhoneNumberUtils.TOA_Unknown));
+                eq(false), eq("5550001"), eq(PhoneNumberUtils.TOA_Unknown));
         verify(mMockBluetoothHeadset).clccResponse(0, 0, 0, 0, false, null, 0);
     }
 
@@ -376,9 +376,9 @@
         mBluetoothPhoneService.mBinder.listCurrentCalls();
 
         verify(mMockBluetoothHeadset).clccResponse(eq(1), eq(0), eq(CALL_STATE_ACTIVE), eq(0),
-                eq(true), eq("555-0000"), eq(PhoneNumberUtils.TOA_Unknown));
+                eq(true), eq("5550000"), eq(PhoneNumberUtils.TOA_Unknown));
         verify(mMockBluetoothHeadset).clccResponse(eq(2), eq(1), eq(CALL_STATE_ACTIVE), eq(0),
-                eq(true), eq("555-0001"), eq(PhoneNumberUtils.TOA_Unknown));
+                eq(true), eq("5550001"), eq(PhoneNumberUtils.TOA_Unknown));
         verify(mMockBluetoothHeadset).clccResponse(0, 0, 0, 0, false, null, 0);
     }
 
@@ -398,7 +398,7 @@
 
         mBluetoothPhoneService.mBinder.listCurrentCalls();
         verify(mMockBluetoothHeadset).clccResponse(1, 1, CALL_STATE_WAITING, 0, false,
-                "555-0000", PhoneNumberUtils.TOA_Unknown);
+                "5550000", PhoneNumberUtils.TOA_Unknown);
         verify(mMockBluetoothHeadset).clccResponse(0, 0, 0, 0, false, null, 0);
         verify(mMockBluetoothHeadset, times(2)).clccResponse(anyInt(),
                 anyInt(), anyInt(), anyInt(), anyBoolean(), anyString(), anyInt());
@@ -433,7 +433,7 @@
 
         mBluetoothPhoneService.mBinder.listCurrentCalls();
         verify(mMockBluetoothHeadset).clccResponse(1, 1, CALL_STATE_INCOMING, 0, false,
-                "555-0000", PhoneNumberUtils.TOA_Unknown);
+                "5550000", PhoneNumberUtils.TOA_Unknown);
         verify(mMockBluetoothHeadset).clccResponse(0, 0, 0, 0, false, null, 0);
         verify(mMockBluetoothHeadset, times(2)).clccResponse(anyInt(),
                 anyInt(), anyInt(), anyInt(), anyBoolean(), anyString(), anyInt());
@@ -449,11 +449,11 @@
         when(ringingCall.isIncoming()).thenReturn(true);
         when(ringingCall.isConference()).thenReturn(false);
         when(ringingCall.getGatewayInfo()).thenReturn(new GatewayInfo(null, null,
-                Uri.parse("tel:555-0000")));
+                Uri.parse("tel:5550000")));
 
         mBluetoothPhoneService.mBinder.listCurrentCalls();
         verify(mMockBluetoothHeadset).clccResponse(1, 1, CALL_STATE_INCOMING, 0, false,
-                "555-0000", PhoneNumberUtils.TOA_Unknown);
+                "5550000", PhoneNumberUtils.TOA_Unknown);
 
         // Test Caching of old call indicies in clcc
         when(ringingCall.getState()).thenReturn(CallState.ACTIVE);
@@ -467,9 +467,9 @@
 
         mBluetoothPhoneService.mBinder.listCurrentCalls();
         verify(mMockBluetoothHeadset).clccResponse(1, 1, CALL_STATE_ACTIVE, 0, false,
-                "555-0000", PhoneNumberUtils.TOA_Unknown);
+                "5550000", PhoneNumberUtils.TOA_Unknown);
         verify(mMockBluetoothHeadset).clccResponse(2, 1, CALL_STATE_HELD, 0, false,
-                "555-0001", PhoneNumberUtils.TOA_Unknown);
+                "5550001", PhoneNumberUtils.TOA_Unknown);
         verify(mMockBluetoothHeadset, times(2)).clccResponse(0, 0, 0, 0, false, null, 0);
     }
 
@@ -487,7 +487,7 @@
 
         mBluetoothPhoneService.mBinder.listCurrentCalls();
         verify(mMockBluetoothHeadset).clccResponse(1, 0, CALL_STATE_ALERTING, 0, false,
-                "555-0000", PhoneNumberUtils.TOA_Unknown);
+                "5550000", PhoneNumberUtils.TOA_Unknown);
         verify(mMockBluetoothHeadset).clccResponse(0, 0, 0, 0, false, null, 0);
         verify(mMockBluetoothHeadset, times(2)).clccResponse(anyInt(),
                 anyInt(), anyInt(), anyInt(), anyBoolean(), anyString(), anyInt());
@@ -514,9 +514,9 @@
 
         mBluetoothPhoneService.mBinder.listCurrentCalls();
         verify(mMockBluetoothHeadset).clccResponse(1, 0, CALL_STATE_ALERTING, 0, false,
-                "555-0000", PhoneNumberUtils.TOA_Unknown);
+                "5550000", PhoneNumberUtils.TOA_Unknown);
         verify(mMockBluetoothHeadset).clccResponse(2, 1, CALL_STATE_HELD, 0, false,
-                "555-0001", PhoneNumberUtils.TOA_Unknown);
+                "5550001", PhoneNumberUtils.TOA_Unknown);
         verify(mMockBluetoothHeadset).clccResponse(0, 0, 0, 0, false, null, 0);
         verify(mMockBluetoothHeadset, times(3)).clccResponse(anyInt(),
                 anyInt(), anyInt(), anyInt(), anyBoolean(), anyString(), anyInt());
@@ -543,12 +543,12 @@
     @MediumTest
     public void testQueryPhoneState() throws Exception {
         Call ringingCall = createRingingCall();
-        when(ringingCall.getHandle()).thenReturn(Uri.parse("tel:555-0000"));
+        when(ringingCall.getHandle()).thenReturn(Uri.parse("tel:5550000"));
 
         mBluetoothPhoneService.mBinder.queryPhoneState();
 
         verify(mMockBluetoothHeadset).phoneStateChanged(eq(0), eq(0), eq(CALL_STATE_INCOMING),
-                eq("555-0000"), eq(PhoneNumberUtils.TOA_Unknown));
+                eq("5550000"), eq(PhoneNumberUtils.TOA_Unknown));
     }
 
     @MediumTest
@@ -705,12 +705,12 @@
     @MediumTest
     public void testOnCallAddedRinging() throws Exception {
         Call ringingCall = createRingingCall();
-        when(ringingCall.getHandle()).thenReturn(Uri.parse("tel:555-000"));
+        when(ringingCall.getHandle()).thenReturn(Uri.parse("tel:555000"));
 
         mBluetoothPhoneService.mCallsManagerListener.onCallAdded(ringingCall);
 
         verify(mMockBluetoothHeadset).phoneStateChanged(eq(0), eq(0), eq(CALL_STATE_INCOMING),
-                eq("555-000"), eq(PhoneNumberUtils.TOA_Unknown));
+                eq("555000"), eq(PhoneNumberUtils.TOA_Unknown));
 
     }
 
@@ -814,7 +814,7 @@
                 CallState.ACTIVE, CallState.ON_HOLD);
 
         verify(mMockBluetoothHeadset, never()).phoneStateChanged(eq(0), eq(2), eq(CALL_STATE_HELD),
-                eq("555-0000"), eq(PhoneNumberUtils.TOA_Unknown));
+                eq("5550000"), eq(PhoneNumberUtils.TOA_Unknown));
     }
 
     @MediumTest
@@ -856,14 +856,14 @@
     @MediumTest
     public void testBluetoothAdapterReceiver() throws Exception {
         Call ringingCall = createRingingCall();
-        when(ringingCall.getHandle()).thenReturn(Uri.parse("tel:555-0000"));
+        when(ringingCall.getHandle()).thenReturn(Uri.parse("tel:5550000"));
 
         Intent intent = new Intent();
         intent.putExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.STATE_ON);
         mBluetoothPhoneService.mBluetoothAdapterReceiver.onReceive(mContext, intent);
 
         verify(mMockBluetoothHeadset).phoneStateChanged(eq(0), eq(0), eq(CALL_STATE_INCOMING),
-                eq("555-0000"), eq(PhoneNumberUtils.TOA_Unknown));
+                eq("5550000"), eq(PhoneNumberUtils.TOA_Unknown));
     }
 
     private void addCallCapability(Call call, int capability) {
diff --git a/tests/src/com/android/server/telecom/tests/CallAudioRouteStateMachineTest.java b/tests/src/com/android/server/telecom/tests/CallAudioRouteStateMachineTest.java
index d53f270..1f444f2 100644
--- a/tests/src/com/android/server/telecom/tests/CallAudioRouteStateMachineTest.java
+++ b/tests/src/com/android/server/telecom/tests/CallAudioRouteStateMachineTest.java
@@ -235,6 +235,8 @@
         CallAudioState expectedMiddleState = new CallAudioState(false,
                 CallAudioState.ROUTE_WIRED_HEADSET,
                 CallAudioState.ROUTE_WIRED_HEADSET | CallAudioState.ROUTE_SPEAKER);
+        waitForHandlerAction(stateMachine.getHandler(), TEST_TIMEOUT);
+        waitForHandlerAction(stateMachine.getHandler(), TEST_TIMEOUT);
         verifyNewSystemCallAudioState(initState, expectedMiddleState);
         resetMocks(true);
 
diff --git a/tests/src/com/android/server/telecom/tests/DefaultDialerCacheTest.java b/tests/src/com/android/server/telecom/tests/DefaultDialerCacheTest.java
index 4c95508..e73df61 100644
--- a/tests/src/com/android/server/telecom/tests/DefaultDialerCacheTest.java
+++ b/tests/src/com/android/server/telecom/tests/DefaultDialerCacheTest.java
@@ -64,10 +64,11 @@
         mDefaultDialerCache = new DefaultDialerCache(
                 mContext, mMockDefaultDialerManager, new TelecomSystem.SyncRoot() { });
 
-        verify(mContext).registerReceiverAsUser(
+        verify(mContext, times(2)).registerReceiverAsUser(
                 receiverCaptor.capture(), eq(UserHandle.ALL), any(IntentFilter.class),
                 isNull(String.class), isNull(Handler.class));
-        mPackageChangeReceiver = receiverCaptor.getValue();
+        // Receive the first receiver that was captured, the package change receiver.
+        mPackageChangeReceiver = receiverCaptor.getAllValues().get(0);
         mDefaultDialerSettingObserver = mDefaultDialerCache.getContentObserver();
 
         when(mMockDefaultDialerManager.getDefaultDialerApplication(any(Context.class), eq(USER0)))