use of android.net.NetworkInfo in project QiDict by timqi.
the class MainActivity method isNetworkEnable.
private boolean isNetworkEnable() {
ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected())
return true;
return false;
}
use of android.net.NetworkInfo in project Shuttle by timusus.
the class ReconnectionService method onCreate.
@Override
public void onCreate() {
LOGD(TAG, "onCreate() is called");
mCastManager = VideoCastManager.getInstance();
if (!mCastManager.isConnected() && !mCastManager.isConnecting()) {
mCastManager.reconnectSessionIfPossible();
}
// register a broadcast receiver to be notified when screen goes on or off
IntentFilter screenOnOffIntentFilter = new IntentFilter(Intent.ACTION_SCREEN_ON);
screenOnOffIntentFilter.addAction(Intent.ACTION_SCREEN_OFF);
mScreenOnOffBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
LOGD(TAG, "ScreenOnOffBroadcastReceiver: onReceive(): " + intent.getAction());
long timeLeft = getMediaRemainingTime();
if (timeLeft < EPSILON_MS) {
handleTermination();
}
}
};
registerReceiver(mScreenOnOffBroadcastReceiver, screenOnOffIntentFilter);
// register a wifi receiver that would be notified when the network state changes
IntentFilter networkIntentFilter = new IntentFilter();
networkIntentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
mWifiBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
NetworkInfo info = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
boolean connected = info.isConnected();
String networkSsid = connected ? Utils.getWifiSsid(context) : null;
ReconnectionService.this.onWifiConnectivityChanged(connected, networkSsid);
}
}
};
registerReceiver(mWifiBroadcastReceiver, networkIntentFilter);
super.onCreate();
}
use of android.net.NetworkInfo in project Shuttle by timusus.
the class ShuttleUtilsPowerMockTest method testIsOnlineWifiConnected.
@Test
public void testIsOnlineWifiConnected() {
ShuttleApplication mockApplication = mock(ShuttleApplication.class);
SharedPreferences mockSharedPreferences = mock(SharedPreferences.class);
ConnectivityManager mockConnectivityManager = mock(ConnectivityManager.class);
NetworkInfo mockNetworkInfo = mock(NetworkInfo.class);
mockStatic(PreferenceManager.class);
mockStatic(ShuttleApplication.class);
when(PreferenceManager.getDefaultSharedPreferences(any(Context.class))).thenReturn(mockSharedPreferences);
when(ShuttleApplication.getInstance()).thenReturn(mockApplication);
// Mock the connection to Wi-Fi
when(mockApplication.getSystemService(Context.CONNECTIVITY_SERVICE)).thenReturn(mockConnectivityManager);
when(mockConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI)).thenReturn(mockNetworkInfo);
when(mockNetworkInfo.isConnectedOrConnecting()).thenReturn(true);
// Test when we care about Wi-Fi (and it's connected), regardless of user preference
assertThat(ShuttleUtils.isOnline(true)).isTrue();
// Test when we don't care about Wi-Fi (but it's connected anyway), regardless of user preference
assertThat(ShuttleUtils.isOnline(false)).isTrue();
}
use of android.net.NetworkInfo in project Shuttle by timusus.
the class ShuttleUtilsPowerMockTest method testIsOnlineCellularConnected.
@Test
public void testIsOnlineCellularConnected() {
ShuttleApplication mockApplication = mock(ShuttleApplication.class);
SharedPreferences mockSharedPreferences = mock(SharedPreferences.class);
ConnectivityManager mockConnectivityManager = mock(ConnectivityManager.class);
NetworkInfo mockNetworkInfo = mock(NetworkInfo.class);
mockStatic(PreferenceManager.class);
mockStatic(ShuttleApplication.class);
when(PreferenceManager.getDefaultSharedPreferences(any(Context.class))).thenReturn(mockSharedPreferences);
when(ShuttleApplication.getInstance()).thenReturn(mockApplication);
// Mock the connection to cellular data, but not Wi-Fi
when(mockApplication.getSystemService(Context.CONNECTIVITY_SERVICE)).thenReturn(mockConnectivityManager);
when(mockConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI)).thenReturn(null);
when(mockConnectivityManager.getActiveNetworkInfo()).thenReturn(mockNetworkInfo);
when(mockNetworkInfo.isConnectedOrConnecting()).thenReturn(true);
// Test when we care about Wi-Fi and so does the user (but only cellular is connected)
when(mockSharedPreferences.getBoolean(eq("pref_download_wifi_only"), anyBoolean())).thenReturn(true);
assertThat(ShuttleUtils.isOnline(true)).isFalse();
// Test when we care about Wi-Fi, but the user doesn't (and only cellular is connected)
when(mockSharedPreferences.getBoolean(eq("pref_download_wifi_only"), anyBoolean())).thenReturn(false);
assertThat(ShuttleUtils.isOnline(true)).isTrue();
// Test when we don't care about Wi-Fi, even if the user does (and only cellular is connected)
when(mockSharedPreferences.getBoolean(eq("pref_download_wifi_only"), anyBoolean())).thenReturn(true);
assertThat(ShuttleUtils.isOnline(false)).isTrue();
// Test when we don't care about Wi-Fi and neither does the user (and only cellular is connected)
when(mockSharedPreferences.getBoolean(eq("pref_download_wifi_only"), anyBoolean())).thenReturn(false);
assertThat(ShuttleUtils.isOnline(false)).isTrue();
}
use of android.net.NetworkInfo in project glide by bumptech.
the class DefaultConnectivityMonitor method isConnected.
@Synthetic
boolean isConnected(Context context) {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
return networkInfo != null && networkInfo.isConnected();
}
Aggregations