// Path: src/com/guaranacode/android/fastpicasabrowser/tasks/DownloadImageTask.java
// public class DownloadImageTask extends AsyncTask<IStorableModel, Integer, Bitmap> {
// 	private ImageView mView;
// 	private String mUrl;
// 	
// 	public DownloadImageTask(ImageView view) {
// 		mView = view;
// 	}
// 	
// 	@Override
// 	protected Bitmap doInBackground(IStorableModel... storableList) {
// 		mUrl = storableList[0].getUrl();
// 		
// 		Bitmap bitmap = ImageStorage.downloadThumbnailAndStoreLocally(storableList[0]);
// 		return bitmap;
// 	}
// 
// 	protected void onPostExecute(Bitmap result) {
// 		if((null != mView) && (null != result) && (mUrl.equals((String)mView.getTag()))) {
// 			mView.setImageBitmap(result);
// 		}
// 	}
// }
// 
// Path: src/com/guaranacode/android/fastpicasabrowser/util/FileUtils.java
// public class FileUtils {
// 	
// 	/**
// 	 * Creates all directories in the specified path if they don't already exist. Returns
// 	 * true if successful, false otherwise.
// 	 * @param path
// 	 * @return
// 	 */
// 	public static boolean createPath(String path) {
// 		File dirs = new File(path);
// 		return dirs.mkdirs();
// 	}
// 	
// 	/**
// 	 * Deletes a file or a directory and everything in it.
// 	 * @param path
// 	 */
// 	public static void deleteFile(String path) {
// 		File dir = new File(path);
// 		deleteFile(dir);
// 	}
// 	
// 	/**
// 	 * Deletes a file or a directory and everything in it.
// 	 * @param file
// 	 */
// 	public static void deleteFile(File file) {
// 		if((null == file)) {
// 			return;
// 		}
// 		
// 		if(file.isFile()) {
// 			file.delete();
// 		}
// 		else if(file.isDirectory()) {
// 			for(String child : file.list()) {
// 				deleteFile(file.getAbsolutePath() + "/" + child);
// 			}
// 			
// 			file.delete();
// 		}
// 	}
// }
// 
// Path: src/com/guaranacode/android/fastpicasabrowser/util/StorageUtils.java
// public class StorageUtils {
// 	/**
// 	 * Returns true if the media is currently mounted and writable, false otherwise.
// 	 * @return
// 	 */
// 	public static boolean canWriteToExternalStorage() {
// 		return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
// 	}
// 	
// 	/**
// 	 * Returns true if media is currently mounted and readable.
// 	 * @return
// 	 */
// 	public static boolean canReadFromExternalStorage() {
// 		if(canWriteToExternalStorage()) {
// 			return true;
// 		}
// 		
// 		return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED_READ_ONLY);
// 	}
// }
// 
// Path: src/com/guaranacode/android/fastpicasabrowser/util/StringUtils.java
// public class StringUtils {
// 
// 	/**
// 	 * Returns true if the string is null or contains only whitespace.
// 	 * 
// 	 * @param string
// 	 * @return
// 	 */
// 	public static boolean isNullOrEmpty(String string) {
// 		return (null == string) || (string.trim().length() == 0);
// 	}
// }


import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.widget.ImageView;
import com.guaranacode.android.fastpicasabrowser.tasks.DownloadImageTask;
import com.guaranacode.android.fastpicasabrowser.util.FileUtils;
import com.guaranacode.android.fastpicasabrowser.util.StorageUtils;
import com.guaranacode.android.fastpicasabrowser.util.StringUtils;
package com.guaranacode.android.fastpicasabrowser.storage;




/**
 * Image downloading and storage.
 *
 * @author abe@guaranacode.com
 *
 */
public class ImageStorage {

	private static String APP_PACKAGE = "com.guaranacode.fastpicasabrowser";
	
	private static String APP_STORAGE_PATH = APP_PACKAGE + "/files";
	
	private static String TEMP_PHOTO_DIR = "temp_photos";
	
	private static String TEMP_PHOTO_NAME = "temp_photo.jpg";
	
	public static String getLocalPathForThumbnail(IStorableModel model, boolean includeFilename) {
		String relativePath = APP_STORAGE_PATH + "/" + model.getDir();
		
		return getLocalPathForImage(model.getUrl(), relativePath, includeFilename);
	}
	
	public static String getFilenameFromUrl(String imageUrl) {
		if(StringUtils.isNullOrEmpty(imageUrl)) {
			return null;
		}
		
		String filename = null;
		
		int lastSlashIdx = imageUrl.lastIndexOf('/');
		if(lastSlashIdx > 0) {
			filename = imageUrl.substring(lastSlashIdx + 1);
		}
		
		return filename;
	}
	
	private static Bitmap getFromLocalStorage(IStorableModel model) {
		if(null == model) {
			return null;
		}
		
// Path: src/com/guaranacode/android/fastpicasabrowser/tasks/DownloadImageTask.java
// public class DownloadImageTask extends AsyncTask<IStorableModel, Integer, Bitmap> {
// 	private ImageView mView;
// 	private String mUrl;
// 	
// 	public DownloadImageTask(ImageView view) {
// 		mView = view;
// 	}
// 	
// 	@Override
// 	protected Bitmap doInBackground(IStorableModel... storableList) {
// 		mUrl = storableList[0].getUrl();
// 		
// 		Bitmap bitmap = ImageStorage.downloadThumbnailAndStoreLocally(storableList[0]);
// 		return bitmap;
// 	}
// 
// 	protected void onPostExecute(Bitmap result) {
// 		if((null != mView) && (null != result) && (mUrl.equals((String)mView.getTag()))) {
// 			mView.setImageBitmap(result);
// 		}
// 	}
// }
// 
// Path: src/com/guaranacode/android/fastpicasabrowser/util/FileUtils.java
// public class FileUtils {
// 	
// 	/**
// 	 * Creates all directories in the specified path if they don't already exist. Returns
// 	 * true if successful, false otherwise.
// 	 * @param path
// 	 * @return
// 	 */
// 	public static boolean createPath(String path) {
// 		File dirs = new File(path);
// 		return dirs.mkdirs();
// 	}
// 	
// 	/**
// 	 * Deletes a file or a directory and everything in it.
// 	 * @param path
// 	 */
// 	public static void deleteFile(String path) {
// 		File dir = new File(path);
// 		deleteFile(dir);
// 	}
// 	
// 	/**
// 	 * Deletes a file or a directory and everything in it.
// 	 * @param file
// 	 */
// 	public static void deleteFile(File file) {
// 		if((null == file)) {
// 			return;
// 		}
// 		
// 		if(file.isFile()) {
// 			file.delete();
// 		}
// 		else if(file.isDirectory()) {
// 			for(String child : file.list()) {
// 				deleteFile(file.getAbsolutePath() + "/" + child);
// 			}
// 			
// 			file.delete();
// 		}
// 	}
// }
// 
// Path: src/com/guaranacode/android/fastpicasabrowser/util/StorageUtils.java
// public class StorageUtils {
// 	/**
// 	 * Returns true if the media is currently mounted and writable, false otherwise.
// 	 * @return
// 	 */
// 	public static boolean canWriteToExternalStorage() {
// 		return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
// 	}
// 	
// 	/**
// 	 * Returns true if media is currently mounted and readable.
// 	 * @return
// 	 */
// 	public static boolean canReadFromExternalStorage() {
// 		if(canWriteToExternalStorage()) {
// 			return true;
// 		}
// 		
// 		return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED_READ_ONLY);
// 	}
// }
// 
// Path: src/com/guaranacode/android/fastpicasabrowser/util/StringUtils.java
// public class StringUtils {
// 
// 	/**
// 	 * Returns true if the string is null or contains only whitespace.
// 	 * 
// 	 * @param string
// 	 * @return
// 	 */
// 	public static boolean isNullOrEmpty(String string) {
// 		return (null == string) || (string.trim().length() == 0);
// 	}
// }


// Path: src/com/guaranacode/android/fastpicasabrowser/storage/ImageStorage.java
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.widget.ImageView;
import com.guaranacode.android.fastpicasabrowser.tasks.DownloadImageTask;
import com.guaranacode.android.fastpicasabrowser.util.FileUtils;
import com.guaranacode.android.fastpicasabrowser.util.StorageUtils;
import com.guaranacode.android.fastpicasabrowser.util.StringUtils;

package com.guaranacode.android.fastpicasabrowser.storage;




/**
 * Image downloading and storage.
 *
 * @author abe@guaranacode.com
 *
 */
public class ImageStorage {

	private static String APP_PACKAGE = "com.guaranacode.fastpicasabrowser";
	
	private static String APP_STORAGE_PATH = APP_PACKAGE + "/files";
	
	private static String TEMP_PHOTO_DIR = "temp_photos";
	
	private static String TEMP_PHOTO_NAME = "temp_photo.jpg";
	
	public static String getLocalPathForThumbnail(IStorableModel model, boolean includeFilename) {
		String relativePath = APP_STORAGE_PATH + "/" + model.getDir();
		
		return getLocalPathForImage(model.getUrl(), relativePath, includeFilename);
	}
	
	public static String getFilenameFromUrl(String imageUrl) {
		if(StringUtils.isNullOrEmpty(imageUrl)) {
			return null;
		}
		
		String filename = null;
		
		int lastSlashIdx = imageUrl.lastIndexOf('/');
		if(lastSlashIdx > 0) {
			filename = imageUrl.substring(lastSlashIdx + 1);
		}
		
		return filename;
	}
	
	private static Bitmap getFromLocalStorage(IStorableModel model) {
		if(null == model) {
			return null;
		}
		
		if(!StorageUtils.canReadFromExternalStorage()) {