From ae058167b6b58976f85f6793d0246bc1f5c7108c Mon Sep 17 00:00:00 2001 From: dhk2 Date: Wed, 1 Jul 2020 09:04:20 -0700 Subject: [PATCH] fixing duplicate controlelrs --- .../peertube/activity/VideoPlayActivity.java | 90 ++++++++++++++++--- .../fragment/VideoPlayerFragment.java | 5 +- .../schueller/peertube/helper/Constants.java | 1 + .../peertube/service/VideoPlayerService.java | 9 +- 4 files changed, 93 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/net/schueller/peertube/activity/VideoPlayActivity.java b/app/src/main/java/net/schueller/peertube/activity/VideoPlayActivity.java index 7015118..ba64f12 100644 --- a/app/src/main/java/net/schueller/peertube/activity/VideoPlayActivity.java +++ b/app/src/main/java/net/schueller/peertube/activity/VideoPlayActivity.java @@ -21,11 +21,16 @@ package net.schueller.peertube.activity; import android.annotation.SuppressLint; import android.app.AppOpsManager; +import android.app.PendingIntent; import android.app.PictureInPictureParams; +import android.app.RemoteAction; +import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; +import android.content.IntentFilter; import android.content.SharedPreferences; import android.content.res.Configuration; +import android.graphics.drawable.Icon; import android.os.Build; import android.os.Bundle; @@ -49,12 +54,16 @@ import net.schueller.peertube.fragment.VideoMetaDataFragment; import net.schueller.peertube.fragment.VideoPlayerFragment; import net.schueller.peertube.service.VideoPlayerService; -import java.util.Objects; +import java.util.ArrayList; import androidx.fragment.app.FragmentManager; //import static net.schueller.peertube.helper.Constants.BACKGROUND_PLAY_PREF_KEY; +import static com.google.android.exoplayer2.ui.PlayerNotificationManager.ACTION_PAUSE; +import static com.google.android.exoplayer2.ui.PlayerNotificationManager.ACTION_PLAY; +import static com.google.android.exoplayer2.ui.PlayerNotificationManager.ACTION_STOP; +import static net.schueller.peertube.helper.Constants.BACKGROUND_AUDIO; import static net.schueller.peertube.helper.Constants.DEFAULT_THEME; import static net.schueller.peertube.helper.Constants.THEME_PREF_KEY; @@ -62,7 +71,8 @@ public class VideoPlayActivity extends AppCompatActivity { private static final String TAG = "VideoPlayActivity"; private static boolean floatMode = false; - + private static final int REQUEST_CODE = 101; + private BroadcastReceiver receiver; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -112,12 +122,6 @@ public class VideoPlayActivity extends AppCompatActivity { VideoPlayerFragment videoPlayerFragment = (VideoPlayerFragment) getSupportFragmentManager().findFragmentById(R.id.video_player_fragment); assert videoPlayerFragment != null; - Boolean killFloat=intent.getBooleanExtra("killFloat",false); - Log.e(TAG,killFloat.toString()+" "+floatMode); - if (killFloat && floatMode){ - finish(); - return; - } String videoUuid = intent.getStringExtra(VideoListActivity.EXTRA_VIDEOID); Log.v(TAG, "new intent click: " + videoUuid +" is trying to replace: "+videoPlayerFragment.getVideoUuid()); assert videoPlayerFragment != null; @@ -167,7 +171,7 @@ public class VideoPlayActivity extends AppCompatActivity { if (isLandscape) { assert videoPlayerFragment != null; - RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) Objects.requireNonNull(videoPlayerFragment.getView()).getLayoutParams(); + RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) videoPlayerFragment.requireView().getLayoutParams(); params.width = FrameLayout.LayoutParams.MATCH_PARENT; params.height = FrameLayout.LayoutParams.MATCH_PARENT; videoPlayerFragment.getView().setLayoutParams(params); @@ -182,7 +186,7 @@ public class VideoPlayActivity extends AppCompatActivity { } else { assert videoPlayerFragment != null; - RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) Objects.requireNonNull(videoPlayerFragment.getView()).getLayoutParams(); + RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) videoPlayerFragment.requireView().getLayoutParams(); params.width = FrameLayout.LayoutParams.MATCH_PARENT; params.height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 250, getResources().getDisplayMetrics()); videoPlayerFragment.getView().setLayoutParams(params); @@ -357,14 +361,80 @@ public class VideoPlayActivity extends AppCompatActivity { enterPictureInPictureMode(mParams); } + @RequiresApi(api = Build.VERSION_CODES.O) @Override public void onPictureInPictureModeChanged (boolean isInPictureInPictureMode, Configuration newConfig) { + VideoPlayerFragment videoPlayerFragment = (VideoPlayerFragment) + getSupportFragmentManager().findFragmentById(R.id.video_player_fragment); + if (isInPictureInPictureMode) { + videoPlayerFragment.showControls(false); + //create custom actions + setActions(""); + + //setup receiver to handle customer actions + IntentFilter filter = new IntentFilter(); + filter.addAction(ACTION_STOP); + filter.addAction(ACTION_PAUSE); + filter.addAction(ACTION_PLAY); + filter.addAction((BACKGROUND_AUDIO)); + receiver = new BroadcastReceiver() { + @Override + public void onReceive(Context context, Intent intent) { + String action = intent.getAction(); + if (action.equals(ACTION_PAUSE)){ + videoPlayerFragment.pauseVideo(); + } + if (action.equals(ACTION_PLAY)){ + videoPlayerFragment.pauseToggle(); + } + if (action.equals(BACKGROUND_AUDIO)) { + unregisterReceiver(receiver); + finish(); + } + if (action.equals(ACTION_STOP)){ + unregisterReceiver(receiver); + finishAndRemoveTask(); + } + } + }; + registerReceiver(receiver, filter); + Log.v(TAG,"switched to pip "); floatMode=true; } else { + videoPlayerFragment.showControls(true); + if (receiver != null) { + unregisterReceiver(receiver); + } Log.v(TAG,"switched to normal"); floatMode=false; } } + + @RequiresApi(api = Build.VERSION_CODES.O) + public void setActions(String actionCommand) { + + ArrayList actions = new ArrayList<>(); + + Intent actionIntent = new Intent(BACKGROUND_AUDIO); + PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), REQUEST_CODE, actionIntent, 0); + Icon icon = Icon.createWithResource(getApplicationContext(), android.R.drawable.stat_sys_speakerphone); + RemoteAction remoteAction = new RemoteAction(icon, "close pip", "from pip window custom command", pendingIntent); + actions.add(remoteAction); + + actionIntent = new Intent(ACTION_STOP); + pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), REQUEST_CODE, actionIntent, 0); + icon = Icon.createWithResource(getApplicationContext(), com.google.android.exoplayer2.ui.R.drawable.exo_notification_stop); + remoteAction = new RemoteAction(icon, "play", "stop the media", pendingIntent); + actions.add(remoteAction); + + //add custom actions to pip window + PictureInPictureParams params = + new PictureInPictureParams.Builder() + .setActions(actions) + .build(); + setPictureInPictureParams(params); + + } } diff --git a/app/src/main/java/net/schueller/peertube/fragment/VideoPlayerFragment.java b/app/src/main/java/net/schueller/peertube/fragment/VideoPlayerFragment.java index a37af1c..ab0e4d1 100644 --- a/app/src/main/java/net/schueller/peertube/fragment/VideoPlayerFragment.java +++ b/app/src/main/java/net/schueller/peertube/fragment/VideoPlayerFragment.java @@ -264,10 +264,13 @@ public class VideoPlayerFragment extends Fragment implements VideoRendererEventL mService.player.setPlayWhenReady(!mService.player.getPlayWhenReady()); } } + public void showControls(boolean value){ + simpleExoPlayerView.setUseController(value); + } public void stopVideo() { if (mBound) { - Objects.requireNonNull(getContext()).unbindService(mConnection); + requireContext().unbindService(mConnection); mBound = false; } } diff --git a/app/src/main/java/net/schueller/peertube/helper/Constants.java b/app/src/main/java/net/schueller/peertube/helper/Constants.java index 729377f..0e87a3f 100644 --- a/app/src/main/java/net/schueller/peertube/helper/Constants.java +++ b/app/src/main/java/net/schueller/peertube/helper/Constants.java @@ -21,4 +21,5 @@ public class Constants { public static final String THEME_PREF_KEY = "pref_theme"; public static final String DEFAULT_THEME = "AppTheme.BLUE"; public static final String BACKGROUND_PLAY_PREF_KEY = "pref_background_play"; + public static final String BACKGROUND_AUDIO = "BACKGROUND_AUDIO"; } diff --git a/app/src/main/java/net/schueller/peertube/service/VideoPlayerService.java b/app/src/main/java/net/schueller/peertube/service/VideoPlayerService.java index 6d9c9f9..a601af4 100644 --- a/app/src/main/java/net/schueller/peertube/service/VideoPlayerService.java +++ b/app/src/main/java/net/schueller/peertube/service/VideoPlayerService.java @@ -64,6 +64,7 @@ import net.schueller.peertube.model.Video; import static android.media.session.PlaybackState.ACTION_PAUSE; import static android.media.session.PlaybackState.ACTION_PLAY; +import static com.google.android.exoplayer2.ui.PlayerNotificationManager.ACTION_STOP; import static net.schueller.peertube.activity.VideoListActivity.EXTRA_VIDEOID; public class VideoPlayerService extends Service { @@ -251,12 +252,18 @@ public class VideoPlayerService extends Service { @Override public void onNotificationCancelled(int notificationId) { Log.v(TAG, "onNotificationCancelled..."); + stopForeground(true); + Intent killFloat = new Intent(ACTION_STOP); + sendBroadcast(killFloat); + /* + Intent killFloat = new Intent(BROADCAST_ACTION); Intent killFloatingWindow = new Intent(getApplicationContext(),VideoPlayActivity.class); killFloatingWindow.putExtra("killFloat",true); - killFloatingWindow.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + startActivity(killFloatingWindow); // TODO: only kill the notification if we no longer have a bound activity stopForeground(true); + */ } } );