package com.simplenote.android;

import java.util.HashMap;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
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"; // String
	public static final String PASSWORD = "password"; // String
	public static final String BACKGROUND_ENABLED = "background_enabled"; // boolean
	public static final String BACKGROUND = "background"; // int
	public static final String TOKEN = "token"; // String

	@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);
		}
	}
	/**
	 * Get login data from preferences
	 * @param context from which to retrieve preferences
	 * @return a HashMap with preference keys and values
	 */
	public static HashMap<String,String> getLoginPreferences(Context context) {
		SharedPreferences prefs = context.getSharedPreferences(Constants.PREFS_NAME, Context.MODE_PRIVATE);
		HashMap<String,String> values = getStringPreferences(prefs, new String[] { Preferences.EMAIL, Preferences.PASSWORD, Preferences.TOKEN });
		return values;
	}
	/**
	 * Get a set of String values from the Preferences, key won't exist if the default value is returned
	 * @param context from which to retrieve preferences
	 * @param keys of the preference values to retrieve
	 * @return a HashMap with keys as the keys and the corresponding values
	 */
	private static HashMap<String,String> getStringPreferences(SharedPreferences prefs, String[] keys) {
		HashMap<String,String> hash = new HashMap<String, String>();
		for (String key : keys) {
			String value = prefs.getString(key, "");
			if (!value.equals("")) {
				hash.put(key, prefs.getString(key, ""));
			}
		}
		return hash;
	}
}
@@ -225,16 +225,65 @@
 rences;%0A
+import android.content.SharedPreferences.Editor;%0A
 import a
@@ -2597,32 +2597,1658 @@
 values;%0A%09%7D%0A%09/**%0A
+%09 * Save login data into preferences%0A%09 * @param context from which to retrieve preferences%0A%09 * @param email to save%0A%09 * @param password to save%0A%09 * @param auth to save%0A%09 * @return a HashMap with preference keys and values%0A%09 */%0A%09public static HashMap%3CString,String%3E setLoginPreferences(Context context, String email, String password, String auth) %7B%0A%09%09HashMap%3CString,String%3E values = new HashMap%3CString,String%3E();%0A%09%09Editor editor = context.getSharedPreferences(Constants.PREFS_NAME, Context.MODE_PRIVATE).edit();%0A%09%09editor.putString(EMAIL, email).putString(PASSWORD, password).putString(TOKEN, auth);%0A%09%09editor.commit();%0A%09%09values.put(EMAIL, email);%0A%09%09values.put(PASSWORD, password);%0A%09%09values.put(TOKEN, auth);%0A%09%09return values;%0A%09%7D%0A%09/**%0A%09 * Save login data into preferences, shortcut for setLoginPreferences(context, email, password, null);%0A%09 * @param context from which to retrieve preferences%0A%09 * @param email to save%0A%09 * @param password to save%0A%09 * @return a HashMap with preference keys and values%0A%09 */%0A%09public static HashMap%3CString,String%3E setLoginPreferences(Context context, String email, String password) %7B%0A%09%09return setLoginPreferences(context, email, password, null);%0A%09%7D%0A%09/**%0A%09 * Save authorization token into preferences%0A%09 * @param context from which to retrieve preferences%0A%09 * @param auth to save%0A%09 * @return true if the new values were successfully written to persistent storage.%0A%09 */%0A%09public static boolean setAuthToken(Context context, String auth) %7B%0A%09%09Editor editor = context.getSharedPreferences(Constants.PREFS_NAME, Context.MODE_PRIVATE).edit();%0A%09%09editor.putString(TOKEN, auth);%0A%09%09return editor.commit();%0A%09%7D%0A%09/**%0A
 %09 * Get a set of

Add utility methods to Preferences for easy saving of data