[Android] Implement the new scheduler in the main TV activity

Nicolas Pomepuy git at videolan.org
Tue Jul 25 14:57:29 UTC 2023


vlc-android | branch: master | Nicolas Pomepuy <nicolas at videolabs.io> | Thu Jul 20 11:58:07 2023 +0200| [de5cf46498168ab88eee814036a37d9b321c28dc] | committer: Duncan McNamara

Implement the new scheduler in the main TV activity

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

 .../org/videolan/television/ui/MainTvActivity.kt   | 39 ++++++++++------------
 1 file changed, 17 insertions(+), 22 deletions(-)

diff --git a/application/television/src/main/java/org/videolan/television/ui/MainTvActivity.kt b/application/television/src/main/java/org/videolan/television/ui/MainTvActivity.kt
index c5a7c0c5ba..35c1045a92 100644
--- a/application/television/src/main/java/org/videolan/television/ui/MainTvActivity.kt
+++ b/application/television/src/main/java/org/videolan/television/ui/MainTvActivity.kt
@@ -24,7 +24,6 @@ import android.annotation.TargetApi
 import android.content.Intent
 import android.os.Build
 import android.os.Bundle
-import android.os.Message
 import android.view.KeyEvent
 import android.view.View
 import android.widget.ProgressBar
@@ -34,39 +33,35 @@ import org.videolan.television.ui.browser.BaseTvActivity
 import org.videolan.tools.RESULT_RESCAN
 import org.videolan.tools.RESULT_RESTART
 import org.videolan.tools.RESULT_RESTART_APP
-import org.videolan.tools.WeakHandler
 import org.videolan.vlc.ScanProgress
 import org.videolan.vlc.StartActivity
 import org.videolan.vlc.gui.helpers.hf.StoragePermissionsDelegate
 import org.videolan.vlc.reloadLibrary
+import org.videolan.vlc.util.LifecycleAwareScheduler
+import org.videolan.vlc.util.SchedulerCallback
 import org.videolan.vlc.util.Util
 
 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
-class MainTvActivity : BaseTvActivity(), StoragePermissionsDelegate.CustomActionController {
+class MainTvActivity : BaseTvActivity(), StoragePermissionsDelegate.CustomActionController, SchedulerCallback {
 
     private lateinit var browseFragment: MainTvFragment
     private lateinit var progressBar: ProgressBar
-    private val handler = MainTvActivityHandler(this)
+    lateinit var scheduler: LifecycleAwareScheduler
 
 
-    class MainTvActivityHandler(owner: MainTvActivity) : WeakHandler<MainTvActivity>(owner) {
-
-        override fun handleMessage(msg: Message) {
-            val activity = owner ?: return
-            when (msg.what) {
-                SHOW_LOADING -> activity.progressBar.visibility = View.VISIBLE
-                HIDE_LOADING -> {
-                    removeMessages(SHOW_LOADING)
-                    activity.progressBar.visibility = View.GONE
-                    super.handleMessage(msg)
-                }
-                else -> super.handleMessage(msg)
+    override fun onTaskTriggered(id: String, data: Bundle) {
+        when (id) {
+            SHOW_LOADING -> progressBar.visibility = View.VISIBLE
+            HIDE_LOADING -> {
+                scheduler.cancelAction(SHOW_LOADING)
+                progressBar.visibility = View.GONE
             }
         }
     }
 
     override fun onCreate(savedInstanceState: Bundle?) {
         super.onCreate(savedInstanceState)
+        scheduler =  LifecycleAwareScheduler(this)
 
         Util.checkCpuCompatibility(this)
 
@@ -98,21 +93,21 @@ class MainTvActivity : BaseTvActivity(), StoragePermissionsDelegate.CustomAction
     }
 
     override fun onParsingServiceStarted() {
-        handler.sendEmptyMessage(SHOW_LOADING)
+        scheduler.startAction(SHOW_LOADING)
     }
 
     override fun onParsingServiceProgress(scanProgress: ScanProgress?) {
         if (progressBar.visibility == View.GONE && Medialibrary.getInstance().isWorking)
-            handler.sendEmptyMessage(SHOW_LOADING)
+            scheduler.startAction(SHOW_LOADING)
     }
 
     override fun onParsingServiceFinished() {
         if (!Medialibrary.getInstance().isWorking)
-            handler.sendEmptyMessageDelayed(HIDE_LOADING, 500)
+            scheduler.scheduleAction(HIDE_LOADING, 500)
     }
 
     fun hideLoading() {
-        handler.sendEmptyMessageDelayed(HIDE_LOADING, 500)
+        scheduler.scheduleAction(HIDE_LOADING, 500)
     }
 
     override fun onStorageAccessGranted() {
@@ -130,7 +125,7 @@ class MainTvActivity : BaseTvActivity(), StoragePermissionsDelegate.CustomAction
         const val BROWSER_TYPE = "browser_type"
 
         const val TAG = "VLC/MainTvActivity"
-        private const val SHOW_LOADING = 0
-        private const val HIDE_LOADING = 1
+        private const val SHOW_LOADING = "show_loading"
+        private const val HIDE_LOADING = "hide_loading"
     }
 }



More information about the Android mailing list