Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 6c1c1308 authored by Sven Dawitz's avatar Sven Dawitz
Browse files

Issue 3005: Decrease cpu usage for charging animation to nearly 0

A user pointed out 10-20% cpu usage for battery charging animation
on lowest possible cpu frequency. I optimized this a bit.

- Using an array to cache the mini icons now on creation
- So no more cpu overhead for updating the mini icon frame (former
  usage of temporary bitmaps and involved garbage collection)
- Using half the animation frames, since only every 2 frame (20%)
  the icon for battery actually changes

Change-Id: I510c223a07fa4da82ff4eb866bf0e3a2cca33dc1
parent f1df5d9d
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -59,6 +59,7 @@
            android:orientation="horizontal"/>

        <com.android.systemui.statusbar.CmBatteryMiniIcon
            android:id="@+id/CmBatteryMiniIcon"
            android:textAppearance="@*android:style/TextAppearance.StatusBar.Icon"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
+23 −9
Original line number Diff line number Diff line
@@ -49,10 +49,11 @@ public class CmBatteryMiniIcon extends ImageView {
    static final int BATTERY_MINI_ICON_MARGIN_RIGHT_DIP = 6;

    // duration of each frame in charging animation in millis
    static final int ANIM_FRAME_DURATION = (1000 / 3);
    // halfed the update rate (*2), since only each 20% (2 frames) we got a different battery icon
    static final int ANIM_FRAME_DURATION = (1000 / 3) * 2;

    // duration of each fake-timer call to update animation in millis
    static final int ANIM_TIMER_DURATION = (1000 / 4);
    static final int ANIM_TIMER_DURATION = (1000 / 4) * 2;

    // contains the current bat level, values: 0-100
    private int mBatteryLevel = 0;
@@ -85,6 +86,8 @@ public class CmBatteryMiniIcon extends ImageView {

    private float mDensity;

    private transient Bitmap[] mMiniIconCache;

    // tracks changes to settings, so status bar is auto updated the moment the
    // setting is toggled
    class SettingsObserver extends ContentObserver {
@@ -138,6 +141,7 @@ public class CmBatteryMiniIcon extends ImageView {
        mMatrix.setTranslate(0, 0);
        mMatrix.postScale(mWidthPx, 1);

        updateIconCache();
        updateSettings();
    }

@@ -212,9 +216,10 @@ public class CmBatteryMiniIcon extends ImageView {
            long now = SystemClock.uptimeMillis();

            while (now - mLastMillis > ANIM_FRAME_DURATION) {
                mCurrentFrame = mCurrentFrame + 1;
                // adding 2, since only each 20% a new status is shown
                mCurrentFrame = mCurrentFrame + 2;
                // count to eleven, so fully charged icon also got two frames
                if (mCurrentFrame > 11)
                if (mCurrentFrame > 10)
                    mCurrentFrame = 0;
                mLastMillis = mLastMillis + ANIM_FRAME_DURATION;
            }
@@ -224,13 +229,9 @@ public class CmBatteryMiniIcon extends ImageView {
            mCurrentFrame = 10;
        }

        // get the original battery image
        int frame = (mBatteryPlugged ? mCurrentFrame : mBatteryLevel / 10);
        Bitmap bmBat = getBitmapFor(getBatResourceID(frame));
        // cut one slice of pixels from battery image
        Bitmap bmMiniBat = Bitmap.createBitmap(bmBat, 4, 0, 1, bmBat.getHeight());

        canvas.drawBitmap(bmMiniBat, mMatrix, mPaint);
        canvas.drawBitmap(mMiniIconCache[frame], mMatrix, mPaint);
    }

    private int getBatResourceID(int level) {
@@ -281,4 +282,17 @@ public class CmBatteryMiniIcon extends ImageView {
        else
            setVisibility(View.GONE);
    }

    // should be toggled to private (or inlined at constructor), once StatusBarService.updateResources properly handles theme change
    public void updateIconCache() {
        // set up the icon cache - garbage collector handles old pointer
        mMiniIconCache=new Bitmap[11];

        for(int i=0; i<=10; i++){
            // get the original battery image
            Bitmap bmBat = getBitmapFor(getBatResourceID(i));
            // cut one slice of pixels from battery image
            mMiniIconCache[i] = Bitmap.createBitmap(bmBat, 4, 0, 1, bmBat.getHeight());
        }
    }
}
+3 −0
Original line number Diff line number Diff line
@@ -122,6 +122,7 @@ public class StatusBarService extends Service implements CommandQueue.Callbacks
    // top bar
    TextView mNoNotificationsTitle;
    TextView mClearButton;
    CmBatteryMiniIcon mCmBatteryMiniIcon;
    // drag bar
    CloseDragHandle mCloseView;
    // ongoing
@@ -284,6 +285,7 @@ public class StatusBarService extends Service implements CommandQueue.Callbacks
        mIcons = (LinearLayout)sb.findViewById(R.id.icons);
        mTickerView = sb.findViewById(R.id.ticker);
        mDateView = (DateView)sb.findViewById(R.id.date);
        mCmBatteryMiniIcon = (CmBatteryMiniIcon)sb.findViewById(R.id.CmBatteryMiniIcon);

        mExpandedDialog = new ExpandedDialog(context);
        mExpandedView = expanded;
@@ -1519,6 +1521,7 @@ public class StatusBarService extends Service implements CommandQueue.Callbacks
            ((ImageView)mCloseView.getChildAt(0)).setImageDrawable(res.getDrawable(R.drawable.status_bar_close_on));
            mExpandedView.findViewById(R.id.exp_view_lin_layout).setBackgroundDrawable(res.getDrawable(R.drawable.title_bar_portrait));
            mClearButton.setBackgroundDrawable(res.getDrawable(android.R.drawable.btn_default_small));
            mCmBatteryMiniIcon.updateIconCache();

            // Update icons.
            ArrayList<ViewGroup> iconViewGroups = new ArrayList<ViewGroup>();