Search in sources :

Example 16 with StatElement

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

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

the class StatsProvider method getCurrentCpuStateList.

public ArrayList<StatElement> getCurrentCpuStateList(boolean bFilter) throws Exception {
    // List to store the other usages to
    ArrayList<State> myStates = CpuStates.getTimesInStates();
    ArrayList<StatElement> myStats = new ArrayList<StatElement>();
    if (myStates == null) {
        return myStats;
    }
    String strCurrent = myStates.toString();
    String strRef = "";
    String strRefDescr = "";
    for (int i = 0; i < myStates.size(); i++) {
        State state = myStates.get(i);
        if ((!bFilter) || ((state.m_duration) > 0)) {
            myStats.add(state);
        }
    }
    return myStats;
}
Also used : State(com.asksven.android.common.kernelutils.State) StatElement(com.asksven.android.common.privateapiproxies.StatElement) ArrayList(java.util.ArrayList) SuppressLint(android.annotation.SuppressLint)

Example 18 with StatElement

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

the class StatsProvider method getCurrentProcessStatList.

public ArrayList<StatElement> getCurrentProcessStatList(boolean bFilter, int iSort) throws Exception {
    Context ctx = BbsApplication.getAppContext();
    ArrayList<StatElement> myProcesses = null;
    ArrayList<Process> myRetProcesses = new ArrayList<Process>();
    if (!SysUtils.hasBatteryStatsPermission(ctx)) {
        myProcesses = ProcessStatsDumpsys.getProcesses(ctx);
    } else {
        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;
        }
        myProcesses = mStats.getProcessStats(ctx, statsType);
    }
    // add elements and recalculate the total
    long total = 0;
    for (int i = 0; i < myProcesses.size(); i++) {
        Process ps = (Process) myProcesses.get(i);
        if ((!bFilter) || ((ps.getSystemTime() + ps.getUserTime()) > 0)) {
            total += ps.getSystemTime() + ps.getSystemTime();
            myRetProcesses.add(ps);
        }
    }
    // sort by Duration
    Comparator<Process> myCompTime = new Process.ProcessTimeComparator();
    Collections.sort(myRetProcesses, myCompTime);
    if (LogSettings.DEBUG) {
        Log.d(TAG, "Result " + myProcesses.toString());
    }
    myProcesses.clear();
    for (int i = 0; i < myRetProcesses.size(); i++) {
        myRetProcesses.get(i).setTotal(total);
        myProcesses.add(myRetProcesses.get(i));
    }
    return myProcesses;
}
Also used : Context(android.content.Context) StatElement(com.asksven.android.common.privateapiproxies.StatElement) ArrayList(java.util.ArrayList) Process(com.asksven.android.common.privateapiproxies.Process) BatteryStatsProxy(com.asksven.android.common.privateapiproxies.BatteryStatsProxy) SuppressLint(android.annotation.SuppressLint)

Example 19 with StatElement

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

the class StatsProvider method getCurrentNetworkUsageStatList.

public ArrayList<StatElement> getCurrentNetworkUsageStatList(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> myNetworkStats = 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;
    }
    myNetworkStats = mStats.getNetworkUsageStats(ctx, statsType);
    ArrayList<NetworkUsage> myRetNetworkStats = new ArrayList<NetworkUsage>();
    for (int i = 0; i < myNetworkStats.size(); i++) {
        NetworkUsage netStat = (NetworkUsage) myNetworkStats.get(i);
        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) BatteryStatsProxy(com.asksven.android.common.privateapiproxies.BatteryStatsProxy) SuppressLint(android.annotation.SuppressLint)

Example 20 with StatElement

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

the class UpdateLargeWidgetService method onStart.

@Override
public void onStart(Intent intent, int startId) {
    if (LogSettings.DEBUG) {
        Log.d(TAG, "Service started");
    }
    // Create some random data
    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this.getApplicationContext());
    int[] allWidgetIds = intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);
    ComponentName thisWidget = new ComponentName(getApplicationContext(), LargeWidgetProvider.class);
    int[] allWidgetIds2 = appWidgetManager.getAppWidgetIds(thisWidget);
    if (LogSettings.DEBUG) {
        Log.w(TAG, "From Intent" + String.valueOf(allWidgetIds.length));
        Log.w(TAG, "Direct" + String.valueOf(allWidgetIds2.length));
    }
    StatsProvider stats = StatsProvider.getInstance();
    // make sure to flush cache
    BatteryStatsProxy.getInstance(this).invalidate();
    for (int widgetId : allWidgetIds) {
        RemoteViews remoteViews = new RemoteViews(this.getApplicationContext().getPackageName(), R.layout.large_widget_layout);
        // we change the bg color of the layout based on alpha from prefs
        SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
        int opacity = sharedPrefs.getInt("large_widget_bg_opacity", 20);
        opacity = (255 * opacity) / 100;
        remoteViews.setInt(R.id.layout, "setBackgroundColor", (opacity << 24) & android.graphics.Color.BLACK);
        // retrieve stats
        String refFrom = sharedPrefs.getString("large_widget_default_stat_type", Reference.UNPLUGGED_REF_FILENAME);
        boolean showPct = sharedPrefs.getBoolean("large_widget_show_pct", false);
        boolean showTitle = sharedPrefs.getBoolean("widget_show_stat_type", true);
        long timeAwake = 0;
        long timeDeepSleep = 0;
        long timeScreenOn = 0;
        long timeSince = 0;
        long sumPWakelocks = 0;
        long sumKWakelocks = 0;
        try {
            Reference currentRef = StatsProvider.getInstance().getUncachedPartialReference(0);
            Reference fromRef = ReferenceStore.getReferenceByName(refFrom, this);
            ArrayList<StatElement> otherStats = stats.getOtherUsageStatList(true, fromRef, false, true, currentRef);
            if ((otherStats == null) || (otherStats.size() == 1)) {
                // the desired stat type is unavailable, pick the alternate one and go on with that one
                refFrom = sharedPrefs.getString("widget_fallback_stat_type", Reference.UNPLUGGED_REF_FILENAME);
                fromRef = ReferenceStore.getReferenceByName(refFrom, this);
                otherStats = stats.getOtherUsageStatList(true, fromRef, false, true, currentRef);
            }
            if ((otherStats != null) && (otherStats.size() > 1)) {
                try {
                    timeAwake = ((Misc) stats.getElementByKey(otherStats, StatsProvider.LABEL_MISC_AWAKE)).getTimeOn();
                    timeScreenOn = ((Misc) stats.getElementByKey(otherStats, "Screen On")).getTimeOn();
                } catch (Exception e) {
                    timeAwake = 0;
                    timeScreenOn = 0;
                }
                timeSince = StatsProvider.getInstance().getSince(fromRef, currentRef);
                ArrayList<StatElement> pWakelockStats = stats.getWakelockStatList(true, fromRef, 0, 0, currentRef);
                sumPWakelocks = stats.sum(pWakelockStats);
                ArrayList<StatElement> kWakelockStats = stats.getKernelWakelockStatList(true, fromRef, 0, 0, currentRef);
                sumKWakelocks = stats.sum(kWakelockStats);
                Misc deepSleepStat = ((Misc) stats.getElementByKey(otherStats, "Deep Sleep"));
                if (deepSleepStat != null) {
                    timeDeepSleep = deepSleepStat.getTimeOn();
                } else {
                    timeDeepSleep = 0;
                }
                if (!showTitle) {
                    remoteViews.setInt(R.id.stat_type, "setVisibility", View.GONE);
                }
                // Set the text
                remoteViews.setTextViewText(R.id.stat_type, fromRef.getLabel());
                remoteViews.setTextViewText(R.id.since, DateUtils.formatDurationShort(timeSince));
                if (showPct) {
                    remoteViews.setTextViewText(R.id.awake, StringUtils.formatRatio(timeAwake, timeSince));
                    remoteViews.setTextViewText(R.id.deep_sleep, StringUtils.formatRatio(timeDeepSleep, timeSince));
                    remoteViews.setTextViewText(R.id.screen_on, StringUtils.formatRatio(timeScreenOn, timeSince));
                } else {
                    remoteViews.setTextViewText(R.id.awake, DateUtils.formatDurationShort(timeAwake));
                    remoteViews.setTextViewText(R.id.deep_sleep, DateUtils.formatDurationShort(timeDeepSleep));
                    remoteViews.setTextViewText(R.id.screen_on, DateUtils.formatDurationShort(timeScreenOn));
                }
                // and the font size
                float fontSize = Float.valueOf(sharedPrefs.getString("large_widget_font_size", "10"));
                remoteViews.setFloat(R.id.staticSince, "setTextSize", fontSize);
                remoteViews.setFloat(R.id.staticAwake, "setTextSize", fontSize);
                remoteViews.setFloat(R.id.staticDeepSleep, "setTextSize", fontSize);
                remoteViews.setFloat(R.id.staticScreenOn, "setTextSize", fontSize);
                remoteViews.setFloat(R.id.staticKWL, "setTextSize", fontSize);
                remoteViews.setFloat(R.id.staticPWL, "setTextSize", fontSize);
                remoteViews.setFloat(R.id.stat_type, "setTextSize", fontSize);
                remoteViews.setFloat(R.id.since, "setTextSize", fontSize);
                remoteViews.setFloat(R.id.awake, "setTextSize", fontSize);
                remoteViews.setFloat(R.id.deep_sleep, "setTextSize", fontSize);
                remoteViews.setFloat(R.id.screen_on, "setTextSize", fontSize);
                remoteViews.setFloat(R.id.kwl, "setTextSize", fontSize);
                remoteViews.setFloat(R.id.wl, "setTextSize", fontSize);
                if ((sumPWakelocks == 1) && (pWakelockStats.size() == 1)) {
                    // there was no reference
                    remoteViews.setTextViewText(R.id.wl, "n/a");
                } else {
                    if (showPct) {
                        remoteViews.setTextViewText(R.id.wl, StringUtils.formatRatio(sumPWakelocks, timeSince));
                    } else {
                        remoteViews.setTextViewText(R.id.wl, DateUtils.formatDurationShort(sumPWakelocks));
                    }
                }
                if ((sumKWakelocks == 1) && (kWakelockStats.size() == 1)) {
                    // there was no reference
                    remoteViews.setTextViewText(R.id.kwl, "n/a");
                } else {
                    if (showPct) {
                        remoteViews.setTextViewText(R.id.kwl, StringUtils.formatRatio(sumKWakelocks, timeSince));
                    } else {
                        remoteViews.setTextViewText(R.id.kwl, DateUtils.formatDurationShort(sumKWakelocks));
                    }
                }
                WidgetBars graph = new WidgetBars();
                ArrayList<Long> serie = new ArrayList<Long>();
                serie.add(timeSince);
                serie.add(timeDeepSleep);
                serie.add(timeAwake);
                serie.add(timeScreenOn);
                serie.add(sumKWakelocks);
                serie.add(sumPWakelocks);
                remoteViews.setImageViewBitmap(R.id.graph, graph.getBitmap(this, serie));
            } else {
                // no stat available
                // Set the text
                String notAvailable = "n/a";
                if (fromRef != null) {
                    remoteViews.setTextViewText(R.id.stat_type, fromRef.m_fileName);
                } else {
                    remoteViews.setTextViewText(R.id.stat_type, notAvailable);
                }
                remoteViews.setTextViewText(R.id.since, notAvailable);
                remoteViews.setTextViewText(R.id.awake, notAvailable);
                remoteViews.setTextViewText(R.id.screen_on, notAvailable);
                remoteViews.setTextViewText(R.id.wl, notAvailable);
                remoteViews.setTextViewText(R.id.kwl, notAvailable);
            }
        } catch (Exception e) {
            Log.e(TAG, "Exception: " + Log.getStackTraceString(e));
        } finally {
            if (LogSettings.DEBUG) {
                Log.d(TAG, "Since: " + DateUtils.formatDurationShort(timeSince));
                Log.d(TAG, "Awake: " + DateUtils.formatDurationShort(timeAwake));
                Log.d(TAG, "Screen on: " + DateUtils.formatDurationShort(timeScreenOn));
                Log.d(TAG, "P. Wl.: " + DateUtils.formatDurationShort(sumPWakelocks));
                Log.d(TAG, "K. Wl.: " + DateUtils.formatDurationShort(sumKWakelocks));
            }
            // Register an onClickListener for the graph -> refresh
            Intent clickIntent = new Intent(this.getApplicationContext(), LargeWidgetProvider.class);
            clickIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
            clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, allWidgetIds);
            PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            remoteViews.setOnClickPendingIntent(R.id.layout, pendingIntent);
            // Register an onClickListener for the widget -> call main activity
            Intent i = new Intent(Intent.ACTION_MAIN);
            PackageManager manager = getPackageManager();
            i = manager.getLaunchIntentForPackage(getPackageName());
            i.addCategory(Intent.CATEGORY_LAUNCHER);
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            int stat = Integer.valueOf(sharedPrefs.getString("widget_default_stat", "0"));
            i.putExtra(StatsActivity.STAT, stat);
            i.putExtra(StatsActivity.STAT_TYPE_FROM, refFrom);
            i.putExtra(StatsActivity.STAT_TYPE_TO, Reference.CURRENT_REF_FILENAME);
            PendingIntent clickPI = PendingIntent.getActivity(this.getApplicationContext(), PI_CODE, i, PendingIntent.FLAG_UPDATE_CURRENT);
            remoteViews.setOnClickPendingIntent(R.id.graph, clickPI);
            appWidgetManager.updateAppWidget(widgetId, remoteViews);
        }
    }
    stopSelf();
    super.onStart(intent, startId);
}
Also used : SharedPreferences(android.content.SharedPreferences) Reference(com.asksven.betterbatterystats.data.Reference) AppWidgetManager(android.appwidget.AppWidgetManager) Misc(com.asksven.android.common.privateapiproxies.Misc) ArrayList(java.util.ArrayList) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) RemoteViews(android.widget.RemoteViews) PackageManager(android.content.pm.PackageManager) StatsProvider(com.asksven.betterbatterystats.data.StatsProvider) StatElement(com.asksven.android.common.privateapiproxies.StatElement) ComponentName(android.content.ComponentName) PendingIntent(android.app.PendingIntent) WidgetBars(com.asksven.betterbatterystats.widgets.WidgetBars)

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