Commit 043b3f0d authored by Geoffrey Métais's avatar Geoffrey Métais

Refactor app theme preference

parent e9e52313
......@@ -53,7 +53,7 @@
app:layout_constraintGuide_percent="0.5" />
<FrameLayout
android:id="@+id/lightTheme"
android:id="@+id/dayNightTheme"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/theme_selection_rounded"
......@@ -68,11 +68,11 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
app:srcCompat="@drawable/ic_theme_light" />
app:srcCompat="@drawable/ic_theme_daynight" />
</FrameLayout>
<FrameLayout
android:id="@+id/darkTheme"
android:id="@+id/lightTheme"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
......@@ -86,11 +86,11 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
app:srcCompat="@drawable/ic_theme_dark" />
app:srcCompat="@drawable/ic_theme_light" />
</FrameLayout>
<FrameLayout
android:id="@+id/dayNightTheme"
android:id="@+id/darkTheme"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
......@@ -104,7 +104,7 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
app:srcCompat="@drawable/ic_theme_daynight" />
app:srcCompat="@drawable/ic_theme_dark" />
</FrameLayout>
<TextView
......@@ -117,10 +117,41 @@
android:maxWidth="250dp"
android:background="@drawable/theme_selection_rounded"
android:padding="8dp"
android:text="@string/light_theme"
android:text="@string/daynight_explanation"
android:textAlignment="center"
android:textColor="@color/white"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/darkTheme" />
<Switch
android:id="@+id/follow_system_switch"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="@string/theme_follow_system_mode"
app:layout_constraintTop_toBottomOf="@+id/themeDescription"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:checked="true"
android:textSize="18sp"
android:visibility="gone"/>
<!--Switch
android:id="@+id/follow_battery_switch"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="Follow battery saver mode"
app:layout_constraintTop_toBottomOf="@+id/follow_system_switch"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:checked="true"
android:textSize="18sp"/-->
</androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file
......@@ -409,4 +409,20 @@
<item>YUV</item>
</string-array>
<string-array name="daynight_mode_entries">
<item>@string/daynight_follow_system_title</item>
<!--item>@string/daynight_battery_title</item-->
<item>@string/daynight_title</item>
<item>@string/light_theme</item>
<item>@string/enable_black_theme</item>
</string-array>
<string-array name="daynight_mode_values">
<item>-1</item>
<!--item>3</item-->
<item>0</item>
<item>1</item>
<item>2</item>
</string-array>
</resources>
\ No newline at end of file
......@@ -293,6 +293,8 @@
<string name="enable_black_theme_summary">Change the interface colors for better comfort in low light environments.</string>
<string name="subtitle_text_encoding">Subtitle text encoding</string>
<string name="daynight_title">DayNight mode</string>
<string name="daynight_follow_system_title">Follow system mode</string>
<string name="daynight_battery_title">Follow Battery saver</string>
<string name="daynight_summary">Automatically switch to night mode at appropriate time</string>
<string name="extra_prefs_category">Extra settings</string>
......@@ -687,4 +689,5 @@
<string name="onboarding_scan_customize">Let me choose which folders VLC will scan</string>
<string name="button_medialibrary_preferences">Go to Medialibrary preferences</string>
<string name="light_theme">Light theme</string>
<string name="theme_follow_system_mode">Follow system mode</string>
</resources>
......@@ -4,19 +4,12 @@
android:key="ui_category"
android:title="@string/interface_prefs_screen">
<androidx.preference.SwitchPreferenceCompat
android:defaultValue="false"
android:key="enable_black_theme"
android:summary="@string/enable_black_theme_summary"
android:title="@string/enable_black_theme"
android:disableDependentsState="true" />
<androidx.preference.SwitchPreferenceCompat
android:defaultValue="false"
android:key="daynight"
android:dependency="enable_black_theme"
android:summary="@string/daynight_summary"
android:title="@string/daynight_title" />
<ListPreference
android:defaultValue="-1"
android:key="app_theme"
android:title="@string/daynight_title"
android:entries="@array/daynight_mode_entries"
android:entryValues="@array/daynight_mode_values" />
<CheckBoxPreference
android:key="tv_ui"
......
......@@ -2,26 +2,20 @@ package org.videolan.vlc.gui;
import android.content.SharedPreferences;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AppCompatDelegate;
import org.videolan.vlc.R;
import org.videolan.vlc.VLCApplication;
import org.videolan.vlc.util.AndroidDevices;
import org.videolan.vlc.util.Settings;
public class BaseActivity extends AppCompatActivity {
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AppCompatDelegate;
static {
AppCompatDelegate.setDefaultNightMode(VLCApplication.getAppContext() != null && Settings.INSTANCE.getInstance(VLCApplication.getAppContext()).getBoolean("daynight", false) ? AppCompatDelegate.MODE_NIGHT_AUTO : AppCompatDelegate.MODE_NIGHT_NO);
}
public class BaseActivity extends AppCompatActivity {
protected SharedPreferences mSettings;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
/* Get settings */
mSettings = Settings.INSTANCE.getInstance(this);
/* Theme must be applied before super.onCreate */
applyTheme();
......@@ -29,9 +23,17 @@ public class BaseActivity extends AppCompatActivity {
}
private void applyTheme() {
boolean enableBlackTheme = mSettings.getBoolean("enable_black_theme", false);
if (AndroidDevices.showTvUi(this) || enableBlackTheme) {
setTheme(R.style.Theme_VLC_Black);
if (AndroidDevices.showTvUi(this)) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
return;
}
if (mSettings.contains("app_theme")) {
AppCompatDelegate.setDefaultNightMode(Integer.valueOf(mSettings.getString("app_theme", "-1")));
} else if (mSettings.contains("daynight") || mSettings.contains("enable_black_theme")) { // legacy support
final boolean daynight = mSettings.getBoolean("daynight", false);
final boolean dark = mSettings.getBoolean("enable_black_theme", false);
final int mode = dark ? AppCompatDelegate.MODE_NIGHT_YES : daynight ? AppCompatDelegate.MODE_NIGHT_AUTO : AppCompatDelegate.MODE_NIGHT_NO;
AppCompatDelegate.setDefaultNightMode(mode);
}
}
}
......@@ -46,7 +46,6 @@ class OnboardingActivity : AppCompatActivity(), ViewPager.OnPageChangeListener,
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
window.statusBarColor = ContextCompat.getColor(this, R.color.onboarding_grey_dark)
}
......@@ -134,6 +133,7 @@ class OnboardingActivity : AppCompatActivity(), ViewPager.OnPageChangeListener,
.putInt(KEY_MEDIALIBRARY_SCAN, if (viewModel.scanStorages) ML_SCAN_ON else ML_SCAN_OFF)
.putInt("fragment_id", if (viewModel.scanStorages) R.id.nav_video else R.id.nav_directories)
.putInt(PREF_FIRST_RUN, BuildConfig.VERSION_CODE)
.putString("app_theme", viewModel.theme.toString())
.apply()
if (!viewModel.scanStorages) MediaParsingService.preselectedStorages.clear()
startMedialibrary(firstRun = true, upgrade = true, parse = viewModel.scanStorages)
......
package org.videolan.vlc.gui.onboarding
import android.os.Build
import android.os.Bundle
import android.preference.PreferenceManager
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import android.widget.CompoundButton
import androidx.appcompat.app.AppCompatDelegate
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import kotlinx.android.synthetic.main.onboarding_theme.*
import org.videolan.vlc.R
class OnboardingThemeFragment : Fragment() {
private lateinit var themeDescription: TextView
private lateinit var lightThemeSelector: View
private lateinit var darkThemeSelector: View
private lateinit var dayNightTheme: View
private var currentThemeIsLight = true
class OnboardingThemeFragment : Fragment(), CompoundButton.OnCheckedChangeListener, View.OnClickListener {
private lateinit var viewModel: OnboardingViewModel
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.onboarding_theme, container, false)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
viewModel = getOnboardingModel()
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
lightThemeSelector = view.findViewById(R.id.lightTheme)
darkThemeSelector = view.findViewById(R.id.darkTheme)
dayNightTheme = view.findViewById(R.id.dayNightTheme)
themeDescription = view.findViewById(R.id.themeDescription)
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.onboarding_theme, container, false)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) follow_system_switch.visibility = View.VISIBLE
val themeSelectorListener = View.OnClickListener {
it.background = ContextCompat.getDrawable(requireActivity(), R.drawable.theme_selection_rounded)
when (it) {
lightThemeSelector -> {
darkThemeSelector.background = null
dayNightTheme.background = null
currentThemeIsLight = true
themeDescription.setText(R.string.light_theme)
PreferenceManager.getDefaultSharedPreferences(requireActivity())
.edit()
.putBoolean("enable_black_theme", false)
.putBoolean("daynight", false)
.apply()
}
darkThemeSelector -> {
themeDescription.setText(R.string.enable_black_theme)
lightThemeSelector.background = null
dayNightTheme.background = null
currentThemeIsLight = false
PreferenceManager.getDefaultSharedPreferences(requireActivity())
.edit()
.putBoolean("enable_black_theme", true)
.putBoolean("daynight", false)
.apply()
}
dayNightTheme -> {
themeDescription.setText(R.string.daynight_explanation)
lightThemeSelector.background = null
darkThemeSelector.background = null
PreferenceManager.getDefaultSharedPreferences(requireActivity())
.edit()
.putBoolean("enable_black_theme", false)
.putBoolean("daynight", true)
.apply()
}
}
lightTheme.setOnClickListener(this)
darkTheme.setOnClickListener(this)
dayNightTheme.setOnClickListener(this)
follow_system_switch.isChecked = viewModel.enableSystemNight
follow_system_switch.setOnCheckedChangeListener(this)
}
override fun onCheckedChanged(buttonView: CompoundButton, isChecked: Boolean) {
when (buttonView) {
follow_system_switch -> viewModel.theme = if (isChecked) AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM else AppCompatDelegate.MODE_NIGHT_AUTO
}
}
lightThemeSelector.setOnClickListener(themeSelectorListener)
darkThemeSelector.setOnClickListener(themeSelectorListener)
dayNightTheme.setOnClickListener(themeSelectorListener)
override fun onClick(view: View) {
view.background = ContextCompat.getDrawable(requireActivity(), R.drawable.theme_selection_rounded)
when (view) {
lightTheme -> {
darkTheme.background = null
dayNightTheme.background = null
viewModel.theme = AppCompatDelegate.MODE_NIGHT_NO
themeDescription.setText(R.string.light_theme)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) follow_system_switch.visibility = View.GONE
}
darkTheme -> {
themeDescription.setText(R.string.enable_black_theme)
lightTheme.background = null
dayNightTheme.background = null
viewModel.theme = AppCompatDelegate.MODE_NIGHT_YES
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) follow_system_switch.visibility = View.GONE
}
dayNightTheme -> {
themeDescription.setText(R.string.daynight_explanation)
lightTheme.background = null
darkTheme.background = null
viewModel.theme = AppCompatDelegate.MODE_NIGHT_AUTO
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) follow_system_switch.visibility = View.VISIBLE
}
}
}
companion object {
fun newInstance(): OnboardingThemeFragment {
return OnboardingThemeFragment()
}
fun newInstance() = OnboardingThemeFragment()
}
}
\ No newline at end of file
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
......@@ -11,6 +13,8 @@ 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
}
fun FragmentActivity.getOnboardingModel() = ViewModelProviders.of(this).get(OnboardingViewModel::class.java)
......
......@@ -32,6 +32,7 @@ import org.videolan.vlc.R;
import org.videolan.vlc.util.Settings;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AppCompatDelegate;
import androidx.appcompat.widget.Toolbar;
import androidx.core.view.ViewCompat;
import videolan.org.commontools.LiveEvent;
......@@ -99,10 +100,14 @@ public class PreferencesActivity extends AppCompatActivity {
}
private void applyTheme() {
SharedPreferences pref = Settings.INSTANCE.getInstance(this);
boolean enableBlackTheme = pref.getBoolean("enable_black_theme", false);
if (enableBlackTheme) {
setTheme(R.style.Theme_VLC_Black);
final SharedPreferences pref = Settings.INSTANCE.getInstance(this);
if (pref.contains("app_theme")) {
AppCompatDelegate.setDefaultNightMode(Integer.valueOf(pref.getString("app_theme", "-1")));
} else if (pref.contains("daynight") || pref.contains("enable_black_theme")) { // legacy support
final boolean daynight = pref.getBoolean("daynight", false);
final boolean dark = pref.getBoolean("enable_black_theme", false);
final int mode = dark ? AppCompatDelegate.MODE_NIGHT_YES : daynight ? AppCompatDelegate.MODE_NIGHT_AUTO : AppCompatDelegate.MODE_NIGHT_NO;
AppCompatDelegate.setDefaultNightMode(mode);
}
}
......
......@@ -31,10 +31,8 @@ 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;
import androidx.preference.SwitchPreferenceCompat;
......@@ -72,25 +70,27 @@ public class PreferencesUi extends BasePreferenceFragment implements SharedPrefe
@Override
public boolean onPreferenceTreeClick(Preference preference) {
if (preference.getKey() == null)
return false;
if (preference.getKey() == null) return false;
switch (preference.getKey()){
case "tv_ui":
((PreferencesActivity) getActivity()).setRestartApp();
return true;
case "enable_black_theme":
((PreferencesActivity) getActivity()).exitAndRescan();
return true;
case "daynight":
AppCompatDelegate.setDefaultNightMode(((SwitchPreferenceCompat)preference).isChecked() ? AppCompatDelegate.MODE_NIGHT_AUTO : AppCompatDelegate.MODE_NIGHT_NO);
return true;
}
return super.onPreferenceTreeClick(preference);
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if (key.equals("set_locale")) UiTools.restartDialog(getActivity());
else if (key.equals("browser_show_all_files")) ((PreferencesActivity) getActivity()).setRestart();
switch (key) {
case "set_locale":
UiTools.restartDialog(getActivity());
break;
case "browser_show_all_files":
((PreferencesActivity) getActivity()).setRestart();
break;
case "app_theme":
((PreferencesActivity) getActivity()).exitAndRescan();
break;
}
}
private void prepareLocaleList() {
......
......@@ -55,10 +55,9 @@ public class PreferencesUi extends BasePreferenceFragment implements SharedPrefe
findPreference("enable_clone_mode").setVisible(false);
findPreference("tv_ui").setVisible(AndroidDevices.hasTsp);
findPreference("enable_black_theme").setVisible(false);
findPreference("app_theme").setVisible(false);
findPreference("secondary_display_category").setVisible(false);
findPreference("secondary_display_category_summary").setVisible(false);
findPreference("daynight").setVisible(false);
findPreference("blurred_cover_background").setVisible(false);
findPreference("resume_playback").setVisible(false);
prepareLocaleList();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment