From: tobiasKaminsky Date: Sat, 9 Aug 2014 08:19:02 +0000 (+0200) Subject: Remote fetch of thumbnail X-Git-Tag: oc-android-1.7.0_signed~163^2~8^2~7 X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/commitdiff_plain/c30ab40fdd5efc013a237f2670400bd3e67ae367?ds=inline;hp=--cc Remote fetch of thumbnail --- c30ab40fdd5efc013a237f2670400bd3e67ae367 diff --git a/res/layout/image_fragment.xml b/res/layout/image_fragment.xml new file mode 100644 index 00000000..340ee03b --- /dev/null +++ b/res/layout/image_fragment.xml @@ -0,0 +1,49 @@ + + + + + + + + + + + + + + + diff --git a/res/layout/image_item.xml b/res/layout/image_item.xml new file mode 100644 index 00000000..bd9f8d71 --- /dev/null +++ b/res/layout/image_item.xml @@ -0,0 +1,119 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/com/owncloud/android/ui/adapter/DiskLruImageCache.java b/src/com/owncloud/android/ui/adapter/DiskLruImageCache.java new file mode 100644 index 00000000..5b401c28 --- /dev/null +++ b/src/com/owncloud/android/ui/adapter/DiskLruImageCache.java @@ -0,0 +1,172 @@ +package com.owncloud.android.ui.adapter; + +import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +import android.content.Context; +import android.graphics.Bitmap; +import android.graphics.Bitmap.CompressFormat; +import android.graphics.BitmapFactory; +import android.os.Environment; +import android.util.Log; + +import com.jakewharton.disklrucache.DiskLruCache; +import com.owncloud.android.BuildConfig; +import com.owncloud.android.utils.Log_OC; + +public class DiskLruImageCache { + + private DiskLruCache mDiskCache; + private CompressFormat mCompressFormat = CompressFormat.JPEG; + private int mCompressQuality = 70; + private static final int APP_VERSION = 1; + private static final int VALUE_COUNT = 1; + private static final String TAG = "DiskLruImageCache"; + + public DiskLruImageCache( Context context,String uniqueName, int diskCacheSize, + CompressFormat compressFormat, int quality ) { + try { + final File diskCacheDir = getDiskCacheDir(context, uniqueName ); + mDiskCache = DiskLruCache.open( diskCacheDir, APP_VERSION, VALUE_COUNT, diskCacheSize ); + mCompressFormat = compressFormat; + mCompressQuality = quality; + } catch (IOException e) { + e.printStackTrace(); + } + } + + private boolean writeBitmapToFile( Bitmap bitmap, DiskLruCache.Editor editor ) + throws IOException, FileNotFoundException { + OutputStream out = null; + try { + out = new BufferedOutputStream( editor.newOutputStream( 0 ), Utils.IO_BUFFER_SIZE ); + return bitmap.compress( mCompressFormat, mCompressQuality, out ); + } finally { + if ( out != null ) { + out.close(); + } + } + } + + private File getDiskCacheDir(Context context, String uniqueName) { + + // Check if media is mounted or storage is built-in, if so, try and use external cache dir + // otherwise use internal cache dir + final String cachePath = + Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) || + !Utils.isExternalStorageRemovable() ? + Utils.getExternalCacheDir(context).getPath() : + context.getCacheDir().getPath(); + + Log_OC.d("DiskCache", "create dir: " + cachePath + File.separator + uniqueName); + + return new File(cachePath + File.separator + uniqueName); + } + + public void put( String key, Bitmap data ) { + + DiskLruCache.Editor editor = null; + try { + editor = mDiskCache.edit( key ); + if ( editor == null ) { + return; + } + + if( writeBitmapToFile( data, editor ) ) { + mDiskCache.flush(); + editor.commit(); + if ( BuildConfig.DEBUG ) { + Log.d( "cache_test_DISK_", "image put on disk cache " + key ); + } + } else { + editor.abort(); + if ( BuildConfig.DEBUG ) { + Log.d( "cache_test_DISK_", "ERROR on: image put on disk cache " + key ); + } + } + } catch (IOException e) { + if ( BuildConfig.DEBUG ) { + Log.d( "cache_test_DISK_", "ERROR on: image put on disk cache " + key ); + } + try { + if ( editor != null ) { + editor.abort(); + } + } catch (IOException ignored) { + } + } + + } + + public Bitmap getBitmap( String key ) { + + Bitmap bitmap = null; + DiskLruCache.Snapshot snapshot = null; + try { + + snapshot = mDiskCache.get( key ); + if ( snapshot == null ) { + return null; + } + final InputStream in = snapshot.getInputStream( 0 ); + if ( in != null ) { + final BufferedInputStream buffIn = + new BufferedInputStream( in, Utils.IO_BUFFER_SIZE ); + bitmap = BitmapFactory.decodeStream( buffIn ); + } + } catch ( IOException e ) { + e.printStackTrace(); + } finally { + if ( snapshot != null ) { + snapshot.close(); + } + } + + if ( BuildConfig.DEBUG ) { + Log.d( "cache_test_DISK_", bitmap == null ? "not found" : "image read from disk " + key); + } + + return bitmap; + + } + + public boolean containsKey( String key ) { + + boolean contained = false; + DiskLruCache.Snapshot snapshot = null; + try { + snapshot = mDiskCache.get( key ); + contained = snapshot != null; + } catch (IOException e) { + e.printStackTrace(); + } finally { + if ( snapshot != null ) { + snapshot.close(); + } + } + + return contained; + + } + + public void clearCache() { + if ( BuildConfig.DEBUG ) { + Log.d( "cache_test_DISK_", "disk cache CLEARED"); + } + try { + mDiskCache.delete(); + } catch ( IOException e ) { + e.printStackTrace(); + } + } + + public File getCacheFolder() { + return mDiskCache.getDirectory(); + } + +} \ No newline at end of file diff --git a/src/com/owncloud/android/ui/adapter/Utils.java b/src/com/owncloud/android/ui/adapter/Utils.java new file mode 100644 index 00000000..181cc925 --- /dev/null +++ b/src/com/owncloud/android/ui/adapter/Utils.java @@ -0,0 +1,35 @@ +package com.owncloud.android.ui.adapter; + +import java.io.File; + +import android.content.Context; +import android.os.Build; +import android.os.Environment; + +public class Utils { + public static final int IO_BUFFER_SIZE = 8 * 1024; + + private Utils() {}; + + public static boolean isExternalStorageRemovable() { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) { + return Environment.isExternalStorageRemovable(); + } + return true; + } + + public static File getExternalCacheDir(Context context) { + if (hasExternalCacheDir()) { + return context.getExternalCacheDir(); + } + + // Before Froyo we need to construct the external cache dir ourselves + final String cacheDir = "/Android/data/" + context.getPackageName() + "/cache/"; + return new File(Environment.getExternalStorageDirectory().getPath() + cacheDir); + } + + public static boolean hasExternalCacheDir() { + return Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO; + } + +} diff --git a/third_party/disklrucache-2.0.2.jar b/third_party/disklrucache-2.0.2.jar new file mode 100644 index 00000000..ca7907d0 Binary files /dev/null and b/third_party/disklrucache-2.0.2.jar differ