Search in sources :

Example 1 with Alarm

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

use of com.asksven.android.common.privateapiproxies.Alarm 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 {
                            if (CommonLogSettings.DEBUG) {
                                Log.i(TAG, "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 Alarm

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

the class StatsProvider method getCurrentAlarmsStatList.

public ArrayList<StatElement> getCurrentAlarmsStatList(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> myAlarms = null;
    // use root if available as root delivers more data
    if (SysUtils.hasBatteryStatsPermission(ctx) && SysUtils.hasDumpsysPermission(ctx)) {
        myAlarms = AlarmsDumpsys.getAlarms();
    } else if (permsNotNeeded || SysUtils.hasBatteryStatsPermission(ctx)) {
        Log.i(TAG, "Accessing Alarms in API mode as dumpsys has failed");
        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;
        }
        myAlarms = mStats.getWakeupStats(ctx, statsType);
    } else {
        return myStats;
    }
    ArrayList<Alarm> myRetAlarms = new ArrayList<Alarm>();
    // if we are using custom ref. always retrieve "stats current"
    // sort @see
    // com.asksven.android.common.privateapiproxies.Walkelock.compareTo
    long elapsedRealtime = SystemClock.elapsedRealtime();
    for (int i = 0; i < myAlarms.size(); i++) {
        Alarm alarm = (Alarm) myAlarms.get(i);
        if (alarm != null) {
            if ((!bFilter) || ((alarm.getWakeups()) > 0)) {
                alarm.setTimeRunning(elapsedRealtime);
                myRetAlarms.add(alarm);
            }
        }
    }
    Collections.sort(myRetAlarms);
    for (int i = 0; i < myRetAlarms.size(); i++) {
        myStats.add((StatElement) myRetAlarms.get(i));
    }
    if (LogSettings.DEBUG) {
        Log.d(TAG, "Result " + myStats.toString());
    }
    return myStats;
}
Also used : Context(android.content.Context) SharedPreferences(android.content.SharedPreferences) StatElement(com.asksven.android.common.privateapiproxies.StatElement) Alarm(com.asksven.android.common.privateapiproxies.Alarm) ArrayList(java.util.ArrayList) BatteryStatsProxy(com.asksven.android.common.privateapiproxies.BatteryStatsProxy) SuppressLint(android.annotation.SuppressLint)

Example 4 with Alarm

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

the class StatsProvider method getAlarmsStatList.

/**
 * Get the Alarm Stat to be displayed
 *
 * @param bFilter
 *            defines if zero-values should be filtered out
 * @return a List of Other usages sorted by duration (descending)
 * @throws Exception
 *             if the API call failed
 */
public ArrayList<StatElement> getAlarmsStatList(boolean bFilter, Reference refFrom, Reference refTo) throws Exception {
    Context ctx = BbsApplication.getAppContext();
    ArrayList<StatElement> myStats = new ArrayList<StatElement>();
    SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(ctx);
    // to process alarms we need either root or the perms to access the private API
    if (!SysUtils.hasBatteryStatsPermission(ctx)) {
        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> myAlarms = null;
    // get the current value
    if ((refTo.m_refAlarms != null) && (!refTo.m_refAlarms.isEmpty())) {
        myAlarms = refTo.m_refAlarms;
    } else {
        myStats.add(new Notification(ctx.getString(R.string.NO_STATS)));
        return myStats;
    }
    // Collections.sort(myAlarms);
    ArrayList<Alarm> myRetAlarms = new ArrayList<Alarm>();
    // if we are using custom ref. always retrieve "stats current"
    // sort @see
    // com.asksven.android.common.privateapiproxies.Walkelock.compareTo
    String strCurrent = myAlarms.toString();
    String strRef = "";
    String strRefDescr = "";
    if (LogSettings.DEBUG) {
        if (refFrom != null) {
            strRefDescr = refFrom.whoAmI();
            if (refFrom.m_refAlarms != null) {
                strRef = refFrom.m_refAlarms.toString();
            } else {
                strRef = "Alarms is null";
            }
        } else {
            strRefDescr = "Reference is null";
        }
        Log.d(TAG, "Processing alarms 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 < myAlarms.size(); i++) {
        Alarm alarm = ((Alarm) myAlarms.get(i)).clone();
        if ((!bFilter) || ((alarm.getWakeups()) > 0)) {
            alarm.substractFromRef(refFrom.m_refAlarms);
            // threshold
            if ((!bFilter) || ((alarm.getWakeups()) > 0)) {
                myRetAlarms.add(alarm);
            }
        }
    }
    Collections.sort(myRetAlarms);
    for (int i = 0; i < myRetAlarms.size(); i++) {
        myStats.add((StatElement) myRetAlarms.get(i));
    }
    if (LogSettings.DEBUG) {
        Log.d(TAG, "Result " + myStats.toString());
    }
    return myStats;
}
Also used : Context(android.content.Context) SharedPreferences(android.content.SharedPreferences) StatElement(com.asksven.android.common.privateapiproxies.StatElement) Alarm(com.asksven.android.common.privateapiproxies.Alarm) ArrayList(java.util.ArrayList) Notification(com.asksven.android.common.privateapiproxies.Notification) SuppressLint(android.annotation.SuppressLint)

Example 5 with Alarm

use of com.asksven.android.common.privateapiproxies.Alarm 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);
    if (LogSettings.DEBUG) {
        Log.i(TAG, "Values: " + entry.getVals());
    }
    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) m_context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.stat_row, 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());
    }
    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 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));
    GraphablePie gauge = (GraphablePie) convertView.findViewById(R.id.Gauge);
    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);
    }
    ImageView iconView = (ImageView) convertView.findViewById(R.id.icon);
    LinearLayout iconLayout = (LinearLayout) convertView.findViewById(R.id.LayoutIcon);
    // 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;
}
Also used : SharedPreferences(android.content.SharedPreferences) NetworkUsage(com.asksven.android.common.privateapiproxies.NetworkUsage) Misc(com.asksven.android.common.privateapiproxies.Misc) SensorUsage(com.asksven.android.common.privateapiproxies.SensorUsage) Process(com.asksven.android.common.privateapiproxies.Process) SuppressLint(android.annotation.SuppressLint) NativeKernelWakelock(com.asksven.android.common.privateapiproxies.NativeKernelWakelock) State(com.asksven.android.common.kernelutils.State) StatElement(com.asksven.android.common.privateapiproxies.StatElement) LayoutInflater(android.view.LayoutInflater) Alarm(com.asksven.android.common.privateapiproxies.Alarm) GraphablePie(com.asksven.betterbatterystats.widgets.GraphablePie) TextView(android.widget.TextView) ImageView(android.widget.ImageView) LinearLayout(android.widget.LinearLayout)

Aggregations

Alarm (com.asksven.android.common.privateapiproxies.Alarm)8 StatElement (com.asksven.android.common.privateapiproxies.StatElement)8 Matcher (java.util.regex.Matcher)5 Pattern (java.util.regex.Pattern)5 SuppressLint (android.annotation.SuppressLint)3 SharedPreferences (android.content.SharedPreferences)3 Context (android.content.Context)2 ArrayList (java.util.ArrayList)2 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 BatteryStatsProxy (com.asksven.android.common.privateapiproxies.BatteryStatsProxy)1 Misc (com.asksven.android.common.privateapiproxies.Misc)1 NativeKernelWakelock (com.asksven.android.common.privateapiproxies.NativeKernelWakelock)1 NetworkUsage (com.asksven.android.common.privateapiproxies.NetworkUsage)1 Notification (com.asksven.android.common.privateapiproxies.Notification)1 Process (com.asksven.android.common.privateapiproxies.Process)1 SensorUsage (com.asksven.android.common.privateapiproxies.SensorUsage)1