PageRenderTime 73ms CodeModel.GetById 40ms app.highlight 13ms RepoModel.GetById 18ms app.codeStats 0ms

/FallingSandpaper/src/sand/wallpaper/opengl/Preferences.java

http://thelements.googlecode.com/
Java | 162 lines | 122 code | 17 blank | 23 comment | 20 complexity | 1189a128155a0c8c9bf686508d9213ba MD5 | raw file
  1package sand.wallpaper.opengl;
  2
  3import android.content.Context;
  4import android.content.SharedPreferences;
  5import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
  6import android.os.Bundle;
  7import android.preference.CheckBoxPreference;
  8import android.preference.ListPreference;
  9import android.preference.PreferenceActivity;
 10import android.preference.PreferenceCategory;
 11import android.preference.PreferenceManager;
 12import android.preference.PreferenceScreen;
 13import android.util.Log;
 14
 15public class Preferences extends PreferenceActivity implements OnSharedPreferenceChangeListener
 16{
 17	@Override
 18	protected void onCreate(Bundle savedInstanceState)
 19	{
 20		super.onCreate(savedInstanceState);
 21		
 22		getPreferenceManager().setSharedPreferencesName("TheElementsPrefs");
 23		
 24		//Set the preference screen created by createPreferenceHierarchy()
 25		setPreferenceScreen(createPreferenceHierarchy());
 26		getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
 27	}
 28	
 29	//Function to set up the preferences
 30	private PreferenceScreen createPreferenceHierarchy()
 31	{
 32		//Define the root PreferenceScreen
 33		PreferenceScreen root = getPreferenceManager().createPreferenceScreen(this);
 34		
 35		//Look and feel category and add to root
 36		PreferenceCategory look_feel_cat = new PreferenceCategory(this);
 37		look_feel_cat.setTitle(R.string.look_feel_title);
 38		root.addPreference(look_feel_cat);
 39		
 40        //Background color list preference
 41        ListPreference back_color_pref = new ListPreference(this);
 42        back_color_pref.setEntries(R.array.entries_list_preference);
 43        back_color_pref.setEntryValues(R.array.entryvalues_list_preference);
 44        back_color_pref.setDialogTitle(R.string.back_color_pref_title);
 45        back_color_pref.setKey("back_color_pref");
 46        back_color_pref.setTitle(R.string.back_color_pref_title);
 47        back_color_pref.setSummary(R.string.back_color_pref_summary);
 48        root.addPreference(back_color_pref);
 49        
 50        //Other category
 51        PreferenceCategory other_cat = new PreferenceCategory(this);
 52        other_cat.setTitle(R.string.other_title);
 53        root.addPreference(other_cat);
 54        
 55        //Flip screen checkbox
 56        CheckBoxPreference flip_screen_pref = new CheckBoxPreference(this);
 57        flip_screen_pref.setTitle("Flip Screen");
 58        flip_screen_pref.setSummary("Flip screen (use only if nothing draws on screen)");
 59        flip_screen_pref.setKey("flip_screen_pref");
 60        root.addPreference(flip_screen_pref);
 61        
 62        //Game category
 63        PreferenceCategory game_cat = new PreferenceCategory(this);
 64        game_cat.setTitle(R.string.game_cat_title);
 65        root.addPreference(game_cat);
 66        
 67        //Accelerometer checkbox
 68        CheckBoxPreference accel_pref = new CheckBoxPreference(this);
 69        accel_pref.setTitle(R.string.accel_pref_title);
 70        accel_pref.setSummary(R.string.accel_pref_summary);
 71        accel_pref.setKey("accel_pref");
 72        //root.addPreference(accel_pref);
 73        
 74        //Random checkbox
 75        CheckBoxPreference random_pref = new CheckBoxPreference(this);
 76        random_pref.setTitle(R.string.random_pref_title);
 77        random_pref.setSummary(R.string.random_pref_summary);
 78        random_pref.setKey("random_pref");
 79        root.addPreference(random_pref);
 80        
 81        //Create the shared prefs used to access all this
 82        final SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
 83        
 84        //Set default values
 85        if(!sharedPrefs.contains("back_color_pref"))
 86        	back_color_pref.setValue("black");
 87        if(!sharedPrefs.contains("flip_screen_pref"))
 88        	flip_screen_pref.setChecked(false);
 89        /*if(!sharedPrefs.contains("accel_pref"))
 90        	accel_pref.setChecked(false);*/
 91        if(!sharedPrefs.contains("random_pref"))
 92        	random_pref.setChecked(true);
 93        
 94        return root;
 95	}
 96	
 97	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
 98	{
 99		if(key == "back_color_pref")
100		{
101			if(sharedPreferences.getString("back_color_pref", "black").equalsIgnoreCase("black"))
102			{
103				DemoActivity.setBackgroundColor(0);
104			}
105			else
106			{
107				DemoActivity.setBackgroundColor(1);
108			}
109		}
110		else if(key == "flip_screen_pref")
111		{
112			if(sharedPreferences.getBoolean("flip_screen_pref", false) == false)
113			{
114				DemoActivity.setFlip(0);
115			}
116			else
117			{
118				DemoActivity.setFlip(1);
119			}
120		}
121		else if(key == "accel_pref")
122		{
123			if(sharedPreferences.getBoolean("accel_pref", false) == false)
124			{
125				DemoActivity.setAccelOnOff(0);
126			}
127			else
128			{
129				DemoActivity.setAccelOnOff(1);
130			}
131		}
132	}
133	
134	public static void setPreferences(Context context)
135	{
136		final SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
137		if(sharedPreferences.getString("back_color_pref", "black") == "black")
138		{
139			DemoActivity.setBackgroundColor(0);
140		}
141		else
142		{
143			DemoActivity.setBackgroundColor(1);
144		}
145		if(sharedPreferences.getBoolean("flip_screen_pref", false) == false)
146		{
147			DemoActivity.setFlip(0);
148		}
149		else
150		{
151			DemoActivity.setFlip(1);
152		}
153		/*if(sharedPreferences.getBoolean("accel_pref", false) == false)
154		{
155			DemoActivity.setAccelOnOff(0);
156		}
157		else
158		{
159			DemoActivity.setAccelOnOff(1);
160		}*/
161	}
162}