Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 12dcbe7c authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Clear media switcher items when in phone call" into qt-dev

parents 41656de5 095c5be4
Loading
Loading
Loading
Loading
+18 −8
Original line number Diff line number Diff line
@@ -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();
        }

@@ -198,4 +193,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;
    }
}
+19 −1
Original line number Diff line number Diff line
@@ -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);
@@ -124,6 +130,18 @@ public class MediaOutputSliceTest {
        assertThat(rows).isEqualTo(0);
    }

    @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();