import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
+import android.content.pm.PackageManager;
import android.database.ContentObserver;
import android.hardware.display.DisplayManager;
import android.hardware.display.WifiDisplay;
}
public static boolean isAvailable(Context context) {
- try {
- return context.getSystemService(Context.DISPLAY_SERVICE) != null
- && context.getSystemService(Context.WIFI_P2P_SERVICE) != null;
- } catch (Exception e) {
- // Service is not registered, so this is definitely not available.
- return false;
- }
+ return context.getSystemService(Context.DISPLAY_SERVICE) != null
+ && context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI_DIRECT)
+ && context.getSystemService(Context.WIFI_P2P_SERVICE) != null;
}
private void scheduleUpdate(int changes) {
import android.app.Activity;
import android.content.Context;
+import android.content.pm.PackageManager;
import android.hardware.display.DisplayManager;
import android.media.MediaRouter;
import android.net.wifi.p2p.WifiP2pManager;
private SummaryLoader mSummaryLoader;
@Mock
private MediaRouter mMediaRouter;
+ @Mock
+ private PackageManager mPackageManager;
private SummaryLoader.SummaryProvider mSummaryProvider;
MockitoAnnotations.initMocks(this);
when(mActivity.getSystemService(Context.MEDIA_ROUTER_SERVICE))
.thenReturn(mMediaRouter);
+ when(mActivity.getPackageManager()).thenReturn(mPackageManager);
+ when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_WIFI_DIRECT)).thenReturn(true);
mSummaryProvider = WifiDisplaySettings.SUMMARY_PROVIDER_FACTORY.createSummaryProvider(
mActivity, mSummaryLoader);
}
@Test
- public void isAvailable_noService_shouldReturnFalse() {
+ public void isAvailable_nullService_shouldReturnFalse() {
assertThat(WifiDisplaySettings.isAvailable(mActivity))
.isFalse();
}
@Test
- public void isAvailable_throwException_shouldReturnFalse() {
- when(mActivity.getSystemService(Context.WIFI_P2P_SERVICE))
- .thenThrow(new IllegalStateException());
+ public void isAvailable_noWifiDirectFeature_shouldReturnFalse() {
+ when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_WIFI_DIRECT))
+ .thenReturn(false);
assertThat(WifiDisplaySettings.isAvailable(mActivity))
.isFalse();