Adjusting the SettingsActivity

Case discrimination with AppCompatDelegate to cover both cases.
This commit is contained in:
ErikWaegerle 2020-06-24 16:37:59 +02:00
parent 650bd797ba
commit 1106ee84bb

View file

@ -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,11 +50,42 @@ 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);
}
public void restartActivity() {
Intent i = new Intent(getApplicationContext(), MainActivity.class);
startActivity(i);
finish();
}
private void setupActionBar () {
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
@ -69,7 +107,6 @@ public class SettingsActivity extends AppCompatPreferenceActivity {
return super.onMenuItemSelected(featureId, item);
}
/**
* {@inheritDoc}
*/