Skip to content

Commit

Permalink
Use system animator duration scale for animations
Browse files Browse the repository at this point in the history
So animations follow the system settings (and can be shut off
completely when "Remove animations" is on).

This commit also decreases the duration of the ripple effect to
just 200ms (if `animatorDurationScale` is 1, the default value).
So all animations share the same duration time now.
  • Loading branch information
markusfisch committed Feb 19, 2024
1 parent dc99264 commit 77f5f34
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import android.view.View;
import android.view.ViewConfiguration;

import de.markusfisch.android.pielauncher.preference.Preferences;

public class Ripple {
private final Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
private final Point hotSpot = new Point();
Expand Down Expand Up @@ -91,9 +93,12 @@ public void cancel() {
offset = 0;
}

public boolean draw(Canvas canvas) {
public boolean draw(Canvas canvas, Preferences prefs) {
long duration = Math.round(prefs.getAnimationDuration());
if (duration <= 0) {
return false;
}
long delta = SystemClock.uptimeMillis() - offset;
long duration = 300L;
if (delta < 0) {
return offset > 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public class Preferences {
private static final String ICON_PACK = "icon_pack";

private final SharedPreferences preferences;
private final SystemSettings systemSettings;

private boolean skipSetup = false;
private int orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
Expand All @@ -48,6 +49,7 @@ public class Preferences {

public Preferences(Context context) {
preferences = PreferenceManager.getDefaultSharedPreferences(context);
systemSettings = new SystemSettings(context.getContentResolver());

DisplayMetrics dm = context.getResources().getDisplayMetrics();
int defaultOrientation = dm.heightPixels > dm.widthPixels
Expand Down Expand Up @@ -180,6 +182,10 @@ public void setIconPack(String iconPack) {
put(ICON_PACK, iconPack).apply();
}

public float getAnimationDuration() {
return 200f * systemSettings.getAnimatorDurationScale();
}

private SharedPreferences.Editor put(String key, boolean value) {
return put(editor -> editor.putBoolean(key, value));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package de.markusfisch.android.pielauncher.preference;

import android.content.ContentResolver;
import android.database.ContentObserver;
import android.os.Build;
import android.provider.Settings;

class SystemSettings {
private static final boolean HAS_GLOBAL =
Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1;

private final ContentResolver contentResolver;

private float animatorDurationScale = 1f;

public SystemSettings(ContentResolver contentResolver) {
this.contentResolver = contentResolver;
contentResolver.registerContentObserver(
HAS_GLOBAL
? Settings.Global.getUriFor(Settings.Global.ANIMATOR_DURATION_SCALE)
: Settings.System.getUriFor(Settings.System.TRANSITION_ANIMATION_SCALE),
false,
new ContentObserver(null) {
@Override
public void onChange(boolean selfChange) {
onAnimatorScaleChanged();
}
});
onAnimatorScaleChanged();
}

public float getAnimatorDurationScale() {
return animatorDurationScale;
}

private void onAnimatorScaleChanged() {
if (HAS_GLOBAL) {
animatorDurationScale = Settings.Global.getFloat(contentResolver,
Settings.Global.ANIMATOR_DURATION_SCALE,
animatorDurationScale);
} else {
animatorDurationScale = Settings.System.getFloat(contentResolver,
Settings.System.TRANSITION_ANIMATION_SCALE,
animatorDurationScale);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,40 @@
import android.util.AttributeSet;
import android.widget.ImageView;

import de.markusfisch.android.pielauncher.app.PieLauncherApp;
import de.markusfisch.android.pielauncher.graphics.Ripple;
import de.markusfisch.android.pielauncher.preference.Preferences;

public class ActionButton extends ImageView {
private final Ripple ripple = Ripple.newPressRipple();

private Preferences prefs;

public ActionButton(Context context) {
super(context);
initTouchListener();
init(context);
}

public ActionButton(Context context, AttributeSet attrs) {
super(context, attrs);
initTouchListener();
init(context);
}

public ActionButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initTouchListener();
init(context);
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (ripple.draw(canvas)) {
if (ripple.draw(canvas, prefs)) {
invalidate();
}
}

private void initTouchListener() {
private void init(Context context) {
prefs = PieLauncherApp.getPrefs(context);
setOnTouchListener(ripple.getOnTouchListener());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ public interface ListListener {
private static final int MODE_PIE = 0;
private static final int MODE_LIST = 1;
private static final int MODE_EDIT = 2;
private static final float ANIM_DURATION = 200f;

private final ArrayList<AppMenu.Icon> backup = new ArrayList<>();
private final ArrayList<AppMenu.Icon> ungrabbedIcons = new ArrayList<>();
Expand Down Expand Up @@ -334,7 +333,7 @@ protected void onDraw(Canvas canvas) {
invalidate = drawEditor(canvas);
break;
}
if (ripple.draw(canvas) || invalidate) {
if (ripple.draw(canvas, prefs) || invalidate) {
invalidate();
}
if (PieLauncherApp.appMenu.isIndexing()) {
Expand Down Expand Up @@ -1043,12 +1042,17 @@ private boolean drawList(Canvas canvas) {
int magSize = Math.round(Math.max(cellWidth, cellHeight) * .1f);
boolean invalidate = false;
if (highlightedFrom > 0) {
long now = SystemClock.uptimeMillis();
float f = Math.min(1f, (now - highlightedFrom) / ANIM_DURATION);
if (f < 1f) {
invalidate = true;
float ad = prefs.getAnimationDuration();
if (ad > 0) {
long now = SystemClock.uptimeMillis();
float f = Math.min(1f, (now - highlightedFrom) / ad);
if (f < 1f) {
invalidate = true;
}
magSize = Math.round(magSize * f);
} else {
magSize = 0;
}
magSize = Math.round(magSize * f);
}
for (int i = 0; i < size; ++i) {
if (y > viewTop && y < viewBottom) {
Expand Down Expand Up @@ -1086,12 +1090,16 @@ private boolean drawEditor(Canvas canvas) {
}
boolean invalidate = false;
if (hasIcon) {
long now = SystemClock.uptimeMillis();
float f = Math.min(1f, (now - grabbedIconAt) / ANIM_DURATION);
if (f < 1f) {
invalidate = true;
float radius = actionSize;
float ad = prefs.getAnimationDuration();
if (ad > 0) {
long now = SystemClock.uptimeMillis();
float f = Math.min(1f, (now - grabbedIconAt) / ad);
if (f < 1f) {
invalidate = true;
}
radius *= f;
}
float radius = f * actionSize;
drawAction(canvas, iconRemove, iconStartRect, radius);
drawAction(canvas, PieLauncherApp.iconPack.hasPacks()
? iconEdit : iconHide, iconCenterRect, radius);
Expand Down Expand Up @@ -1151,16 +1159,21 @@ private void drawEditablePie(int centerX, int centerY) {
}

private boolean drawPieMenu(Canvas canvas) {
float ad = prefs.getAnimationDuration();
float f = 0;
long now = SystemClock.uptimeMillis();
if (fadeInFrom > 0) {
f = Math.min(1f, (now - fadeInFrom) / ANIM_DURATION);
} else {
long delta = now - fadeOutFrom;
if (delta < ANIM_DURATION) {
// Ensure f < 1f so invalidate() is invoked.
f = Math.min(.99999f, 1f - delta / ANIM_DURATION);
if (ad > 0) {
long now = SystemClock.uptimeMillis();
if (fadeInFrom > 0) {
f = Math.min(1f, (now - fadeInFrom) / ad);
} else {
long delta = now - fadeOutFrom;
if (delta < ad) {
// Ensure f < 1f so invalidate() is invoked.
f = Math.min(.99999f, 1f - delta / ad);
}
}
} else {
f = fadeInFrom > 0 ? 1f : 0f;
}
if (f <= 0) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,40 @@
import android.util.AttributeSet;
import android.widget.TextView;

import de.markusfisch.android.pielauncher.app.PieLauncherApp;
import de.markusfisch.android.pielauncher.graphics.Ripple;
import de.markusfisch.android.pielauncher.preference.Preferences;

public class PreferenceView extends TextView {
private final Ripple ripple = Ripple.newPressRipple();

private Preferences prefs;

public PreferenceView(Context context) {
super(context);
initTouchListener();
init(context);
}

public PreferenceView(Context context, AttributeSet attrs) {
super(context, attrs);
initTouchListener();
init(context);
}

public PreferenceView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initTouchListener();
init(context);
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (ripple.draw(canvas)) {
if (ripple.draw(canvas, prefs)) {
invalidate();
}
}

private void initTouchListener() {
private void init(Context context) {
prefs = PieLauncherApp.getPrefs(context);
setOnTouchListener(ripple.getOnTouchListener());
}
}

0 comments on commit 77f5f34

Please sign in to comment.