未验证 提交 c06432da 编写于 作者: A amirh 提交者: GitHub

Delay the removal of the onDrawListener. (#6052)

In Android O removing the ViewTreeObserver.onDrawListener from the
listener call crashes. Instead we post a runnable to remove it.
上级 c23690f0
...@@ -98,9 +98,9 @@ class VirtualDisplayController { ...@@ -98,9 +98,9 @@ class VirtualDisplayController {
embeddedView.addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() { embeddedView.addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() {
@Override @Override
public void onViewAttachedToWindow(View v) { public void onViewAttachedToWindow(View v) {
embeddedView.getViewTreeObserver().addOnDrawListener(new ViewTreeObserver.OnDrawListener() { OneTimeOnDrawListener.schedule(embeddedView, new Runnable() {
@Override @Override
public void onDraw() { public void run() {
// We need some delay here until the frame propagates through the vd surface to to the texture, // We need some delay here until the frame propagates through the vd surface to to the texture,
// 128ms was picked pretty arbitrarily based on trial and error. // 128ms was picked pretty arbitrarily based on trial and error.
// As long as we invoke the runnable after a new frame is available we avoid the scaling jank // As long as we invoke the runnable after a new frame is available we avoid the scaling jank
...@@ -108,7 +108,6 @@ class VirtualDisplayController { ...@@ -108,7 +108,6 @@ class VirtualDisplayController {
// We should ideally run onNewSizeFrameAvailable ASAP to make the embedded view more responsive // We should ideally run onNewSizeFrameAvailable ASAP to make the embedded view more responsive
// following a resize. // following a resize.
embeddedView.postDelayed(onNewSizeFrameAvailable, 128); embeddedView.postDelayed(onNewSizeFrameAvailable, 128);
embeddedView.getViewTreeObserver().removeOnDrawListener(this);
} }
}); });
embeddedView.removeOnAttachStateChangeListener(this); embeddedView.removeOnAttachStateChangeListener(this);
...@@ -135,4 +134,36 @@ class VirtualDisplayController { ...@@ -135,4 +134,36 @@ class VirtualDisplayController {
PlatformView platformView = mPresentation.getView(); PlatformView platformView = mPresentation.getView();
return platformView.getView(); return platformView.getView();
} }
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
static class OneTimeOnDrawListener implements ViewTreeObserver.OnDrawListener {
static void schedule(View view, Runnable runnable) {
OneTimeOnDrawListener listener = new OneTimeOnDrawListener(view, runnable);
view.getViewTreeObserver().addOnDrawListener(listener);
}
final View mView;
Runnable mOnDrawRunnable;
OneTimeOnDrawListener(View view, Runnable onDrawRunnable) {
this.mView = view;
this.mOnDrawRunnable = onDrawRunnable;
}
@Override
public void onDraw() {
if (mOnDrawRunnable == null) {
return;
}
mOnDrawRunnable.run();
mOnDrawRunnable = null;
mView.post(new Runnable() {
@Override
public void run() {
mView.getViewTreeObserver().removeOnDrawListener(OneTimeOnDrawListener.this);
}
});
}
}
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册