Search in sources :

Example 6 with Notification

use of com.asksven.android.common.privateapiproxies.Notification 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);
    ArrayList<StatElement> myStats = new ArrayList<StatElement>();
    if (!SysUtils.hasBatteryStatsPermission(ctx)) {
        // stop straight away of root features are disabled
        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> 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;
}
Also used : Context(android.content.Context) SharedPreferences(android.content.SharedPreferences) ArrayList(java.util.ArrayList) Process(com.asksven.android.common.privateapiproxies.Process) Notification(com.asksven.android.common.privateapiproxies.Notification) SuppressLint(android.annotation.SuppressLint) StatElement(com.asksven.android.common.privateapiproxies.StatElement)

Example 7 with Notification

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

the class StatsProvider method getWakelockStatList.

/**
 * Get the 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> getWakelockStatList(boolean bFilter, Reference refFrom, int iPctType, int iSort, Reference refTo) throws Exception {
    Context ctx = BbsApplication.getAppContext();
    // this is the best practice described here: https://android-developers.googleblog.com/2011/03/identifying-app-installations.html
    String entropy = Settings.Secure.getString(ctx.getContentResolver(), Settings.Secure.ANDROID_ID);
    ArrayList<StatElement> myStats = new ArrayList<StatElement>();
    SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(ctx);
    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> myWakelocks = null;
    if ((refTo.m_refWakelocks != null) && (!refTo.m_refWakelocks.isEmpty())) {
        myWakelocks = refTo.m_refWakelocks;
    } else {
        myStats.add(new Notification(ctx.getString(R.string.NO_STATS)));
        return myStats;
    }
    ArrayList<Wakelock> myRetWakelocks = new ArrayList<Wakelock>();
    String strCurrent = myWakelocks.toString();
    String strRef = "";
    String strRefDescr = "";
    if (LogSettings.DEBUG) {
        if (refFrom != null) {
            strRefDescr = refFrom.whoAmI();
            if (refFrom.m_refWakelocks != null) {
                strRef = refFrom.m_refWakelocks.toString();
            } else {
                strRef = "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 < myWakelocks.size(); i++) {
        Wakelock wl = ((Wakelock) myWakelocks.get(i)).clone(entropy);
        if ((!bFilter) || ((wl.getDuration() / 1000) > 0)) {
            // we must distinguish two situations
            // a) we use custom stat type
            // b) we use regular stat type
            wl.substractFromRef(refFrom.m_refWakelocks);
            // threshold
            if ((!bFilter) || ((wl.getDuration() / 1000) > 0)) {
                myRetWakelocks.add(wl);
            } else {
                if (LogSettings.DEBUG) {
                    Log.i(TAG, "Skipped " + wl.toString() + " because duration < 1s");
                }
            }
        }
    }
    if (LogSettings.DEBUG) {
        Log.i(TAG, "Result has " + myRetWakelocks.size() + " entries");
    }
    // com.asksven.android.common.privateapiproxies.Walkelock.compareTo
    switch(iSort) {
        case 0:
            // by Duration
            Comparator<Wakelock> myCompTime = new Wakelock.WakelockTimeComparator();
            Collections.sort(myRetWakelocks, myCompTime);
            break;
        case 1:
            // by Count
            Comparator<Wakelock> myCompCount = new Wakelock.WakelockCountComparator();
            Collections.sort(myRetWakelocks, myCompCount);
            break;
    }
    for (int i = 0; i < myRetWakelocks.size(); i++) {
        myStats.add((StatElement) myRetWakelocks.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) Wakelock(com.asksven.android.common.privateapiproxies.Wakelock) NativeKernelWakelock(com.asksven.android.common.privateapiproxies.NativeKernelWakelock) StatElement(com.asksven.android.common.privateapiproxies.StatElement)

Example 8 with Notification

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

the class StatsProvider method getOtherUsageStatList.

/**
 * Get the Other Usage 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> getOtherUsageStatList(boolean bFilter, Reference refFrom, boolean bFilterView, boolean bWidget, Reference refTo) throws Exception {
    Context ctx = BbsApplication.getAppContext();
    ArrayList<StatElement> myStats = new ArrayList<StatElement>();
    // if on of the refs is null return
    if ((refFrom == null) || (refTo == null)) {
        myStats.add(new Notification(ctx.getString(R.string.NO_REF_ERR)));
        return myStats;
    }
    // List to store the other usages to
    ArrayList<StatElement> myUsages = new ArrayList<StatElement>();
    if ((refTo.m_refOther != null) && (!refTo.m_refOther.isEmpty())) {
        myUsages = refTo.m_refOther;
    } else {
        myStats.add(new Notification(ctx.getString(R.string.NO_STATS)));
        return myStats;
    }
    String strRefFrom = "";
    String strRefTo = "";
    String strRefFromDescr = "";
    String strRefToDescr = "";
    if (LogSettings.DEBUG) {
        if (refFrom != null) {
            strRefFromDescr = refFrom.whoAmI();
            if (refFrom.m_refOther != null) {
                strRefFrom = refFrom.m_refOther.toString();
            } else {
                strRefFrom = "Other is null";
            }
        }
        if (refTo != null) {
            strRefToDescr = refTo.whoAmI();
            if (refTo.m_refOther != null) {
                strRefTo = refTo.m_refOther.toString();
            } else {
                strRefTo = "Other is null";
            }
        }
        Log.d(TAG, "Processing Other from " + refFrom.m_fileName + " to " + refTo.m_fileName);
        Log.d(TAG, "Reference from: " + strRefFromDescr);
        Log.d(TAG, "Reference to: " + strRefToDescr);
        Log.d(TAG, "It is now " + DateUtils.now());
        Log.d(TAG, "Substracting " + strRefFrom);
        Log.d(TAG, "from " + strRefTo);
    }
    for (int i = 0; i < myUsages.size(); i++) {
        Misc usage = ((Misc) myUsages.get(i)).clone();
        if (LogSettings.DEBUG) {
            Log.d(TAG, "Current value: " + usage.getName() + " " + usage.getData(StatsProvider.getInstance().getSince(refFrom, refTo)));
        }
        if ((!bFilter) || (usage.getTimeOn() > 0)) {
            usage.substractFromRef(refFrom.m_refOther);
            if ((!bFilter) || (usage.getTimeOn() > 0)) {
                if (LogSettings.DEBUG) {
                    Log.d(TAG, "Result value: " + usage.getName() + " " + usage.getData(StatsProvider.getInstance().getSince(refFrom, refTo)));
                }
                myStats.add((StatElement) usage);
            }
        }
    }
    return myStats;
}
Also used : Context(android.content.Context) StatElement(com.asksven.android.common.privateapiproxies.StatElement) ArrayList(java.util.ArrayList) Misc(com.asksven.android.common.privateapiproxies.Misc) Notification(com.asksven.android.common.privateapiproxies.Notification) SuppressLint(android.annotation.SuppressLint)

Example 9 with Notification

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

the class StatsProvider method getStatList.

/**
 * Get the Stat to be displayed
 *
 * @return a List of StatElements sorted (descending)
 */
public ArrayList<StatElement> getStatList(int iStat, String refFromName, int iSort, String refToName) throws Exception {
    Context ctx = BbsApplication.getAppContext();
    SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(BbsApplication.getAppContext());
    boolean bFilterStats = sharedPrefs.getBoolean("filter_data", true);
    boolean developerMode = sharedPrefs.getBoolean("developer", false);
    Reference refFrom = ReferenceStore.getReferenceByName(refFromName, ctx);
    Reference refTo = ReferenceStore.getReferenceByName(refToName, ctx);
    if ((refFrom == null) || (refTo == null) || (refFromName == null) || (refToName == null) || (refFromName.equals("")) || (refToName.equals(""))) {
        Log.e(TAG, "Reference from or to are empty: (" + refFromName + ", " + refToName + ")");
        return null;
    }
    if (refFrom.equals(refToName)) {
        Toast.makeText(ctx, ctx.getString(R.string.message_identical_references, refFromName, refToName), Toast.LENGTH_LONG).show();
    }
    int iPctType = 0;
    if ((!developerMode) && (this.getIsCharging(ctx))) {
        ArrayList<StatElement> myRet = new ArrayList<StatElement>();
        myRet.add(new Notification(ctx.getString(R.string.NO_STATS_WHEN_CHARGING)));
        return myRet;
    }
    switch(iStat) {
        case 0:
            return getOtherUsageStatList(bFilterStats, refFrom, true, false, refTo);
        case 1:
            return getKernelWakelockStatList(bFilterStats, refFrom, iPctType, iSort, refTo);
        case 2:
            return getWakelockStatList(bFilterStats, refFrom, iPctType, iSort, refTo);
        case 3:
            return getAlarmsStatList(bFilterStats, refFrom, refTo);
        case 4:
            return getNetworkUsageStatList(bFilterStats, refFrom, refTo);
        case 5:
            return getCpuStateList(refFrom, refTo, bFilterStats);
        case 6:
            return getProcessStatList(bFilterStats, refFrom, iSort, refTo);
        case 7:
            return getSensorStatList(bFilterStats, refFrom, refTo);
    }
    return new ArrayList<StatElement>();
}
Also used : Context(android.content.Context) SharedPreferences(android.content.SharedPreferences) StatElement(com.asksven.android.common.privateapiproxies.StatElement) ArrayList(java.util.ArrayList) SuppressLint(android.annotation.SuppressLint) Notification(com.asksven.android.common.privateapiproxies.Notification)

Example 10 with Notification

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

Aggregations

Notification (com.asksven.android.common.privateapiproxies.Notification)10 StatElement (com.asksven.android.common.privateapiproxies.StatElement)10 SuppressLint (android.annotation.SuppressLint)9 Context (android.content.Context)9 ArrayList (java.util.ArrayList)9 SharedPreferences (android.content.SharedPreferences)7 NativeKernelWakelock (com.asksven.android.common.privateapiproxies.NativeKernelWakelock)2 LinearLayout (android.widget.LinearLayout)1 ListView (android.widget.ListView)1 TextView (android.widget.TextView)1 State (com.asksven.android.common.kernelutils.State)1 Alarm (com.asksven.android.common.privateapiproxies.Alarm)1 Misc (com.asksven.android.common.privateapiproxies.Misc)1 NetworkUsage (com.asksven.android.common.privateapiproxies.NetworkUsage)1 Process (com.asksven.android.common.privateapiproxies.Process)1 SensorUsage (com.asksven.android.common.privateapiproxies.SensorUsage)1 Wakelock (com.asksven.android.common.privateapiproxies.Wakelock)1 StatsAdapter (com.asksven.betterbatterystats.adapters.StatsAdapter)1 Reference (com.asksven.betterbatterystats.data.Reference)1