Allow to enter PiP automatically

This commit is contained in:
ByteHamster 2018-04-09 19:43:50 +02:00
parent 1831430a6e
commit 64221f7f56
6 changed files with 56 additions and 10 deletions

View File

@ -226,7 +226,7 @@ public abstract class MediaplayerActivity extends CastEnabledActivity implements
@Override @Override
protected void onPause() { protected void onPause() {
if (!supportsAndisInPictureInPictureMode()) { if (!compatIsInPictureInPictureMode()) {
if (controller != null) { if (controller != null) {
controller.reinitServiceIfPaused(); controller.reinitServiceIfPaused();
controller.pause(); controller.pause();
@ -917,7 +917,7 @@ public abstract class MediaplayerActivity extends CastEnabledActivity implements
} }
} }
/* package */ boolean supportsAndisInPictureInPictureMode() { /* package */ boolean compatIsInPictureInPictureMode() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && supportsPictureInPicture()) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && supportsPictureInPicture()) {
return isInPictureInPictureMode(); return isInPictureInPictureMode();
} else { } else {

View File

@ -24,6 +24,7 @@ import android.widget.ProgressBar;
import android.widget.SeekBar; import android.widget.SeekBar;
import de.danoeh.antennapod.R; import de.danoeh.antennapod.R;
import de.danoeh.antennapod.core.feed.MediaType; import de.danoeh.antennapod.core.feed.MediaType;
import de.danoeh.antennapod.core.preferences.UserPreferences;
import de.danoeh.antennapod.core.service.playback.PlaybackService; import de.danoeh.antennapod.core.service.playback.PlaybackService;
import de.danoeh.antennapod.core.service.playback.PlayerStatus; import de.danoeh.antennapod.core.service.playback.PlayerStatus;
import de.danoeh.antennapod.core.util.playback.ExternalMedia; import de.danoeh.antennapod.core.util.playback.ExternalMedia;
@ -101,14 +102,22 @@ public class VideoplayerActivity extends MediaplayerActivity {
@Override @Override
protected void onStop() { protected void onStop() {
super.onStop(); super.onStop();
if (!supportsAndisInPictureInPictureMode()) { if (!compatIsInPictureInPictureMode()) {
videoControlsHider.stop(); videoControlsHider.stop();
} }
} }
@Override
public void onUserLeaveHint () {
if (!compatIsInPictureInPictureMode() && UserPreferences.getVideoBackgroundBehavior()
== UserPreferences.VideoBackgroundBehavior.PICTURE_IN_PICTURE) {
compatEnterPictureInPicture();
}
}
@Override @Override
protected void onPause() { protected void onPause() {
if (!supportsAndisInPictureInPictureMode()) { if (!compatIsInPictureInPictureMode()) {
if (controller != null && controller.getStatus() == PlayerStatus.PLAYING) { if (controller != null && controller.getStatus() == PlayerStatus.PLAYING) {
controller.pause(); controller.pause();
} }
@ -191,7 +200,7 @@ public class VideoplayerActivity extends MediaplayerActivity {
private final View.OnTouchListener onVideoviewTouched = (v, event) -> { private final View.OnTouchListener onVideoviewTouched = (v, event) -> {
if (event.getAction() == MotionEvent.ACTION_DOWN) { if (event.getAction() == MotionEvent.ACTION_DOWN) {
if (supportsAndisInPictureInPictureMode()) { if (compatIsInPictureInPictureMode()) {
return true; return true;
} }
videoControlsHider.stop(); videoControlsHider.stop();
@ -390,16 +399,20 @@ public class VideoplayerActivity extends MediaplayerActivity {
@Override @Override
public boolean onOptionsItemSelected(MenuItem item) { public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.player_go_to_picture_in_picture) { if (item.getItemId() == R.id.player_go_to_picture_in_picture) {
if (supportsPictureInPicture() && Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { compatEnterPictureInPicture();
getSupportActionBar().hide();
hideVideoControls(false);
enterPictureInPictureMode();
}
return true; return true;
} }
return super.onOptionsItemSelected(item); return super.onOptionsItemSelected(item);
} }
private void compatEnterPictureInPicture() {
if (supportsPictureInPicture() && Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
getSupportActionBar().hide();
hideVideoControls(false);
enterPictureInPictureMode();
}
}
private static class VideoControlsHider extends Handler { private static class VideoControlsHider extends Handler {
private static final int DELAY = 2500; private static final int DELAY = 2500;

View File

@ -171,6 +171,14 @@
android:key="prefResumeAfterCall" android:key="prefResumeAfterCall"
android:summary="@string/pref_resumeAfterCall_sum" android:summary="@string/pref_resumeAfterCall_sum"
android:title="@string/pref_resumeAfterCall_title"/> android:title="@string/pref_resumeAfterCall_title"/>
<com.afollestad.materialdialogs.prefs.MaterialListPreference
android:defaultValue="stop"
android:entries="@array/video_background_behavior_options"
android:entryValues="@array/video_background_behavior_values"
android:key="prefVideoBehavior"
android:summary="@string/pref_videoBehavior_sum"
android:title="@string/pref_videoBehavior_title"
app:useStockLayout="true"/>
</PreferenceCategory> </PreferenceCategory>
<PreferenceCategory android:title="@string/network_pref"> <PreferenceCategory android:title="@string/network_pref">

View File

@ -74,6 +74,7 @@ public class UserPreferences {
private static final String PREF_PLAYBACK_SPEED_ARRAY = "prefPlaybackSpeedArray"; private static final String PREF_PLAYBACK_SPEED_ARRAY = "prefPlaybackSpeedArray";
private static final String PREF_PAUSE_PLAYBACK_FOR_FOCUS_LOSS = "prefPauseForFocusLoss"; private static final String PREF_PAUSE_PLAYBACK_FOR_FOCUS_LOSS = "prefPauseForFocusLoss";
private static final String PREF_RESUME_AFTER_CALL = "prefResumeAfterCall"; private static final String PREF_RESUME_AFTER_CALL = "prefResumeAfterCall";
private static final String PREF_VIDEO_BEHAVIOR = "prefVideoBehavior";
// Network // Network
private static final String PREF_ENQUEUE_DOWNLOADED = "prefEnqueueDownloaded"; private static final String PREF_ENQUEUE_DOWNLOADED = "prefEnqueueDownloaded";
@ -661,6 +662,13 @@ public class UserPreferences {
.apply(); .apply();
} }
public static VideoBackgroundBehavior getVideoBackgroundBehavior() {
switch (prefs.getString(PREF_VIDEO_BEHAVIOR, "stop")) {
case "stop": return VideoBackgroundBehavior.STOP;
case "pip": return VideoBackgroundBehavior.PICTURE_IN_PICTURE;
default: return VideoBackgroundBehavior.STOP;
}
}
public static EpisodeCleanupAlgorithm getEpisodeCleanupAlgorithm() { public static EpisodeCleanupAlgorithm getEpisodeCleanupAlgorithm() {
int cleanupValue = Integer.parseInt(prefs.getString(PREF_EPISODE_CLEANUP, "-1")); int cleanupValue = Integer.parseInt(prefs.getString(PREF_EPISODE_CLEANUP, "-1"));
@ -839,4 +847,8 @@ public class UserPreferences {
public static boolean isCastEnabled() { public static boolean isCastEnabled() {
return prefs.getBoolean(PREF_CAST_ENABLED, false); return prefs.getBoolean(PREF_CAST_ENABLED, false);
} }
public enum VideoBackgroundBehavior {
STOP, PICTURE_IN_PICTURE
}
} }

View File

@ -224,4 +224,14 @@
<item>@string/fast_forward_label</item> <item>@string/fast_forward_label</item>
<item>@string/skip_episode_label</item> <item>@string/skip_episode_label</item>
</string-array> </string-array>
<string-array name="video_background_behavior_options">
<item>@string/stop_playback</item>
<item>@string/player_go_to_picture_in_picture</item>
</string-array>
<string-array name="video_background_behavior_values">
<item>stop</item>
<item>pip</item>
</string-array>
</resources> </resources>

View File

@ -440,6 +440,9 @@
<string name="pref_cast_message_free_flavor">Chromecast requires third party proprietary libraries that are disabled in this version of AntennaPod</string> <string name="pref_cast_message_free_flavor">Chromecast requires third party proprietary libraries that are disabled in this version of AntennaPod</string>
<string name="pref_enqueue_downloaded_title">Enqueue Downloaded</string> <string name="pref_enqueue_downloaded_title">Enqueue Downloaded</string>
<string name="pref_enqueue_downloaded_summary">Add downloaded episodes to the queue</string> <string name="pref_enqueue_downloaded_summary">Add downloaded episodes to the queue</string>
<string name="pref_videoBehavior_title">Video behavior</string>
<string name="pref_videoBehavior_sum">Behavior when leaving video playback</string>
<string name="stop_playback">Stop playback</string>
<!-- Auto-Flattr dialog --> <!-- Auto-Flattr dialog -->
<string name="auto_flattr_enable">Enable automatic flattring</string> <string name="auto_flattr_enable">Enable automatic flattring</string>