The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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; |
Ken Shirriff | 1719a39 | 2009-12-07 15:57:35 -0800 | [diff] [blame] | 20 | import android.net.TrafficStats; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 21 | import android.os.INetStatService; |
Dan Egnor | 621bc54 | 2010-03-25 16:20:14 -0700 | [diff] [blame] | 22 | import android.os.SystemClock; |
| 23 | |
| 24 | import java.io.FileDescriptor; |
| 25 | import java.io.PrintWriter; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | |
| 27 | public class NetStatService extends INetStatService.Stub { |
Dan Egnor | 621bc54 | 2010-03-25 16:20:14 -0700 | [diff] [blame] | 28 | private final Context mContext; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | |
| 30 | public NetStatService(Context context) { |
Dan Egnor | 621bc54 | 2010-03-25 16:20:14 -0700 | [diff] [blame] | 31 | mContext = context; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | public long getMobileTxPackets() { |
Ken Shirriff | 5c19aec | 2010-02-04 13:33:45 -0800 | [diff] [blame] | 35 | return TrafficStats.getMobileTxPackets(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | public long getMobileRxPackets() { |
Ken Shirriff | 5c19aec | 2010-02-04 13:33:45 -0800 | [diff] [blame] | 39 | return TrafficStats.getMobileRxPackets(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | public long getMobileTxBytes() { |
Ken Shirriff | 1719a39 | 2009-12-07 15:57:35 -0800 | [diff] [blame] | 43 | return TrafficStats.getMobileTxBytes(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | public long getMobileRxBytes() { |
Ken Shirriff | 1719a39 | 2009-12-07 15:57:35 -0800 | [diff] [blame] | 47 | return TrafficStats.getMobileRxBytes(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | public long getTotalTxPackets() { |
Ken Shirriff | 5c19aec | 2010-02-04 13:33:45 -0800 | [diff] [blame] | 51 | return TrafficStats.getTotalTxPackets(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | public long getTotalRxPackets() { |
Ken Shirriff | 5c19aec | 2010-02-04 13:33:45 -0800 | [diff] [blame] | 55 | return TrafficStats.getTotalRxPackets(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | public long getTotalTxBytes() { |
Ken Shirriff | 1719a39 | 2009-12-07 15:57:35 -0800 | [diff] [blame] | 59 | return TrafficStats.getTotalTxBytes(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | public long getTotalRxBytes() { |
Ken Shirriff | 1719a39 | 2009-12-07 15:57:35 -0800 | [diff] [blame] | 63 | return TrafficStats.getTotalRxBytes(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 64 | } |
Dan Egnor | 621bc54 | 2010-03-25 16:20:14 -0700 | [diff] [blame] | 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 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 96 | } |