Home | History | Annotate | Download | only in trafficcatcher
      1 package com.arm.devlib.netstats;
      2 
      3 import java.lang.InterruptedException;
      4 import java.lang.System;
      5 import java.lang.Thread;
      6 import java.util.ArrayList;
      7 import java.util.Arrays;
      8 import java.util.List;
      9 import java.util.Map;
     10 import java.util.HashMap;
     11 
     12 import android.app.Activity;
     13 import android.app.IntentService;
     14 import android.content.Intent;
     15 import android.content.pm.ApplicationInfo;
     16 import android.content.pm.PackageManager;
     17 import android.net.TrafficStats;
     18 import android.os.Bundle;
     19 import android.util.Log;
     20 
     21 class TrafficPoller implements Runnable {
     22 
     23     private String tag;
     24     private int period;
     25     private PackageManager pm;
     26     private static String TAG = "TrafficMetrics";
     27     private List<String> packageNames;
     28     private Map<String, Map<String, Long>> previousValues;
     29 
     30     public TrafficPoller(String tag, PackageManager pm, int period, List<String> packages) {
     31         this.tag = tag;
     32         this.pm = pm;
     33         this.period = period;
     34         this.packageNames = packages;
     35         this.previousValues = new HashMap<String, Map<String, Long>>();
     36     }
     37 
     38     public void run() {
     39         try {
     40             while (true) {
     41                 Thread.sleep(this.period);
     42                 getPakagesInfo();
     43                 if (Thread.interrupted()) {
     44                     throw new InterruptedException();
     45                 }
     46             }
     47         } catch (InterruptedException e) {
     48         }
     49     }
     50 
     51     public void getPakagesInfo() {
     52         List<ApplicationInfo> apps;
     53         if (this.packageNames == null) {
     54             apps = pm.getInstalledApplications(0);
     55             for (ApplicationInfo app : apps) {
     56             }
     57         } else {
     58             apps = new ArrayList<ApplicationInfo>();
     59             for (String packageName : packageNames) {
     60                 try {
     61                     ApplicationInfo info = pm.getApplicationInfo(packageName,  0);
     62                     apps.add(info);
     63                 } catch (PackageManager.NameNotFoundException e) {
     64                     e.printStackTrace();
     65                 }
     66             }
     67         }
     68 
     69         for (ApplicationInfo appInfo : apps) {
     70             int uid = appInfo.uid;
     71             String name = appInfo.packageName;
     72             long time =  System.currentTimeMillis();
     73             long received = TrafficStats.getUidRxBytes(uid);
     74             long sent = TrafficStats.getUidTxBytes(uid);
     75 
     76             if (!this.previousValues.containsKey(name)) {
     77                 this.previousValues.put(name, new HashMap<String, Long>());
     78                 this.previousValues.get(name).put("sent", sent);
     79                 this.previousValues.get(name).put("received", received);
     80                 Log.i(this.tag, String.format("INITIAL \"%s\" TX: %d RX: %d",
     81                                               name, sent, received));
     82             } else {
     83                 long previosSent = this.previousValues.get(name).put("sent", sent);
     84                 long previosReceived = this.previousValues.get(name).put("received", received);
     85                 Log.i(this.tag, String.format("%d \"%s\" TX: %d RX: %d",
     86                                               time, name,
     87                                               sent - previosSent,
     88                                               received - previosReceived));
     89             }
     90         }
     91     }
     92 }
     93 
     94 public class TrafficMetricsService extends IntentService {
     95 
     96     private static String TAG = "TrafficMetrics";
     97     private Thread thread;
     98     private static int defaultPollingPeriod = 5000;
     99 
    100     public TrafficMetricsService() {
    101             super("TrafficMetrics");
    102     }
    103 
    104     @Override
    105     public void onHandleIntent(Intent intent) {
    106         List<String> packages = null;
    107         String runTag = intent.getStringExtra("tag");
    108         if (runTag == null) {
    109             runTag = TAG;
    110         }
    111         String packagesString = intent.getStringExtra("packages");
    112         int pollingPeriod = intent.getIntExtra("period", this.defaultPollingPeriod);
    113         if (packagesString != null) {
    114             packages = new ArrayList<String>(Arrays.asList(packagesString.split(",")));
    115         }
    116 
    117         if (this.thread != null) {
    118             Log.e(runTag, "Attemping to start when monitoring is already in progress.");
    119             return;
    120         }
    121         this.thread = new Thread(new TrafficPoller(runTag, getPackageManager(), pollingPeriod, packages));
    122         this.thread.start();
    123     }
    124 }
    125