[Android] Use a proper confirmation UI for TV settings

Nicolas Pomepuy git at videolan.org
Wed Aug 7 13:41:51 UTC 2024


vlc-android | branch: master | Nicolas Pomepuy <nicolas at videolabs.io> | Thu Aug  1 14:09:35 2024 +0200| [b08e97af98193c16414523b4e23775e33da9b949] | committer: Duncan McNamara

Use a proper confirmation UI for TV settings

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

 .../main/java/org/videolan/resources/Constants.kt  |  1 +
 .../ui/preferences/PreferencesAdvanced.kt          | 22 +++++++++----
 .../src/org/videolan/vlc/gui/helpers/UiTools.kt    | 37 +++++++++++++++++++---
 3 files changed, 48 insertions(+), 12 deletions(-)

diff --git a/application/resources/src/main/java/org/videolan/resources/Constants.kt b/application/resources/src/main/java/org/videolan/resources/Constants.kt
index 394cc0b76c..91dc6c0ef1 100644
--- a/application/resources/src/main/java/org/videolan/resources/Constants.kt
+++ b/application/resources/src/main/java/org/videolan/resources/Constants.kt
@@ -235,6 +235,7 @@ const val COMPATERROR_ACTIVITY = "org.videolan.vlc.gui.CompatErrorActivity"
 const val TV_SEARCH_ACTIVITY = "org.videolan.television.ui.SearchActivity"
 const val MOBILE_SEARCH_ACTIVITY = "org.videolan.vlc.gui.SearchActivity"
 const val TV_MAIN_ACTIVITY = "org.videolan.television.ui.MainTvActivity"
+const val TV_CONFIRMATION_ACTIVITY = "org.videolan.television.ui.dialogs.ConfirmationTvActivity"
 const val MOBILE_MAIN_ACTIVITY = "org.videolan.vlc.gui.MainActivity"
 const val MOVIEPEDIA_ACTIVITY = "org.videolan.moviepedia.ui.MoviepediaActivity"
 const val TV_AUDIOPLAYER_ACTIVITY = "org.videolan.television.ui.audioplayer.AudioPlayerActivity"
diff --git a/application/television/src/main/java/org/videolan/television/ui/preferences/PreferencesAdvanced.kt b/application/television/src/main/java/org/videolan/television/ui/preferences/PreferencesAdvanced.kt
index baabb788db..ca5766b028 100644
--- a/application/television/src/main/java/org/videolan/television/ui/preferences/PreferencesAdvanced.kt
+++ b/application/television/src/main/java/org/videolan/television/ui/preferences/PreferencesAdvanced.kt
@@ -62,6 +62,7 @@ import org.videolan.resources.KEY_MEDIA_LAST_PLAYLIST_RESUME
 import org.videolan.resources.ROOM_DATABASE
 import org.videolan.resources.SCHEME_PACKAGE
 import org.videolan.resources.VLCInstance
+import org.videolan.television.ui.dialogs.ConfirmationTvActivity
 import org.videolan.tools.BitmapCache
 import org.videolan.tools.DAV1D_THREAD_NUMBER
 import org.videolan.tools.Settings
@@ -92,6 +93,7 @@ import java.io.File
 import java.io.IOException
 
 private const val FILE_PICKER_RESULT_CODE = 10000
+private const val RESTART_CODE = 10001
 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
 class PreferencesAdvanced : BasePreferenceFragment(), SharedPreferences.OnSharedPreferenceChangeListener, CoroutineScope by MainScope() {
     override fun getXml(): Int {
@@ -307,18 +309,24 @@ class PreferencesAdvanced : BasePreferenceFragment(), SharedPreferences.OnShared
 
     override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
         super.onActivityResult(requestCode, resultCode, data)
+        if (requestCode == RESTART_CODE) {
+            if (resultCode == ConfirmationTvActivity.ACTION_ID_POSITIVE) {
+                android.os.Process.killProcess(android.os.Process.myPid())
+            }
+        }
         if (data == null) return
         if (requestCode == FILE_PICKER_RESULT_CODE) {
             if (data.hasExtra(EXTRA_MRL)) {
                 launch {
-                    launch {
-                        PreferenceParser.restoreSettings(activity, Uri.parse(data.getStringExtra(
-                            EXTRA_MRL
-                        )))
-                    }
-                    VLCInstance.restart()
+                    PreferenceParser.restoreSettings(
+                        activity, Uri.parse(
+                            data.getStringExtra(
+                                EXTRA_MRL
+                            )
+                        )
+                    )
                 }
-                UiTools.restartDialog(activity!!, true)
+                UiTools.restartDialog(activity!!, true, RESTART_CODE, this)
             }
         }
     }
diff --git a/application/vlc-android/src/org/videolan/vlc/gui/helpers/UiTools.kt b/application/vlc-android/src/org/videolan/vlc/gui/helpers/UiTools.kt
index 5a13d3817f..f07c3d83f5 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/helpers/UiTools.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/helpers/UiTools.kt
@@ -64,6 +64,7 @@ import androidx.core.net.toUri
 import androidx.core.os.bundleOf
 import androidx.core.view.MenuItemCompat
 import androidx.databinding.BindingAdapter
+import androidx.fragment.app.Fragment
 import androidx.fragment.app.FragmentActivity
 import androidx.lifecycle.Lifecycle
 import androidx.lifecycle.findViewTreeLifecycleOwner
@@ -811,11 +812,37 @@ object UiTools {
     }
 
 
-    fun restartDialog(context: Context, fromLeanback:Boolean = false) {
-        val builder = if (fromLeanback) AlertDialog.Builder(context, R.style.Theme_AppCompat) else AlertDialog.Builder(context)
-        builder
-                .setTitle(context.resources.getString(R.string.restart_vlc))
-                .setMessage(context.resources.getString(R.string.restart_message))
+    fun restartDialog(
+        activity: Activity,
+        fromLeanback: Boolean = false,
+        leanbackResultCode: Int = 0,
+        leanbackCaller: Any? = null
+    ) {
+
+        if (fromLeanback) {
+            val intent = Intent(Intent.ACTION_VIEW).setClassName(activity, TV_CONFIRMATION_ACTIVITY)
+
+            intent.putExtra(
+                "confirmation_dialog_title",
+                activity.getString(R.string.restart_vlc)
+            )
+            intent.putExtra(
+                "confirmation_dialog_text",
+                activity.getString(R.string.restart_message)
+            )
+            when (leanbackCaller) {
+                is Activity -> leanbackCaller.startActivityForResult(intent, leanbackResultCode)
+                is Fragment -> leanbackCaller.startActivityForResult(intent, leanbackResultCode)
+                is android.app.Fragment -> leanbackCaller.startActivityForResult(intent, leanbackResultCode)
+                else -> throw IllegalStateException("Invalid caller")
+            }
+
+            return
+        }
+
+        AlertDialog.Builder(activity)
+                .setTitle(activity.resources.getString(R.string.restart_vlc))
+                .setMessage(activity.resources.getString(R.string.restart_message))
                 .setPositiveButton(R.string.restart_message_OK) { _, _ -> android.os.Process.killProcess(android.os.Process.myPid()) }
                 .setNegativeButton(R.string.restart_message_Later, null)
                 .create()



More information about the Android mailing list