[Android] Persist the safe mode state in the Settings

Nicolas Pomepuy git at videolan.org
Wed Jul 12 08:30:36 UTC 2023


vlc-android | branch: master | Nicolas Pomepuy <nicolas at videolabs.io> | Tue Jun 27 07:53:31 2023 +0200| [ddf9939416c6e6ea77a628c12487ff885ecd37e9] | committer: Duncan McNamara

Persist the safe mode state in the Settings

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

 .../src/main/java/org/videolan/tools/Settings.kt     |  6 ++++++
 .../src/org/videolan/vlc/gui/PinCodeActivity.kt      |  1 -
 .../gui/preferences/PreferencesParentalControl.kt    | 20 ++++++++++++++++++++
 .../videolan/vlc/gui/preferences/PreferencesUi.kt    |  4 ++--
 4 files changed, 28 insertions(+), 3 deletions(-)

diff --git a/application/tools/src/main/java/org/videolan/tools/Settings.kt b/application/tools/src/main/java/org/videolan/tools/Settings.kt
index 2434fa10aa..2e2a14ea5d 100644
--- a/application/tools/src/main/java/org/videolan/tools/Settings.kt
+++ b/application/tools/src/main/java/org/videolan/tools/Settings.kt
@@ -30,6 +30,7 @@ object Settings : SingletonHolder<SharedPreferences, Context>({ init(it.applicat
     var showTrackNumber = true
     var tvFoldersFirst = true
     var incognitoMode = false
+    var safeMode = false
     private var audioControlsChangeListener: (() -> Unit)? = null
     lateinit var device : DeviceInfo
         private set
@@ -53,9 +54,13 @@ object Settings : SingletonHolder<SharedPreferences, Context>({ init(it.applicat
         showTrackNumber = prefs.getBoolean(ALBUMS_SHOW_TRACK_NUMBER, true)
         tvFoldersFirst = prefs.getBoolean(TV_FOLDERS_FIRST, true)
         incognitoMode = prefs.getBoolean(KEY_INCOGNITO, false)
+        safeMode = prefs.getBoolean(KEY_SAFE_MODE, false) && prefs.getString(KEY_SAFE_MODE_PIN, "")?.isNotBlank() == true
         return prefs
     }
 
+    fun Context.isPinCodeSet() = Settings.getInstance(this).getString(KEY_SAFE_MODE_PIN, "")?.isNotBlank() == true
+
+
     /**
      * Trigger the [audioControlsChangeListener] to update the UI
      */
@@ -205,6 +210,7 @@ const val WIDGETS_PREVIEW_PLAYING = "widgets_preview_playing"
 
 const val KEY_SAFE_MODE_PIN = "safe_mode_pin"
 const val KEY_RESTRICT_SETTINGS = "restrict_settings"
+const val KEY_SAFE_MODE = "safe_mode"
 
 class DeviceInfo(context: Context) {
     val pm = context.packageManager
diff --git a/application/vlc-android/src/org/videolan/vlc/gui/PinCodeActivity.kt b/application/vlc-android/src/org/videolan/vlc/gui/PinCodeActivity.kt
index d82be20bfe..8fc2bfa8b4 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/PinCodeActivity.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/PinCodeActivity.kt
@@ -247,4 +247,3 @@ enum class PinCodeReason {
     FIRST_CREATION, MODIFY, CHECK
 }
 
-fun Context.isPinCodeSet() = Settings.getInstance(this).getString(KEY_SAFE_MODE_PIN, "")?.isNotBlank() == true
diff --git a/application/vlc-android/src/org/videolan/vlc/gui/preferences/PreferencesParentalControl.kt b/application/vlc-android/src/org/videolan/vlc/gui/preferences/PreferencesParentalControl.kt
index e872335440..733ab96075 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/preferences/PreferencesParentalControl.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/preferences/PreferencesParentalControl.kt
@@ -27,6 +27,9 @@ import android.content.SharedPreferences
 import android.os.Bundle
 import androidx.activity.result.contract.ActivityResultContracts
 import androidx.preference.Preference
+import org.videolan.tools.KEY_SAFE_MODE
+import org.videolan.tools.Settings
+import org.videolan.tools.Settings.isPinCodeSet
 import org.videolan.vlc.R
 import org.videolan.vlc.gui.PinCodeActivity
 import org.videolan.vlc.gui.PinCodeReason
@@ -48,6 +51,18 @@ class PreferencesParentalControl : BasePreferenceFragment(), SharedPreferences.O
         super.onCreate(savedInstanceState)
 
     }
+
+    override fun onStart() {
+        super.onStart()
+        preferenceScreen.sharedPreferences.registerOnSharedPreferenceChangeListener(this)
+    }
+
+    override fun onStop() {
+        super.onStop()
+        preferenceScreen.sharedPreferences
+                .unregisterOnSharedPreferenceChangeListener(this)
+    }
+
     override fun onPreferenceTreeClick(preference: Preference): Boolean {
         if (preference.key == null) return false
         when (preference.key) {
@@ -60,5 +75,10 @@ class PreferencesParentalControl : BasePreferenceFragment(), SharedPreferences.O
     }
 
     override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences, key: String) {
+        when (key) {
+            KEY_SAFE_MODE -> {
+                Settings.safeMode = sharedPreferences.getBoolean(key, false) && requireActivity().isPinCodeSet()
+            }
+        }
     }
 }
diff --git a/application/vlc-android/src/org/videolan/vlc/gui/preferences/PreferencesUi.kt b/application/vlc-android/src/org/videolan/vlc/gui/preferences/PreferencesUi.kt
index 961269cca5..796b4e2565 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/preferences/PreferencesUi.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/preferences/PreferencesUi.kt
@@ -47,13 +47,13 @@ import org.videolan.tools.PREF_TV_UI
 import org.videolan.tools.RESULT_UPDATE_SEEN_MEDIA
 import org.videolan.tools.SHOW_VIDEO_THUMBNAILS
 import org.videolan.tools.Settings
+import org.videolan.tools.Settings.isPinCodeSet
 import org.videolan.tools.putSingle
 import org.videolan.vlc.BuildConfig
 import org.videolan.vlc.R
 import org.videolan.vlc.gui.PinCodeActivity
 import org.videolan.vlc.gui.PinCodeReason
 import org.videolan.vlc.gui.helpers.UiTools
-import org.videolan.vlc.gui.isPinCodeSet
 
 
 class PreferencesUi : BasePreferenceFragment(), SharedPreferences.OnSharedPreferenceChangeListener {
@@ -138,7 +138,7 @@ class PreferencesUi : BasePreferenceFragment(), SharedPreferences.OnSharedPrefer
             "media_seen" -> requireActivity().setResult(RESULT_UPDATE_SEEN_MEDIA)
             "parental_control" -> {
                 if (requireActivity().isPinCodeSet())
-                loadFragment(PreferencesParentalControl())
+                    loadFragment(PreferencesParentalControl())
                 else {
                     val intent = PinCodeActivity.getIntent(requireActivity(), PinCodeReason.FIRST_CREATION)
                     pinCodeResult.launch(intent)



More information about the Android mailing list