package com.simplenote.android;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.SystemClock;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.util.Log;

public class Preferences extends PreferenceActivity {
	public static final String EMAIL = "email";
	public static final String PASSWORD = "password";
	public static final String BACKGROUND_ENABLED = "background_enabled";
	public static final String BACKGROUND = "background";
	public static final String TOKEN = "token";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.xml.preferences);
	}
	
	@Override
	protected void onStop() {
		applyPreferences();
		super.onStop();
	}
	
	// TODO: Apply these at startup...
	// TODO: Cancel alarm if checkbox disabled
	public void applyPreferences() {
		SharedPreferences mPrefs = getSharedPreferences(Constants.PREFS_NAME, 0);

		// Set up the AlarmManager service
		Intent intent = new Intent(this, AlarmReceiver.class);
		intent.putExtra(EMAIL, mPrefs.getString(EMAIL, ""));
		intent.putExtra(PASSWORD, mPrefs.getString(PASSWORD, ""));     

		PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
		SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);

		if (preferences.getBoolean(BACKGROUND_ENABLED, false)) {
			Log.d(Constants.TAG, "Applying alarm every " + preferences.getString(Preferences.BACKGROUND, "2"));
			AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
			am.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), 
					60 * 60 * 1000 * Integer.parseInt(preferences.getString(BACKGROUND, "2")), pendingIntent);
		}
	}
}
@@ -26,16 +26,43 @@
 droid;%0A%0A
+import java.util.HashMap;%0A%0A
 import a
@@ -78,32 +78,32 @@
 p.AlarmManager;%0A
-
 import android.a
@@ -112,32 +112,64 @@
 .PendingIntent;%0A
+import android.content.Context;%0A
 import android.c
@@ -497,16 +497,26 @@
 %22email%22;
+ // String
 %0A%09public
@@ -558,16 +558,26 @@
 ssword%22;
+ // String
 %0A%09public
@@ -639,16 +639,27 @@
 nabled%22;
+ // boolean
 %0A%09public
@@ -705,16 +705,23 @@
 ground%22;
+ // int
 %0A%09public
@@ -757,16 +757,26 @@
 %22token%22;
+ // String
 %0A%0A%09@Over
@@ -924,18 +924,17 @@
 es);%0A%09%7D%0A
-%09
 %0A
+
 %09@Overri
@@ -1010,10 +1010,9 @@
 %0A%09%7D%0A
-%09
 %0A
+
 %09// 
@@ -1411,13 +1411,8 @@
 %22));
-     
 %0A%0A%09%09
@@ -2055,12 +2055,1134 @@
 t);%0A
-
 %09%09%7D%0A%09%7D%0A
+%09/**%0A%09 * Get login data from preferences%0A%09 * @param context from which to retrieve preferences%0A%09 * @return a HashMap with preference keys and values%0A%09 */%0A%09public static HashMap%3CString,String%3E getLoginPreferences(Context context) %7B%0A%09%09SharedPreferences prefs = context.getSharedPreferences(Constants.PREFS_NAME, Context.MODE_PRIVATE);%0A%09%09HashMap%3CString,String%3E values = getStringPreferences(prefs, new String%5B%5D %7B Preferences.EMAIL, Preferences.PASSWORD, Preferences.TOKEN %7D);%0A%09%09return values;%0A%09%7D%0A%09/**%0A%09 * Get a set of String values from the Preferences, key won't exist if the default value is returned%0A%09 * @param context from which to retrieve preferences%0A%09 * @param keys of the preference values to retrieve%0A%09 * @return a HashMap with keys as the keys and the corresponding values%0A%09 */%0A%09private static HashMap%3CString,String%3E getStringPreferences(SharedPreferences prefs, String%5B%5D keys) %7B%0A%09%09HashMap%3CString,String%3E hash = new HashMap%3CString, String%3E();%0A%09%09for (String key : keys) %7B%0A%09%09%09String value = prefs.getString(key, %22%22);%0A%09%09%09if (!value.equals(%22%22)) %7B%0A%09%09%09%09hash.put(key, prefs.getString(key, %22%22));%0A%09%09%09%7D%0A%09%09%7D%0A%09%09return hash;%0A%09%7D%0A
 %7D

Add static some utility methods to the Preferences class