From 52e64b6d854b265c7a9a2a210b4a17de712d54b3 Mon Sep 17 00:00:00 2001 From: Thomas Date: Fri, 16 Oct 2020 17:25:52 +0200 Subject: [PATCH] Fix issue #9 --- .../fedilab/fedilabtube/PeertubeActivity.java | 53 +++++++++++++++++-- 1 file changed, 50 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/app/fedilab/fedilabtube/PeertubeActivity.java b/app/src/main/java/app/fedilab/fedilabtube/PeertubeActivity.java index 994fe4e..65125f2 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/PeertubeActivity.java +++ b/app/src/main/java/app/fedilab/fedilabtube/PeertubeActivity.java @@ -18,7 +18,10 @@ import android.Manifest; import android.annotation.SuppressLint; import android.app.Activity; import android.app.Dialog; +import android.content.BroadcastReceiver; +import android.content.Context; import android.content.Intent; +import android.content.IntentFilter; import android.content.SharedPreferences; import android.content.pm.ActivityInfo; import android.content.pm.PackageManager; @@ -154,6 +157,8 @@ public class PeertubeActivity extends AppCompatActivity implements CommentListAd private boolean sepiaSearch; private ActivityPeertubeBinding binding; private List commentsThread; + private BroadcastReceiver mPowerKeyReceiver = null; + public static void hideKeyboard(Activity activity) { if (activity != null && activity.getWindow() != null) { activity.getWindow().getDecorView(); @@ -162,6 +167,7 @@ public class PeertubeActivity extends AppCompatActivity implements CommentListAd imm.hideSoftInputFromWindow(activity.getWindow().getDecorView().getWindowToken(), 0); } } + private boolean isPlayInMinimized; @Override protected void onCreate(Bundle savedInstanceState) { @@ -193,7 +199,7 @@ public class PeertubeActivity extends AppCompatActivity implements CommentListAd ); binding.videoContainer.setLayoutParams(param); } - + isPlayInMinimized = false; if (getSupportActionBar() != null) getSupportActionBar().setDisplayHomeAsUpEnabled(true); @@ -297,6 +303,7 @@ public class PeertubeActivity extends AppCompatActivity implements CommentListAd }else { playVideo(); } + registBroadcastReceiver(); } @@ -830,6 +837,7 @@ public class PeertubeActivity extends AppCompatActivity implements CommentListAd player.setPlayWhenReady(false); player.release(); } + unregisterReceiver(); if (fullScreenDialog != null && fullScreenDialog.isShowing()) { fullScreenDialog.dismiss(); } @@ -838,11 +846,48 @@ public class PeertubeActivity extends AppCompatActivity implements CommentListAd @Override protected void onPause() { super.onPause(); - if (player != null && !playInMinimized) { + if (player != null && !isPlayInMinimized) { player.setPlayWhenReady(false); } } + private void registBroadcastReceiver() { + final IntentFilter theFilter = new IntentFilter(); + theFilter.addAction(Intent.ACTION_SCREEN_ON); + theFilter.addAction(Intent.ACTION_SCREEN_OFF); + + mPowerKeyReceiver = new BroadcastReceiver() { + @Override + public void onReceive(Context context, Intent intent) { + String strAction = intent.getAction(); + if (strAction.equals(Intent.ACTION_SCREEN_OFF) || strAction.equals(Intent.ACTION_SCREEN_ON)) { + if (player != null && isPlayInMinimized) { + player.setPlayWhenReady(!strAction.equals(Intent.ACTION_SCREEN_OFF)); + } + } + } + }; + + getApplicationContext().registerReceiver(mPowerKeyReceiver, theFilter); + } + + private void unregisterReceiver() { + int apiLevel = Build.VERSION.SDK_INT; + + if (apiLevel >= 7) { + try { + getApplicationContext().unregisterReceiver(mPowerKeyReceiver); + } + catch (IllegalArgumentException e) { + mPowerKeyReceiver = null; + } + } + else { + getApplicationContext().unregisterReceiver(mPowerKeyReceiver); + mPowerKeyReceiver = null; + } + } + @RequiresApi(api = Build.VERSION_CODES.N) @Override @@ -852,6 +897,7 @@ public class PeertubeActivity extends AppCompatActivity implements CommentListAd private void enterVideoMode() { if (playInMinimized && Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && player != null) { + isPlayInMinimized = true; MediaSessionCompat mediaSession = new MediaSessionCompat(this, getPackageName()); MediaSessionConnector mediaSessionConnector = new MediaSessionConnector(mediaSession); mediaSessionConnector.setPlayer(player); @@ -885,6 +931,7 @@ public class PeertubeActivity extends AppCompatActivity implements CommentListAd } else { setFullscreen(FullScreenMediaController.fullscreen.OFF); if (onStopCalled) { + isPlayInMinimized = false; finishAndRemoveTask(); } } @@ -900,7 +947,7 @@ public class PeertubeActivity extends AppCompatActivity implements CommentListAd public void onResume() { super.onResume(); onStopCalled = false; - if (player != null && !playInMinimized) { + if (player != null && !player.isPlaying()) { player.setPlayWhenReady(true); } }