Search in sources :

Example 1 with Misc

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

the class UpdateMediumWidgetService 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);
    Log.w(TAG, "From Intent" + String.valueOf(allWidgetIds.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.medium_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.BOOT_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));
                    }
                }
            } 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.deep_sleep, 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, "Deep Sleep: " + DateUtils.formatDurationShort(timeDeepSleep));
                Log.d(TAG, "Screen on: " + DateUtils.formatDurationShort(timeScreenOn));
                Log.d(TAG, "PWL: " + DateUtils.formatDurationShort(sumPWakelocks));
                Log.d(TAG, "KWL: " + DateUtils.formatDurationShort(sumKWakelocks));
            }
            // Register an onClickListener for the graph -> refresh
            Intent clickIntent = new Intent(this.getApplicationContext(), MediumWidgetProvider.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.layout2, 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) 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) PendingIntent(android.app.PendingIntent)

Example 2 with Misc

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

the class UpdateWidgetService method onHandleWork.

@Override
protected void onHandleWork(Intent intent) {
    // We have received work to do.  The system or framework is already
    // holding a wake lock for us at this point, so we can just go.
    Log.i(TAG, "onHandleWork: " + intent);
    if (LogSettings.DEBUG) {
        Log.d(TAG, "Service started");
    }
    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this.getApplicationContext());
    int[] allWidgetIds = intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);
    StatsProvider stats = StatsProvider.getInstance();
    // make sure to flush cache
    BatteryStatsProxy proxy = BatteryStatsProxy.getInstance(this);
    if (proxy != null) {
        proxy.invalidate();
    }
    if (allWidgetIds != null) {
        if (allWidgetIds.length == 0) {
            Log.i(TAG, "allWidgetIds was empty");
        }
        for (int widgetId : allWidgetIds) {
            Log.i(TAG, "Update widget " + widgetId);
            RemoteViews remoteViews = new RemoteViews(this.getApplicationContext().getPackageName(), R.layout.widget);
            final int cellSize = 40;
            int width = 3;
            int height = 2;
            int widthDim = 0;
            int heightDim = 0;
            if (Build.VERSION.SDK_INT >= 16) {
                Bundle widgetOptions = appWidgetManager.getAppWidgetOptions(widgetId);
                width = AppWidget.sizeToCells(widgetOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH) - 10);
                height = AppWidget.sizeToCells(widgetOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT) + 10);
                widthDim = widgetOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH);
                heightDim = widgetOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT);
                Log.i(TAG, "[" + widgetId + "] height=" + height + " (" + widgetOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT) + ")");
                Log.i(TAG, "[" + widgetId + "] width=" + width + "(" + widgetOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH) + ")");
                remoteViews = new RemoteViews(this.getPackageName(), R.layout.widget_horz);
                if ((height <= 2) && (width <= 2)) {
                    // switch to image only
                    Log.i(TAG, "[" + widgetId + "] using image-only layout");
                    remoteViews = new RemoteViews(this.getPackageName(), R.layout.widget);
                } else if (height < width) {
                    // switch to horizontal
                    Log.i(TAG, "[" + widgetId + "] using horizontal layout");
                    remoteViews = new RemoteViews(this.getPackageName(), R.layout.widget_horz);
                } else {
                    // switch to vertical
                    Log.i(TAG, "[" + widgetId + "] using vertical layout");
                    remoteViews = new RemoteViews(this.getPackageName(), R.layout.widget_vert);
                }
            }
            // we change the bg color of the layout based on alpha from prefs
            SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
            int opacity = sharedPrefs.getInt("new_widget_bg_opacity", 20);
            opacity = (255 * opacity) / 100;
            remoteViews.setInt(R.id.background, "setBackgroundColor", (opacity << 24) & android.graphics.Color.BLACK);
            // remoteViews.setInt(R.id.layoutBackground, "setImageAlpha", opacity);
            long timeAwake = 0;
            long timeSince = 0;
            long timeScreenOn = 0;
            long timeDeepSleep = 0;
            long timePWL = 0;
            long timeKWL = 0;
            String refFrom = sharedPrefs.getString("new_widget_default_stat_type", Reference.UNPLUGGED_REF_FILENAME);
            try {
                // retrieve stats
                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);
                }
                timeSince = StatsProvider.getInstance().getSince(fromRef, currentRef);
                if ((otherStats != null) && (otherStats.size() > 1)) {
                    Misc timeAwakeStat = (Misc) stats.getElementByKey(otherStats, StatsProvider.LABEL_MISC_AWAKE);
                    if (timeAwakeStat != null) {
                        timeAwake = timeAwakeStat.getTimeOn();
                    } else {
                        timeAwake = 0;
                    }
                    Misc timeScreenOnStat = (Misc) stats.getElementByKey(otherStats, "Screen On");
                    if (timeScreenOnStat != null) {
                        timeScreenOn = timeScreenOnStat.getTimeOn();
                    } else {
                        timeScreenOn = 0;
                    }
                    Misc deepSleepStat = ((Misc) stats.getElementByKey(otherStats, "Deep Sleep"));
                    if (deepSleepStat != null) {
                        timeDeepSleep = deepSleepStat.getTimeOn();
                    } else {
                        timeDeepSleep = 0;
                    }
                    ArrayList<StatElement> pWakelockStats = stats.getWakelockStatList(true, fromRef, 0, 0, currentRef);
                    timePWL = stats.sum(pWakelockStats);
                    ArrayList<StatElement> kWakelockStats = stats.getKernelWakelockStatList(true, fromRef, 0, 0, currentRef);
                    timeKWL = stats.sum(kWakelockStats);
                } else {
                }
            } catch (Exception e) {
                Log.e(TAG, "Exception: " + Log.getStackTraceString(e));
            } finally {
                if (LogSettings.DEBUG) {
                    Log.d(TAG, "Reference: " + refFrom);
                    Log.d(TAG, "Since: " + DateUtils.formatShort(timeSince) + " " + AppWidget.formatDuration(timeSince) + " " + timeSince);
                    Log.d(TAG, "Awake: " + DateUtils.formatShort(timeAwake) + " " + AppWidget.formatDuration(timeAwake) + " " + timeAwake);
                    Log.d(TAG, "Screen on: " + DateUtils.formatShort(timeScreenOn) + " " + AppWidget.formatDuration(timeScreenOn) + " " + timeScreenOn);
                    Log.d(TAG, "Deep sleep: " + DateUtils.formatShort(timeDeepSleep) + " " + AppWidget.formatDuration(timeDeepSleep) + " " + timeDeepSleep);
                    Log.d(TAG, "KWL: " + DateUtils.formatShort(timeKWL) + " " + AppWidget.formatDuration(timeKWL) + " " + timeKWL);
                    Log.d(TAG, "PWL: " + DateUtils.formatShort(timePWL) + " " + AppWidget.formatDuration(timePWL) + " " + timePWL);
                }
                WidgetSummary graph = new WidgetSummary();
                graph.setAwake(timeAwake);
                graph.setScreenOn(timeScreenOn);
                graph.setDeepSleep(timeDeepSleep);
                graph.setDuration(timeSince);
                graph.setKWL(timeKWL);
                graph.setPWL(timePWL);
                DisplayMetrics metrics = this.getResources().getDisplayMetrics();
                Log.i(TAG, "Widget Dimensions: height=" + heightDim + " width=" + widthDim);
                Float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, Math.min(Math.max(Math.min(widthDim, heightDim), 80), 160), metrics);
                Log.i(TAG, "BitmapDip=" + Math.min(Math.max(Math.min(widthDim, heightDim), 80), 160) + ", BitmapPx=" + px.intValue());
                graph.setBitmapSizePx(px.intValue());
                remoteViews.setImageViewBitmap(R.id.imageView1, graph.getBitmap(this));
                boolean show_pwc_only = sharedPrefs.getBoolean("widget_show_pct", false);
                if (show_pwc_only) {
                    UpdateWidgetService.setValuesToPct(remoteViews, timeAwake, timeSince, timeScreenOn, timeDeepSleep, timePWL, timeKWL);
                } else {
                    if ((width > height) && (width <= 4)) {
                        UpdateWidgetService.setValuesToDuration(remoteViews, timeAwake, timeSince, timeScreenOn, timeDeepSleep, timePWL, timeKWL);
                    } else {
                        UpdateWidgetService.setValuesToDurationAndPct(remoteViews, timeAwake, timeSince, timeScreenOn, timeDeepSleep, timePWL, timeKWL);
                    }
                }
                boolean showColor = sharedPrefs.getBoolean("text_widget_color", true);
                UpdateWidgetService.setTextColor(remoteViews, showColor, this);
                // tap zones
                // Register an onClickListener for the graph -> refresh
                Intent clickIntentRefresh = new Intent(this.getApplicationContext(), AppWidget.class);
                clickIntentRefresh.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
                clickIntentRefresh.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, allWidgetIds);
                PendingIntent pendingIntentRefresh = PendingIntent.getBroadcast(getApplicationContext(), 0, clickIntentRefresh, PendingIntent.FLAG_UPDATE_CURRENT);
                remoteViews.setOnClickPendingIntent(R.id.imageViewRefresh, pendingIntentRefresh);
                // 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.imageView1, clickPI);
                appWidgetManager.updateAppWidget(widgetId, remoteViews);
            }
        }
    } else {
        Log.i(TAG, "allWidgetIds was null");
    }
    Log.i(TAG, "Completed service @ " + DateUtils.formatDurationLong(SystemClock.elapsedRealtime()));
}
Also used : SharedPreferences(android.content.SharedPreferences) Bundle(android.os.Bundle) Reference(com.asksven.betterbatterystats.data.Reference) AppWidgetManager(android.appwidget.AppWidgetManager) Misc(com.asksven.android.common.privateapiproxies.Misc) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) DisplayMetrics(android.util.DisplayMetrics) BatteryStatsProxy(com.asksven.android.common.privateapiproxies.BatteryStatsProxy) WidgetSummary(com.asksven.betterbatterystats.widgets.WidgetSummary) RemoteViews(android.widget.RemoteViews) PackageManager(android.content.pm.PackageManager) StatsProvider(com.asksven.betterbatterystats.data.StatsProvider) StatElement(com.asksven.android.common.privateapiproxies.StatElement) PendingIntent(android.app.PendingIntent)

Example 3 with Misc

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

the class BbsDashClockExtension method onUpdateData.

@Override
protected void onUpdateData(int reason) {
    // Get preference value.
    SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
    // we want to refresh out stats each time the screen goes on
    setUpdateWhenScreenOn(true);
    // collect some data
    String refFrom = sharedPrefs.getString("dashclock_default_stat_type", Reference.UNPLUGGED_REF_FILENAME);
    long timeAwake = 0;
    long timeSince = 0;
    long drain = 0;
    String strAwake = "";
    String strDrain = "";
    StatsProvider stats = StatsProvider.getInstance();
    // make sure to flush cache
    BatteryStatsProxy.getInstance(this).invalidate();
    try {
        Reference toRef = StatsProvider.getInstance().getUncachedPartialReference(0);
        Reference fromRef = ReferenceStore.getReferenceByName(refFrom, this);
        ArrayList<StatElement> otherStats = stats.getOtherUsageStatList(true, fromRef, false, true, toRef);
        timeSince = stats.getSince(fromRef, toRef);
        drain = stats.getBatteryLevelStat(fromRef, toRef);
        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("dashclock_fallback_stat_type", Reference.BOOT_REF_FILENAME);
            fromRef = ReferenceStore.getReferenceByName(refFrom, this);
            otherStats = stats.getOtherUsageStatList(true, fromRef, false, true, toRef);
        }
        if ((otherStats != null) && (otherStats.size() > 1)) {
            Misc timeAwakeStat = (Misc) stats.getElementByKey(otherStats, StatsProvider.LABEL_MISC_AWAKE);
            if (timeAwakeStat != null) {
                timeAwake = timeAwakeStat.getTimeOn();
            } else {
                timeAwake = 0;
            }
        }
    } catch (Exception e) {
        Log.e(TAG, "Exception: " + Log.getStackTraceString(e));
    } finally {
        if (LogSettings.DEBUG) {
            Log.d(TAG, "Awake: " + DateUtils.formatDuration(timeAwake));
            Log.d(TAG, "Since: " + DateUtils.formatDuration(timeSince));
            Log.d(TAG, "Drain: " + drain);
            if (timeSince != 0) {
                Log.d(TAG, "Drain %/h: " + drain / timeSince);
            } else {
                Log.d(TAG, "Drain %/h: 0");
            }
        }
        // strAwake = DateUtils.formatDurationCompressed(timeAwake);
        strAwake = StringUtils.formatRatio(timeAwake, timeSince);
        if (timeSince != 0) {
            float pct = drain / ((float) timeSince / 1000F / 60F / 60F);
            strDrain = String.format("%.1f", pct) + "%/h";
        } else {
            strDrain = "0 %/h";
        }
    }
    String refs = getString(R.string.label_since) + " " + Reference.getLabel(refFrom);
    // Publish the extension data update.
    publishUpdate(new ExtensionData().visible(true).icon(R.drawable.ic_dashclock).status(strDrain + ", " + strAwake).expandedTitle(strAwake + " " + getString(R.string.label_awake_abbrev) + ", " + strDrain).expandedBody(refs).clickIntent(new Intent(this, StatsActivity.class)));
}
Also used : SharedPreferences(android.content.SharedPreferences) StatsProvider(com.asksven.betterbatterystats.data.StatsProvider) Reference(com.asksven.betterbatterystats.data.Reference) StatElement(com.asksven.android.common.privateapiproxies.StatElement) Misc(com.asksven.android.common.privateapiproxies.Misc) Intent(android.content.Intent) ExtensionData(com.google.android.apps.dashclock.api.ExtensionData)

Example 4 with Misc

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

Example 5 with Misc

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

the class UpdateSmallWidgetService method onStart.

@Override
public void onStart(Intent intent, int startId) {
    if (LogSettings.DEBUG) {
        Log.d(TAG, "Service started");
    }
    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this.getApplicationContext());
    int[] allWidgetIds = intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);
    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.small_widget_layout);
        // make sure to make the widget visible as it may have been previously hidden
        remoteViews.setInt(R.id.graph, "setVisibility", View.VISIBLE);
        // we change the bg color of the layout based on alpha from prefs
        SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
        int opacity = sharedPrefs.getInt("small_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("small_widget_default_stat_type", Reference.UNPLUGGED_REF_FILENAME);
        boolean showTitle = sharedPrefs.getBoolean("widget_show_stat_type", true);
        long timeAwake = 0;
        long timeScreenOn = 0;
        long timeDeepSleep = 0;
        if (!showTitle) {
            remoteViews.setInt(R.id.stat_type, "setVisibility", View.GONE);
        }
        remoteViews.setTextViewText(R.id.stat_type, Reference.getLabel(refFrom));
        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)) {
                Misc timeAwakeStat = (Misc) stats.getElementByKey(otherStats, StatsProvider.LABEL_MISC_AWAKE);
                if (timeAwakeStat != null) {
                    timeAwake = timeAwakeStat.getTimeOn();
                } else {
                    timeAwake = 0;
                }
                Misc timeScreenOnStat = (Misc) stats.getElementByKey(otherStats, "Screen On");
                if (timeScreenOnStat != null) {
                    timeScreenOn = timeScreenOnStat.getTimeOn();
                } else {
                    timeScreenOn = 0;
                }
                Misc deepSleepStat = ((Misc) stats.getElementByKey(otherStats, "Deep Sleep"));
                if (deepSleepStat != null) {
                    timeDeepSleep = deepSleepStat.getTimeOn();
                } else {
                    timeDeepSleep = 0;
                }
            } else {
                // no proper reference found
                remoteViews.setInt(R.id.graph, "setVisibility", View.GONE);
            }
        } catch (Exception e) {
            Log.e(TAG, "Exception: " + Log.getStackTraceString(e));
        } finally {
            if (LogSettings.DEBUG) {
                Log.d(TAG, "Awake: " + DateUtils.formatDuration(timeAwake));
                Log.d(TAG, "Screen on: " + DateUtils.formatDuration(timeScreenOn));
                Log.d(TAG, "Deep sleep: " + DateUtils.formatDuration(timeDeepSleep));
            }
            WidgetBattery graph = new WidgetBattery();
            graph.setAwake(timeAwake);
            graph.setScreenOn(timeScreenOn);
            graph.setDeepSleep(timeDeepSleep);
            remoteViews.setImageViewBitmap(R.id.graph, graph.getBitmap(this));
            // tap behavior depends on preferences
            boolean refreshOnTap = sharedPrefs.getBoolean("small_widget_refresh_on_tap", true);
            // Register an onClickListener for the graph -> refresh
            Intent clickIntent = new Intent(this.getApplicationContext(), SmallWidgetProvider.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);
            if (refreshOnTap) {
                remoteViews.setOnClickPendingIntent(R.id.layout, pendingIntent);
            } else {
                remoteViews.setOnClickPendingIntent(R.id.stat_type, 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.layout, clickPI);
            }
            appWidgetManager.updateAppWidget(widgetId, remoteViews);
        }
    }
    stopSelf();
    super.onStart(intent, startId);
}
Also used : WidgetBattery(com.asksven.betterbatterystats.widgets.WidgetBattery) SharedPreferences(android.content.SharedPreferences) Reference(com.asksven.betterbatterystats.data.Reference) AppWidgetManager(android.appwidget.AppWidgetManager) Misc(com.asksven.android.common.privateapiproxies.Misc) 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) PendingIntent(android.app.PendingIntent)

Aggregations

Misc (com.asksven.android.common.privateapiproxies.Misc)11 StatElement (com.asksven.android.common.privateapiproxies.StatElement)11 SharedPreferences (android.content.SharedPreferences)9 Intent (android.content.Intent)7 Reference (com.asksven.betterbatterystats.data.Reference)7 StatsProvider (com.asksven.betterbatterystats.data.StatsProvider)7 PendingIntent (android.app.PendingIntent)6 AppWidgetManager (android.appwidget.AppWidgetManager)6 PackageManager (android.content.pm.PackageManager)6 RemoteViews (android.widget.RemoteViews)6 SuppressLint (android.annotation.SuppressLint)4 Bundle (android.os.Bundle)3 BatteryStatsProxy (com.asksven.android.common.privateapiproxies.BatteryStatsProxy)3 ArrayList (java.util.ArrayList)3 Context (android.content.Context)2 DisplayMetrics (android.util.DisplayMetrics)2 WidgetSummary (com.asksven.betterbatterystats.widgets.WidgetSummary)2 ComponentName (android.content.ComponentName)1 ReceiverCallNotAllowedException (android.content.ReceiverCallNotAllowedException)1 LayoutInflater (android.view.LayoutInflater)1