[Android] Fix onBackPressed deprecations

Nicolas Pomepuy git at videolan.org
Mon Nov 14 09:54:16 UTC 2022


vlc-android | branch: master | Nicolas Pomepuy <nicolas at videolabs.io> | Fri Nov  4 09:09:27 2022 +0100| [2181c71f56da9f84554a3ab713dc5536a90f1d54] | committer: Nicolas Pomepuy

Fix onBackPressed deprecations

> https://code.videolan.org/videolan/vlc-android/commit/2181c71f56da9f84554a3ab713dc5536a90f1d54
---

 .../ui/audioplayer/AudioPlayerActivity.kt          | 25 +++++-----
 .../vlc/gui/AudioPlayerContainerActivity.kt        | 11 +++--
 .../videolan/vlc/gui/browser/FilePickerActivity.kt | 19 ++++----
 .../videolan/vlc/gui/video/VideoPlayerActivity.kt  | 54 +++++++++++-----------
 4 files changed, 57 insertions(+), 52 deletions(-)

diff --git a/application/television/src/main/java/org/videolan/television/ui/audioplayer/AudioPlayerActivity.kt b/application/television/src/main/java/org/videolan/television/ui/audioplayer/AudioPlayerActivity.kt
index 558c82a48..6957a6470 100644
--- a/application/television/src/main/java/org/videolan/television/ui/audioplayer/AudioPlayerActivity.kt
+++ b/application/television/src/main/java/org/videolan/television/ui/audioplayer/AudioPlayerActivity.kt
@@ -30,6 +30,7 @@ import android.support.v4.media.session.PlaybackStateCompat
 import android.text.format.DateFormat
 import android.view.*
 import android.widget.SeekBar
+import androidx.activity.OnBackPressedCallback
 import androidx.databinding.DataBindingUtil
 import androidx.lifecycle.ViewModelProvider
 import androidx.lifecycle.lifecycleScope
@@ -128,6 +129,18 @@ class AudioPlayerActivity : BaseTvActivity(),KeycodeListener  {
             true
         }
         bookmarkModel = BookmarkModel.get(this)
+        onBackPressedDispatcher.addCallback(this, object : OnBackPressedCallback(true) {
+            override fun handleOnBackPressed() {
+                if (this at AudioPlayerActivity::optionsDelegate.isInitialized && optionsDelegate.isShowing()) {
+                    optionsDelegate.hide()
+                    return
+                }
+                if (::bookmarkListDelegate.isInitialized && bookmarkListDelegate.visible) {
+                    bookmarkListDelegate.hide()
+                    return
+                }
+            }
+        })
     }
 
     private var timelineListener: SeekBar.OnSeekBarChangeListener = object : SeekBar.OnSeekBarChangeListener {
@@ -156,18 +169,6 @@ class AudioPlayerActivity : BaseTvActivity(),KeycodeListener  {
         }
     }
 
-    override fun onBackPressed() {
-        if (this::optionsDelegate.isInitialized && optionsDelegate.isShowing()) {
-            optionsDelegate.hide()
-            return
-        }
-        if (::bookmarkListDelegate.isInitialized && bookmarkListDelegate.visible) {
-            bookmarkListDelegate.hide()
-            return
-        }
-        super.onBackPressed()
-    }
-
     override fun refresh() {}
 
     private var wasPlaying = false
diff --git a/application/vlc-android/src/org/videolan/vlc/gui/AudioPlayerContainerActivity.kt b/application/vlc-android/src/org/videolan/vlc/gui/AudioPlayerContainerActivity.kt
index ab1413c30..f76e9e6da 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/AudioPlayerContainerActivity.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/AudioPlayerContainerActivity.kt
@@ -32,6 +32,7 @@ import android.view.*
 import android.widget.FrameLayout
 import android.widget.ProgressBar
 import android.widget.TextView
+import androidx.activity.OnBackPressedCallback
 import androidx.appcompat.widget.Toolbar
 import androidx.appcompat.widget.ViewStubCompat
 import androidx.coordinatorlayout.widget.CoordinatorLayout
@@ -166,6 +167,11 @@ open class AudioPlayerContainerActivity : BaseActivity(), KeycodeListener {
 
             WindowInsetsCompat.CONSUMED
         }
+        onBackPressedDispatcher.addCallback(this, object : OnBackPressedCallback(true) {
+            override fun handleOnBackPressed() {
+                if (slideDownAudioPlayer()) return
+            }
+        })
     }
 
     /**
@@ -383,11 +389,6 @@ open class AudioPlayerContainerActivity : BaseActivity(), KeycodeListener {
         super.onResume()
     }
 
-    override fun onBackPressed() {
-        if (slideDownAudioPlayer()) return
-        super.onBackPressed()
-    }
-
     override fun onOptionsItemSelected(item: MenuItem): Boolean {
 
         // Handle item selection
diff --git a/application/vlc-android/src/org/videolan/vlc/gui/browser/FilePickerActivity.kt b/application/vlc-android/src/org/videolan/vlc/gui/browser/FilePickerActivity.kt
index 43046ce85..b5903de83 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/browser/FilePickerActivity.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/browser/FilePickerActivity.kt
@@ -27,6 +27,7 @@ import android.content.res.Configuration
 import android.os.Bundle
 import android.view.Gravity
 import android.view.View
+import androidx.activity.OnBackPressedCallback
 import androidx.core.os.bundleOf
 import org.videolan.medialibrary.interfaces.media.MediaWrapper
 import org.videolan.resources.util.parcelable
@@ -53,15 +54,15 @@ class FilePickerActivity : BaseActivity() {
         ft.replace(R.id.fragment_placeholder, FilePickerFragment().apply { arguments = bundleOf(KEY_MEDIA to intent.parcelable<MediaWrapper>(KEY_MEDIA), KEY_PICKER_TYPE to intent.getIntExtra(KEY_PICKER_TYPE, 0)) }, "picker")
         ft.commit()
         window.attributes.gravity = Gravity.BOTTOM
-    }
-
-    override fun onBackPressed() {
-        val fpf = supportFragmentManager.findFragmentById(R.id.fragment_placeholder) as FilePickerFragment
-        when {
-            fpf.isRootDirectory -> finish()
-            supportFragmentManager.backStackEntryCount > 0 -> super.onBackPressed()
-            else -> fpf.browseUp()
-        }
+        onBackPressedDispatcher.addCallback(this, object : OnBackPressedCallback(true) {
+            override fun handleOnBackPressed() {
+                val fpf = supportFragmentManager.findFragmentById(R.id.fragment_placeholder) as FilePickerFragment
+                when {
+                    fpf.isRootDirectory -> finish()
+                    else -> fpf.browseUp()
+                }
+            }
+        })
     }
 
     fun onCloseClick(@Suppress("UNUSED_PARAMETER") v:View) {
diff --git a/application/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.kt b/application/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.kt
index 8ea6e45df..e15cb34db 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.kt
@@ -48,6 +48,7 @@ import android.view.animation.DecelerateInterpolator
 import android.view.animation.RotateAnimation
 import android.widget.*
 import android.widget.SeekBar.OnSeekBarChangeListener
+import androidx.activity.OnBackPressedCallback
 import androidx.appcompat.app.AlertDialog
 import androidx.appcompat.app.AppCompatActivity
 import androidx.appcompat.app.AppCompatDelegate
@@ -521,6 +522,33 @@ open class VideoPlayerActivity : AppCompatActivity(), PlaybackService.Callback,
                         }
             }
         }
+
+        onBackPressedDispatcher.addCallback(this, object : OnBackPressedCallback(true) {
+            override fun handleOnBackPressed() {
+                if (optionsDelegate?.isShowing() == true) {
+                    optionsDelegate?.hide()
+                } else if (resizeDelegate.isShowing()) {
+                    resizeDelegate.hideResizeOverlay()
+                } else if (lockBackButton) {
+                    lockBackButton = false
+                    handler.sendEmptyMessageDelayed(RESET_BACK_LOCK, 2000)
+                    Toast.makeText(applicationContext, getString(R.string.back_quit_lock), Toast.LENGTH_SHORT).show()
+                } else if (isPlaylistVisible) {
+                    overlayDelegate.togglePlaylist()
+                } else if (isPlaybackSettingActive) {
+                    delayDelegate.endPlaybackSetting()
+                } else if (isShowing && service?.playlistManager?.videoStatsOn?.value == true) {
+                    //hides video stats if they are displayed
+                    service?.playlistManager?.videoStatsOn?.postValue(false)
+                } else if (overlayDelegate.isBookmarkShown()) {
+                    overlayDelegate.hideBookmarks()
+                } else if ((AndroidDevices.isAndroidTv || isTalkbackIsEnabled()) && isShowing && !isLocked) {
+                    overlayDelegate.hideOverlay(true)
+                } else {
+                    exitOK()
+                }
+            }
+        })
     }
 
     override fun onAttachedToWindow() {
@@ -1006,32 +1034,6 @@ open class VideoPlayerActivity : AppCompatActivity(), PlaybackService.Callback,
         return if (result) true else super.dispatchGenericMotionEvent(event)
     }
 
-    override fun onBackPressed() {
-        if (optionsDelegate?.isShowing() == true) {
-            optionsDelegate?.hide()
-        } else if (resizeDelegate.isShowing()) {
-            resizeDelegate.hideResizeOverlay()
-        } else if (lockBackButton) {
-            lockBackButton = false
-            handler.sendEmptyMessageDelayed(RESET_BACK_LOCK, 2000)
-            Toast.makeText(applicationContext, getString(R.string.back_quit_lock), Toast.LENGTH_SHORT).show()
-        } else if (isPlaylistVisible) {
-            overlayDelegate.togglePlaylist()
-        } else if (isPlaybackSettingActive) {
-            delayDelegate.endPlaybackSetting()
-        } else if (isShowing && service?.playlistManager?.videoStatsOn?.value == true) {
-            //hides video stats if they are displayed
-            service?.playlistManager?.videoStatsOn?.postValue(false)
-        } else if (overlayDelegate.isBookmarkShown()) {
-            overlayDelegate.hideBookmarks()
-        } else if ((AndroidDevices.isAndroidTv || isTalkbackIsEnabled()) && isShowing && !isLocked) {
-            overlayDelegate.hideOverlay(true)
-        } else {
-            exitOK()
-            super.onBackPressed()
-        }
-    }
-
     override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
         if (service == null || keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_BUTTON_B)
             return super.onKeyDown(keyCode, event)



More information about the Android mailing list