Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 50 additions & 7 deletions app/src/main/java/com/brouken/player/PlayerActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import android.window.OnBackInvokedCallback;
import android.window.OnBackInvokedDispatcher;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
Expand Down Expand Up @@ -214,6 +216,8 @@ public class PlayerActivity extends Activity {
}
};

final Object onBackInvokedCallback = createOnBackInvokedCallback();

@RequiresApi(api = Build.VERSION_CODES.O)
@Override
protected void onCreate(Bundle savedInstanceState) {
Expand Down Expand Up @@ -684,6 +688,21 @@ public void onTargetClick(TapTargetView view) {
errorToShow = null;
}
}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU && isTvBox) {
if (visibility == View.VISIBLE) {
if (player != null && player.isPlaying()) {
//noinspection DataFlowIssue
getOnBackInvokedDispatcher().registerOnBackInvokedCallback(
OnBackInvokedDispatcher.PRIORITY_DEFAULT,
(OnBackInvokedCallback) onBackInvokedCallback
);
}
} else {
//noinspection DataFlowIssue
getOnBackInvokedDispatcher().unregisterOnBackInvokedCallback((OnBackInvokedCallback) onBackInvokedCallback);
}
}
}
});

Expand Down Expand Up @@ -715,6 +734,18 @@ public void onAnimationEnd(Animator animation) {
Utils.scanMediaStorage(this);
}
}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.BAKLAVA) {
getOnBackInvokedDispatcher().registerOnBackInvokedCallback(
OnBackInvokedDispatcher.PRIORITY_SYSTEM_NAVIGATION_OBSERVER,
() -> restorePlayStateAllowed = false
);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
getOnBackInvokedDispatcher().registerOnBackInvokedCallback(
OnBackInvokedDispatcher.PRIORITY_DEFAULT,
this::onBackPressed
);
}
}

@Override
Expand Down Expand Up @@ -823,7 +854,6 @@ protected void onNewIntent(Intent intent) {
}
}

@SuppressLint("GestureBackNavigation")
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
Expand Down Expand Up @@ -907,13 +937,18 @@ public boolean onKeyDown(int keyCode, KeyEvent event) {
return true;
}
break;
//noinspection "GestureBackNavigation"
case KeyEvent.KEYCODE_BACK:
if (isTvBox) {
if (controllerVisible && player != null && player.isPlaying()) {
playerView.hideController();
return true;
} else {
onBackPressed();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
return super.onKeyDown(keyCode, event);
} else {
if (isTvBox) {
if (controllerVisible && player != null && player.isPlaying()) {
playerView.hideController();
return true;
} else {
onBackPressed();
}
}
}
break;
Expand Down Expand Up @@ -2319,4 +2354,12 @@ private void updateButtonRotation() {
}
}
}

private Object createOnBackInvokedCallback() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
return (OnBackInvokedCallback) () -> playerView.hideController();
} else {
return null;
}
}
}