PageRenderTime 347ms CodeModel.GetById 111ms app.highlight 65ms RepoModel.GetById 116ms app.codeStats 1ms

/TheElements/src/sand/falling/opengl/preferences/PreferencesFromCode.java

http://thelements.googlecode.com/
Java | 206 lines | 169 code | 28 blank | 9 comment | 29 complexity | 89f6b67f40b0e2af6eb36b89456b76a7 MD5 | raw file
  1package sand.falling.opengl.preferences;
  2
  3
  4import sand.falling.opengl.MainActivity;
  5import sand.falling.opengl.R;
  6import android.content.Context;
  7import android.content.SharedPreferences;
  8import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
  9import android.os.Bundle;
 10import android.preference.CheckBoxPreference;
 11import android.preference.ListPreference;
 12import android.preference.PreferenceActivity;
 13import android.preference.PreferenceCategory;
 14import android.preference.PreferenceManager;
 15import android.preference.PreferenceScreen;
 16import android.util.Log;
 17import android.widget.Toast;
 18
 19
 20public class PreferencesFromCode extends PreferenceActivity implements OnSharedPreferenceChangeListener
 21{
 22
 23    @Override
 24    protected void onCreate(Bundle savedInstanceState)
 25    {
 26        super.onCreate(savedInstanceState);
 27        
 28        setPreferenceScreen(createPreferenceHierarchy());
 29        getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
 30    }
 31
 32    private PreferenceScreen createPreferenceHierarchy()
 33    {
 34        // Root
 35        PreferenceScreen root = getPreferenceManager().createPreferenceScreen(this);
 36        
 37        // Inline preferences 
 38        PreferenceCategory inlinePrefCat = new PreferenceCategory(this);
 39        inlinePrefCat.setTitle("Look and Feel");
 40        root.addPreference(inlinePrefCat);
 41        
 42       
 43        
 44        // List preference
 45        ListPreference listPref = new ListPreference(this);
 46        listPref.setEntries(R.array.entries_list_preference);
 47        listPref.setEntryValues(R.array.entryvalues_list_preference);
 48        listPref.setDialogTitle("Background");
 49        listPref.setKey("background_color");
 50        listPref.setTitle("Background Color");
 51        listPref.setSummary("Sets the color of the background");
 52        
 53        root.addPreference(listPref);
 54
 55       
 56        PreferenceCategory launchPrefCat = new PreferenceCategory(this);
 57        launchPrefCat.setTitle("Other");
 58        root.addPreference(launchPrefCat);
 59        
 60        CheckBoxPreference togglePref = new CheckBoxPreference(this);
 61        togglePref.setKey("flip_settings");
 62        togglePref.setTitle("Flip Screen");
 63        togglePref.setSummary("Flip Screen (Use only if nothing draws on screen)");
 64        root.addPreference(togglePref);
 65        
 66        CheckBoxPreference sleep_on_off = new CheckBoxPreference(this);
 67        sleep_on_off.setKey("sleep");
 68        sleep_on_off.setTitle("Sleep On/Off");
 69        sleep_on_off.setSummary("Allow the screen to sleep during play or not");
 70        root.addPreference(sleep_on_off);
 71        
 72        CheckBoxPreference ui_on_off = new CheckBoxPreference(this);
 73        ui_on_off.setKey("ui");
 74        ui_on_off.setTitle("UI On/Off");
 75        ui_on_off.setSummary("Turn the UI on or off");
 76        root.addPreference(ui_on_off);
 77        
 78        PreferenceCategory game = new PreferenceCategory(this);
 79        game.setTitle("Game");
 80        root.addPreference(game);
 81        
 82        CheckBoxPreference acceler = new CheckBoxPreference(this);
 83        acceler.setKey("accel_control");
 84        acceler.setTitle("Accelerometer Control");
 85        acceler.setSummary("Turn accelerometer control of gravity on/off ");
 86        root.addPreference(acceler);
 87        
 88        
 89        final SharedPreferences sharedPrefs =
 90            PreferenceManager.getDefaultSharedPreferences(this);
 91        if (!sharedPrefs.contains("background_color"))
 92        {
 93        	listPref.setValue("black");
 94        }
 95        if (!sharedPrefs.contains("flip_settings"))
 96        {
 97        	togglePref.setChecked(false);        	
 98        }
 99        if (!sharedPrefs.contains("ui"))
100        {
101        	ui_on_off.setChecked(true);
102        }
103        if (!sharedPrefs.contains("sleep"))
104        {
105        	sleep_on_off.setChecked(true);
106        }
107        if (!sharedPrefs.contains("accel_control"))
108        {
109        	acceler.setChecked(false);
110        }
111        
112        return root;
113    }
114    
115    //When something is clicked, this finds it and calls a JNI function to change it in the c code
116    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
117    {
118        // Let's do something when my counter preference value changes
119        //Log.v("DemoActivity", "onpref called");
120        if (key == "background_color")
121        {
122        	//Log.v("DemoActivity", "onpref color: " + sharedPreferences.getString(key, "black"));
123        	if (sharedPreferences.getString( key, "black").equals("black"))
124        	{
125        		MainActivity.setBackgroundColor(0);
126        	}
127        	else
128        	{
129        		MainActivity.setBackgroundColor(1);
130        	}
131        }
132        else if (key == "flip_settings")
133        {
134        	Log.v("DemoActivity", "onpref flip");
135        	if ( sharedPreferences.getBoolean(key, false) == true)
136        	{
137        		MainActivity.setFlip(1);
138        	}
139        	else
140        	{
141        		MainActivity.setFlip(0);
142        	}
143        }
144        else if (key == "ui")
145        {
146        	MainActivity.ui = sharedPreferences.getBoolean(key, true);
147        	Toast.makeText(this, "UI changed: please restart for effect", Toast.LENGTH_LONG).show();
148        }
149        else if (key == "sleep")
150        {
151        	MainActivity.sand_view.setKeepScreenOn(sharedPreferences.getBoolean(key,true));
152        }
153        else if (key == "accel_control")
154        {
155        	if ( sharedPreferences.getBoolean(key,false) == true){
156        		MainActivity.setAccelOnOff(1);
157        	}
158        	else{
159        		MainActivity.setAccelOnOff(0);
160        	}
161        }
162    }
163    //used to set stuff up when the app opens and retrieve the settings
164    public static void setpreferences(Context context)
165    {
166        final SharedPreferences sharedPreferences =
167            PreferenceManager.getDefaultSharedPreferences(context);
168    	if (sharedPreferences.getString("background_color", "black").equals("black"))
169    	{
170    		MainActivity.setBackgroundColor(0);
171    	}
172    	else
173    	{  
174    		MainActivity.setBackgroundColor(1);
175    	}
176    	if (sharedPreferences.getBoolean("flip_settings", false) == true) //flip screen stuff
177    	{
178    		MainActivity.setFlip(1);
179    	}
180    	else
181    	{
182    		MainActivity.setFlip(0);
183    	}
184    	if (sharedPreferences.getBoolean("accel_control", false) == true) //flip screen stuff
185    	{
186    		MainActivity.setAccelOnOff(1);
187    	}
188    	else
189    	{
190    		MainActivity.setAccelOnOff(0);
191    	}
192    	
193 
194    	
195    	//Set the bool variable ui in the Activity
196    	MainActivity.ui = sharedPreferences.getBoolean("ui", true);
197    }
198    public static void setScreenOnOff(Context context) //set screen on or off, needs to be seperate because sand_view is created after setPrefs is called
199    {
200    	final SharedPreferences sharedPreferences =
201            PreferenceManager.getDefaultSharedPreferences(context);
202    	
203    	MainActivity.sand_view.setKeepScreenOn(sharedPreferences.getBoolean("screen",true)); //keep screen on or not
204    	
205    }
206}