Search in sources :

Example 1 with NativeKernelWakelock

use of com.asksven.android.common.privateapiproxies.NativeKernelWakelock 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 NativeKernelWakelock

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

the class StatsProvider method getCurrentKernelWakelockStatList.

public ArrayList<StatElement> getCurrentKernelWakelockStatList(boolean bFilter, int iPctType, int iSort) throws Exception {
    Context ctx = BbsApplication.getAppContext();
    ArrayList<StatElement> myStats = new ArrayList<StatElement>();
    ArrayList<StatElement> myKernelWakelocks = null;
    SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(ctx);
    boolean permsNotNeeded = sharedPrefs.getBoolean("ignore_system_app", false);
    if (sharedPrefs.getBoolean("force_kwl_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;
        }
        myKernelWakelocks = mStats.getKernelWakelockStats(ctx, statsType, false);
    } else {
        // we must support both "old" (/proc/wakelocks) and "new formats
        if (Wakelocks.fileIsWorldReadableExists() || WakeupSources.fileIsWorldReadableExists()) {
            if (Wakelocks.fileIsWorldReadableExists()) {
                Log.i(TAG, "Using Wakelocks file");
                myKernelWakelocks = Wakelocks.parseProcWakelocks(ctx);
            } else {
                Log.i(TAG, "Using Wakeupsources file");
                // Build.DEVICE: g3, g2 or p1
                if ((Build.BRAND.equals("lge")) && (Build.DEVICE.equals("g3")) || Build.DEVICE.equals("p1") || (Build.DEVICE.equals("g2"))) {
                    Log.i(TAG, "Using LG G2, G3, G4 specific wakeup sources");
                    myKernelWakelocks = WakeupSourcesLg.parseWakeupSources(ctx);
                } else {
                    myKernelWakelocks = WakeupSources.parseWakeupSources(ctx);
                }
            }
        } else if (permsNotNeeded || SysUtils.hasBatteryStatsPermission(ctx)) {
            Log.i(TAG, "Falling back to 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;
            }
            myKernelWakelocks = mStats.getKernelWakelockStats(ctx, statsType, false);
        } else {
            Log.e(TAG, "Unable to access kernel wakelocks with either method");
            return myStats;
        }
    }
    ArrayList<NativeKernelWakelock> myRetKernelWakelocks = new ArrayList<NativeKernelWakelock>();
    for (int i = 0; i < myKernelWakelocks.size(); i++) {
        NativeKernelWakelock wl = (NativeKernelWakelock) myKernelWakelocks.get(i);
        if ((!bFilter) || ((wl.getDuration()) > 0)) {
            myRetKernelWakelocks.add(wl);
        }
    }
    // com.asksven.android.common.privateapiproxies.Walkelock.compareTo
    switch(iSort) {
        case 0:
            // by Duration
            Comparator<NativeKernelWakelock> myCompTime = new NativeKernelWakelock.TimeComparator();
            Collections.sort(myRetKernelWakelocks, myCompTime);
            break;
        case 1:
            // by Count
            Comparator<NativeKernelWakelock> myCompCount = new NativeKernelWakelock.CountComparator();
            Collections.sort(myRetKernelWakelocks, myCompCount);
            break;
    }
    for (int i = 0; i < myRetKernelWakelocks.size(); i++) {
        myStats.add((StatElement) myRetKernelWakelocks.get(i));
    }
    if (LogSettings.DEBUG) {
        Log.d(TAG, "Result " + myStats.toString());
    }
    return myStats;
}
Also used : Context(android.content.Context) SharedPreferences(android.content.SharedPreferences) ArrayList(java.util.ArrayList) BatteryStatsProxy(com.asksven.android.common.privateapiproxies.BatteryStatsProxy) SuppressLint(android.annotation.SuppressLint) NativeKernelWakelock(com.asksven.android.common.privateapiproxies.NativeKernelWakelock) StatElement(com.asksven.android.common.privateapiproxies.StatElement)

Example 3 with NativeKernelWakelock

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

the class StatsProvider method getKernelWakelockStatList.

/**
 * 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> getKernelWakelockStatList(boolean bFilter, Reference refFrom, int iPctType, int iSort, 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);
    if (!(Wakelocks.fileIsWorldReadableExists() || WakeupSources.fileIsWorldReadableExists() || permsNotNeeded || SysUtils.hasBatteryStatsPermission(ctx))) {
        myStats.add(new Notification(ctx.getString(R.string.KWL_ACCESS_ERROR)));
        return myStats;
    }
    if ((refFrom == null) || (refTo == null)) {
        myStats.add(new Notification(ctx.getString(R.string.NO_REF_ERR)));
        return myStats;
    }
    ArrayList<StatElement> myKernelWakelocks = null;
    if ((refTo.m_refKernelWakelocks != null) && (!refTo.m_refKernelWakelocks.isEmpty())) {
        myKernelWakelocks = refTo.m_refKernelWakelocks;
    } else {
        myStats.add(new Notification(ctx.getString(R.string.NO_STATS)));
        return myStats;
    }
    ArrayList<NativeKernelWakelock> myRetKernelWakelocks = new ArrayList<NativeKernelWakelock>();
    // if we are using custom ref. always retrieve "stats current"
    // sort @see
    // com.asksven.android.common.privateapiproxies.Walkelock.compareTo
    // Collections.sort(myKernelWakelocks);
    String strCurrent = myKernelWakelocks.toString();
    String strRef = "";
    String strRefDescr = "";
    if (LogSettings.DEBUG) {
        if (refFrom != null) {
            strRefDescr = refFrom.whoAmI();
            if (refFrom.m_refKernelWakelocks != null) {
                strRef = refFrom.m_refKernelWakelocks.toString();
            } else {
                strRef = "kernel wakelocks is null";
            }
        } else {
            strRefDescr = "Reference is null";
        }
        Log.d(TAG, "Processing kernel wakelocks 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 < myKernelWakelocks.size(); i++) {
        NativeKernelWakelock wl = ((NativeKernelWakelock) myKernelWakelocks.get(i)).clone();
        if ((!bFilter) || ((wl.getDuration()) > 0)) {
            wl.substractFromRef(refFrom.m_refKernelWakelocks);
            // threshold
            if ((!bFilter) || ((wl.getDuration()) > 0)) {
                myRetKernelWakelocks.add(wl);
            }
        }
    }
    // com.asksven.android.common.privateapiproxies.Walkelock.compareTo
    switch(iSort) {
        case 0:
            // by Duration
            Comparator<NativeKernelWakelock> myCompTime = new NativeKernelWakelock.TimeComparator();
            Collections.sort(myRetKernelWakelocks, myCompTime);
            break;
        case 1:
            // by Count
            Comparator<NativeKernelWakelock> myCompCount = new NativeKernelWakelock.CountComparator();
            Collections.sort(myRetKernelWakelocks, myCompCount);
            break;
    }
    for (int i = 0; i < myRetKernelWakelocks.size(); i++) {
        myStats.add((StatElement) myRetKernelWakelocks.get(i));
    }
    if (LogSettings.DEBUG) {
        Log.d(TAG, "Result " + myStats.toString());
    }
    return myStats;
}
Also used : Context(android.content.Context) SharedPreferences(android.content.SharedPreferences) ArrayList(java.util.ArrayList) Notification(com.asksven.android.common.privateapiproxies.Notification) SuppressLint(android.annotation.SuppressLint) NativeKernelWakelock(com.asksven.android.common.privateapiproxies.NativeKernelWakelock) StatElement(com.asksven.android.common.privateapiproxies.StatElement)

Example 4 with NativeKernelWakelock

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

the class Wakelocks method parseProcWakelocks.

public static ArrayList<StatElement> parseProcWakelocks(Context context) {
    if (CommonLogSettings.DEBUG) {
        Log.i(TAG, "Parsing " + FILE_PATH);
    }
    String delimiter = String.valueOf('\t');
    ArrayList<StatElement> myRet = new ArrayList<StatElement>();
    // format
    // [name, count, expire_count, wake_count, active_since, total_time, sleep_time, max_time, last_change]
    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();
    PackageManager pack = context.getPackageManager();
    // start with 1
    for (int i = 1; i < rows.size(); i++) {
        try {
            // times in file are microseconds
            String[] data = (String[]) rows.get(i);
            String name = data[0];
            int count = Integer.valueOf(data[1]);
            int expire_count = Integer.valueOf(data[2]);
            int wake_count = Integer.valueOf(data[3]);
            long active_since = Long.valueOf(data[4]);
            long total_time = Long.valueOf(data[5]) / 1000000;
            long sleep_time = Long.valueOf(data[6]) / 1000000;
            long max_time = Long.valueOf(data[7]) / 1000000;
            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);
                            }
                        }
                    }
                }
            }
            NativeKernelWakelock 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) PackageManager(android.content.pm.PackageManager) RunningAppProcessInfo(android.app.ActivityManager.RunningAppProcessInfo) StatElement(com.asksven.android.common.privateapiproxies.StatElement)

Example 5 with NativeKernelWakelock

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

the class WakeupSourcesLg 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 {
            // different mapping from LG G3
            // name		active_count	event_count	wakeup_count	expire_count	pending_count	active_since	total_time	max_time	last_change	prevent_suspend_time
            // 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[6]);
            // total_time
            long total_time = Long.valueOf(data[7]);
            // prevent_suspend_time
            long sleep_time = Long.valueOf(data[10]);
            // max_time
            long max_time = Long.valueOf(data[8]);
            // last_change
            long last_change = Long.valueOf(data[9]);
            // 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) NativeKernelWakelock(com.asksven.android.common.privateapiproxies.NativeKernelWakelock) RunningAppProcessInfo(android.app.ActivityManager.RunningAppProcessInfo) StatElement(com.asksven.android.common.privateapiproxies.StatElement)

Aggregations

NativeKernelWakelock (com.asksven.android.common.privateapiproxies.NativeKernelWakelock)6 StatElement (com.asksven.android.common.privateapiproxies.StatElement)6 ArrayList (java.util.ArrayList)5 SuppressLint (android.annotation.SuppressLint)3 ActivityManager (android.app.ActivityManager)3 RunningAppProcessInfo (android.app.ActivityManager.RunningAppProcessInfo)3 SharedPreferences (android.content.SharedPreferences)3 Context (android.content.Context)2 IOException (java.io.IOException)2 PackageManager (android.content.pm.PackageManager)1 LayoutInflater (android.view.LayoutInflater)1 ImageView (android.widget.ImageView)1 LinearLayout (android.widget.LinearLayout)1 TextView (android.widget.TextView)1 State (com.asksven.android.common.kernelutils.State)1 Alarm (com.asksven.android.common.privateapiproxies.Alarm)1 BatteryStatsProxy (com.asksven.android.common.privateapiproxies.BatteryStatsProxy)1 Misc (com.asksven.android.common.privateapiproxies.Misc)1 NetworkUsage (com.asksven.android.common.privateapiproxies.NetworkUsage)1 Notification (com.asksven.android.common.privateapiproxies.Notification)1