From 118c2175f572ad96f0aba716e350ec2de0b61c68 Mon Sep 17 00:00:00 2001 From: Thomas Date: Sat, 19 Sep 2020 14:56:10 +0200 Subject: [PATCH] Play Minimized video when app in background --- app/build.gradle | 1 + app/src/acad/res/values/strings.xml | 10 ++++ app/src/full/res/values/strings.xml | 10 ++++ app/src/main/AndroidManifest.xml | 8 +++- .../fedilab/fedilabtube/PeertubeActivity.java | 46 +++++++++++++++++-- .../fragment/SettingsFragment.java | 45 +++++++++++------- .../ic_baseline_featured_video_24.xml | 10 ++++ app/src/main/res/xml/main_preferences.xml | 6 +++ 8 files changed, 114 insertions(+), 22 deletions(-) create mode 100644 app/src/main/res/drawable/ic_baseline_featured_video_24.xml diff --git a/app/build.gradle b/app/build.gradle index 513bf5c..0874140 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -110,4 +110,5 @@ dependencies { implementation "net.gotev:uploadservice:3.5.2" implementation "net.gotev:uploadservice-okhttp:3.5.2" implementation "com.google.code.gson:gson:2.8.6" + } \ No newline at end of file diff --git a/app/src/acad/res/values/strings.xml b/app/src/acad/res/values/strings.xml index c74c3a5..a5ded81 100644 --- a/app/src/acad/res/values/strings.xml +++ b/app/src/acad/res/values/strings.xml @@ -193,6 +193,7 @@ set_video_mode_choice + set_video_minimize_choice Permet de changer le mode de lecture pour les vidéos (normal, streaming ou via un navigateur). Créer un compte Aperçu @@ -238,4 +239,13 @@ Mise à jour des informations Liste des comptes + + Pause + Lecture + Réduire + Retour rapide + Avance rapide + + Réduire la taille des vidéos + Réduit la taille des vidéos quand l\'application est en arrière plan (Android N+) diff --git a/app/src/full/res/values/strings.xml b/app/src/full/res/values/strings.xml index d69ea1b..e36172f 100644 --- a/app/src/full/res/values/strings.xml +++ b/app/src/full/res/values/strings.xml @@ -135,6 +135,8 @@ Edit set_video_mode_choice + set_video_minimize_choice + Allows to change mode for playing videos (default, streaming or via a browser). @@ -255,4 +257,12 @@ Add an account List of accounts + Pause + Play + Minimize + Fast rewind + Fast forward + + Minimize videos size + Minize videos size when the app is in background (Android N+) \ No newline at end of file diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 9b64f31..e19225c 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -18,6 +18,7 @@ + android:configChanges="screenSize|smallestScreenSize|screenLayout|orientation" + android:label="@string/app_name" + android:resizeableActivity="true" + android:supportsPictureInPicture="true" + tools:targetApi="n" /> playlistForVideo; private List playlists; private PlaylistsVM playlistsViewModel; + private boolean playInMinimized; + private boolean onStopCalled; public static void hideKeyboard(Activity activity) { if (activity != null && activity.getWindow() != null) { @@ -145,6 +148,7 @@ public class PeertubeActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE); fullscreen = FullScreenMediaController.fullscreen.OFF; fullScreenMode = false; @@ -169,7 +173,11 @@ public class PeertubeActivity extends AppCompatActivity { peertube_playlist = findViewById(R.id.peertube_playlist); send = findViewById(R.id.send); - + playInMinimized = sharedpreferences.getBoolean(getString(R.string.set_video_minimize_choice), true); + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N + && !getPackageManager().hasSystemFeature(PackageManager.FEATURE_PICTURE_IN_PICTURE)) { + playInMinimized = false; + } peertube_playlist.setVisibility(View.VISIBLE); peertube_bookmark.setVisibility(View.GONE); @@ -674,22 +682,52 @@ public class PeertubeActivity extends AppCompatActivity { @Override public void onDestroy() { super.onDestroy(); - if (player != null) + if (player != null) { + player.setPlayWhenReady(false); player.release(); + } } @Override protected void onPause() { super.onPause(); - if (player != null) { + if (player != null && !playInMinimized) { player.setPlayWhenReady(false); } } + + @RequiresApi(api = Build.VERSION_CODES.N) + @Override + public void onUserLeaveHint() { + if (playInMinimized) { + enterPictureInPictureMode(); + } + } + + @Override + public void onPictureInPictureModeChanged(boolean isInPictureInPictureMode, Configuration newConfig) { + if (isInPictureInPictureMode) { + setFullscreen(FullScreenMediaController.fullscreen.ON); + } else { + setFullscreen(FullScreenMediaController.fullscreen.OFF); + if (onStopCalled) { + finish(); + } + } + } + + @Override + public void onStop() { + super.onStop(); + onStopCalled = true; + } + @Override public void onResume() { super.onResume(); - if (player != null) { + onStopCalled = false; + if (player != null && !playInMinimized) { player.setPlayWhenReady(true); } } diff --git a/app/src/main/java/app/fedilab/fedilabtube/fragment/SettingsFragment.java b/app/src/main/java/app/fedilab/fedilabtube/fragment/SettingsFragment.java index 31139fc..470c0a3 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/fragment/SettingsFragment.java +++ b/app/src/main/java/app/fedilab/fedilabtube/fragment/SettingsFragment.java @@ -8,6 +8,7 @@ import androidx.fragment.app.FragmentActivity; import androidx.preference.ListPreference; import androidx.preference.PreferenceFragmentCompat; import androidx.preference.PreferenceScreen; +import androidx.preference.SwitchPreference; import java.util.Arrays; import java.util.List; @@ -57,24 +58,33 @@ public class SettingsFragment extends PreferenceFragmentCompat implements Shared @Override public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { - if (key.compareTo(getString(R.string.set_video_mode_choice)) == 0) { - ListPreference set_video_mode_choice = findPreference(getString(R.string.set_video_mode_choice)); - if (set_video_mode_choice != null && getActivity() != null) { - SharedPreferences sharedpreferences = getActivity().getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); - SharedPreferences.Editor editor = sharedpreferences.edit(); - switch (set_video_mode_choice.getValue()) { - case "0": - editor.putInt(Helper.SET_VIDEO_MODE, Helper.VIDEO_MODE_NORMAL); - break; - case "1": - editor.putInt(Helper.SET_VIDEO_MODE, Helper.VIDEO_MODE_STREAMING); - break; - case "2": - editor.putInt(Helper.SET_VIDEO_MODE, Helper.VIDEO_MODE_WEBVIEW); - break; + + if (getActivity() != null) { + SharedPreferences sharedpreferences = getActivity().getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); + SharedPreferences.Editor editor = sharedpreferences.edit(); + + if (key.compareTo(getString(R.string.set_video_mode_choice)) == 0) { + ListPreference set_video_mode_choice = findPreference(getString(R.string.set_video_mode_choice)); + if (set_video_mode_choice != null) { + switch (set_video_mode_choice.getValue()) { + case "0": + editor.putInt(Helper.SET_VIDEO_MODE, Helper.VIDEO_MODE_NORMAL); + break; + case "1": + editor.putInt(Helper.SET_VIDEO_MODE, Helper.VIDEO_MODE_STREAMING); + break; + case "2": + editor.putInt(Helper.SET_VIDEO_MODE, Helper.VIDEO_MODE_WEBVIEW); + break; + } + } - editor.apply(); } + if (key.compareTo(getString(R.string.set_video_minimize_choice)) == 0) { + SwitchPreference set_video_minimize_choice = findPreference(getString(R.string.set_video_minimize_choice)); + editor.putBoolean(getString(R.string.set_video_minimize_choice), set_video_minimize_choice.isChecked()); + } + editor.apply(); } } @@ -105,5 +115,8 @@ public class SettingsFragment extends PreferenceFragmentCompat implements Shared set_video_mode_choice.setValueIndex(video_mode); } + boolean minimized = sharedpref.getBoolean(getString(R.string.set_video_minimize_choice), true); + SwitchPreference set_video_minimize_choice = findPreference(getString(R.string.set_video_minimize_choice)); + set_video_minimize_choice.setChecked(minimized); } } diff --git a/app/src/main/res/drawable/ic_baseline_featured_video_24.xml b/app/src/main/res/drawable/ic_baseline_featured_video_24.xml new file mode 100644 index 0000000..3c2bd93 --- /dev/null +++ b/app/src/main/res/drawable/ic_baseline_featured_video_24.xml @@ -0,0 +1,10 @@ + + + diff --git a/app/src/main/res/xml/main_preferences.xml b/app/src/main/res/xml/main_preferences.xml index 32b5353..3ea823b 100644 --- a/app/src/main/res/xml/main_preferences.xml +++ b/app/src/main/res/xml/main_preferences.xml @@ -8,4 +8,10 @@ android:key="@string/set_video_mode_choice" android:summary="@string/set_video_mode_description" android:title="@string/set_video_mode" /> + + \ No newline at end of file