From 42aa846accf450791edf0b254db044867179eb41 Mon Sep 17 00:00:00 2001 From: gitknilch Date: Sun, 24 Feb 2013 21:13:19 +0100 Subject: [PATCH] try to use the andriod way of Preferences and ruin everything[tm] --- AndroidManifest.xml | 2 +- bin/.gitignore | 2 + gen/.gitignore | 1 + res/{drawable => drawable-mdpi}/icon.png | Bin .../kshisen_bgnd.png | Bin res/{drawable => drawable-mdpi}/tileset.png | Bin res/layout/main.xml | 12 --- res/layout/options.xml | 37 ------- res/values/arrays.xml | 9 ++ res/values/strings.xml | 8 +- res/xml/preferences.xml | 23 ++++ src/de/cwde/shisensho/SettingsActivity.java | 63 +++++++++++ src/de/cwde/shisensho/ShisenSho.java | 25 ++--- src/de/cwde/shisensho/ShisenShoActivity.java | 5 +- .../shisensho/ShisenShoOptionsActivity.java | 101 ------------------ src/de/cwde/shisensho/ShisenShoView.java | 10 +- 16 files changed, 123 insertions(+), 175 deletions(-) create mode 100644 bin/.gitignore create mode 100644 gen/.gitignore rename res/{drawable => drawable-mdpi}/icon.png (100%) rename res/{drawable => drawable-mdpi}/kshisen_bgnd.png (100%) rename res/{drawable => drawable-mdpi}/tileset.png (100%) delete mode 100644 res/layout/main.xml delete mode 100644 res/layout/options.xml create mode 100644 res/xml/preferences.xml create mode 100644 src/de/cwde/shisensho/SettingsActivity.java delete mode 100644 src/de/cwde/shisensho/ShisenShoOptionsActivity.java diff --git a/AndroidManifest.xml b/AndroidManifest.xml index a517c81..d197126 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -16,7 +16,7 @@ diff --git a/bin/.gitignore b/bin/.gitignore new file mode 100644 index 0000000..d6b7ef3 --- /dev/null +++ b/bin/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore diff --git a/gen/.gitignore b/gen/.gitignore new file mode 100644 index 0000000..7673daa --- /dev/null +++ b/gen/.gitignore @@ -0,0 +1 @@ +de diff --git a/res/drawable/icon.png b/res/drawable-mdpi/icon.png similarity index 100% rename from res/drawable/icon.png rename to res/drawable-mdpi/icon.png diff --git a/res/drawable/kshisen_bgnd.png b/res/drawable-mdpi/kshisen_bgnd.png similarity index 100% rename from res/drawable/kshisen_bgnd.png rename to res/drawable-mdpi/kshisen_bgnd.png diff --git a/res/drawable/tileset.png b/res/drawable-mdpi/tileset.png similarity index 100% rename from res/drawable/tileset.png rename to res/drawable-mdpi/tileset.png diff --git a/res/layout/main.xml b/res/layout/main.xml deleted file mode 100644 index 3a5f117..0000000 --- a/res/layout/main.xml +++ /dev/null @@ -1,12 +0,0 @@ - - - - diff --git a/res/layout/options.xml b/res/layout/options.xml deleted file mode 100644 index 1c1352f..0000000 --- a/res/layout/options.xml +++ /dev/null @@ -1,37 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - diff --git a/res/values/arrays.xml b/res/values/arrays.xml index d24dbe9..14a60b4 100644 --- a/res/values/arrays.xml +++ b/res/values/arrays.xml @@ -9,5 +9,14 @@ Easy Hard + + + 21 + + + 1 + 2 + 3 + diff --git a/res/values/strings.xml b/res/values/strings.xml index 4b0535b..ba7f9f8 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -17,8 +17,8 @@ Get the source code: TODO Size Difficulty - Size: - Difficulty: - Gravity: - Time counter: + 2 + 1 + Time Counter + Gravity diff --git a/res/xml/preferences.xml b/res/xml/preferences.xml new file mode 100644 index 0000000..327f350 --- /dev/null +++ b/res/xml/preferences.xml @@ -0,0 +1,23 @@ + + + + + + + \ No newline at end of file diff --git a/src/de/cwde/shisensho/SettingsActivity.java b/src/de/cwde/shisensho/SettingsActivity.java new file mode 100644 index 0000000..f2ceaa2 --- /dev/null +++ b/src/de/cwde/shisensho/SettingsActivity.java @@ -0,0 +1,63 @@ +package de.cwde.shisensho; + +import android.content.SharedPreferences; +import android.content.SharedPreferences.OnSharedPreferenceChangeListener; +import android.os.Bundle; +import android.preference.*; + +public class SettingsActivity extends PreferenceActivity + implements OnSharedPreferenceChangeListener { + + private ShisenSho app; + + private static final String KEY_PREF_DIFF = "pref_diff"; + private static final String KEY_PREF_SIZE = "pref_size"; + //private static final String KEY_PREF_GRAV = "pref_grav"; + //private static final String KEY_PREF_TIME = "pref_time"; + + @SuppressWarnings("deprecation") + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + app = ShisenSho.app(); + addPreferencesFromResource(R.xml.preferences); + } + + @Override + public void onBackPressed() { + app.setOptions(); + super.onBackPressed(); + } + + @SuppressWarnings("deprecation") + @Override + protected void onResume() { + super.onResume(); + getPreferenceScreen().getSharedPreferences() + .registerOnSharedPreferenceChangeListener(this); + } + + @SuppressWarnings("deprecation") + @Override + protected void onPause() { + super.onPause(); + getPreferenceScreen().getSharedPreferences() + .unregisterOnSharedPreferenceChangeListener(this); + } + + + public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { + if (key.equals(KEY_PREF_DIFF)) { + @SuppressWarnings("deprecation") + Preference myPref = findPreference(key); + // Set summary to be the user-description for the selected value + myPref.setSummary(sharedPreferences.getString(key, "")); + } + if (key.equals(KEY_PREF_SIZE)) { + @SuppressWarnings("deprecation") + Preference myPref = findPreference(key); + // Set summary to be the user-description for the selected value + myPref.setSummary(sharedPreferences.getString(key, "")); + } + } +} diff --git a/src/de/cwde/shisensho/ShisenSho.java b/src/de/cwde/shisensho/ShisenSho.java index 8fe7d30..654e4e0 100644 --- a/src/de/cwde/shisensho/ShisenSho.java +++ b/src/de/cwde/shisensho/ShisenSho.java @@ -1,7 +1,8 @@ package de.cwde.shisensho; import android.app.Application; -import android.os.Bundle; +import android.content.SharedPreferences; +import android.preference.PreferenceManager; import android.util.Log; public class ShisenSho extends Application { @@ -70,22 +71,16 @@ public class ShisenSho extends Application { @Override public void onCreate() { super.onCreate(); + PreferenceManager.setDefaultValues(this, R.xml.preferences, false); } - public Bundle getOptions() { - Bundle options = new Bundle(); - options.putInt("size", size); - options.putInt("difficulty", difficulty); - options.putBoolean("gravity", gravity); - options.putBoolean("timeCounter", timeCounter); - return options; - } - - public void setOptions(Bundle options) { - int size = options.getInt("size"); - int difficulty = options.getInt("difficulty"); - boolean gravity = options.getBoolean("gravity"); - boolean timeCounter = options.getBoolean("timeCounter"); + public void setOptions() { + SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this); + + int size = sharedPref.getInt("size", 1); + int difficulty = sharedPref.getInt("difficulty", 1); + boolean gravity = sharedPref.getBoolean("gravity", true); + boolean timeCounter = sharedPref.getBoolean("timeCounter", true); boolean needsReset = false; diff --git a/src/de/cwde/shisensho/ShisenShoActivity.java b/src/de/cwde/shisensho/ShisenShoActivity.java index b87979f..8ba59af 100644 --- a/src/de/cwde/shisensho/ShisenShoActivity.java +++ b/src/de/cwde/shisensho/ShisenShoActivity.java @@ -1,7 +1,5 @@ package de.cwde.shisensho; -import de.cwde.shisensho.R; - import android.app.Activity; import android.app.AlertDialog; import android.content.Intent; @@ -9,6 +7,7 @@ import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; import android.os.Bundle; +import android.preference.PreferenceManager; import android.text.SpannableString; import android.text.util.Linkify; import android.view.Menu; @@ -25,6 +24,8 @@ public class ShisenShoActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + + PreferenceManager.setDefaultValues(this, R.xml.preferences, false); requestWindowFeature(Window.FEATURE_NO_TITLE); //getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, diff --git a/src/de/cwde/shisensho/ShisenShoOptionsActivity.java b/src/de/cwde/shisensho/ShisenShoOptionsActivity.java deleted file mode 100644 index 9d3ecbb..0000000 --- a/src/de/cwde/shisensho/ShisenShoOptionsActivity.java +++ /dev/null @@ -1,101 +0,0 @@ -package de.cwde.shisensho; - -import java.io.Serializable; - -import de.cwde.shisensho.R; - -import android.app.Activity; -import android.os.Bundle; -import android.view.View; -import android.view.Window; -import android.widget.AdapterView; -import android.widget.ArrayAdapter; -import android.widget.CompoundButton; -import android.widget.Spinner; -import android.widget.ToggleButton; - -public class ShisenShoOptionsActivity extends Activity { - - Bundle state; - ShisenSho app; - - private void appToState (boolean merge) { - String[] fields = { "size", "difficulty", "gravity", "timeCounter" }; - Bundle options = app.getOptions(); - if (state == null) state = new Bundle(); - for (int i=0; i arg0, View arg1, - int pos, long arg3) { - state.putInt("size", pos+1); - } - - public void onNothingSelected(AdapterView arg0) { } - }); - - s = (Spinner) findViewById(R.id.difficulty); - adapter = ArrayAdapter.createFromResource( - this, R.array.difficulties, android.R.layout.simple_spinner_item); - adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); - s.setAdapter(adapter); - s.setSelection(2-state.getInt("difficulty")); - s.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { - public void onItemSelected(AdapterView arg0, View arg1, - int pos, long arg3) { - state.putInt("difficulty", 2-pos); - } - - public void onNothingSelected(AdapterView arg0) { } - }); - - tb = (ToggleButton) findViewById(R.id.gravity); - tb.setChecked(state.getBoolean("gravity")); - tb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { - public void onCheckedChanged(CompoundButton arg0, boolean arg1) { - state.putBoolean("gravity", arg1); - } - }); - - tb = (ToggleButton) findViewById(R.id.timeCounter); - tb.setChecked(state.getBoolean("timeCounter")); - tb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { - public void onCheckedChanged(CompoundButton arg0, boolean arg1) { - state.putBoolean("timeCounter", arg1); - } - }); - } - - @Override - public void onBackPressed() { - app.setOptions(state); - super.onBackPressed(); - } - -} diff --git a/src/de/cwde/shisensho/ShisenShoView.java b/src/de/cwde/shisensho/ShisenShoView.java index cb1a57c..48d16cb 100644 --- a/src/de/cwde/shisensho/ShisenShoView.java +++ b/src/de/cwde/shisensho/ShisenShoView.java @@ -5,8 +5,6 @@ import java.util.Locale; import java.util.Timer; import java.util.TimerTask; -import de.cwde.shisensho.R; - import android.app.Activity; import android.content.Context; import android.graphics.Bitmap; @@ -64,6 +62,11 @@ class ShisenShoView extends SurfaceView implements SurfaceHolder.Callback { surfaceHolder.addCallback(this); } + public ShisenShoView(Context ctx) { + super((Context)ctx); + // silence lint? + } + private void paint(StatePaint pstate) { this.pstate=pstate; repaint(); @@ -229,6 +232,7 @@ class ShisenShoView extends SurfaceView implements SurfaceHolder.Callback { } } + @SuppressWarnings("deprecation") public void drawMessage(Canvas canvas, int x, int y, boolean centered, String message, String color, float textSize) { Paint paint = new Paint(); paint.setColor(Color.parseColor(color)); @@ -659,4 +663,4 @@ class ShisenShoView extends SurfaceView implements SurfaceHolder.Callback { paint.setFlags(Paint.ANTI_ALIAS_FLAG); } */ -} \ No newline at end of file +} -- 2.39.5