Merge "Migrate MediaController to use OnBackInvokedDispatcher" into tm-dev
This commit is contained in:
@@ -16,6 +16,7 @@
|
|||||||
|
|
||||||
package android.widget;
|
package android.widget;
|
||||||
|
|
||||||
|
import android.annotation.NonNull;
|
||||||
import android.compat.annotation.UnsupportedAppUsage;
|
import android.compat.annotation.UnsupportedAppUsage;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.res.Resources;
|
import android.content.res.Resources;
|
||||||
@@ -30,10 +31,14 @@ import android.view.LayoutInflater;
|
|||||||
import android.view.MotionEvent;
|
import android.view.MotionEvent;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
|
import android.view.ViewRootImpl;
|
||||||
import android.view.Window;
|
import android.view.Window;
|
||||||
import android.view.WindowManager;
|
import android.view.WindowManager;
|
||||||
import android.view.accessibility.AccessibilityManager;
|
import android.view.accessibility.AccessibilityManager;
|
||||||
import android.widget.SeekBar.OnSeekBarChangeListener;
|
import android.widget.SeekBar.OnSeekBarChangeListener;
|
||||||
|
import android.window.OnBackInvokedCallback;
|
||||||
|
import android.window.OnBackInvokedDispatcher;
|
||||||
|
import android.window.WindowOnBackInvokedDispatcher;
|
||||||
|
|
||||||
import com.android.internal.policy.PhoneWindow;
|
import com.android.internal.policy.PhoneWindow;
|
||||||
|
|
||||||
@@ -115,6 +120,27 @@ public class MediaController extends FrameLayout {
|
|||||||
private CharSequence mPlayDescription;
|
private CharSequence mPlayDescription;
|
||||||
private CharSequence mPauseDescription;
|
private CharSequence mPauseDescription;
|
||||||
private final AccessibilityManager mAccessibilityManager;
|
private final AccessibilityManager mAccessibilityManager;
|
||||||
|
private boolean mBackCallbackRegistered;
|
||||||
|
/** Handles back invocation */
|
||||||
|
private final OnBackInvokedCallback mBackCallback = new OnBackInvokedCallback() {
|
||||||
|
@Override
|
||||||
|
public void onBackInvoked() {
|
||||||
|
hide();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
/** Handles decor view attach state change */
|
||||||
|
private final OnAttachStateChangeListener mAttachStateListener =
|
||||||
|
new OnAttachStateChangeListener() {
|
||||||
|
@Override
|
||||||
|
public void onViewAttachedToWindow(@NonNull View v) {
|
||||||
|
registerOnBackInvokedCallback();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onViewDetachedFromWindow(@NonNull View v) {
|
||||||
|
unregisterOnBackInvokedCallback();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
public MediaController(Context context, AttributeSet attrs) {
|
public MediaController(Context context, AttributeSet attrs) {
|
||||||
super(context, attrs);
|
super(context, attrs);
|
||||||
@@ -151,6 +177,7 @@ public class MediaController extends FrameLayout {
|
|||||||
mWindow.requestFeature(Window.FEATURE_NO_TITLE);
|
mWindow.requestFeature(Window.FEATURE_NO_TITLE);
|
||||||
mDecor = mWindow.getDecorView();
|
mDecor = mWindow.getDecorView();
|
||||||
mDecor.setOnTouchListener(mTouchListener);
|
mDecor.setOnTouchListener(mTouchListener);
|
||||||
|
mDecor.addOnAttachStateChangeListener(mAttachStateListener);
|
||||||
mWindow.setContentView(this);
|
mWindow.setContentView(this);
|
||||||
mWindow.setBackgroundDrawableResource(android.R.color.transparent);
|
mWindow.setBackgroundDrawableResource(android.R.color.transparent);
|
||||||
|
|
||||||
@@ -395,6 +422,7 @@ public class MediaController extends FrameLayout {
|
|||||||
removeCallbacks(mFadeOut);
|
removeCallbacks(mFadeOut);
|
||||||
postDelayed(mFadeOut, timeout);
|
postDelayed(mFadeOut, timeout);
|
||||||
}
|
}
|
||||||
|
registerOnBackInvokedCallback();
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isShowing() {
|
public boolean isShowing() {
|
||||||
@@ -416,6 +444,7 @@ public class MediaController extends FrameLayout {
|
|||||||
Log.w("MediaController", "already removed");
|
Log.w("MediaController", "already removed");
|
||||||
}
|
}
|
||||||
mShowing = false;
|
mShowing = false;
|
||||||
|
unregisterOnBackInvokedCallback();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -718,6 +747,35 @@ public class MediaController extends FrameLayout {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void unregisterOnBackInvokedCallback() {
|
||||||
|
if (!mBackCallbackRegistered) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
ViewRootImpl viewRootImpl = mDecor.getViewRootImpl();
|
||||||
|
if (viewRootImpl != null
|
||||||
|
&& WindowOnBackInvokedDispatcher.isOnBackInvokedCallbackEnabled(
|
||||||
|
viewRootImpl.mContext)) {
|
||||||
|
viewRootImpl.getOnBackInvokedDispatcher()
|
||||||
|
.unregisterOnBackInvokedCallback(mBackCallback);
|
||||||
|
}
|
||||||
|
mBackCallbackRegistered = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void registerOnBackInvokedCallback() {
|
||||||
|
if (mBackCallbackRegistered) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
ViewRootImpl viewRootImpl = mDecor.getViewRootImpl();
|
||||||
|
if (viewRootImpl != null
|
||||||
|
&& WindowOnBackInvokedDispatcher.isOnBackInvokedCallbackEnabled(
|
||||||
|
viewRootImpl.mContext)) {
|
||||||
|
viewRootImpl.getOnBackInvokedDispatcher().registerOnBackInvokedCallback(
|
||||||
|
OnBackInvokedDispatcher.PRIORITY_DEFAULT, mBackCallback);
|
||||||
|
mBackCallbackRegistered = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public interface MediaPlayerControl {
|
public interface MediaPlayerControl {
|
||||||
void start();
|
void start();
|
||||||
void pause();
|
void pause();
|
||||||
|
|||||||
Reference in New Issue
Block a user