use of com.asksven.android.common.privateapiproxies.NetworkUsage in project BetterBatteryStats by asksven.
the class Netstats method parseNetstats.
public static ArrayList<StatElement> parseNetstats(List<String> stats) {
ArrayList<StatElement> myStats = new ArrayList<StatElement>();
if ((stats != null) && (stats.size() != 0)) {
// String strRes = res.getResultLine();
if (// (!strRes.contains("Permission Denial"))
true) {
ArrayList<String> keys = new ArrayList<String>();
keys.add(KEY_IDX);
keys.add(KEY_IFACE);
keys.add(KEY_TAG_HEX);
keys.add(KEY_UID);
keys.add(KEY_COUNTER_SET);
keys.add(KEY_RX_BYTES);
keys.add(KEY_RX_PACKETS);
keys.add(KEY_TX_BYTES);
keys.add(KEY_TX_PACKETS);
final ArrayList<String> values = new ArrayList<String>();
final HashMap<String, String> parsed = new HashMap<String, String>();
// ArrayList<String> myRes = res.getResult(); // getTestData();
// process the file, starting on line 2
long totalBytes = 0;
for (int i = 1; i < stats.size(); i++) {
String line = stats.get(i);
StringUtils.splitLine(line, values);
StringUtils.parseLine(keys, values, parsed);
try {
// Netstat entry = new Netstat();
NetworkUsage entry = new NetworkUsage(StringUtils.getParsedInt(parsed, KEY_UID), parsed.get(KEY_IFACE), StringUtils.getParsedLong(parsed, KEY_RX_BYTES), StringUtils.getParsedLong(parsed, KEY_TX_BYTES));
myStats = addToStats(myStats, entry);
totalBytes += entry.getTotalBytes();
} catch (Exception e) {
Log.e(TAG, "An error occured while parsing " + line + ": " + e.getMessage());
}
}
// set the total so that we can calculate the ratio
for (int i = 0; i < myStats.size(); i++) {
myStats.get(i).setTotal(totalBytes);
}
}
}
return myStats;
}
use of com.asksven.android.common.privateapiproxies.NetworkUsage in project BetterBatteryStats by asksven.
the class StatsProvider method getNetworkUsageStatList.
/**
* Get the Kernel Wakelock Stat to be displayed
*
* @param bFilter
* defines if zero-values should be filtered out
* @return a List of Wakelocks sorted by duration (descending)
* @throws Exception
* if the API call failed
*/
public ArrayList<StatElement> getNetworkUsageStatList(boolean bFilter, Reference refFrom, Reference refTo) throws Exception {
Context ctx = BbsApplication.getAppContext();
ArrayList<StatElement> myStats = new ArrayList<StatElement>();
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(ctx);
boolean permsNotNeeded = sharedPrefs.getBoolean("ignore_system_app", false);
// stop straight away if no root permissions or no perms to access data directly
if (!(permsNotNeeded || SysUtils.hasBatteryStatsPermission(ctx) || RootShell.getInstance().hasRootPermissions())) {
myStats.add(new Notification(ctx.getString(R.string.NO_PERM_ERR)));
return myStats;
}
if ((refFrom == null) || (refTo == null)) {
myStats.add(new Notification(ctx.getString(R.string.NO_REF_ERR)));
return myStats;
}
ArrayList<StatElement> myNetworkStats = null;
if ((refTo.m_refNetworkStats != null) && (!refTo.m_refNetworkStats.isEmpty())) {
myNetworkStats = refTo.m_refNetworkStats;
} else {
myStats.add(new Notification(ctx.getString(R.string.NO_STATS)));
return myStats;
}
ArrayList<NetworkUsage> myRetNetworkStats = new ArrayList<NetworkUsage>();
// if we are using custom ref. always retrieve "stats current"
// sort @see
// com.asksven.android.common.privateapiproxies.Walkelock.compareTo
// Collections.sort(myNetworkStats);
String strCurrent = "";
String strRef = "";
String strRefDescr = "";
if (LogSettings.DEBUG) {
if (refFrom != null) {
strRefDescr = refFrom.whoAmI();
if (refFrom.m_refNetworkStats != null) {
strRef = refFrom.m_refNetworkStats.toString();
} else {
strRef = "Network stats is null";
}
} else {
strRefDescr = "Reference is null";
}
Log.d(TAG, "Processing network stats from " + refFrom.m_fileName + " to " + refTo.m_fileName);
Log.d(TAG, "Reference used: " + strRefDescr);
Log.d(TAG, "It is now " + DateUtils.now());
Log.d(TAG, "Substracting " + strRef);
Log.d(TAG, "from " + strCurrent);
}
for (int i = 0; i < myNetworkStats.size(); i++) {
NetworkUsage netStat = ((NetworkUsage) myNetworkStats.get(i)).clone();
if ((!bFilter) || ((netStat.getTotalBytes()) > 0)) {
netStat.substractFromRef(refFrom.m_refNetworkStats);
// threshold
if ((!bFilter) || ((netStat.getTotalBytes()) > 0)) {
myRetNetworkStats.add(netStat);
}
}
}
// recalculate the total
long total = 0;
for (int i = 0; i < myRetNetworkStats.size(); i++) {
total += myRetNetworkStats.get(i).getTotalBytes();
}
Collections.sort(myRetNetworkStats);
for (int i = 0; i < myRetNetworkStats.size(); i++) {
myRetNetworkStats.get(i).setTotal(total);
myStats.add((StatElement) myRetNetworkStats.get(i));
}
if (LogSettings.DEBUG) {
Log.d(TAG, "Result " + myStats.toString());
}
return myStats;
}
use of com.asksven.android.common.privateapiproxies.NetworkUsage in project BetterBatteryStats by asksven.
the class StatsProvider method getCurrentNetworkUsageStatList.
public ArrayList<StatElement> getCurrentNetworkUsageStatList(boolean bFilter) throws Exception {
Context ctx = BbsApplication.getAppContext();
ArrayList<StatElement> myStats = new ArrayList<StatElement>();
// stop straight away of root features are disabled
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(ctx);
boolean permsNotNeeded = sharedPrefs.getBoolean("ignore_system_app", false);
ArrayList<StatElement> myNetworkStats = null;
if (sharedPrefs.getBoolean("force_network_api", false)) {
Log.i(TAG, "Setting set to force the use of the API for kernel wakelocks");
BatteryStatsProxy mStats = BatteryStatsProxy.getInstance(ctx);
int statsType = 0;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
statsType = BatteryStatsTypesLolipop.STATS_CURRENT;
} else {
statsType = BatteryStatsTypes.STATS_CURRENT;
}
myNetworkStats = mStats.getNetworkUsageStats(ctx, statsType);
} else {
if (permsNotNeeded || SysUtils.hasBatteryStatsPermission(ctx)) {
Log.i(TAG, "Using API");
BatteryStatsProxy mStats = BatteryStatsProxy.getInstance(ctx);
int statsType = 0;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
statsType = BatteryStatsTypesLolipop.STATS_CURRENT;
} else {
statsType = BatteryStatsTypes.STATS_CURRENT;
}
myNetworkStats = mStats.getNetworkUsageStats(ctx, statsType);
} else if (Netstats.fileExists() && RootShell.getInstance().hasRootPermissions()) {
myNetworkStats = Netstats.parseNetstats();
} else {
Log.e(TAG, "Unable to access kernel wakelocks with either method");
return myStats;
}
}
// /////////////////////////////////////////
ArrayList<NetworkUsage> myRetNetworkStats = new ArrayList<NetworkUsage>();
for (int i = 0; i < myNetworkStats.size(); i++) {
NetworkUsage netStat = (NetworkUsage) myNetworkStats.get(i);
if ((!bFilter) || ((netStat.getTotalBytes()) > 0)) {
myRetNetworkStats.add(netStat);
}
}
// recalculate the total
long total = 0;
for (int i = 0; i < myRetNetworkStats.size(); i++) {
total += myRetNetworkStats.get(i).getTotalBytes();
}
Collections.sort(myRetNetworkStats);
for (int i = 0; i < myRetNetworkStats.size(); i++) {
myRetNetworkStats.get(i).setTotal(total);
myStats.add((StatElement) myRetNetworkStats.get(i));
}
if (LogSettings.DEBUG) {
Log.d(TAG, "Result " + myStats.toString());
}
return myStats;
}
use of com.asksven.android.common.privateapiproxies.NetworkUsage in project BetterBatteryStats by asksven.
the class Netstats method addToStats.
/**
* Stats may be duplicate for one uid+iface so we sum them up
* @param stats
* @param entry
* @return
*/
static ArrayList<StatElement> addToStats(ArrayList<StatElement> stats, NetworkUsage entry) {
boolean merged = false;
for (int i = 0; i < stats.size(); i++) {
NetworkUsage current = (NetworkUsage) stats.get(i);
if ((current.getuid() == entry.getuid()) && (current.getInterface().equals(entry.getInterface()))) {
current.addBytesReceived(entry.getBytesReceived());
current.addBytesSent(entry.getBytesSent());
merged = true;
break;
}
}
// if not summed up add normally
if (!merged) {
stats.add(entry);
}
return stats;
}
use of com.asksven.android.common.privateapiproxies.NetworkUsage in project BetterBatteryStats by asksven.
the class StatsAdapter method getView.
public View getView(int position, View convertView, ViewGroup viewGroup) {
StatElement entry = m_listData.get(position);
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this.m_context);
boolean bShowBars = sharedPrefs.getBoolean("show_gauge", false);
if (LogSettings.DEBUG) {
Log.i(TAG, "Values: " + entry.getVals());
}
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) m_context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// depending on settings show new pie gauge or old bar gauge
if (!bShowBars) {
convertView = inflater.inflate(R.layout.stat_row, null);
} else {
convertView = inflater.inflate(R.layout.stat_row_gauge, null);
}
}
final float scale = this.m_context.getResources().getDisplayMetrics().density;
TextView tvName = (TextView) convertView.findViewById(R.id.TextViewName);
// ///////////////////////////////////////
// we do some stuff here to handle settings about font size and thumbnail size
String fontSize = sharedPrefs.getString("medium_font_size", "16");
int mediumFontSize = Integer.parseInt(fontSize);
// we need to change "since" fontsize
tvName.setTextSize(TypedValue.COMPLEX_UNIT_SP, mediumFontSize);
// We need to handle an exception here: Sensors do not have a name so we use the fqn instead
if (entry instanceof SensorUsage) {
tvName.setText(entry.getFqn(UidNameResolver.getInstance()));
} else {
tvName.setText(entry.getName());
}
boolean bShowKb = sharedPrefs.getBoolean("enable_kb", true);
ImageView iconKb = (ImageView) convertView.findViewById(R.id.imageKB);
iconKb.setVisibility(View.INVISIBLE);
TextView tvFqn = (TextView) convertView.findViewById(R.id.TextViewFqn);
tvFqn.setText(entry.getFqn(UidNameResolver.getInstance()));
TextView tvData = (TextView) convertView.findViewById(R.id.TextViewData);
// for alarms the values is wakeups per hour so we need to take the time as reference for the text
if (entry instanceof Alarm) {
tvData.setText(entry.getData((long) m_timeSince));
} else {
tvData.setText(entry.getData((long) m_maxValue));
}
// LinearLayout myLayout = (LinearLayout) convertView.findViewById(R.id.LinearLayoutBar);
LinearLayout myFqnLayout = (LinearLayout) convertView.findViewById(R.id.LinearLayoutFqn);
LinearLayout myRow = (LinearLayout) convertView.findViewById(R.id.LinearLayoutEntry);
// long press for "copy to clipboard"
convertView.setOnLongClickListener(new OnItemLongClickListener(position));
if (!bShowBars) {
GraphablePie gauge = (GraphablePie) convertView.findViewById(R.id.Gauge);
// ///////////////////////////////////////
// we do some stuff here to handle settings about font size and thumbnail size
String iconDim = sharedPrefs.getString("thumbnail_size", "56");
int iconSize = Integer.parseInt(iconDim);
int pixels = (int) (iconSize * scale + 0.5f);
// we need to change "since" fontsize
gauge.getLayoutParams().height = pixels;
gauge.getLayoutParams().width = pixels;
gauge.requestLayout();
// //////////////////////////////////////////////////////////////////////////////////
if (entry instanceof NetworkUsage) {
gauge.setValue(entry.getValues()[0], ((NetworkUsage) entry).getTotal());
} else {
double max = m_maxValue;
// avoid rounding errors leading to values > 100 %
if (entry.getValues()[0] > max) {
max = entry.getValues()[0];
Log.i(TAG, "Upping gauge max to " + max);
}
gauge.setValue(entry.getValues()[0], max);
}
} else {
GraphableBars buttonBar = (GraphableBars) convertView.findViewById(R.id.ButtonBar);
int iHeight = 10;
try {
iHeight = Integer.valueOf(sharedPrefs.getString("graph_bar_height", "10"));
} catch (Exception e) {
iHeight = 10;
}
if (iHeight == 0) {
iHeight = 10;
}
buttonBar.setMinimumHeight(iHeight);
buttonBar.setName(entry.getName());
buttonBar.setValues(entry.getValues(), m_maxValue);
}
ImageView iconView = (ImageView) convertView.findViewById(R.id.icon);
LinearLayout iconLayout = (LinearLayout) convertView.findViewById(R.id.LayoutIcon);
// ///////////////////////////////////////
// we do some stuff here to handle settings about font size and thumbnail size
String iconDim = sharedPrefs.getString("thumbnail_size", "56");
int iconSize = Integer.parseInt(iconDim);
int pixels = (int) (iconSize * scale + 0.5f);
// we need to change "since" fontsize
iconView.getLayoutParams().width = pixels;
iconView.getLayoutParams().height = pixels;
iconView.requestLayout();
// show / hide fqn text
if ((entry instanceof Process) || (entry instanceof State) || (entry instanceof Misc) || (entry instanceof NativeKernelWakelock) || (entry instanceof Alarm) || (entry instanceof SensorUsage)) {
myFqnLayout.setVisibility(View.GONE);
} else {
myFqnLayout.setVisibility(View.VISIBLE);
}
// show / hide package icons (we show / hide the whole layout as it contains a margin that must be hidded as well
if ((entry instanceof NativeKernelWakelock) || (entry instanceof State) || (entry instanceof Misc)) {
iconView.setVisibility(View.GONE);
} else {
iconView.setVisibility(View.VISIBLE);
iconView.setImageDrawable(entry.getIcon(UidNameResolver.getInstance()));
// set a click listener for the list
iconView.setOnClickListener(new OnPackageClickListener(position));
}
// add on click listener for the list entry if details are availble
if ((entry instanceof Alarm) || (entry instanceof NativeKernelWakelock) || (entry instanceof SensorUsage)) {
convertView.setOnClickListener(new OnItemClickListener(position));
}
return convertView;
}
Aggregations