Code cleanup

This commit is contained in:
Stefan Schueller 2020-07-05 13:56:17 +02:00
parent ded16379f1
commit a56dc55921
2 changed files with 108 additions and 76 deletions

View File

@ -36,6 +36,7 @@ import android.os.Bundle;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi; import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
@ -72,7 +73,8 @@ public class VideoPlayActivity extends AppCompatActivity {
private static final String TAG = "VideoPlayActivity"; private static final String TAG = "VideoPlayActivity";
private static boolean floatMode = false; static boolean floatMode = false;
private static final int REQUEST_CODE = 101; private static final int REQUEST_CODE = 101;
private BroadcastReceiver receiver; private BroadcastReceiver receiver;
@ -96,21 +98,21 @@ public class VideoPlayActivity extends AppCompatActivity {
remoteAction = new RemoteAction(icon, "play", "stop the media", pendingIntent); remoteAction = new RemoteAction(icon, "play", "stop the media", pendingIntent);
actions.add(remoteAction); actions.add(remoteAction);
assert videoPlayerFragment != null;
if (videoPlayerFragment.isPaused()) { if (videoPlayerFragment.isPaused()) {
Log.e(TAG, "setting actions with play button"); Log.e(TAG, "setting actions with play button");
actionIntent = new Intent(ACTION_PLAY); actionIntent = new Intent(ACTION_PLAY);
pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), REQUEST_CODE, actionIntent, 0); pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), REQUEST_CODE, actionIntent, 0);
icon = Icon.createWithResource(getApplicationContext(), com.google.android.exoplayer2.ui.R.drawable.exo_notification_play); icon = Icon.createWithResource(getApplicationContext(), com.google.android.exoplayer2.ui.R.drawable.exo_notification_play);
remoteAction = new RemoteAction(icon, "play", "play the media", pendingIntent); remoteAction = new RemoteAction(icon, "play", "play the media", pendingIntent);
actions.add(remoteAction);
} else { } else {
Log.e(TAG, "setting actions with pause button"); Log.e(TAG, "setting actions with pause button");
actionIntent = new Intent(ACTION_PAUSE); actionIntent = new Intent(ACTION_PAUSE);
pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), REQUEST_CODE, actionIntent, 0); pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), REQUEST_CODE, actionIntent, 0);
icon = Icon.createWithResource(getApplicationContext(), com.google.android.exoplayer2.ui.R.drawable.exo_notification_pause); icon = Icon.createWithResource(getApplicationContext(), com.google.android.exoplayer2.ui.R.drawable.exo_notification_pause);
remoteAction = new RemoteAction(icon, "pause", "pause the media", pendingIntent); remoteAction = new RemoteAction(icon, "pause", "pause the media", pendingIntent);
actions.add(remoteAction);
} }
actions.add(remoteAction);
//add custom actions to pip window //add custom actions to pip window
@ -125,6 +127,7 @@ public class VideoPlayActivity extends AppCompatActivity {
FragmentManager fragmentManager = getSupportFragmentManager(); FragmentManager fragmentManager = getSupportFragmentManager();
VideoPlayerFragment videoPlayerFragment = (VideoPlayerFragment) fragmentManager.findFragmentById(R.id.video_player_fragment); VideoPlayerFragment videoPlayerFragment = (VideoPlayerFragment) fragmentManager.findFragmentById(R.id.video_player_fragment);
assert videoPlayerFragment != null;
videoPlayerFragment.showControls(false); videoPlayerFragment.showControls(false);
//create custom actions //create custom actions
makePipControls(); makePipControls();
@ -139,6 +142,7 @@ public class VideoPlayActivity extends AppCompatActivity {
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
String action = intent.getAction(); String action = intent.getAction();
assert action != null;
if (action.equals(ACTION_PAUSE)) { if (action.equals(ACTION_PAUSE)) {
videoPlayerFragment.pauseVideo(); videoPlayerFragment.pauseVideo();
makePipControls(); makePipControls();
@ -169,6 +173,7 @@ public class VideoPlayActivity extends AppCompatActivity {
FragmentManager fragmentManager = getSupportFragmentManager(); FragmentManager fragmentManager = getSupportFragmentManager();
VideoPlayerFragment videoPlayerFragment = (VideoPlayerFragment) fragmentManager.findFragmentById(R.id.video_player_fragment); VideoPlayerFragment videoPlayerFragment = (VideoPlayerFragment) fragmentManager.findFragmentById(R.id.video_player_fragment);
assert videoPlayerFragment != null;
videoPlayerFragment.showControls(true); videoPlayerFragment.showControls(true);
if (receiver != null) { if (receiver != null) {
unregisterReceiver(receiver); unregisterReceiver(receiver);
@ -249,7 +254,7 @@ public class VideoPlayActivity extends AppCompatActivity {
} }
@Override @Override
public void onConfigurationChanged(Configuration newConfig) { public void onConfigurationChanged(@NonNull Configuration newConfig) {
Log.v(TAG, "onConfigurationChanged()..."); Log.v(TAG, "onConfigurationChanged()...");
super.onConfigurationChanged(newConfig); super.onConfigurationChanged(newConfig);
@ -271,7 +276,8 @@ public class VideoPlayActivity extends AppCompatActivity {
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) videoPlayerFragment.requireView().getLayoutParams(); RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) videoPlayerFragment.requireView().getLayoutParams();
params.width = FrameLayout.LayoutParams.MATCH_PARENT; params.width = FrameLayout.LayoutParams.MATCH_PARENT;
params.height = isLandscape ? FrameLayout.LayoutParams.MATCH_PARENT : (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 250, getResources().getDisplayMetrics()); params.height = isLandscape ? FrameLayout.LayoutParams.MATCH_PARENT : (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 250, getResources().getDisplayMetrics());
videoPlayerFragment.getView().setLayoutParams(params);
videoPlayerFragment.requireView().setLayoutParams(params);
if (videoMetaFragment != null) { if (videoMetaFragment != null) {
FragmentTransaction transaction = fragmentManager.beginTransaction() FragmentTransaction transaction = fragmentManager.beginTransaction()
@ -338,100 +344,117 @@ public class VideoPlayActivity extends AppCompatActivity {
@SuppressLint("NewApi") @SuppressLint("NewApi")
@Override @Override
public void onUserLeaveHint() { public void onUserLeaveHint() {
Log.v(TAG, "onUserLeaveHint()...");
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this); SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
FragmentManager fragmentManager = getSupportFragmentManager(); FragmentManager fragmentManager = getSupportFragmentManager();
VideoPlayerFragment videoPlayerFragment = (VideoPlayerFragment) fragmentManager.findFragmentById(R.id.video_player_fragment); VideoPlayerFragment videoPlayerFragment = (VideoPlayerFragment) fragmentManager.findFragmentById(R.id.video_player_fragment);
String backgroundBehavior = sharedPref.getString("pref_background_behavior", "backgroundStop");
switch (backgroundBehavior) { String backgroundBehavior = sharedPref.getString("pref_background_behavior", getString(R.string.pref_background_stop_key));
case "backgroundStop":
Log.v(TAG, "stop the video"); assert videoPlayerFragment != null;
videoPlayerFragment.pauseVideo(); assert backgroundBehavior != null;
stopService(new Intent(this, VideoPlayerService.class));
super.onBackPressed(); if (backgroundBehavior.equals(getString(R.string.pref_background_stop_key))) {
break; Log.v(TAG, "stop the video");
case "backgroundAudio":
Log.v(TAG, "play the Audio"); videoPlayerFragment.pauseVideo();
super.onBackPressed(); stopService(new Intent(this, VideoPlayerService.class));
break; super.onBackPressed();
case "backgroundFloat":
Log.v(TAG, "play in floating video"); } else if (backgroundBehavior.equals(getString(R.string.pref_background_audio_key))) {
//canEnterPIPMode makes sure API level is high enough Log.v(TAG, "play the Audio");
if (canEnterPipMode(this)) { super.onBackPressed();
Log.v(TAG, "enabling pip");
enterPipMode(); } else if (backgroundBehavior.equals(getString(R.string.pref_background_float_key))) {
} else { Log.v(TAG, "play in floating video");
Log.v(TAG, "unable to use pip"); //canEnterPIPMode makes sure API level is high enough
} if (canEnterPipMode(this)) {
break; Log.v(TAG, "enabling pip");
enterPipMode();
} else {
Log.v(TAG, "unable to use pip");
}
} else {
// Deal with bad entries from older version
Log.v(TAG, "No setting, fallback");
super.onBackPressed();
} }
Log.v(TAG, "onUserLeaveHint()...");
} }
// @RequiresApi(api = Build.VERSION_CODES.O) // @RequiresApi(api = Build.VERSION_CODES.O)
@SuppressLint("NewApi") @SuppressLint("NewApi")
public void onBackPressed() { public void onBackPressed() {
Log.v(TAG, "onBackPressed()...");
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this); SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
VideoPlayerFragment videoPlayerFragment = (VideoPlayerFragment) VideoPlayerFragment videoPlayerFragment = (VideoPlayerFragment)
getSupportFragmentManager().findFragmentById(R.id.video_player_fragment); getSupportFragmentManager().findFragmentById(R.id.video_player_fragment);
//copying Youtube behavior to have back button exit full screen. assert videoPlayerFragment != null;
// copying Youtube behavior to have back button exit full screen.
if (videoPlayerFragment.getIsFullscreen()) { if (videoPlayerFragment.getIsFullscreen()) {
Log.v(TAG, "exiting full screen"); Log.v(TAG, "exiting full screen");
videoPlayerFragment.fullScreenToggle(); videoPlayerFragment.fullScreenToggle();
return; return;
} }
// pause video if pref is enabled
if (sharedPref.getBoolean("pref_back_pause", true)) { if (sharedPref.getBoolean("pref_back_pause", true)) {
videoPlayerFragment.pauseVideo(); videoPlayerFragment.pauseVideo();
} }
String backgroundBehavior = sharedPref.getString("pref_background_behavior", "backgroundStop"); String backgroundBehavior = sharedPref.getString("pref_background_behavior", getString(R.string.pref_background_stop_key));
assert backgroundBehavior != null;
// Log.v(TAG,"backgroundBehavior: " + backgroundBehavior); if (backgroundBehavior.equals(getString(R.string.pref_background_stop_key))) {
Log.v(TAG, "stop the video");
videoPlayerFragment.pauseVideo();
stopService(new Intent(this, VideoPlayerService.class));
super.onBackPressed();
switch (backgroundBehavior) { } else if (backgroundBehavior.equals(getString(R.string.pref_background_audio_key))) {
case "backgroundStop": Log.v(TAG, "play the Audio");
Log.v(TAG, "stop the video"); super.onBackPressed();
videoPlayerFragment.pauseVideo();
stopService(new Intent(this, VideoPlayerService.class)); } else if (backgroundBehavior.equals(getString(R.string.pref_background_float_key))) {
Log.v(TAG, "play in floating video");
//canEnterPIPMode makes sure API level is high enough
if (canEnterPipMode(this)) {
Log.v(TAG, "enabling pip");
enterPipMode();
//fixes problem where back press doesn't bring up video list after returning from PIP mode
Intent intentSettings = new Intent(this, VideoListActivity.class);
this.startActivity(intentSettings);
} else {
Log.v(TAG, "Unable to enter PIP mode");
super.onBackPressed(); super.onBackPressed();
break; }
case "backgroundAudio":
Log.v(TAG, "play the Audio"); } else {
super.onBackPressed(); // Deal with bad entries from older version
break; Log.v(TAG, "No setting, fallback");
case "backgroundFloat": super.onBackPressed();
Log.v(TAG, "play in floating video");
//canEnterPIPMode makes sure API level is high enough
if (canEnterPipMode(this)) {
Log.v(TAG, "enabling pip");
enterPipMode();
//fixes problem where back press doesn't bring up video list after returning from PIP mode
Intent intentSettings = new Intent(this, VideoListActivity.class);
this.startActivity(intentSettings);
} else {
Log.v(TAG, "Unable to enter PIP mode");
super.onBackPressed();
}
break;
default:
// Deal with bad entries from older version
Log.v(TAG, "No setting, fallback");
super.onBackPressed();
break;
} }
Log.v(TAG, "onBackPressed()...");
} }
public boolean canEnterPipMode(Context context) { public boolean canEnterPipMode(Context context) {
Log.v(TAG, "api version " + Build.VERSION.SDK_INT); Log.v(TAG, "api version " + Build.VERSION.SDK_INT);
if (Build.VERSION.SDK_INT < 28) { if (Build.VERSION.SDK_INT > 27) {
return false; AppOpsManager appOpsManager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
return (AppOpsManager.MODE_ALLOWED == appOpsManager.checkOp(AppOpsManager.OPSTR_PICTURE_IN_PICTURE, android.os.Process.myUid(), context.getPackageName()));
} }
AppOpsManager appOpsManager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE); return false;
return (AppOpsManager.MODE_ALLOWED == appOpsManager.checkOpNoThrow(AppOpsManager.OPSTR_PICTURE_IN_PICTURE, android.os.Process.myUid(), context.getPackageName()));
} }
@RequiresApi(api = Build.VERSION_CODES.O) @RequiresApi(api = Build.VERSION_CODES.O)
@ -452,14 +475,20 @@ public class VideoPlayActivity extends AppCompatActivity {
FragmentManager fragmentManager = getSupportFragmentManager(); FragmentManager fragmentManager = getSupportFragmentManager();
VideoPlayerFragment videoPlayerFragment = (VideoPlayerFragment) fragmentManager.findFragmentById(R.id.video_player_fragment); VideoPlayerFragment videoPlayerFragment = (VideoPlayerFragment) fragmentManager.findFragmentById(R.id.video_player_fragment);
if (isInPictureInPictureMode) { if (videoPlayerFragment != null) {
changedToPipMode();
Log.v(TAG, "switched to pip "); if (isInPictureInPictureMode) {
videoPlayerFragment.useController(false); changedToPipMode();
Log.v(TAG, "switched to pip ");
videoPlayerFragment.useController(false);
} else {
changedToNormalMode();
Log.v(TAG, "switched to normal");
videoPlayerFragment.useController(true);
}
} else { } else {
changedToNormalMode(); Log.e(TAG, "videoPlayerFragment is NULL");
Log.v(TAG, "switched to normal");
videoPlayerFragment.useController(true);
} }
} }

View File

@ -1,6 +1,9 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<resources> <resources>
<string name="pref_background_audio_key" translatable="false">backgroundAudio</string>
<string name="pref_background_stop_key" translatable="false">backgroundStop</string>
<string name="pref_background_float_key" translatable="false">backgroundFloat</string>
<string-array name="backgroundBehavior"> <string-array name="backgroundBehavior">
<item>@string/pref_background_audio</item> <item>@string/pref_background_audio</item>
@ -9,9 +12,9 @@
</string-array> </string-array>
<string-array name="backgroundBehaviorValues"> <string-array name="backgroundBehaviorValues">
<item>backgroundAudio</item> <item>@string/pref_background_audio_key</item>
<item>backgroundStop</item> <item>@string/pref_background_stop_key</item>
<item>backgroundFloat</item> <item>@string/pref_background_float_key</item>
</string-array> </string-array>
<string-array name="themeArray"> <string-array name="themeArray">