[Android] DayNight mode migration

Geoffrey Métais git at videolan.org
Tue Apr 9 14:18:41 CEST 2019


vlc-android | branch: master | Geoffrey Métais <geoffrey at videolan.org> | Tue Apr  9 14:18:40 2019 +0200| [cc42e18e1c435d808b1eacc2eb572b77591e7295] | committer: Geoffrey Métais

DayNight mode migration

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

 .../vlc/gui/onboarding/OnboardingThemeFragment.kt  |  3 ++-
 .../vlc/gui/onboarding/OnboardingViewModel.kt      |  5 ++--
 .../vlc/gui/preferences/PreferencesUi.java         | 31 +++++++++++++++++-----
 .../src/org/videolan/vlc/util/AndroidDevices.java  |  5 ++++
 4 files changed, 33 insertions(+), 11 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/gui/onboarding/OnboardingThemeFragment.kt b/vlc-android/src/org/videolan/vlc/gui/onboarding/OnboardingThemeFragment.kt
index cd0c3ef26..e030a5e8d 100644
--- a/vlc-android/src/org/videolan/vlc/gui/onboarding/OnboardingThemeFragment.kt
+++ b/vlc-android/src/org/videolan/vlc/gui/onboarding/OnboardingThemeFragment.kt
@@ -11,6 +11,7 @@ import androidx.core.content.ContextCompat
 import androidx.fragment.app.Fragment
 import kotlinx.android.synthetic.main.onboarding_theme.*
 import org.videolan.vlc.R
+import org.videolan.vlc.util.AndroidDevices
 
 class OnboardingThemeFragment : Fragment(), CompoundButton.OnCheckedChangeListener, View.OnClickListener {
 
@@ -33,7 +34,7 @@ class OnboardingThemeFragment : Fragment(), CompoundButton.OnCheckedChangeListen
         lightTheme.setOnClickListener(this)
         darkTheme.setOnClickListener(this)
         dayNightTheme.setOnClickListener(this)
-        follow_system_switch.isChecked = viewModel.enableSystemNight
+        follow_system_switch.isChecked = AndroidDevices.canUseSystemNightMode()
         follow_system_switch.setOnCheckedChangeListener(this)
     }
 
diff --git a/vlc-android/src/org/videolan/vlc/gui/onboarding/OnboardingViewModel.kt b/vlc-android/src/org/videolan/vlc/gui/onboarding/OnboardingViewModel.kt
index 052aebdcd..caf12deaf 100644
--- a/vlc-android/src/org/videolan/vlc/gui/onboarding/OnboardingViewModel.kt
+++ b/vlc-android/src/org/videolan/vlc/gui/onboarding/OnboardingViewModel.kt
@@ -1,11 +1,11 @@
 package org.videolan.vlc.gui.onboarding
 
-import android.os.Build
 import androidx.appcompat.app.AppCompatDelegate
 import androidx.fragment.app.Fragment
 import androidx.fragment.app.FragmentActivity
 import androidx.lifecycle.ViewModel
 import androidx.lifecycle.ViewModelProviders
+import org.videolan.vlc.util.AndroidDevices
 
 
 class OnboardingViewModel() : ViewModel() {
@@ -13,8 +13,7 @@ class OnboardingViewModel() : ViewModel() {
     var customizeMediaFolders = false
     var permissionGranted = false
     var adapterCount = 3
-    var enableSystemNight = Build.VERSION.SDK_INT > Build.VERSION_CODES.P || Build.VERSION.SDK_INT == Build.VERSION_CODES.P && "samsung" == Build.MANUFACTURER.toLowerCase()
-    var theme = if (enableSystemNight) AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM else AppCompatDelegate.MODE_NIGHT_AUTO
+    var theme = if (AndroidDevices.canUseSystemNightMode()) AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM else AppCompatDelegate.MODE_NIGHT_AUTO
 }
 
 fun FragmentActivity.getOnboardingModel() = ViewModelProviders.of(this).get(OnboardingViewModel::class.java)
diff --git a/vlc-android/src/org/videolan/vlc/gui/preferences/PreferencesUi.java b/vlc-android/src/org/videolan/vlc/gui/preferences/PreferencesUi.java
index b6606e558..987ec44fc 100644
--- a/vlc-android/src/org/videolan/vlc/gui/preferences/PreferencesUi.java
+++ b/vlc-android/src/org/videolan/vlc/gui/preferences/PreferencesUi.java
@@ -31,6 +31,7 @@ import org.videolan.vlc.gui.helpers.UiTools;
 import org.videolan.vlc.util.AndroidDevices;
 import org.videolan.vlc.util.LocalePair;
 
+import androidx.appcompat.app.AppCompatDelegate;
 import androidx.preference.ListPreference;
 import androidx.preference.Preference;
 
@@ -48,6 +49,29 @@ public class PreferencesUi extends BasePreferenceFragment implements SharedPrefe
         return R.string.interface_prefs_screen;
     }
 
+    @Override
+    public void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+        findPreference("resume_playback").setVisible(AndroidDevices.isPhone);
+        prepareLocaleList();
+        setupTheme();
+    }
+
+    private void setupTheme() {
+        final SharedPreferences prefs = getPreferenceScreen().getSharedPreferences();
+        if (!prefs.contains("app_theme")) {
+            int theme = AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM;
+            if (prefs.getBoolean("daynight", false) && !AndroidDevices.canUseSystemNightMode()) {
+                theme = AppCompatDelegate.MODE_NIGHT_AUTO;
+            } else if (prefs.contains("enable_black_theme")) {
+                if (prefs.getBoolean("enable_black_theme", false))
+                theme = AppCompatDelegate.MODE_NIGHT_YES;
+                else theme = AppCompatDelegate.MODE_NIGHT_NO;
+            }
+            prefs.edit().putString("app_theme", String.valueOf(theme)).apply();
+        }
+    }
+
     @Override
     public void onStart() {
         super.onStart();
@@ -61,13 +85,6 @@ public class PreferencesUi extends BasePreferenceFragment implements SharedPrefe
                 .unregisterOnSharedPreferenceChangeListener(this);
     }
 
-    @Override
-    public void onCreate(Bundle savedInstanceState) {
-        super.onCreate(savedInstanceState);
-        findPreference("resume_playback").setVisible(AndroidDevices.isPhone);
-        prepareLocaleList();
-    }
-
     @Override
     public boolean onPreferenceTreeClick(Preference preference) {
         if (preference.getKey() == null) return false;
diff --git a/vlc-android/src/org/videolan/vlc/util/AndroidDevices.java b/vlc-android/src/org/videolan/vlc/util/AndroidDevices.java
index 6baa8f8fb..cb80a1198 100644
--- a/vlc-android/src/org/videolan/vlc/util/AndroidDevices.java
+++ b/vlc-android/src/org/videolan/vlc/util/AndroidDevices.java
@@ -191,6 +191,11 @@ public class AndroidDevices {
         return false;
     }
 
+    public static boolean canUseSystemNightMode() {
+        return Build.VERSION.SDK_INT > Build.VERSION_CODES.P ||
+                Build.VERSION.SDK_INT == Build.VERSION_CODES.P && "samsung".equals(Build.MANUFACTURER.toLowerCase());
+    }
+
     private static boolean hasPlayServices(PackageManager pm) {
         try {
             pm.getPackageInfo("com.google.android.gsf", PackageManager.GET_SERVICES);



More information about the Android mailing list