| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| AndroidCache |
|
| 1.2;1.2 |
| 1 | package org.codeforamerica.open311.internals.caching; | |
| 2 | ||
| 3 | import android.content.Context; | |
| 4 | import android.content.SharedPreferences; | |
| 5 | import android.content.SharedPreferences.Editor; | |
| 6 | ||
| 7 | /** | |
| 8 | * Uses the <a href= | |
| 9 | * "http://developer.android.com/reference/android/content/SharedPreferences.html" | |
| 10 | * >SharedPreferences</a> of Android to store the data. Singleton class. | |
| 11 | * | |
| 12 | * @author Santiago MunĂn <santimunin@gmail.com> | |
| 13 | * | |
| 14 | */ | |
| 15 | public class AndroidCache extends AbstractCache { | |
| 16 | /** | |
| 17 | * Unique instance of the class. | |
| 18 | */ | |
| 19 | private static AndroidCache instance; | |
| 20 | private SharedPreferences preferences; | |
| 21 | ||
| 22 | /** | |
| 23 | * Returns the unique instance of the class. | |
| 24 | * | |
| 25 | * @param context | |
| 26 | * Android context. Needed the first time is called to build the | |
| 27 | * {@link SharedPreferences}. | |
| 28 | * @return Unique instance of the class. | |
| 29 | */ | |
| 30 | public static AndroidCache getInstance(Context context) { | |
| 31 | 0 | if (instance == null) { |
| 32 | 0 | instance = new AndroidCache(context); |
| 33 | } | |
| 34 | 0 | return instance; |
| 35 | } | |
| 36 | ||
| 37 | /** | |
| 38 | * Prevents this class to be instantiate from outside itself. | |
| 39 | * | |
| 40 | * @param context | |
| 41 | * Android context. Needed the first time is called to build the | |
| 42 | * {@link SharedPreferences}. | |
| 43 | */ | |
| 44 | 0 | private AndroidCache(Context context) { |
| 45 | 0 | preferences = context.getSharedPreferences(FILE, 0); |
| 46 | 0 | } |
| 47 | ||
| 48 | @Override | |
| 49 | public void deleteCache() { | |
| 50 | 0 | Editor editor = preferences.edit(); |
| 51 | 0 | editor.clear(); |
| 52 | 0 | editor.commit(); |
| 53 | 0 | } |
| 54 | ||
| 55 | @Override | |
| 56 | protected void saveProperty(String key, String value) { | |
| 57 | 0 | Editor editor = preferences.edit(); |
| 58 | 0 | editor.putString(key, value); |
| 59 | 0 | editor.commit(); |
| 60 | 0 | } |
| 61 | ||
| 62 | @Override | |
| 63 | protected String getProperty(String key) { | |
| 64 | 0 | return preferences.getString(key, ""); |
| 65 | } | |
| 66 | } |