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,23 +344,30 @@ 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":
assert videoPlayerFragment != null;
assert backgroundBehavior != null;
if (backgroundBehavior.equals(getString(R.string.pref_background_stop_key))) {
Log.v(TAG, "stop the video"); Log.v(TAG, "stop the video");
videoPlayerFragment.pauseVideo(); videoPlayerFragment.pauseVideo();
stopService(new Intent(this, VideoPlayerService.class)); stopService(new Intent(this, VideoPlayerService.class));
super.onBackPressed(); super.onBackPressed();
break;
case "backgroundAudio": } else if (backgroundBehavior.equals(getString(R.string.pref_background_audio_key))) {
Log.v(TAG, "play the Audio"); Log.v(TAG, "play the Audio");
super.onBackPressed(); super.onBackPressed();
break;
case "backgroundFloat": } else if (backgroundBehavior.equals(getString(R.string.pref_background_float_key))) {
Log.v(TAG, "play in floating video"); Log.v(TAG, "play in floating video");
//canEnterPIPMode makes sure API level is high enough //canEnterPIPMode makes sure API level is high enough
if (canEnterPipMode(this)) { if (canEnterPipMode(this)) {
@ -363,46 +376,55 @@ public class VideoPlayActivity extends AppCompatActivity {
} else { } else {
Log.v(TAG, "unable to use pip"); Log.v(TAG, "unable to use pip");
} }
break;
} 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);
assert videoPlayerFragment != null;
// copying Youtube behavior to have back button exit full screen. // 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))) {
switch (backgroundBehavior) {
case "backgroundStop":
Log.v(TAG, "stop the video"); Log.v(TAG, "stop the video");
videoPlayerFragment.pauseVideo(); videoPlayerFragment.pauseVideo();
stopService(new Intent(this, VideoPlayerService.class)); stopService(new Intent(this, VideoPlayerService.class));
super.onBackPressed(); super.onBackPressed();
break;
case "backgroundAudio": } else if (backgroundBehavior.equals(getString(R.string.pref_background_audio_key))) {
Log.v(TAG, "play the Audio"); Log.v(TAG, "play the Audio");
super.onBackPressed(); super.onBackPressed();
break;
case "backgroundFloat": } else if (backgroundBehavior.equals(getString(R.string.pref_background_float_key))) {
Log.v(TAG, "play in floating video"); Log.v(TAG, "play in floating video");
//canEnterPIPMode makes sure API level is high enough //canEnterPIPMode makes sure API level is high enough
if (canEnterPipMode(this)) { if (canEnterPipMode(this)) {
@ -415,23 +437,24 @@ public class VideoPlayActivity extends AppCompatActivity {
Log.v(TAG, "Unable to enter PIP mode"); Log.v(TAG, "Unable to enter PIP mode");
super.onBackPressed(); super.onBackPressed();
} }
break;
default: } else {
// Deal with bad entries from older version // Deal with bad entries from older version
Log.v(TAG, "No setting, fallback"); Log.v(TAG, "No setting, fallback");
super.onBackPressed(); 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); AppOpsManager appOpsManager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
return (AppOpsManager.MODE_ALLOWED == appOpsManager.checkOpNoThrow(AppOpsManager.OPSTR_PICTURE_IN_PICTURE, android.os.Process.myUid(), context.getPackageName())); return (AppOpsManager.MODE_ALLOWED == appOpsManager.checkOp(AppOpsManager.OPSTR_PICTURE_IN_PICTURE, android.os.Process.myUid(), context.getPackageName()));
}
return false;
} }
@RequiresApi(api = Build.VERSION_CODES.O) @RequiresApi(api = Build.VERSION_CODES.O)
@ -452,6 +475,8 @@ 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 (videoPlayerFragment != null) {
if (isInPictureInPictureMode) { if (isInPictureInPictureMode) {
changedToPipMode(); changedToPipMode();
Log.v(TAG, "switched to pip "); Log.v(TAG, "switched to pip ");
@ -461,6 +486,10 @@ public class VideoPlayActivity extends AppCompatActivity {
Log.v(TAG, "switched to normal"); Log.v(TAG, "switched to normal");
videoPlayerFragment.useController(true); videoPlayerFragment.useController(true);
} }
} else {
Log.e(TAG, "videoPlayerFragment is NULL");
}
} }
} }

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">