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