package fire.eagle.android;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.*;
import jfireeagle.Token;
import java.util.*;
public class Preferences
{
static private SharedPreferences prefs;
static private SharedPreferences.Editor editor;
static private final String TRANSMIT_FREQUENCY = "fireeagle.transmit.frequency.minutes";
static private final int FIFTEEN_MINUTES = 15;
static private final int SIXTY_MINUTES = 60;
static private final int DEFAULT_TRANSMIT_FREQUENCY = SIXTY_MINUTES;
static private final String REQUEST_TOKEN_NAME = "request";
static private final String USER_SPECIFIC_ACCESS_TOKEN_NAME = "user-specific-access";
static private final String GENERAL_ACCESS_TOKEN_NAME = "general-access";
static private final String TOKEN_PREFIX = "oauth.token";
static private final String TOKEN_PUBLIC_KEY = "public";
static private final String TOKEN_SECRET = "secret";
static private final long MILLISECONDS_PER_MINUTE = (60 * 1000);
public static void reload(Context ctx)
{
prefs = PreferenceManager.getDefaultSharedPreferences(ctx);
editor = prefs.edit();
}
static private Token getToken(String name)
{
Token t = new Token();
t.setPublicKey(prefs.getString(TOKEN_PREFIX + "." + name + "." + TOKEN_PUBLIC_KEY, null));
t.setSecret(prefs.getString(TOKEN_PREFIX + "." + name + "." + TOKEN_SECRET, null));
System.out.println("got yer Token: " + name + " --- " + t.toString());
return t;
}
static private void updateToken(String name, Token t)
{
editor.putString(TOKEN_PREFIX + "." + name + "." + TOKEN_PUBLIC_KEY, t.getPublicKey());
editor.putString(TOKEN_PREFIX + "." + name + "." + TOKEN_SECRET, t.getSecret());
System.out.println("updated Token: " + name + " --- " + t.toString());
}
static public Token getRequestToken()
{
return getToken(REQUEST_TOKEN_NAME);
}
static public void updateRequestToken(Token t)
{
updateToken(REQUEST_TOKEN_NAME, t);
}
static public Token getUserSpecificAccessToken()
{
return getToken(USER_SPECIFIC_ACCESS_TOKEN_NAME);
}
static public void updateUserSpecificAccessToken(Token t)
{
updateToken(USER_SPECIFIC_ACCESS_TOKEN_NAME, t);
}
static public Token getGeneralAccessToken()
{
return getToken(GENERAL_ACCESS_TOKEN_NAME);
}
static public void updateGeneralAccessToken(Token t)
{
updateToken(GENERAL_ACCESS_TOKEN_NAME, t);
}
static public void updateTransmitFrequency(int minutes)
{
editor.putInt(TRANSMIT_FREQUENCY, minutes);
}
/*
*
* @return number of minutes
*
*/
static public int getTransmitFrequency()
{
int frequency = 0;
String f = prefs.getString(TRANSMIT_FREQUENCY, "" + DEFAULT_TRANSMIT_FREQUENCY);
try
{
frequency = Integer.parseInt(f);
}
catch (Exception ex)
{
frequency = 0;
}
return frequency;
}
static public long getTransmitFrequencyInMilliseconds()
{
long minutes = getTransmitFrequency();
return (minutes * MILLISECONDS_PER_MINUTE);
}
static public void commit()
{
editor.commit();
}
static public List<Preference> getUserVisiblePreferences(Context ctx)
{
List<Preference> list = new ArrayList<Preference>();
ListPreference p = new ListPreference(ctx);
p.setEnabled(true);
p.setKey(TRANSMIT_FREQUENCY);
p.setDefaultValue("" + DEFAULT_TRANSMIT_FREQUENCY);
p.setPersistent(true);
p.setSelectable(true);
p.setTitle("Transmit frequency");
p.setSummary("Transmit frequency in minutes");
p.setDialogTitle("Transmit frequency");
CharSequence[] entries = {
"10 minutes",
"15 minutes",
"30 minutes",
"60 minutes",
"Never"
};
CharSequence[] entryValues = {
"10",
"15",
"30",
"60",
"-1"
};
p.setEntries(entries);
p.setEntryValues(entryValues);
list.add(p);
return list;
}
}