1 /*
2  * Copyright (C) 2013 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.server.wifi;
18 
19 import android.annotation.NonNull;
20 import android.net.wifi.ITrafficStateCallback;
21 import android.net.wifi.WifiManager;
22 import android.os.Handler;
23 import android.os.IBinder;
24 import android.os.Looper;
25 import android.os.RemoteException;
26 import android.util.Log;
27 
28 import com.android.server.wifi.util.ExternalCallbackTracker;
29 
30 import java.io.FileDescriptor;
31 import java.io.PrintWriter;
32 
33 /**
34  * Polls for traffic stats and notifies the clients
35  */
36 public class WifiTrafficPoller {
37 
38     private static final String TAG = "WifiTrafficPoller";
39 
40     private long mTxPkts;
41     private long mRxPkts;
42     /* Tracks last reported data activity */
43     private int mDataActivity;
44 
45     private final ExternalCallbackTracker<ITrafficStateCallback> mRegisteredCallbacks;
46 
WifiTrafficPoller(@onNull Looper looper)47     WifiTrafficPoller(@NonNull Looper looper) {
48         mRegisteredCallbacks = new ExternalCallbackTracker<ITrafficStateCallback>(
49                 new Handler(looper));
50     }
51 
52     /**
53      * Add a new callback to the traffic poller.
54      */
addCallback(IBinder binder, ITrafficStateCallback callback, int callbackIdentifier)55     public void addCallback(IBinder binder, ITrafficStateCallback callback,
56                             int callbackIdentifier) {
57         if (!mRegisteredCallbacks.add(binder, callback, callbackIdentifier)) {
58             Log.e(TAG, "Failed to add callback");
59             return;
60         }
61     }
62 
63     /**
64      * Remove an existing callback from the traffic poller.
65      */
removeCallback(int callbackIdentifier)66     public void removeCallback(int callbackIdentifier) {
67         mRegisteredCallbacks.remove(callbackIdentifier);
68     }
69 
notifyOnDataActivity(long txPkts, long rxPkts)70     void notifyOnDataActivity(long txPkts, long rxPkts) {
71         long sent, received;
72         long preTxPkts = mTxPkts, preRxPkts = mRxPkts;
73         int dataActivity = WifiManager.TrafficStateCallback.DATA_ACTIVITY_NONE;
74         mTxPkts = txPkts;
75         mRxPkts = rxPkts;
76 
77         if (preTxPkts > 0 || preRxPkts > 0) {
78             sent = mTxPkts - preTxPkts;
79             received = mRxPkts - preRxPkts;
80             if (sent > 0) {
81                 dataActivity |= WifiManager.TrafficStateCallback.DATA_ACTIVITY_OUT;
82             }
83             if (received > 0) {
84                 dataActivity |= WifiManager.TrafficStateCallback.DATA_ACTIVITY_IN;
85             }
86 
87             if (dataActivity != mDataActivity) {
88                 mDataActivity = dataActivity;
89                 for (ITrafficStateCallback callback : mRegisteredCallbacks.getCallbacks()) {
90                     try {
91                         callback.onStateChanged(mDataActivity);
92                     } catch (RemoteException e) {
93                         // Failed to reach, skip
94                         // Client removal is handled in WifiService
95                     }
96                 }
97             }
98         }
99     }
100 
101     /**
102      * Dump method for traffic poller.
103      */
dump(FileDescriptor fd, PrintWriter pw, String[] args)104     public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
105         pw.println("mTxPkts " + mTxPkts);
106         pw.println("mRxPkts " + mRxPkts);
107         pw.println("mDataActivity " + mDataActivity);
108         pw.println("mRegisteredCallbacks " + mRegisteredCallbacks.getNumCallbacks());
109     }
110 
111 }
112