From 1106ee84bb4cf81f2b7bd0279565e0dace2b0c40 Mon Sep 17 00:00:00 2001 From: ErikWaegerle Date: Wed, 24 Jun 2020 16:37:59 +0200 Subject: [PATCH] Adjusting the SettingsActivity Case discrimination with AppCompatDelegate to cover both cases. --- .../ui/SettingsActivity.java | 47 +++++++++++++++++-- 1 file changed, 42 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/org/secuso/privacyfriendlysudoku/ui/SettingsActivity.java b/app/src/main/java/org/secuso/privacyfriendlysudoku/ui/SettingsActivity.java index 448b560..dc7da54 100644 --- a/app/src/main/java/org/secuso/privacyfriendlysudoku/ui/SettingsActivity.java +++ b/app/src/main/java/org/secuso/privacyfriendlysudoku/ui/SettingsActivity.java @@ -3,6 +3,8 @@ package org.secuso.privacyfriendlysudoku.ui; import android.annotation.TargetApi; import android.content.Context; +import android.content.Intent; +import android.content.SharedPreferences; import android.content.res.Configuration; import android.os.Build; import android.os.Bundle; @@ -10,14 +12,19 @@ import android.preference.ListPreference; import android.preference.Preference; import android.preference.PreferenceActivity; import androidx.appcompat.app.ActionBar; +import androidx.appcompat.app.AppCompatDelegate; + import android.preference.PreferenceFragment; import android.preference.PreferenceManager; import android.view.MenuItem; import android.view.View; +import android.widget.CompoundButton; +import android.widget.Switch; import org.secuso.privacyfriendlysudoku.ui.view.R; import java.util.List; +import java.util.Map; /** * A {@link PreferenceActivity} that presents a set of application settings. On @@ -43,12 +50,43 @@ public class SettingsActivity extends AppCompatPreferenceActivity { mainContent.setAlpha(0); mainContent.animate().alpha(1).setDuration(BaseActivity.MAIN_CONTENT_FADEIN_DURATION); } + /** + * Set up the {@link android.app.ActionBar}, if the API is available. + */ + + SharedPreferences preferenceManager = PreferenceManager + .getDefaultSharedPreferences(this); + + SharedPreferences.OnSharedPreferenceChangeListener x = new SharedPreferences.OnSharedPreferenceChangeListener() { + + @Override + public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { + + System.out.println(key); + if (key.equals("pref_dark_mode_setting")) { + if (sharedPreferences.getBoolean(key, false )) { + AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES); + restartActivity(); + } else { + AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO); + restartActivity(); + } + + } + } + }; + // Listener registrieren + preferenceManager.registerOnSharedPreferenceChangeListener(x); + } - /** - * Set up the {@link android.app.ActionBar}, if the API is available. - */ - private void setupActionBar() { + public void restartActivity() { + Intent i = new Intent(getApplicationContext(), MainActivity.class); + startActivity(i); + finish(); + } + + private void setupActionBar () { ActionBar actionBar = getSupportActionBar(); if (actionBar != null) { // Show the Up button in the action bar. @@ -69,7 +107,6 @@ public class SettingsActivity extends AppCompatPreferenceActivity { return super.onMenuItemSelected(featureId, item); } - /** * {@inheritDoc} */