/* * Copyright (c) 2012-2013 NetEase, Inc. and other contributors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ package com.netease.qa.emmagee.utils; import java.io.FileNotFoundException; import java.io.IOException; import java.io.RandomAccessFile; import android.util.Log; /** * information of network traffic * * @author andrewleo */ public class TrafficInfo { private static final String LOG_TAG = "Emmagee-" + TrafficInfo.class.getSimpleName(); private String uid; public TrafficInfo(String uid) { this.uid = uid; } /** * get total network traffic, which is the sum of upload and download * traffic. * * @return total traffic include received and send traffic */ public long getTrafficInfo() { Log.i(LOG_TAG, "get traffic information"); RandomAccessFile rafRcv = null, rafSnd = null; String rcvPath = "/proc/uid_stat/" + uid + "/tcp_rcv"; String sndPath = "/proc/uid_stat/" + uid + "/tcp_snd"; long rcvTraffic = -1; long sndTraffic = -1; try { rafRcv = new RandomAccessFile(rcvPath, "r"); rafSnd = new RandomAccessFile(sndPath, "r"); rcvTraffic = Long.parseLong(rafRcv.readLine()); sndTraffic = Long.parseLong(rafSnd.readLine()); } catch (FileNotFoundException e) { rcvTraffic = -1; sndTraffic = -1; } catch (NumberFormatException e) { Log.e(LOG_TAG, "NumberFormatException: " + e.getMessage()); e.printStackTrace(); } catch (IOException e) { Log.e(LOG_TAG, "IOException: " + e.getMessage()); e.printStackTrace(); } finally { try { if (rafRcv != null) { rafRcv.close(); } if (rafSnd != null) rafSnd.close(); } catch (IOException e) { Log.i(LOG_TAG, "close randomAccessFile exception: " + e.getMessage()); } } if (rcvTraffic == -1 || sndTraffic == -1) { return -1; } else return rcvTraffic + sndTraffic; } }