From 3685dd958188dba1d69dce4ed3e4d60ac959c15f Mon Sep 17 00:00:00 2001 From: d3rt0xx <98334773+d3rt0xx@users.noreply.github.com> Date: Mon, 15 Aug 2022 20:37:47 +0200 Subject: [PATCH] Updated TinyDB --- .../java/app/d3rt0xx/flyffdroid/TinyDB.java | 104 ++++++++---------- 1 file changed, 48 insertions(+), 56 deletions(-) diff --git a/app/src/main/java/app/d3rt0xx/flyffdroid/TinyDB.java b/app/src/main/java/app/d3rt0xx/flyffdroid/TinyDB.java index 601e91a..5acd985 100644 --- a/app/src/main/java/app/d3rt0xx/flyffdroid/TinyDB.java +++ b/app/src/main/java/app/d3rt0xx/flyffdroid/TinyDB.java @@ -53,6 +53,26 @@ public TinyDB(Context appContext) { context = appContext; } + /** + * Check if external storage is writable or not + * + * @return true if writable, false otherwise + */ + public static boolean isExternalStorageWritable() { + return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()); + } + + /** + * Check if external storage is readable or not + * + * @return true if readable, false otherwise + */ + public static boolean isExternalStorageReadable() { + String state = Environment.getExternalStorageState(); + + return Environment.MEDIA_MOUNTED.equals(state) || + Environment.MEDIA_MOUNTED_READ_ONLY.equals(state); + } /** * Decodes the Bitmap from 'path' and returns it @@ -73,7 +93,6 @@ public Bitmap getImage(String path) { return bitmapFromPath; } - /** * Returns the String path of the last saved image * @@ -83,7 +102,6 @@ public String getSavedImagePath() { return lastImagePath; } - /** * Saves 'theBitmap' into folder 'theFolder' with the name 'theImageName' * @@ -107,7 +125,6 @@ public String putImage(String theFolder, String theImageName, Bitmap theBitmap) return mFullPath; } - /** * Saves 'theBitmap' into 'fullPath' * @@ -119,6 +136,8 @@ public boolean putImageWithFullPath(String fullPath, Bitmap theBitmap) { return !(fullPath == null || theBitmap == null) && saveBitmap(fullPath, theBitmap); } + // Getters + /** * Creates the path for the image with name 'imageName' in DEFAULT_APP.. directory * @@ -192,8 +211,6 @@ private boolean saveBitmap(String fullPath, Bitmap bitmap) { return (fileCreated && bitmapCompressed && streamClosed); } - // Getters - /** * Get int value from SharedPreferences at 'key'. If key not found, return 0 * @@ -344,32 +361,30 @@ public ArrayList getListBoolean(String key) { } - public ArrayList getListObject(String key, Class mClass){ - Gson gson = new Gson(); + // Put methods - ArrayList objStrings = getListString(key); - ArrayList objects = new ArrayList(); + public ArrayList getListObject(String key, Class mClass) { + Gson gson = new Gson(); - for(String jObjString : objStrings){ - Object value = gson.fromJson(jObjString, mClass); - objects.add(value); - } - return objects; - } + ArrayList objStrings = getListString(key); + ArrayList objects = new ArrayList(); + for (String jObjString : objStrings) { + Object value = gson.fromJson(jObjString, mClass); + objects.add(value); + } + return objects; + } - public T getObject(String key, Class classOfT){ + public T getObject(String key, Class classOfT) { String json = getString(key); Object value = new Gson().fromJson(json, classOfT); if (value == null) throw new NullPointerException(); - return (T)value; + return (T) value; } - - // Put methods - /** * Put int value into SharedPreferences with 'key' and save * @@ -508,23 +523,24 @@ public void putListBoolean(String key, ArrayList boolList) { /** * Put ObJect any type into SharedPrefrences with 'key' and save + * * @param key SharedPreferences key * @param obj is the Object you want to put */ - public void putObject(String key, Object obj){ - checkForNullKey(key); - Gson gson = new Gson(); - putString(key, gson.toJson(obj)); + public void putObject(String key, Object obj) { + checkForNullKey(key); + Gson gson = new Gson(); + putString(key, gson.toJson(obj)); } - public void putListObject(String key, ArrayList objArray){ - checkForNullKey(key); - Gson gson = new Gson(); - ArrayList objStrings = new ArrayList(); - for(Object obj : objArray){ - objStrings.add(gson.toJson(obj)); - } - putListString(key, objStrings); + public void putListObject(String key, ArrayList objArray) { + checkForNullKey(key); + Gson gson = new Gson(); + ArrayList objStrings = new ArrayList(); + for (Object obj : objArray) { + objStrings.add(gson.toJson(obj)); + } + putListString(key, objStrings); } /** @@ -546,7 +562,6 @@ public boolean deleteImage(String path) { return new File(path).delete(); } - /** * Clear SharedPreferences (remove everything) */ @@ -563,7 +578,6 @@ public void clear() { return preferences.getAll(); } - /** * Register SharedPreferences change listener * @@ -586,28 +600,6 @@ public void unregisterOnSharedPreferenceChangeListener( preferences.unregisterOnSharedPreferenceChangeListener(listener); } - - /** - * Check if external storage is writable or not - * - * @return true if writable, false otherwise - */ - public static boolean isExternalStorageWritable() { - return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()); - } - - /** - * Check if external storage is readable or not - * - * @return true if readable, false otherwise - */ - public static boolean isExternalStorageReadable() { - String state = Environment.getExternalStorageState(); - - return Environment.MEDIA_MOUNTED.equals(state) || - Environment.MEDIA_MOUNTED_READ_ONLY.equals(state); - } - /** * Checks if an object exists in Memory *