Search in sources :

Example 11 with StatElement

use of com.asksven.android.common.privateapiproxies.StatElement 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);
    // stop straight away if no root permissions or no perms to access data directly
    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> 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;
}
Also used : Context(android.content.Context) SharedPreferences(android.content.SharedPreferences) StatElement(com.asksven.android.common.privateapiproxies.StatElement) NetworkUsage(com.asksven.android.common.privateapiproxies.NetworkUsage) ArrayList(java.util.ArrayList) Notification(com.asksven.android.common.privateapiproxies.Notification) SuppressLint(android.annotation.SuppressLint)

Example 12 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)

Example 13 with StatElement

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

the class StatsProvider method getCpuStateList.

/**
 * Get the CPU states 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> getCpuStateList(Reference refFrom, Reference refTo, boolean bFilter) throws Exception {
    Context ctx = BbsApplication.getAppContext();
    // List to store the other usages to
    ArrayList<StatElement> myStates = refTo.m_refCpuStates;
    ArrayList<StatElement> myStats = new ArrayList<StatElement>();
    if ((refFrom == null) || (refTo == null)) {
        myStats.add(new Notification(ctx.getString(R.string.NO_REF_ERR)));
        return myStats;
    }
    if (refTo.m_refCpuStates == null) {
        myStats.add(new Notification(ctx.getString(R.string.NO_STATS)));
        return myStats;
    }
    String strCurrent = myStates.toString();
    String strRef = "";
    String strRefDescr = "";
    if (LogSettings.DEBUG) {
        if (refFrom != null) {
            strRefDescr = refFrom.whoAmI();
            if (refFrom.m_refCpuStates != null) {
                strRef = refFrom.m_refCpuStates.toString();
            } else {
                strRef = "CPU States is null";
            }
        }
        Log.d(TAG, "Processing CPU States 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);
    }
    ArrayList<State> myResultStates = new ArrayList<State>();
    for (int i = 0; i < myStates.size(); i++) {
        State state = ((State) myStates.get(i)).clone();
        state.substractFromRef(refFrom.m_refCpuStates);
        if ((!bFilter) || ((state.m_duration) > 0)) {
            myResultStates.add(state);
        }
    }
    Collections.sort(myResultStates);
    for (int i = 0; i < myResultStates.size(); i++) {
        myStats.add(myResultStates.get(i));
    }
    return myStats;
}
Also used : Context(android.content.Context) State(com.asksven.android.common.kernelutils.State) StatElement(com.asksven.android.common.privateapiproxies.StatElement) ArrayList(java.util.ArrayList) Notification(com.asksven.android.common.privateapiproxies.Notification) SuppressLint(android.annotation.SuppressLint)

Example 14 with StatElement

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

the class StatsProvider method getSensorStatList.

// 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);
// 
// ArrayList<StatElement> myAlarms = null;
// 
// 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);
// 
// 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;
// 
// }
/**
 * 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> getSensorStatList(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> mySensorStats = null;
    if ((refTo.m_refSensorUsage != null) && (!refTo.m_refSensorUsage.isEmpty())) {
        mySensorStats = refTo.m_refSensorUsage;
    } else {
        myStats.add(new Notification(ctx.getString(R.string.NO_STATS)));
        return myStats;
    }
    ArrayList<SensorUsage> myRetSensorStats = new ArrayList<SensorUsage>();
    // if we are using custom ref. always retrieve "stats current"
    // sort @see
    // com.asksven.android.common.privateapiproxies.Walkelock.compareTo
    String strCurrent = mySensorStats.toString();
    String strRef = "";
    String strRefDescr = "";
    if (LogSettings.DEBUG) {
        if (refFrom != null) {
            strRefDescr = refFrom.whoAmI();
            if (refFrom.m_refSensorUsage != null) {
                strRef = refFrom.m_refSensorUsage.toString();
            } else {
                strRef = "SensorUsage is null";
            }
        } else {
            strRefDescr = "Reference is null";
        }
        Log.d(TAG, "Processing sensor 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 < mySensorStats.size(); i++) {
        SensorUsage sensor = ((SensorUsage) mySensorStats.get(i)).clone();
        if ((!bFilter) || ((sensor.getTotal()) > 0)) {
            sensor.substractFromRef(refFrom.m_refSensorUsage);
            // threshold
            if ((!bFilter) || ((sensor.getTotal()) > 0)) {
                myRetSensorStats.add(sensor);
            }
        }
    }
    Collections.sort(myRetSensorStats);
    for (int i = 0; i < myRetSensorStats.size(); i++) {
        myStats.add((StatElement) myRetSensorStats.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) ArrayList(java.util.ArrayList) SensorUsage(com.asksven.android.common.privateapiproxies.SensorUsage) Notification(com.asksven.android.common.privateapiproxies.Notification) SuppressLint(android.annotation.SuppressLint)

Example 15 with StatElement

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

Aggregations

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