Skip to content

Commit

Permalink
CHANGELOG: Added charging animation option to the battery bar (Thanks…
Browse files Browse the repository at this point in the history
… to: OnePlus Customizer dev: @DHD2280)
  • Loading branch information
siavash79 committed Sep 30, 2024
1 parent 2423726 commit b8567f7
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .github/edit2MakeNewCanary
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Editing this file will trigger the build script for a new canary..
Editing this file will trigger the build script for a new canary.
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RectShape;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.ViewGroup;
import android.view.animation.LinearInterpolator;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.BaseInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.FrameLayout;
import android.widget.ImageView;
Expand Down Expand Up @@ -65,7 +67,7 @@ public class BatteryBarView extends FrameLayout {
private static boolean animateCharging = false;
private static final int ANIM_DURATION = 1000;
private static final int ANIM_DELAY = 2000;
private final Handler animationHandler = new Handler();
private final Handler animationHandler = new Handler(Looper.getMainLooper());
private Runnable chargingAnimationRunnable;

public static void setStaticColor(List<Float> batteryLevels, int[] batteryColors, boolean indicateCharging, int chargingColor, boolean indicateFastCharging, int fastChargingColor, boolean indicatePowerSave, int powerSaveColor, boolean transitColors, boolean animate) {
Expand Down Expand Up @@ -111,12 +113,6 @@ public void refreshLayout() {

refreshColors(barView.getWidth(), barView.getHeight());
mDrawable.invalidateSelf();

if (isCharging() && animateCharging) {
startChargingAnimation();
} else {
stopChargingAnimation();
}
}

private LayoutParams maskLayoutParams() {
Expand All @@ -133,11 +129,6 @@ public void onSizeChanged(int w, int h, int oldw, int oldh) {
}

private void startChargingAnimation() {
if (!isCharging() || !animateCharging) {
stopChargingAnimation();
return;
}

if (chargingAnimationRunnable == null) {
chargingAnimationRunnable = new Runnable() {
@Override
Expand All @@ -151,20 +142,25 @@ public void run() {
}

private void stopChargingAnimation() {
if (chargingAnimationRunnable != null) {
animationHandler.removeCallbacks(chargingAnimationRunnable);
chargingIndicatorView.post(() -> chargingIndicatorView.setVisibility(GONE));
chargingIndicatorViewForCenter.post(() -> chargingIndicatorViewForCenter.setVisibility(GONE));
chargingAnimationRunnable = null;
}
animationHandler.removeCallbacks(chargingAnimationRunnable);

post(() -> {
chargingIndicatorView.setVisibility(GONE);
chargingIndicatorView.clearAnimation();

chargingIndicatorViewForCenter.setVisibility(GONE);
chargingIndicatorViewForCenter.clearAnimation();
});

chargingAnimationRunnable = null;
}

private void animateChargingIndicator() {
int screenWidth = Resources.getSystem().getDisplayMetrics().widthPixels;

chargingIndicatorView.post(() -> chargingIndicatorView.setVisibility(VISIBLE));
int secondaryVisiblity = (isCenterBased) ? GONE : VISIBLE;
chargingIndicatorViewForCenter.post(() -> chargingIndicatorViewForCenter.setVisibility(secondaryVisiblity));
int secondaryVisibility = (isCenterBased) ? GONE : VISIBLE;
chargingIndicatorViewForCenter.post(() -> chargingIndicatorViewForCenter.setVisibility(secondaryVisibility));

float startX, startXCenter, endX;

Expand All @@ -177,17 +173,20 @@ private void animateChargingIndicator() {
startXCenter = 0;
endX = Math.round(getWidth() * getCurrentLevel() / 100f);
}
if (isCenterBased) endX = getWidth() / 2f;

if (isCenterBased) endX = getWidth() * (1 - ((100 - getCurrentLevel()) / 200f));

BaseInterpolator interpolator = new AccelerateInterpolator();

TranslateAnimation animation = new TranslateAnimation(startX, endX, 0, 0);
animation.setDuration(ANIM_DURATION);
animation.setInterpolator(new LinearInterpolator());
animation.setInterpolator(interpolator);
chargingIndicatorView.startAnimation(animation);

if (isCenterBased) {
TranslateAnimation animationCenter = new TranslateAnimation(startXCenter, endX, 0, 0);
TranslateAnimation animationCenter = new TranslateAnimation(startXCenter, getWidth() - endX, 0, 0);
animationCenter.setDuration(ANIM_DURATION);
animationCenter.setInterpolator(new LinearInterpolator());
animationCenter.setInterpolator(interpolator);
chargingIndicatorViewForCenter.startAnimation(animationCenter);
}
}
Expand Down Expand Up @@ -360,8 +359,15 @@ public void setVisible(boolean visible) {
private void refreshVisibility() {
if (!mIsEnabled || mIsHidden || (onlyWhileCharging && !isCharging())) {
barView.setVisibility(GONE);
stopChargingAnimation();
} else {
barView.setVisibility(VISIBLE);

if(isCharging() && animateCharging) {
startChargingAnimation();
} else {
stopChargingAnimation();
}
}
}

Expand Down

0 comments on commit b8567f7

Please sign in to comment.