Remote fetch of thumbnail
authortobiasKaminsky <tobias@kaminsky.me>
Sat, 9 Aug 2014 08:19:02 +0000 (10:19 +0200)
committertobiasKaminsky <tobias@kaminsky.me>
Sat, 9 Aug 2014 08:19:02 +0000 (10:19 +0200)
res/layout/image_fragment.xml [new file with mode: 0644]
res/layout/image_item.xml [new file with mode: 0644]
src/com/owncloud/android/ui/adapter/DiskLruImageCache.java [new file with mode: 0644]
src/com/owncloud/android/ui/adapter/Utils.java [new file with mode: 0644]
third_party/disklrucache-2.0.2.jar [new file with mode: 0644]

diff --git a/res/layout/image_fragment.xml b/res/layout/image_fragment.xml
new file mode 100644 (file)
index 0000000..340ee03
--- /dev/null
@@ -0,0 +1,49 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- 
+  ownCloud Android client application
+
+  Copyright (C) 2012  Bartek Przybylski
+  Copyright (C) 2012-2013 ownCloud Inc.
+
+  This program is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License version 2,
+  as published by the Free Software Foundation.
+
+  This program is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+  GNU General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ -->
+<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
+       android:layout_width="0dp"
+       android:layout_height="match_parent"
+       android:layout_weight="1" >
+
+    <android.support.v4.widget.SwipeRefreshLayout
+        android:id="@+id/swipe_refresh_files"
+        android:layout_width="match_parent"
+        android:layout_height="match_parent" >
+
+        <com.owncloud.android.ui.ExtendedListView
+            android:id="@+id/list_root"
+            android:layout_width="match_parent"
+            android:layout_height="match_parent" />
+        
+    </android.support.v4.widget.SwipeRefreshLayout>
+
+    <android.support.v4.widget.SwipeRefreshLayout
+        android:id="@+id/swipe_refresh_files_emptyView"
+        android:layout_width="match_parent"
+        android:layout_height="match_parent"
+        android:visibility="gone" >
+
+        <GridView
+            android:id="@+id/grid_list_view"
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content"
+            android:numColumns="3" />
+    </android.support.v4.widget.SwipeRefreshLayout>
+</FrameLayout>
diff --git a/res/layout/image_item.xml b/res/layout/image_item.xml
new file mode 100644 (file)
index 0000000..bd9f8d7
--- /dev/null
@@ -0,0 +1,119 @@
+<?xml version="1.0" encoding="UTF-8"?>\r
+<!--\r
+  ownCloud Android client application\r
+\r
+  Copyright (C) 2012  Bartek Przybylski\r
+  Copyright (C) 2012-2013 ownCloud Inc.\r
+\r
+  This program is free software: you can redistribute it and/or modify\r
+  it under the terms of the GNU General Public License version 2,\r
+  as published by the Free Software Foundation.\r
+\r
+  This program is distributed in the hope that it will be useful,\r
+  but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
+  GNU General Public License for more details.\r
+\r
+  You should have received a copy of the GNU General Public License\r
+  along with this program.  If not, see <http://www.gnu.org/licenses/>.\r
+  \r
+-->\r
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"\r
+    android:id="@+id/ListItemLayout"\r
+    android:layout_width="fill_parent"\r
+    android:layout_height="160dp"\r
+    android:background="@drawable/list_selector"\r
+    android:orientation="horizontal" >\r
+\r
+    <ImageView\r
+        android:id="@+id/imageView1"\r
+        android:layout_width="150dp"\r
+        android:layout_height="150dp"\r
+        android:layout_marginBottom="5dp"\r
+        android:layout_marginLeft="9dp"\r
+        android:layout_marginTop="5dp"\r
+        android:src="@drawable/ic_menu_archive" />\r
+\r
+    <FrameLayout\r
+        android:id="@+id/imageItemFrame"\r
+        android:layout_width="56dp"\r
+        android:layout_height="56dp"\r
+        android:focusable="false"\r
+        android:focusableInTouchMode="false" >\r
+\r
+        <ImageView\r
+            android:id="@+id/sharedIcon"\r
+            android:layout_width="wrap_content"\r
+            android:layout_height="wrap_content"\r
+            android:layout_marginBottom="4dp"\r
+            android:layout_marginLeft="4dp"\r
+            android:layout_marginRight="4dp"\r
+            android:src="@drawable/sharedlink" />\r
+\r
+        <ImageView\r
+            android:id="@+id/sharedWithMeIcon"\r
+            android:layout_width="wrap_content"\r
+            android:layout_height="wrap_content"\r
+            android:layout_marginLeft="4dp"\r
+            android:layout_marginRight="4dp"\r
+            android:layout_marginTop="4dp"\r
+            android:src="@drawable/shared_with_me" />\r
+\r
+        <ImageView\r
+            android:id="@+id/custom_checkbox"\r
+            android:layout_width="wrap_content"\r
+            android:layout_height="wrap_content"\r
+            android:layout_marginLeft="4dp"\r
+            android:layout_marginRight="4dp"\r
+            android:src="@android:drawable/checkbox_off_background" />\r
+\r
+        <ImageView\r
+            android:id="@+id/imageView2"\r
+            android:layout_width="32dp"\r
+            android:layout_height="32dp"\r
+            android:layout_gravity="center_vertical"\r
+            android:layout_marginLeft="22dp"\r
+            android:src="@drawable/local_file_indicator" />\r
+\r
+        <TextView\r
+            android:id="@+id/Filename"\r
+            android:layout_width="wrap_content"\r
+            android:layout_height="wrap_content"\r
+            android:layout_marginLeft="4dp"\r
+            android:layout_marginRight="4dp"\r
+            android:ellipsize="middle"\r
+            android:singleLine="true"\r
+            android:text="TextView"\r
+            android:textColor="#303030"\r
+            android:textSize="16dip" />\r
+\r
+        <ImageView\r
+            android:id="@+id/imageView3"\r
+            android:layout_width="wrap_content"\r
+            android:layout_height="wrap_content"\r
+            android:layout_gravity="bottom|right"\r
+            android:layout_marginBottom="10dp"\r
+            android:layout_marginRight="2dp"\r
+            android:src="@drawable/ic_favorite" />\r
+\r
+        <TextView\r
+            android:id="@+id/last_mod"\r
+            android:layout_width="wrap_content"\r
+            android:layout_height="wrap_content"\r
+            android:layout_weight=".5"\r
+            android:text="TextView"\r
+            android:textColor="@color/list_item_lastmod_and_filesize_text"\r
+            android:textSize="12dip" />\r
+\r
+        <TextView\r
+            android:id="@+id/file_size"\r
+            android:layout_width="wrap_content"\r
+            android:layout_height="wrap_content"\r
+            android:layout_weight=".5"\r
+            android:gravity="right"\r
+            android:text="TextView"\r
+            android:textColor="@color/list_item_lastmod_and_filesize_text"\r
+            android:textSize="12dip" />\r
+    </FrameLayout>\r
+\r
+</LinearLayout>
\ 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 (file)
index 0000000..5b401c2
--- /dev/null
@@ -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 (file)
index 0000000..181cc92
--- /dev/null
@@ -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 (file)
index 0000000..ca7907d
Binary files /dev/null and b/third_party/disklrucache-2.0.2.jar differ