OSDN Git Service

Clear media switcher items when in phone call
authorhughchen <hughchen@google.com>
Thu, 9 May 2019 03:27:25 +0000 (11:27 +0800)
committerhughchen <hughchen@google.com>
Fri, 10 May 2019 02:27:26 +0000 (10:27 +0800)
- update test case

Bug: 132385707
Test: make -j42 RunSettingsRoboTests
Change-Id: I2bbd35e869e8ab5596d280d04fddc98f05629190

src/com/android/settings/media/MediaOutputSlice.java
tests/robotests/src/com/android/settings/media/MediaOutputSliceTest.java

index dee5d62..043960e 100644 (file)
@@ -24,6 +24,7 @@ import android.content.Context;
 import android.content.Intent;
 import android.graphics.drawable.Drawable;
 import android.net.Uri;
+import android.telephony.TelephonyManager;
 import android.text.TextUtils;
 import android.util.Log;
 
@@ -76,14 +77,8 @@ public class MediaOutputSlice implements CustomSliceable {
         final ListBuilder listBuilder = new ListBuilder(mContext, getUri(), ListBuilder.INFINITY)
                 .setAccentColor(COLOR_NOT_TINTED);
 
-        final BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
-        if (!adapter.isEnabled()) {
-            Log.d(TAG, "getSlice() Bluetooth is off");
-            return listBuilder.build();
-        }
-
-        if (getWorker() == null) {
-            Log.d(TAG, "getSlice() Can not get worker through uri!");
+        if (!isVisible()) {
+            Log.d(TAG, "getSlice() is not visible");
             return listBuilder.build();
         }
 
@@ -195,4 +190,19 @@ public class MediaOutputSlice implements CustomSliceable {
     public Class getBackgroundWorkerClass() {
         return MediaDeviceUpdateWorker.class;
     }
+
+    private boolean isVisible() {
+        // To decide Slice's visibility.
+        // Return true if
+        // 1. phone is not in ongoing call mode
+        // 2. worker is not null
+        // 3. Bluetooth is enabled
+        final TelephonyManager telephonyManager =
+                (TelephonyManager)mContext.getSystemService(Context.TELEPHONY_SERVICE);
+        final BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
+
+        return telephonyManager.getCallState() == TelephonyManager.CALL_STATE_IDLE
+                && adapter.isEnabled()
+                && getWorker() != null;
+    }
 }
index 4a34919..ac37a7f 100644 (file)
@@ -34,6 +34,7 @@ import android.bluetooth.BluetoothAdapter;
 import android.content.Context;
 import android.content.Intent;
 import android.graphics.drawable.Drawable;
+import android.telephony.TelephonyManager;
 
 import androidx.slice.Slice;
 import androidx.slice.SliceMetadata;
@@ -55,12 +56,13 @@ import org.robolectric.RobolectricTestRunner;
 import org.robolectric.RuntimeEnvironment;
 import org.robolectric.annotation.Config;
 import org.robolectric.shadow.api.Shadow;
+import org.robolectric.shadows.ShadowTelephonyManager;
 
 import java.util.ArrayList;
 import java.util.List;
 
 @RunWith(RobolectricTestRunner.class)
-@Config(shadows = {ShadowBluetoothAdapter.class})
+@Config(shadows = {ShadowBluetoothAdapter.class, ShadowTelephonyManager.class})
 public class MediaOutputSliceTest {
 
     private static final String TEST_PACKAGE_NAME = "com.fake.android.music";
@@ -80,17 +82,21 @@ public class MediaOutputSliceTest {
     private MediaOutputSlice mMediaOutputSlice;
     private MediaDeviceUpdateWorker mMediaDeviceUpdateWorker;
     private ShadowBluetoothAdapter mShadowBluetoothAdapter;
+    private ShadowTelephonyManager mShadowTelephonyManager;
 
     @Before
     public void setUp() throws Exception {
         MockitoAnnotations.initMocks(this);
         mContext = spy(RuntimeEnvironment.application);
+        mShadowTelephonyManager = Shadow.extract(mContext.getSystemService(
+                Context.TELEPHONY_SERVICE));
 
         // Set-up specs for SliceMetadata.
         SliceProvider.setSpecs(SliceLiveData.SUPPORTED_SPECS);
         // Setup BluetoothAdapter
         mShadowBluetoothAdapter = Shadow.extract(BluetoothAdapter.getDefaultAdapter());
         mShadowBluetoothAdapter.setEnabled(true);
+        mShadowTelephonyManager.setCallState(TelephonyManager.CALL_STATE_IDLE);
 
         mMediaOutputSlice = new MediaOutputSlice(mContext);
         mMediaDeviceUpdateWorker = new MediaDeviceUpdateWorker(mContext, MEDIA_OUTPUT_SLICE_URI);
@@ -125,6 +131,18 @@ public class MediaOutputSliceTest {
     }
 
     @Test
+    public void getSlice_callStateRinging_shouldReturnZeroRow() {
+        mShadowTelephonyManager.setCallState(TelephonyManager.CALL_STATE_RINGING);
+
+        final Slice slice = mMediaOutputSlice.getSlice();
+
+        final int rows = SliceQuery.findAll(slice, FORMAT_SLICE, HINT_LIST_ITEM,
+                null /* nonHints */).size();
+
+        assertThat(rows).isEqualTo(0);
+    }
+
+    @Test
     public void getSlice_shouldHaveActiveDeviceName() {
         mDevices.clear();
         final MediaDevice device = mock(MediaDevice.class);