1 /* -*- Mode: java; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * xscreensaver, Copyright (c) 2016 Jamie Zawinski <jwz@jwz.org>
3 * and Dennis Sheil <dennis@panaceasupplies.com>
5 * Permission to use, copy, modify, distribute, and sell this software and its
6 * documentation for any purpose is hereby granted without fee, provided that
7 * the above copyright notice appear in all copies and that both that
8 * copyright notice and this permission notice appear in supporting
9 * documentation. No representations are made about the suitability of this
10 * software for any purpose. It is provided "as is" without express or
13 * The superclass of every saver's preferences panel.
15 * The only reason the subclasses of this class exist is so that we know
16 * which "_settings.xml" to read -- we extract the base name from self's
19 * project/xscreensaver/res/xml/SAVER_dream.xml refers to it as
20 * android:settingsActivity="SAVER_Settings". If there was some way
21 * to pass an argument from the XML into here, or to otherwise detect
22 * which Dream was instantiating this Settings, we wouldn't need those
23 * hundreds of Settings subclasses.
26 package org.jwz.xscreensaver;
28 import android.content.SharedPreferences;
29 import android.os.Bundle;
31 import android.content.SharedPreferences;
32 import android.preference.PreferenceActivity;
33 import android.preference.Preference;
34 import android.preference.ListPreference;
35 import android.preference.EditTextPreference;
36 import android.preference.CheckBoxPreference;
37 import org.jwz.xscreensaver.SliderPreference;
39 import org.jwz.xscreensaver.R;
41 import java.lang.reflect.Field;
43 public abstract class XScreenSaverSettings extends PreferenceActivity
44 implements SharedPreferences.OnSharedPreferenceChangeListener {
47 protected void onCreate (Bundle icicle) {
48 super.onCreate (icicle);
50 // Extract the saver name from e.g. "BouncingCowSettings"
51 String name = this.getClass().getSimpleName();
52 String tail = "Settings";
53 if (name.endsWith(tail))
54 name = name.substring (0, name.length() - tail.length());
55 name = name.toLowerCase();
57 // #### All of these have been deprecated:
58 // getPreferenceManager()
59 // addPreferencesFromResource(int)
60 // findPreference(CharSequence)
62 getPreferenceManager().setSharedPreferencesName (name);
64 // read R.xml.SAVER_settings dynamically
66 String pref_class = name + "_settings";
67 try { res = R.xml.class.getDeclaredField(pref_class).getInt (null); }
68 catch (Exception e) { }
70 addPreferencesFromResource (res);
72 final int res_final = res;
74 SharedPreferences prefs = getPreferenceManager().getSharedPreferences();
75 prefs.registerOnSharedPreferenceChangeListener (this);
76 updateAllPrefsSummaries (prefs);
78 // Find the "Reset to defaults" button and install a click handler on it.
80 Preference reset = findPreference (name + "_reset");
81 reset.setOnPreferenceClickListener(
82 new Preference.OnPreferenceClickListener() {
84 public boolean onPreferenceClick(Preference preference) {
86 SharedPreferences prefs =
87 getPreferenceManager().getSharedPreferences();
89 // Wipe everything from the preferences hash, then reload defaults.
90 prefs.edit().clear().commit();
91 getPreferenceScreen().removeAll();
92 addPreferencesFromResource (res_final);
94 // I guess we need to re-get this after the removeAll?
95 prefs = getPreferenceManager().getSharedPreferences();
97 // But now we need to iterate over every Preference widget and
98 // push the new value down into it. If you think this all looks
99 // ridiculously non-object-oriented and completely insane, that's
102 Map <String, ?> keys = prefs.getAll();
103 for (Map.Entry <String, ?> entry : keys.entrySet()) {
104 String key = entry.getKey();
105 String val = String.valueOf (entry.getValue());
107 Preference pref = findPreference (key);
108 if (pref instanceof ListPreference) {
109 ((ListPreference) pref).setValue (prefs.getString (key, ""));
110 } else if (pref instanceof SliderPreference) {
111 ((SliderPreference) pref).setValue (prefs.getFloat (key, 0));
112 } else if (pref instanceof EditTextPreference) {
113 ((EditTextPreference) pref).setText (prefs.getString (key, ""));
114 } else if (pref instanceof CheckBoxPreference) {
115 ((CheckBoxPreference) pref).setChecked (
116 prefs.getBoolean (key,false));
119 updatePrefsSummary (prefs, pref);
127 protected void onResume() {
129 SharedPreferences prefs = getPreferenceManager().getSharedPreferences();
130 prefs.registerOnSharedPreferenceChangeListener (this);
131 updateAllPrefsSummaries(prefs);
135 protected void onPause() {
136 getPreferenceManager().getSharedPreferences().
137 unregisterOnSharedPreferenceChangeListener(this);
142 protected void onDestroy() {
143 getPreferenceManager().getSharedPreferences().
144 unregisterOnSharedPreferenceChangeListener(this);
148 public void onSharedPreferenceChanged (SharedPreferences sharedPreferences,
150 updatePrefsSummary(sharedPreferences, findPreference(key));
153 protected void updatePrefsSummary(SharedPreferences sharedPreferences,
158 if (pref instanceof ListPreference) {
159 pref.setTitle (((ListPreference) pref).getEntry());
160 } else if (pref instanceof SliderPreference) {
161 float v = ((SliderPreference) pref).getValue();
162 int i = (int) Math.floor (v);
164 pref.setSummary (String.valueOf (i));
166 pref.setSummary (String.valueOf (v));
167 } else if (pref instanceof EditTextPreference) {
168 pref.setSummary (((EditTextPreference) pref).getText());
172 protected void updateAllPrefsSummaries(SharedPreferences prefs) {
174 Map <String, ?> keys = prefs.getAll();
175 for (Map.Entry <String, ?> entry : keys.entrySet()) {
176 updatePrefsSummary (prefs, findPreference (entry.getKey()));