1 /* 2 * Copyright (C) 2008 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; 18 19 import android.content.Context; 20 import android.net.TrafficStats; 21 import android.os.INetStatService; 22 import android.os.SystemClock; 23 24 import java.io.FileDescriptor; 25 import java.io.PrintWriter; 26 27 public class NetStatService extends INetStatService.Stub { 28 private final Context mContext; 29 30 public NetStatService(Context context) { 31 mContext = context; 32 } 33 34 public long getMobileTxPackets() { 35 return TrafficStats.getMobileTxPackets(); 36 } 37 38 public long getMobileRxPackets() { 39 return TrafficStats.getMobileRxPackets(); 40 } 41 42 public long getMobileTxBytes() { 43 return TrafficStats.getMobileTxBytes(); 44 } 45 46 public long getMobileRxBytes() { 47 return TrafficStats.getMobileRxBytes(); 48 } 49 50 public long getTotalTxPackets() { 51 return TrafficStats.getTotalTxPackets(); 52 } 53 54 public long getTotalRxPackets() { 55 return TrafficStats.getTotalRxPackets(); 56 } 57 58 public long getTotalTxBytes() { 59 return TrafficStats.getTotalTxBytes(); 60 } 61 62 public long getTotalRxBytes() { 63 return TrafficStats.getTotalRxBytes(); 64 } 65 66 @Override 67 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { 68 // This data is accessible to any app -- no permission check needed. 69 70 pw.print("Elapsed: total="); 71 pw.print(SystemClock.elapsedRealtime()); 72 pw.print("ms awake="); 73 pw.print(SystemClock.uptimeMillis()); 74 pw.println("ms"); 75 76 pw.print("Mobile: Tx="); 77 pw.print(getMobileTxBytes()); 78 pw.print("B/"); 79 pw.print(getMobileTxPackets()); 80 pw.print("Pkts Rx="); 81 pw.print(getMobileRxBytes()); 82 pw.print("B/"); 83 pw.print(getMobileRxPackets()); 84 pw.println("Pkts"); 85 86 pw.print("Total: Tx="); 87 pw.print(getTotalTxBytes()); 88 pw.print("B/"); 89 pw.print(getTotalTxPackets()); 90 pw.print("Pkts Rx="); 91 pw.print(getTotalRxBytes()); 92 pw.print("B/"); 93 pw.print(getTotalRxPackets()); 94 pw.println("Pkts"); 95 } 96 } 97