Search in sources :

Example 1 with StatElement

use of com.asksven.android.common.privateapiproxies.StatElement in project BetterBatteryStats by asksven.

the class WakeupSources method parseWakeupSources.

// private static String FILE_PATH = "/sdcard/wakeup_sources.txt";
public static ArrayList<StatElement> parseWakeupSources(Context context) {
    Log.i(TAG, "Parsing " + FILE_PATH);
    String delimiter = String.valueOf('\t');
    delimiter = delimiter + "+";
    ArrayList<StatElement> myRet = new ArrayList<StatElement>();
    // format
    // new [name	active_count	event_count		wakeup_count	expire_count	active_since	total_time	max_time	last_change	prevent_suspend_time]
    ArrayList<String[]> rows = parseDelimitedFile(FILE_PATH, delimiter);
    long msSinceBoot = SystemClock.elapsedRealtime();
    // list the running processes
    ActivityManager actvityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningAppProcessInfo> procInfos = actvityManager.getRunningAppProcesses();
    // start with 1
    for (int i = 1; i < rows.size(); i++) {
        try {
            // times in file are milliseconds
            String[] data = (String[]) rows.get(i);
            // name
            String name = data[0].trim();
            // active_count
            int count = Integer.valueOf(data[1]);
            // expire_count
            int expire_count = Integer.valueOf(data[4]);
            // wakeup_count
            int wake_count = Integer.valueOf(data[3]);
            // active_since
            long active_since = Long.valueOf(data[5]);
            // total_time
            long total_time = Long.valueOf(data[6]);
            // prevent_suspend_time
            long sleep_time = Long.valueOf(data[9]);
            // max_time
            long max_time = Long.valueOf(data[7]);
            // last_change
            long last_change = Long.valueOf(data[8]);
            // post-processing of eventX-YYYY processes
            String details = "";
            // we start with a " here as that is the way the data comes from /proc
            if (name.startsWith("\"event")) {
                String process = name.replaceAll("\"", "");
                if (CommonLogSettings.DEBUG) {
                    Log.d(TAG, "Pattern 'event' found in " + process);
                }
                int proc = 0;
                String[] parts = process.split("-");
                if (parts.length == 2) {
                    try {
                        proc = Integer.valueOf(parts[1]);
                        if (CommonLogSettings.DEBUG) {
                            Log.d(TAG, "Resolving proc name for 'event' " + proc);
                        }
                    } catch (Exception e) {
                        Log.e(TAG, "Cound not split process name " + process);
                    }
                }
                if (proc != 0) {
                    // search for the process in the task list
                    for (int psCount = 0; psCount < procInfos.size(); psCount++) {
                        int id = procInfos.get(psCount).pid;
                        if (id == proc) {
                            String processName = procInfos.get(psCount).processName;
                            details = processName;
                            String appName = "";
                            String[] pkgList = procInfos.get(count).pkgList;
                            for (int j = 0; j < pkgList.length; j++) {
                                if (details.length() > 0) {
                                    details += ", ";
                                }
                                details += pkgList[j];
                            }
                            if (CommonLogSettings.DEBUG) {
                                Log.d(TAG, "Pattern 'event' resolved to " + details);
                            }
                        }
                    }
                }
            }
            if (CommonLogSettings.DEBUG) {
                Log.d(TAG, "Native Kernel wakelock parsed" + " name=" + name + " details=" + details + " count=" + count + " expire_count=" + expire_count + " wake_count=" + wake_count + " active_since=" + active_since + " total_time=" + total_time + " sleep_time=" + sleep_time + " max_time=" + max_time + "last_change=" + last_change + "ms_since_boot=" + msSinceBoot);
            }
            NativeKernelWakelock wl = null;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                // on L sleep time is always 0 so use total time instead
                wl = new NativeKernelWakelock(name, details, count, expire_count, wake_count, active_since, total_time, total_time, max_time, last_change, msSinceBoot);
            } else {
                wl = new NativeKernelWakelock(name, details, count, expire_count, wake_count, active_since, total_time, sleep_time, max_time, last_change, msSinceBoot);
            }
            myRet.add(wl);
        } catch (Exception e) {
        // go on
        }
    }
    return myRet;
}
Also used : ArrayList(java.util.ArrayList) ActivityManager(android.app.ActivityManager) IOException(java.io.IOException) NativeKernelWakelock(com.asksven.android.common.privateapiproxies.NativeKernelWakelock) RunningAppProcessInfo(android.app.ActivityManager.RunningAppProcessInfo) StatElement(com.asksven.android.common.privateapiproxies.StatElement)

Example 2 with StatElement

use of com.asksven.android.common.privateapiproxies.StatElement in project BetterBatteryStats by asksven.

the class AlarmsDumpsys method getAlarmsFrom_6.

protected static ArrayList<StatElement> getAlarmsFrom_6(List<String> res) {
    ArrayList<StatElement> myAlarms = null;
    long nTotalCount = 0;
    if ((res != null) && (res.size() != 0)) {
        Pattern begin = Pattern.compile("Alarm Stats");
        boolean bParsing = false;
        // we are looking for multiline entries in the format
        // ' <package name> +<time>ms running, <number> wakeups
        // '  +<time>ms <number> wakes <number> alarms: act=<intern> (repeating 1..n times)
        Pattern packagePattern = Pattern.compile("\\s\\s.*:([a-z][a-zA-Z0-9\\.]+)\\s\\+(.*), (\\d+) wakeups:");
        Pattern numberPattern = Pattern.compile("\\s\\s\\s\\s\\+([0-9a-z]+)ms (\\d+) wakes (\\d+) alarms(.*)");
        Pattern detailsPattern = Pattern.compile("\\s\\s\\s\\s\\s\\s(\\*alarm\\*|\\*walarm\\*):(.*)");
        myAlarms = new ArrayList<StatElement>();
        Alarm myAlarm = null;
        long nNumber = 0;
        // process the file
        for (int i = 0; i < res.size(); i++) {
            // skip till start mark found
            if (bParsing) {
                // parse the alarms by block
                String line = res.get(i);
                Matcher mPackage = packagePattern.matcher(line);
                Matcher mNumber = numberPattern.matcher(line);
                Matcher mDetails = detailsPattern.matcher(line);
                // first line
                if (mPackage.find()) {
                    try {
                        // if there was a previous Alarm populated store it
                        if (myAlarm != null) {
                            myAlarms.add(myAlarm);
                        }
                        // we are interested in the first token
                        String strPackageName = mPackage.group(1);
                        myAlarm = new Alarm(strPackageName);
                        String strWakeups = mPackage.group(3);
                        long nWakeups = Long.parseLong(strWakeups);
                        myAlarm.setWakeups(nWakeups);
                        nTotalCount += nWakeups;
                    } catch (Exception e) {
                        Log.e(TAG, "Error: parsing error in package line (" + line + ")");
                    }
                }
                // second line
                if (mNumber.find()) {
                    try {
                        // we are interested in the first and second token
                        String strNumber = mNumber.group(2);
                        nNumber = Long.parseLong(strNumber);
                        if (myAlarm == null) {
                            Log.e(TAG, "Error: number line found but without alarm object (" + line + ")");
                        }
                    } catch (Exception e) {
                        Log.e(TAG, "Error: parsing error in number line (" + line + ")");
                    }
                }
                // third line
                if (mDetails.find()) {
                    try {
                        // we are interested in the first and second token
                        String strIntent = mDetails.group(2);
                        if (myAlarm == null) {
                            Log.e(TAG, "Error: number line found but without alarm object (" + line + ")");
                        } else {
                            System.out.println("Added: " + strIntent + "(" + nNumber + ")");
                            myAlarm.addItem(nNumber, strIntent);
                        }
                    } catch (Exception e) {
                        Log.e(TAG, "Error: parsing error in number line (" + line + ")");
                    }
                }
            } else {
                // look for beginning
                Matcher line = begin.matcher(res.get(i));
                if (line.find()) {
                    bParsing = true;
                }
            }
        }
        // the last populated alarms has not been added to the list yet
        if (myAlarm != null) {
            myAlarms.add(myAlarm);
        }
    } else {
        myAlarms = new ArrayList<StatElement>();
        Alarm myAlarm = new Alarm(PERMISSION_DENIED);
        myAlarm.setWakeups(1);
        myAlarms.add(myAlarm);
    }
    for (int i = 0; i < myAlarms.size(); i++) {
        Alarm myAlarm = (Alarm) myAlarms.get(i);
        if (myAlarm != null) {
            myAlarm.setTotalCount(nTotalCount);
        }
    }
    return myAlarms;
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) StatElement(com.asksven.android.common.privateapiproxies.StatElement) Alarm(com.asksven.android.common.privateapiproxies.Alarm)

Example 3 with StatElement

use of com.asksven.android.common.privateapiproxies.StatElement in project BetterBatteryStats by asksven.

the class AlarmsDumpsys method getAlarmsFrom_4_2_2.

protected static ArrayList<StatElement> getAlarmsFrom_4_2_2(List<String> res) {
    ArrayList<StatElement> myAlarms = null;
    long nTotalCount = 0;
    if ((res != null) && (res.size() != 0)) {
        Pattern begin = Pattern.compile("Alarm Stats");
        boolean bParsing = false;
        // we are looking for multiline entries in the format
        // ' <package name> +<time>ms running, <number> wakeups
        // '  +<time>ms <number> wakes <number> alarms: act=<intern> (repeating 1..n times)
        Pattern packagePattern = Pattern.compile("\\s\\s([a-z][a-zA-Z0-9\\.]+)\\s\\+(.*), (\\d+) wakeups:");
        Pattern numberPattern = Pattern.compile("\\s\\s\\s\\s\\+([0-9a-z]+)ms (\\d+) wakes (\\d+) alarms: (act|cmp)=([A-Za-z0-9\\-\\_\\.\\$\\{\\}]+)");
        myAlarms = new ArrayList<StatElement>();
        Alarm myAlarm = null;
        // process the file
        for (int i = 0; i < res.size(); i++) {
            // skip till start mark found
            if (bParsing) {
                // parse the alarms by block
                String line = res.get(i);
                Matcher mPackage = packagePattern.matcher(line);
                Matcher mNumber = numberPattern.matcher(line);
                // first line
                if (mPackage.find()) {
                    try {
                        // if there was a previous Alarm populated store it
                        if (myAlarm != null) {
                            myAlarms.add(myAlarm);
                        }
                        // we are interested in the first token
                        String strPackageName = mPackage.group(1);
                        myAlarm = new Alarm(strPackageName);
                        String strWakeups = mPackage.group(3);
                        long nWakeups = Long.parseLong(strWakeups);
                        myAlarm.setWakeups(nWakeups);
                        nTotalCount += nWakeups;
                    } catch (Exception e) {
                        Log.e(TAG, "Error: parsing error in package line (" + line + ")");
                    }
                }
                // second line (and following till next package)
                if (mNumber.find()) {
                    try {
                        // we are interested in the first and second token
                        String strNumber = mNumber.group(2);
                        String strIntent = mNumber.group(5);
                        long nNumber = Long.parseLong(strNumber);
                        if (myAlarm == null) {
                            Log.e(TAG, "Error: number line found but without alarm object (" + line + ")");
                        } else {
                            myAlarm.addItem(nNumber, strIntent);
                        }
                    } catch (Exception e) {
                        Log.e(TAG, "Error: parsing error in number line (" + line + ")");
                    }
                }
            } else {
                // look for beginning
                Matcher line = begin.matcher(res.get(i));
                if (line.find()) {
                    bParsing = true;
                }
            }
        }
        // the last populated alarms has not been added to the list yet
        myAlarms.add(myAlarm);
    } else {
        myAlarms = new ArrayList<StatElement>();
        Alarm myAlarm = new Alarm(PERMISSION_DENIED);
        myAlarm.setWakeups(1);
        myAlarms.add(myAlarm);
    }
    for (int i = 0; i < myAlarms.size(); i++) {
        Alarm myAlarm = (Alarm) myAlarms.get(i);
        if (myAlarm != null) {
            myAlarm.setTotalCount(nTotalCount);
        }
    }
    return myAlarms;
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) StatElement(com.asksven.android.common.privateapiproxies.StatElement) Alarm(com.asksven.android.common.privateapiproxies.Alarm)

Example 4 with StatElement

use of com.asksven.android.common.privateapiproxies.StatElement 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;
}
Also used : HashMap(java.util.HashMap) StatElement(com.asksven.android.common.privateapiproxies.StatElement) NetworkUsage(com.asksven.android.common.privateapiproxies.NetworkUsage) ArrayList(java.util.ArrayList) IOException(java.io.IOException)

Example 5 with StatElement

use of com.asksven.android.common.privateapiproxies.StatElement in project BetterBatteryStats by asksven.

the class StatsProvider method sum.

public long sum(ArrayList<StatElement> myList) {
    long ret = 0;
    if (myList == null) {
        Log.d(TAG, "sum was called with a null list");
        return 0;
    }
    if (myList.size() == 0) {
        Log.d(TAG, "sum was called with an empty list");
        return 0;
    }
    for (int i = 0; i < myList.size(); i++) {
        // make sure nothing goes wrong
        try {
            StatElement item = myList.get(i);
            ret += item.getValues()[0];
        } catch (Exception e) {
            Log.e(TAG, "An error occcured " + e.getMessage());
            GenericLogger.stackTrace(TAG, e.getStackTrace());
        }
    }
    return ret;
}
Also used : StatElement(com.asksven.android.common.privateapiproxies.StatElement) SuppressLint(android.annotation.SuppressLint) ReceiverCallNotAllowedException(android.content.ReceiverCallNotAllowedException) BatteryInfoUnavailableException(com.asksven.android.common.privateapiproxies.BatteryInfoUnavailableException)

Aggregations

StatElement (com.asksven.android.common.privateapiproxies.StatElement)36 ArrayList (java.util.ArrayList)22 SuppressLint (android.annotation.SuppressLint)20 SharedPreferences (android.content.SharedPreferences)19 Context (android.content.Context)16 Notification (com.asksven.android.common.privateapiproxies.Notification)10 Misc (com.asksven.android.common.privateapiproxies.Misc)9 Alarm (com.asksven.android.common.privateapiproxies.Alarm)8 NativeKernelWakelock (com.asksven.android.common.privateapiproxies.NativeKernelWakelock)8 BatteryStatsProxy (com.asksven.android.common.privateapiproxies.BatteryStatsProxy)7 Reference (com.asksven.betterbatterystats.data.Reference)7 Intent (android.content.Intent)6 PackageManager (android.content.pm.PackageManager)6 StatsProvider (com.asksven.betterbatterystats.data.StatsProvider)6 Matcher (java.util.regex.Matcher)6 Pattern (java.util.regex.Pattern)6 PendingIntent (android.app.PendingIntent)5 AppWidgetManager (android.appwidget.AppWidgetManager)4 RemoteViews (android.widget.RemoteViews)4 NetworkUsage (com.asksven.android.common.privateapiproxies.NetworkUsage)4