use of com.asksven.android.common.privateapiproxies.Process in project BetterBatteryStats by asksven.
the class StatsProvider method getCurrentProcessStatList.
public ArrayList<StatElement> getCurrentProcessStatList(boolean bFilter, int iSort) throws Exception {
Context ctx = BbsApplication.getAppContext();
ArrayList<StatElement> myProcesses = null;
ArrayList<Process> myRetProcesses = new ArrayList<Process>();
if (!SysUtils.hasBatteryStatsPermission(ctx)) {
myProcesses = ProcessStatsDumpsys.getProcesses(ctx);
} else {
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;
}
myProcesses = mStats.getProcessStats(ctx, statsType);
}
// add elements and recalculate the total
long total = 0;
for (int i = 0; i < myProcesses.size(); i++) {
Process ps = (Process) myProcesses.get(i);
if ((!bFilter) || ((ps.getSystemTime() + ps.getUserTime()) > 0)) {
total += ps.getSystemTime() + ps.getSystemTime();
myRetProcesses.add(ps);
}
}
// sort by Duration
Comparator<Process> myCompTime = new Process.ProcessTimeComparator();
Collections.sort(myRetProcesses, myCompTime);
if (LogSettings.DEBUG) {
Log.d(TAG, "Result " + myProcesses.toString());
}
myProcesses.clear();
for (int i = 0; i < myRetProcesses.size(); i++) {
myRetProcesses.get(i).setTotal(total);
myProcesses.add(myRetProcesses.get(i));
}
return myProcesses;
}
use of com.asksven.android.common.privateapiproxies.Process in project BetterBatteryStats by asksven.
the class StatsProvider method getProcessStatList.
/**
* Get the Process 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> getProcessStatList(boolean bFilter, Reference refFrom, int iSort, Reference refTo) throws Exception {
Context ctx = BbsApplication.getAppContext();
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(ctx);
boolean permsNotNeeded = sharedPrefs.getBoolean("ignore_system_app", false);
ArrayList<StatElement> myStats = new ArrayList<StatElement>();
if (!(SysUtils.hasBatteryStatsPermission(ctx) || permsNotNeeded)) {
// stop straight away of root features are disabled
if (!SysUtils.hasBatteryStatsPermission(ctx)) {
myStats.add(new Notification(ctx.getString(R.string.NO_ROOT_ERR)));
return myStats;
}
}
if ((refFrom == null) || (refTo == null)) {
myStats.add(new Notification(ctx.getString(R.string.NO_REF_ERR)));
return myStats;
}
ArrayList<StatElement> myProcesses = null;
ArrayList<Process> myRetProcesses = new ArrayList<Process>();
if ((refTo.m_refProcesses != null) && (!refTo.m_refProcesses.isEmpty())) {
myProcesses = refTo.m_refProcesses;
} else {
myStats.add(new Notification(ctx.getString(R.string.NO_STATS)));
return myStats;
}
String strCurrent = myProcesses.toString();
String strRef = "";
String strRefDescr = "";
if (LogSettings.DEBUG) {
if (refFrom != null) {
strRefDescr = refFrom.whoAmI();
if (refFrom.m_refProcesses != null) {
strRef = refFrom.m_refProcesses.toString();
} else {
strRef = "Process is null";
}
} else {
strRefDescr = "Reference is null";
}
Log.d(TAG, "Processing processes 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);
}
// add relevant elements and recalculate the total
long total = 0;
for (int i = 0; i < myProcesses.size(); i++) {
Process ps = ((Process) myProcesses.get(i)).clone();
if ((!bFilter) || ((ps.getSystemTime() + ps.getUserTime()) > 0)) {
ps.substractFromRef(refFrom.m_refProcesses);
// threshold
if ((!bFilter) || ((ps.getSystemTime() + ps.getUserTime()) > 0)) {
total += ps.getSystemTime() + ps.getUserTime();
myRetProcesses.add(ps);
}
}
}
// sort by Duration
Comparator<Process> myCompTime = new Process.ProcessTimeComparator();
Collections.sort(myRetProcesses, myCompTime);
for (int i = 0; i < myRetProcesses.size(); i++) {
myRetProcesses.get(i).setTotal(total);
myStats.add((StatElement) myRetProcesses.get(i));
}
if (LogSettings.DEBUG) {
Log.d(TAG, "Result " + myStats.toString());
}
return myStats;
}
use of com.asksven.android.common.privateapiproxies.Process in project BetterBatteryStats by asksven.
the class ProcessStatsDumpsys method getProcesses.
protected static HashMap<String, List<Process>> getProcesses(List<String> res) {
HashMap<String, List<Process>> xref = new HashMap<String, List<Process>>();
final String START_PATTERN = "Statistics since last charge";
final String STOP_PATTERN = "Statistics since last unplugged";
if ((res != null) && (res.size() != 0)) {
Pattern begin = Pattern.compile(START_PATTERN);
Pattern end = Pattern.compile(STOP_PATTERN);
boolean bParsing = false;
Pattern patternUser = Pattern.compile("\\s\\s((u0a)?\\d+):");
Pattern patternProcess = Pattern.compile("\\s\\s\\s\\sProc\\s(.*):");
Pattern patternCpu = Pattern.compile("\\s\\s\\s\\s\\s\\sCPU:\\s(.*) usr \\+ (.*) krn.*");
Pattern patternStarts = Pattern.compile("\\s\\s\\s\\s\\s\\s(\\d+) proc starts");
String user = "";
String process = "";
long userCpu = 0;
long systemCpu = 0;
int starts = 0;
for (int i = 0; i < res.size(); i++) {
// skip till start mark found
if (bParsing) {
// look for end
Matcher endMatcher = end.matcher(res.get(i));
if (endMatcher.find()) {
// add whatever was not saved yet
if (!user.equals("") && !process.equals("")) {
Process myProc = new Process(process, userCpu, systemCpu, starts);
List<Process> myList = xref.get(user);
if (myList == null) {
myList = new ArrayList<Process>();
xref.put(user, myList);
}
myList.add(myProc);
}
break;
}
String line = res.get(i);
Matcher mUser = patternUser.matcher(line);
Matcher mProcess = patternProcess.matcher(line);
Matcher mCpu = patternCpu.matcher(line);
Matcher mStarts = patternStarts.matcher(line);
if (mUser.find()) {
// check if we had detected something previously
if (!user.equals("") && !process.equals("")) {
Process myProc = new Process(process, userCpu, systemCpu, starts);
List<Process> myList = xref.get(user);
if (myList == null) {
myList = new ArrayList<Process>();
xref.put(user, myList);
}
myList.add(myProc);
}
user = mUser.group(1);
process = "";
userCpu = 0;
systemCpu = 0;
starts = 0;
}
if (mProcess.find()) {
// check if we had detected something previously
if (!user.equals("") && !process.equals("")) {
Process myProc = new Process(process, userCpu, systemCpu, starts);
List<Process> myList = xref.get(user);
if (myList == null) {
myList = new ArrayList<Process>();
xref.put(user, myList);
}
myList.add(myProc);
}
process = mProcess.group(1);
userCpu = 0;
systemCpu = 0;
starts = 0;
}
if (mCpu.find()) {
userCpu = DateUtils.durationToLong(mCpu.group(1));
systemCpu = DateUtils.durationToLong(mCpu.group(2));
}
if (mStarts.find()) {
starts = Integer.valueOf(mStarts.group(1));
}
} else {
// look for beginning
Matcher line = begin.matcher(res.get(i));
if (line.find()) {
bParsing = true;
}
}
}
}
return xref;
}
use of com.asksven.android.common.privateapiproxies.Process in project BetterBatteryStats by asksven.
the class ProcessStatsDumpsys method getProcesses.
/**
* Returns a list of alarm value objects
* @return
* @throws Exception
*/
public static ArrayList<StatElement> getProcesses(Context ctx) {
ArrayList myProcesses = new ArrayList<Process>();
// get the list of all installed packages
PackageManager pm = ctx.getPackageManager();
List<ApplicationInfo> apps = pm.getInstalledApplications(PackageManager.GET_META_DATA);
// List<PackageInfo> packages = pm.getInstalledPackages(PackageManager.GET_META_DATA);
HashMap<String, Integer> xrefPackages = new HashMap<String, Integer>();
for (int i = 0; i < apps.size(); i++) {
xrefPackages.put(apps.get(i).packageName, apps.get(i).uid);
}
List<String> res = null;
if (SysUtils.hasDumpsysPermission(ctx)) {
res = NonRootShell.getInstance().run("dumpsys batterystats");
} else {
res = RootShell.getInstance().run("dumpsys batterystats");
}
HashMap<String, List<Process>> xrefUserNames = getProcesses(res);
// go through the processes and set the proper uid
Iterator<String> userNames = xrefUserNames.keySet().iterator();
while (userNames.hasNext()) {
String userName = userNames.next();
List<Process> procs = xrefUserNames.get(userName);
int uid = -1;
if (!userName.equals("")) {
if (userName.startsWith("u0a")) {
// resolve though xrefPackages
uid = -1;
} else {
uid = Integer.valueOf(userName);
}
}
for (int i = 0; i < procs.size(); i++) {
Process proc = procs.get(i);
if (uid == -1) {
String packageName = proc.getName();
if ((packageName != null) && (xrefPackages != null)) {
try {
Integer lookupUid = xrefPackages.get(packageName);
if (lookupUid != null) {
uid = lookupUid;
} else {
Log.d(TAG, "Package " + packageName + " was not found in xref");
}
} catch (Exception e) {
Log.e(TAG, "An error occured when retrieving uid=" + uid + " for package=" + packageName);
}
}
}
proc.setUid(uid);
myProcesses.add(proc);
}
}
return myProcesses;
}
use of com.asksven.android.common.privateapiproxies.Process 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