| 1 | /* |
| 2 | * Tomdroid |
| 3 | * Tomboy on Android |
| 4 | * http://www.launchpad.net/tomdroid |
| 5 | * |
| 6 | * Copyright 2011, Olivier Bilodeau <olivier@bottomlesspit.org> |
| 7 | * Copyright 2009, Benoit Garret <benoit.garret_launchpad@gadz.org> |
| 8 | * |
| 9 | * This file is part of Tomdroid. |
| 10 | * |
| 11 | * Tomdroid is free software: you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License as published by |
| 13 | * the Free Software Foundation, either version 3 of the License, or |
| 14 | * (at your option) any later version. |
| 15 | * |
| 16 | * Tomdroid is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License |
| 22 | * along with Tomdroid. If not, see <http://www.gnu.org/licenses/>. |
| 23 | */ |
| 24 | package org.tomdroid.util; |
| 25 | |
| 26 | import android.content.Context; |
| 27 | import android.content.SharedPreferences; |
| 28 | import android.preference.PreferenceManager; |
| 29 | |
| 30 | public class Preferences { |
| 31 | |
| 32 | public enum Key { |
| 33 | SYNC_SERVICE ("sync_service", "sdcard"), |
| 34 | SYNC_SERVER_ROOT_API ("sync_server_root_api", ""), |
| 35 | SYNC_SERVER_USER_API ("sync_server_user_api", ""), |
| 36 | SYNC_SERVER ("sync_server", "https://one.ubuntu.com/notes"), |
| 37 | CLEAR_SEARCH_HISTORY ("clearSearchHistory", ""), |
| 38 | ACCESS_TOKEN ("access_token", ""), |
| 39 | ACCESS_TOKEN_SECRET ("access_token_secret", ""), |
| 40 | REQUEST_TOKEN ("request_token", ""), |
| 41 | REQUEST_TOKEN_SECRET ("request_token_secret", ""), |
| 42 | OAUTH_10A ("oauth_10a", false), |
| 43 | AUTHORIZE_URL ("authorize_url", ""), |
| 44 | ACCESS_TOKEN_URL ("access_token_url", ""), |
| 45 | REQUEST_TOKEN_URL ("request_token_url", ""), |
| 46 | LATEST_SYNC_REVISION ("latest_sync_revision", 0L), |
| 47 | SORT_ORDER ("sort_order", "sort_date"), |
| 48 | FIRST_RUN ("first_run", true); |
| 49 | |
| 50 | private String name = ""; |
| 51 | private Object defaultValue = ""; |
| 52 | |
| 53 | Key(String name, Object defaultValue) { |
| 54 | this.name = name; |
| 55 | this.defaultValue = defaultValue; |
| 56 | } |
| 57 | |
| 58 | public String getName() { |
| 59 | return name; |
| 60 | } |
| 61 | |
| 62 | public Object getDefault() { |
| 63 | return defaultValue; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | private static SharedPreferences client = null; |
| 68 | private static SharedPreferences.Editor editor = null; |
| 69 | |
| 70 | public static void init(Context context, boolean clean) { |
| 71 | |
| 72 | client = PreferenceManager.getDefaultSharedPreferences(context); |
| 73 | editor = client.edit(); |
| 74 | |
| 75 | if (clean) |
| 76 | editor.clear().commit(); |
| 77 | } |
| 78 | |
| 79 | public static String getString(Key key) { |
| 80 | |
| 81 | return client.getString(key.getName(), (String) key.getDefault()); |
| 82 | } |
| 83 | |
| 84 | public static void putString(Key key, String value) { |
| 85 | |
| 86 | if (value == null) |
| 87 | editor.putString(key.getName(), (String)key.getDefault()); |
| 88 | else |
| 89 | editor.putString(key.getName(), value); |
| 90 | editor.commit(); |
| 91 | } |
| 92 | |
| 93 | public static long getLong(Key key) { |
| 94 | |
| 95 | return client.getLong(key.getName(), (Long)key.getDefault()); |
| 96 | } |
| 97 | |
| 98 | public static void putLong(Key key, long value) { |
| 99 | |
| 100 | editor.putLong(key.getName(), value); |
| 101 | editor.commit(); |
| 102 | } |
| 103 | |
| 104 | public static boolean getBoolean(Key key) { |
| 105 | |
| 106 | return client.getBoolean(key.getName(), (Boolean)key.getDefault()); |
| 107 | } |
| 108 | |
| 109 | public static void putBoolean(Key key, boolean value) { |
| 110 | |
| 111 | editor.putBoolean(key.getName(), value); |
| 112 | editor.commit(); |
| 113 | } |
| 114 | } |