package com.sjjd.hu.utils;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import android.util.Log;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;
public class TinyDB {
private SharedPreferences preferences;
private String DEFAULT_APP_IMAGEDATA_DIRECTORY;
private String lastImagePath = "";
public TinyDB(Context appContext) {
preferences = PreferenceManager.getDefaultSharedPreferences(appContext);
}
/**
* Decodes the Bitmap from 'path' and returns it
*
* @param path image path
* @return the Bitmap from 'path'
*/
public Bitmap getImage(String path) {
Bitmap bitmapFromPath = null;
try {
bitmapFromPath = BitmapFactory.decodeFile(path);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return bitmapFromPath;
}
/**
* Returns the String path of the last saved image
*
* @return string path of the last saved image
*/
public String getSavedImagePath() {
return lastImagePath;
}
/**
* Saves 'theBitmap' into folder 'theFolder' with the name 'theImageName'
*
* @param theFolder the folder path dir you want to save it to e.g "DropBox/WorkImages"
* @param theImageName the name you want to assign to the image file e.g "MeAtLunch.png"
* @param theBitmap the image you want to save as a Bitmap
* @return returns the full path(file system address) of the saved image
*/
public String putImage(String theFolder, String theImageName, Bitmap theBitmap) {
if (theFolder == null || theImageName == null || theBitmap == null)
return null;
this.DEFAULT_APP_IMAGEDATA_DIRECTORY = theFolder;
String mFullPath = setupFullPath(theImageName);
if (!mFullPath.equals("")) {
lastImagePath = mFullPath;
saveBitmap(mFullPath, theBitmap);
}
return mFullPath;
}
/**
* Saves 'theBitmap' into 'fullPath'
*
* @param fullPath full path of the image file e.g. "Images/MeAtLunch.png"
* @param theBitmap the image you want to save as a Bitmap
* @return true if image was saved, false otherwise
*/
public boolean putImageWithFullPath(String fullPath, Bitmap theBitmap) {
return !(fullPath == null || theBitmap == null) && saveBitmap(fullPath, theBitmap);
}
/**
* Creates the path for the image with name 'imageName' in DEFAULT_APP.. directory
*
* @param imageName name of the image
* @return the full path of the image. If it failed to create directory, return empty string
*/
private String setupFullPath(String imageName) {
File mFolder = new File(Environment.getExternalStorageDirectory(), DEFAULT_APP_IMAGEDATA_DIRECTORY);
if (isExternalStorageReadable() && isExternalStorageWritable() && !mFolder.exists()) {
if (!mFolder.mkdirs()) {
Log.e("ERROR", "Failed to setup folder");
return "";
}
}
return mFolder.getPath() + '/' + imageName;
}
/**
* Saves the Bitmap as a PNG file at path 'fullPath'
*
* @param fullPath path of the image file
* @param bitmap the image as a Bitmap
* @return true if it successfully saved, false otherwise
*/
private boolean saveBitmap(String fullPath, Bitmap bitmap) {
if (fullPath == null || bitmap == null)
return false;
boolean fileCreated = false;
boolean bitmapCompressed = false;
boolean streamClosed = false;
File imageFile = new File(fullPath);
if (imageFile.exists())
if (!imageFile.delete())
return false;
try {
fileCreated = imageFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
FileOutputStream out = null;
try {
out = new FileOutputStream(imageFile);
bitmapCompressed = bitmap.compress(CompressFormat.PNG, 100, out);
} catch (Exception e) {
e.printStackTrace();
bitmapCompressed = false;
} finally {
if (out != null) {
try {
out.flush();
out.close();
streamClosed = true;
} catch (IOException e) {
e.printStackTrace();
streamClosed = false;
}
}
}
return (fileCreated && bitmapCompressed && streamClosed);
}
// Getters
/**
* Get int value from SharedPreferences at 'key'. If key not found, return 'defaultValue'
*
* @param key SharedPreferences key
* @param defaultValue int value returned if key was not found
* @return int value at 'key' or 'defaultValue' if key not found
*/
public int getInt(String key) {
return preferences.getInt(key, 0);
}
/**
* Get parsed ArrayList of Integers from SharedPreferences at 'key'
*
* @param key SharedPreferences key
* @return ArrayList of Integers
*/
public ArrayList<Integer> getListInt(String key) {
String[] myList = TextUtils.split(preferences.getString(key, ""), "‚‗‚");
ArrayList<String> arrayToList = new ArrayList<String>(Arrays.asList(myList));
ArrayList<Integer> newList = new ArrayList<Integer>();
for (String item : arrayToList)
newList.add(Integer.parseInt(item));
return newList;
}
/**
* Get long value from SharedPreferences at 'key'. If key not found, return 'defaultValue'
*
* @param key SharedPreferences key
* @param defaultValue long value returned if key was not found
* @return long value at 'key' or 'defaultValue' if key not found
*/
public long getLong(String key, long defaultValue) {
return preferences.getLong(key, defaultValue);
}
/**
* Get float value from SharedPreferences at 'key'. If key not found, return 'defaultValue'
*
* @param key SharedPreferences key
* @param defaultValue float value returned if key was not found
* @return float value at 'key' or 'defaultValue' if key not found
*/
public float getFloat(String key) {
return preferences.getFloat(key, 0);
}
/**
* Get double value from SharedPreferences at 'key'. If exception thrown, return 'defaultValue'
*
* @param key SharedPreferences key
* @param defaultValue double value returned if exception is thrown
* @return double value at 'key' or 'defaultValue' if exception is thrown
*/
public double getDouble(String key, double defaultValue) {
String number = getString(key);
try {
return Double.parseDouble(number);
} catch (NumberFormatException e) {
return defaultValue;
}
}
/**
* Get parsed ArrayList of Double from SharedPreferences at 'key'
*
* @param key SharedPreferences key
* @return ArrayList of Double
*/
public ArrayList<Double> getListDouble(String key) {
String[] myList = TextUtils.split(preferences.getString(key, ""), "‚‗‚");
ArrayList<String> arrayToList = new ArrayList<String>(Arrays.asList(myList));
ArrayList<Double> newList = new ArrayList<Double>();
for (String item : arrayToList)
newList.add(Double.parseDouble(item));
return newList;
}
/**
* Get