package com.czbix.v2ex.common;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.support.v4.net.ConnectivityManagerCompat;

import com.czbix.v2ex.AppCtx;

public class DeviceStatus {
    private static final DeviceStatus instance;

    private final ConnectivityManager mConnectivityManager;

    static {
        instance = new DeviceStatus(AppCtx.getInstance());
    }

    public static DeviceStatus getInstance() {
        return instance;
    }

    private boolean mIsNetworkMetered;
    private boolean mIsNetworkConnected;

    DeviceStatus(Context context) {
        mConnectivityManager = ((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE));
        context.registerReceiver(new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                updateNetworkStatus();
            }
        }, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));

        updateNetworkStatus();
    }

    private void updateNetworkStatus() {
        mIsNetworkMetered = ConnectivityManagerCompat.isActiveNetworkMetered(mConnectivityManager);
        mIsNetworkConnected = mConnectivityManager.getActiveNetworkInfo().isConnected();
    }

    public boolean isNetworkMetered() {
        return mIsNetworkMetered;
    }

    public boolean isNetworkConnected() {
        return mIsNetworkConnected;
    }
}

@@ -203,24 +203,56 @@
 ityManager;%0A
+import android.net.NetworkInfo;%0A
 import andro
@@ -1305,24 +1305,25 @@
 nager);%0A
+%0A
         
 mIsNetwo
@@ -1314,35 +1314,51 @@
         
-mIs
+final 
 Network
-Connected
+Info activeNetworkInfo
  = mConn
@@ -1395,16 +1395,168 @@
 rkInfo()
+;%0A        if (activeNetworkInfo == null) %7B%0A            mIsNetworkConnected = false;%0A        %7D else %7B%0A            mIsNetworkConnected = activeNetworkInfo
 .isConne
@@ -1559,24 +1559,34 @@
 onnected();%0A
+        %7D%0A
     %7D%0A%0A    p

fix NPE