Search in sources :

Example 1 with WidgetBattery

use of com.asksven.betterbatterystats.widgets.WidgetBattery 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

PendingIntent (android.app.PendingIntent)1 AppWidgetManager (android.appwidget.AppWidgetManager)1 Intent (android.content.Intent)1 SharedPreferences (android.content.SharedPreferences)1 PackageManager (android.content.pm.PackageManager)1 RemoteViews (android.widget.RemoteViews)1 Misc (com.asksven.android.common.privateapiproxies.Misc)1 StatElement (com.asksven.android.common.privateapiproxies.StatElement)1 Reference (com.asksven.betterbatterystats.data.Reference)1 StatsProvider (com.asksven.betterbatterystats.data.StatsProvider)1 WidgetBattery (com.asksven.betterbatterystats.widgets.WidgetBattery)1