FileDocCategorySizeDatePackage
WifiTrafficPoller.javaAPI DocAndroid 5.1 API8609Thu Mar 12 22:22:52 GMT 2015com.android.server.wifi

WifiTrafficPoller

public final class WifiTrafficPoller extends Object

Fields Summary
private boolean
DBG
private boolean
VDBG
private final String
TAG
private static final int
POLL_TRAFFIC_STATS_INTERVAL_MSECS
Interval in milliseconds between polling for traffic statistics
private static final int
ENABLE_TRAFFIC_STATS_POLL
private static final int
TRAFFIC_STATS_POLL
private static final int
ADD_CLIENT
private static final int
REMOVE_CLIENT
private boolean
mEnableTrafficStatsPoll
private int
mTrafficStatsPollToken
private long
mTxPkts
private long
mRxPkts
private int
mDataActivity
private final List
mClients
private AtomicBoolean
mScreenOn
private final TrafficHandler
mTrafficHandler
private android.net.NetworkInfo
mNetworkInfo
private final String
mInterface
Constructors Summary
WifiTrafficPoller(android.content.Context context, String iface)


        
        mInterface = iface;
        mTrafficHandler = new TrafficHandler();

        IntentFilter filter = new IntentFilter();
        filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
        filter.addAction(Intent.ACTION_SCREEN_OFF);
        filter.addAction(Intent.ACTION_SCREEN_ON);

        context.registerReceiver(
                new BroadcastReceiver() {
                    @Override
                    public void onReceive(Context context, Intent intent) {
                        if (intent.getAction().equals(
                                WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
                            mNetworkInfo = (NetworkInfo) intent.getParcelableExtra(
                                    WifiManager.EXTRA_NETWORK_INFO);
                        } else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
                            mScreenOn.set(false);
                        } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
                            mScreenOn.set(true);
                        }
                        evaluateTrafficStatsPolling();
                    }
                }, filter);
    
Methods Summary
voidaddClient(android.os.Messenger client)

        Message.obtain(mTrafficHandler, ADD_CLIENT, client).sendToTarget();
    
voiddump(java.io.FileDescriptor fd, java.io.PrintWriter pw, java.lang.String[] args)

        pw.println("mEnableTrafficStatsPoll " + mEnableTrafficStatsPoll);
        pw.println("mTrafficStatsPollToken " + mTrafficStatsPollToken);
        pw.println("mTxPkts " + mTxPkts);
        pw.println("mRxPkts " + mRxPkts);
        pw.println("mDataActivity " + mDataActivity);
    
voidenableVerboseLogging(int verbose)

        if (verbose > 0 ) {
            DBG = true;
        } else {
            DBG = false;
        }
    
private voidevaluateTrafficStatsPolling()

        Message msg;
        if (mNetworkInfo == null) return;
        if (mNetworkInfo.getDetailedState() == CONNECTED && mScreenOn.get()) {
            msg = Message.obtain(mTrafficHandler,
                    ENABLE_TRAFFIC_STATS_POLL, 1, 0);
        } else {
            msg = Message.obtain(mTrafficHandler,
                    ENABLE_TRAFFIC_STATS_POLL, 0, 0);
        }
        msg.sendToTarget();
    
private voidnotifyOnDataActivity()

        long sent, received;
        long preTxPkts = mTxPkts, preRxPkts = mRxPkts;
        int dataActivity = WifiManager.DATA_ACTIVITY_NONE;

        mTxPkts = TrafficStats.getTxPackets(mInterface);
        mRxPkts = TrafficStats.getRxPackets(mInterface);

        if (VDBG) {
            Log.e(TAG, " packet count Tx="
                    + Long.toString(mTxPkts)
                    + " Rx="
                    + Long.toString(mRxPkts));
        }

        if (preTxPkts > 0 || preRxPkts > 0) {
            sent = mTxPkts - preTxPkts;
            received = mRxPkts - preRxPkts;
            if (sent > 0) {
                dataActivity |= WifiManager.DATA_ACTIVITY_OUT;
            }
            if (received > 0) {
                dataActivity |= WifiManager.DATA_ACTIVITY_IN;
            }

            if (dataActivity != mDataActivity && mScreenOn.get()) {
                mDataActivity = dataActivity;
                if (DBG) {
                    Log.e(TAG, "notifying of data activity "
                            + Integer.toString(mDataActivity));
                }
                for (Messenger client : mClients) {
                    Message msg = Message.obtain();
                    msg.what = WifiManager.DATA_ACTIVITY_NOTIFICATION;
                    msg.arg1 = mDataActivity;
                    try {
                        client.send(msg);
                    } catch (RemoteException e) {
                        // Failed to reach, skip
                        // Client removal is handled in WifiService
                    }
                }
            }
        }
    
voidremoveClient(android.os.Messenger client)

        Message.obtain(mTrafficHandler, REMOVE_CLIENT, client).sendToTarget();