Skip to content

Commit ed5f45a

Browse files
author
Jeff Brown
committed
Animate status bar flings on vsync.
Use the Choreographer for timing animations. Ideally we would rewrite this code to use the animation framework instead, but this change to use the Choreographer directly is easier and less intrusive for now. Change-Id: Ibb322de9424d4d109203951d48263766200ed7e8
1 parent 84fa241 commit ed5f45a

File tree

1 file changed

+49
-42
lines changed

1 file changed

+49
-42
lines changed

packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java

Lines changed: 49 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import android.util.Log;
4848
import android.util.Slog;
4949
import android.util.TypedValue;
50+
import android.view.Choreographer;
5051
import android.view.Display;
5152
import android.view.Gravity;
5253
import android.view.IWindowManager;
@@ -106,8 +107,6 @@ public class PhoneStatusBar extends BaseStatusBar {
106107
static final int EXPANDED_LEAVE_ALONE = -10000;
107108
static final int EXPANDED_FULL_OPEN = -10001;
108109

109-
private static final int MSG_ANIMATE = 100;
110-
private static final int MSG_ANIMATE_REVEAL = 101;
111110
private static final int MSG_OPEN_NOTIFICATION_PANEL = 1000;
112111
private static final int MSG_CLOSE_NOTIFICATION_PANEL = 1001;
113112
private static final int MSG_SHOW_INTRUDER = 1002;
@@ -207,14 +206,12 @@ public class PhoneStatusBar extends BaseStatusBar {
207206
boolean mTracking;
208207
VelocityTracker mVelocityTracker;
209208

210-
static final int ANIM_FRAME_DURATION = (1000/60);
211-
209+
Choreographer mChoreographer;
212210
boolean mAnimating;
213-
long mCurAnimationTime;
214211
float mAnimY;
215212
float mAnimVel;
216213
float mAnimAccel;
217-
long mAnimLastTime;
214+
long mAnimLastTimeNanos;
218215
boolean mAnimatingReveal = false;
219216
int mViewDelta;
220217
int[] mAbsPos = new int[2];
@@ -310,6 +307,8 @@ public boolean onTouch(View v, MotionEvent event) {
310307

311308
mStatusBarView.mService = this;
312309

310+
mChoreographer = Choreographer.getInstance();
311+
313312
try {
314313
boolean showNav = mWindowManager.hasNavigationBar();
315314
if (DEBUG) Slog.v(TAG, "hasNavigationBar=" + showNav);
@@ -1090,12 +1089,6 @@ private class H extends BaseStatusBar.H {
10901089
public void handleMessage(Message m) {
10911090
super.handleMessage(m);
10921091
switch (m.what) {
1093-
case MSG_ANIMATE:
1094-
doAnimation();
1095-
break;
1096-
case MSG_ANIMATE_REVEAL:
1097-
doRevealAnimation();
1098-
break;
10991092
case MSG_OPEN_NOTIFICATION_PANEL:
11001093
animateExpand();
11011094
break;
@@ -1113,6 +1106,20 @@ public void handleMessage(Message m) {
11131106
}
11141107
}
11151108

1109+
final Runnable mAnimationCallback = new Runnable() {
1110+
@Override
1111+
public void run() {
1112+
doAnimation(mChoreographer.getFrameTimeNanos());
1113+
}
1114+
};
1115+
1116+
final Runnable mRevealAnimationCallback = new Runnable() {
1117+
@Override
1118+
public void run() {
1119+
doRevealAnimation(mChoreographer.getFrameTimeNanos());
1120+
}
1121+
};
1122+
11161123
View.OnFocusChangeListener mFocusChangeListener = new View.OnFocusChangeListener() {
11171124
public void onFocusChange(View v, boolean hasFocus) {
11181125
// Because 'v' is a ViewGroup, all its children will be (un)selected
@@ -1241,11 +1248,11 @@ void performCollapse() {
12411248
}
12421249
}
12431250

1244-
void doAnimation() {
1251+
void doAnimation(long frameTimeNanos) {
12451252
if (mAnimating) {
12461253
if (SPEW) Slog.d(TAG, "doAnimation");
12471254
if (SPEW) Slog.d(TAG, "doAnimation before mAnimY=" + mAnimY);
1248-
incrementAnim();
1255+
incrementAnim(frameTimeNanos);
12491256
if (SPEW) Slog.d(TAG, "doAnimation after mAnimY=" + mAnimY);
12501257
if (mAnimY >= getExpandedViewMaxHeight()-1) {
12511258
if (SPEW) Slog.d(TAG, "Animation completed to expanded state.");
@@ -1261,8 +1268,8 @@ else if (mAnimY < getStatusBarHeight()) {
12611268
}
12621269
else {
12631270
updateExpandedViewPos((int)mAnimY);
1264-
mCurAnimationTime += ANIM_FRAME_DURATION;
1265-
mHandler.sendMessageAtTime(mHandler.obtainMessage(MSG_ANIMATE), mCurAnimationTime);
1271+
mChoreographer.postCallback(Choreographer.CALLBACK_ANIMATION,
1272+
mAnimationCallback, null);
12661273
}
12671274
}
12681275
}
@@ -1274,31 +1281,30 @@ void stopTracking() {
12741281
mVelocityTracker = null;
12751282
}
12761283

1277-
void incrementAnim() {
1278-
long now = SystemClock.uptimeMillis();
1279-
float t = ((float)(now - mAnimLastTime)) / 1000; // ms -> s
1284+
void incrementAnim(long frameTimeNanos) {
1285+
final long deltaNanos = Math.max(frameTimeNanos - mAnimLastTimeNanos, 0);
1286+
final float t = deltaNanos * 0.000000001f; // ns -> s
12801287
final float y = mAnimY;
12811288
final float v = mAnimVel; // px/s
12821289
final float a = mAnimAccel; // px/s/s
12831290
mAnimY = y + (v*t) + (0.5f*a*t*t); // px
12841291
mAnimVel = v + (a*t); // px/s
1285-
mAnimLastTime = now; // ms
1292+
mAnimLastTimeNanos = frameTimeNanos; // ns
12861293
//Slog.d(TAG, "y=" + y + " v=" + v + " a=" + a + " t=" + t + " mAnimY=" + mAnimY
12871294
// + " mAnimAccel=" + mAnimAccel);
12881295
}
12891296

1290-
void doRevealAnimation() {
1297+
void doRevealAnimation(long frameTimeNanos) {
12911298
final int h = getCloseViewHeight() + getStatusBarHeight();
12921299
if (mAnimatingReveal && mAnimating && mAnimY < h) {
1293-
incrementAnim();
1300+
incrementAnim(frameTimeNanos);
12941301
if (mAnimY >= h) {
12951302
mAnimY = h;
12961303
updateExpandedViewPos((int)mAnimY);
12971304
} else {
12981305
updateExpandedViewPos((int)mAnimY);
1299-
mCurAnimationTime += ANIM_FRAME_DURATION;
1300-
mHandler.sendMessageAtTime(mHandler.obtainMessage(MSG_ANIMATE_REVEAL),
1301-
mCurAnimationTime);
1306+
mChoreographer.postCallback(Choreographer.CALLBACK_ANIMATION,
1307+
mRevealAnimationCallback, null);
13021308
}
13031309
}
13041310
}
@@ -1318,20 +1324,20 @@ void prepareTracking(int y, boolean opening) {
13181324
updateExpandedViewPos((int)mAnimY);
13191325
mAnimating = true;
13201326
mAnimatingReveal = true;
1321-
mHandler.removeMessages(MSG_ANIMATE);
1322-
mHandler.removeMessages(MSG_ANIMATE_REVEAL);
1323-
long now = SystemClock.uptimeMillis();
1324-
mAnimLastTime = now;
1325-
mCurAnimationTime = now + ANIM_FRAME_DURATION;
1326-
mAnimating = true;
1327-
mHandler.sendMessageAtTime(mHandler.obtainMessage(MSG_ANIMATE_REVEAL),
1328-
mCurAnimationTime);
1327+
mAnimLastTimeNanos = System.nanoTime();
1328+
mChoreographer.removeCallbacks(Choreographer.CALLBACK_ANIMATION,
1329+
mAnimationCallback, null);
1330+
mChoreographer.removeCallbacks(Choreographer.CALLBACK_ANIMATION,
1331+
mRevealAnimationCallback, null);
1332+
mChoreographer.postCallback(Choreographer.CALLBACK_ANIMATION,
1333+
mRevealAnimationCallback, null);
13291334
makeExpandedVisible();
13301335
} else {
13311336
// it's open, close it?
13321337
if (mAnimating) {
13331338
mAnimating = false;
1334-
mHandler.removeMessages(MSG_ANIMATE);
1339+
mChoreographer.removeCallbacks(Choreographer.CALLBACK_ANIMATION,
1340+
mAnimationCallback, null);
13351341
}
13361342
updateExpandedViewPos(y + mViewDelta);
13371343
}
@@ -1392,13 +1398,15 @@ void performFling(int y, float vel, boolean always) {
13921398
//Slog.d(TAG, "mAnimY=" + mAnimY + " mAnimVel=" + mAnimVel
13931399
// + " mAnimAccel=" + mAnimAccel);
13941400

1395-
long now = SystemClock.uptimeMillis();
1396-
mAnimLastTime = now;
1397-
mCurAnimationTime = now + ANIM_FRAME_DURATION;
1401+
mAnimLastTimeNanos = System.nanoTime();
13981402
mAnimating = true;
1399-
mHandler.removeMessages(MSG_ANIMATE);
1400-
mHandler.removeMessages(MSG_ANIMATE_REVEAL);
1401-
mHandler.sendMessageAtTime(mHandler.obtainMessage(MSG_ANIMATE), mCurAnimationTime);
1403+
1404+
mChoreographer.removeCallbacks(Choreographer.CALLBACK_ANIMATION,
1405+
mAnimationCallback, null);
1406+
mChoreographer.removeCallbacks(Choreographer.CALLBACK_ANIMATION,
1407+
mRevealAnimationCallback, null);
1408+
mChoreographer.postCallback(Choreographer.CALLBACK_ANIMATION,
1409+
mAnimationCallback, null);
14021410
stopTracking();
14031411
}
14041412

@@ -1780,8 +1788,7 @@ public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
17801788
pw.println(" mAnimating=" + mAnimating
17811789
+ ", mAnimY=" + mAnimY + ", mAnimVel=" + mAnimVel
17821790
+ ", mAnimAccel=" + mAnimAccel);
1783-
pw.println(" mCurAnimationTime=" + mCurAnimationTime
1784-
+ " mAnimLastTime=" + mAnimLastTime);
1791+
pw.println(" mAnimLastTimeNanos=" + mAnimLastTimeNanos);
17851792
pw.println(" mAnimatingReveal=" + mAnimatingReveal
17861793
+ " mViewDelta=" + mViewDelta);
17871794
pw.println(" mDisplayMetrics=" + mDisplayMetrics);

0 commit comments

Comments
 (0)