Search in sources :

Example 6 with NetworkInfo

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;
}
Also used : NetworkInfo(android.net.NetworkInfo) ConnectivityManager(android.net.ConnectivityManager)

Example 7 with NetworkInfo

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();
}
Also used : Context(android.content.Context) IntentFilter(android.content.IntentFilter) NetworkInfo(android.net.NetworkInfo) Intent(android.content.Intent) BroadcastReceiver(android.content.BroadcastReceiver)

Example 8 with NetworkInfo

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();
}
Also used : Context(android.content.Context) SharedPreferences(android.content.SharedPreferences) NetworkInfo(android.net.NetworkInfo) ConnectivityManager(android.net.ConnectivityManager) ShuttleApplication(com.simplecity.amp_library.ShuttleApplication) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 9 with NetworkInfo

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();
}
Also used : Context(android.content.Context) SharedPreferences(android.content.SharedPreferences) NetworkInfo(android.net.NetworkInfo) ConnectivityManager(android.net.ConnectivityManager) ShuttleApplication(com.simplecity.amp_library.ShuttleApplication) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 10 with NetworkInfo

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();
}
Also used : NetworkInfo(android.net.NetworkInfo) ConnectivityManager(android.net.ConnectivityManager) Synthetic(com.bumptech.glide.util.Synthetic)

Aggregations

NetworkInfo (android.net.NetworkInfo)511 ConnectivityManager (android.net.ConnectivityManager)274 Intent (android.content.Intent)47 LinkProperties (android.net.LinkProperties)38 NetworkState (android.net.NetworkState)26 Test (org.junit.Test)26 PendingIntent (android.app.PendingIntent)23 NetworkCapabilities (android.net.NetworkCapabilities)18 Network (android.net.Network)17 LargeTest (android.test.suitebuilder.annotation.LargeTest)17 RemoteException (android.os.RemoteException)16 Context (android.content.Context)15 NetworkAgentInfo (com.android.server.connectivity.NetworkAgentInfo)15 WifiInfo (android.net.wifi.WifiInfo)13 Bundle (android.os.Bundle)13 TestUtils.mockNetworkInfo (com.squareup.picasso.TestUtils.mockNetworkInfo)12 IOException (java.io.IOException)11 WifiManager (android.net.wifi.WifiManager)9 IntentFilter (android.content.IntentFilter)8 BroadcastReceiver (android.content.BroadcastReceiver)7