Merge pull request #687 from owncloud/thumbnails_from_server
authorDavid A. Velasco <dvelasco@owncloud.com>
Mon, 3 Nov 2014 12:38:58 +0000 (13:38 +0100)
committerDavid A. Velasco <dvelasco@owncloud.com>
Mon, 3 Nov 2014 12:38:58 +0000 (13:38 +0100)
Get remote thumbnails from server (OC8)

res/layout/list_item.xml
res/values/dims.xml [new file with mode: 0644]
src/com/owncloud/android/MainApp.java
src/com/owncloud/android/datamodel/FileDataStorageManager.java
src/com/owncloud/android/datamodel/ThumbnailsCacheManager.java
src/com/owncloud/android/operations/SynchronizeFolderOperation.java
src/com/owncloud/android/ui/adapter/FileListListAdapter.java

index a4113de..c6c7b92 100644 (file)
 \r
         <ImageView\r
             android:id="@+id/imageView2"\r
-            android:layout_width="32dp"\r
-            android:layout_height="32dp"\r
+            android:layout_width="@dimen/file_icon_size"\r
+            android:layout_height="@dimen/file_icon_size"\r
             android:layout_gravity="center_vertical"\r
             android:layout_marginLeft="22dp"\r
             android:src="@drawable/local_file_indicator" />\r
 \r
         <ImageView\r
             android:id="@+id/imageView1"\r
-            android:layout_width="32dp"\r
-            android:layout_height="32dp"\r
+            android:layout_width="@dimen/file_icon_size"\r
+            android:layout_height="@dimen/file_icon_size"\r
             android:layout_gravity="center_vertical"\r
             android:layout_marginLeft="9dp"\r
             android:src="@drawable/ic_menu_archive" />\r
diff --git a/res/values/dims.xml b/res/values/dims.xml
new file mode 100644 (file)
index 0000000..d433cba
--- /dev/null
@@ -0,0 +1,21 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+  ownCloud Android client application
+
+  Copyright (C) 2014 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/>.
+-->
+<resources>
+       <dimen name="file_icon_size">32dp</dimen>
+</resources>
index e04239d..c2a4c68 100644 (file)
@@ -55,7 +55,7 @@ public class MainApp extends Application {
         } else {
             OwnCloudClientManagerFactory.setDefaultPolicy(Policy.ALWAYS_NEW_CLIENT);
         }
-        
+
         // initialise thumbnails cache on background thread
         new ThumbnailsCacheManager.InitDiskCacheTask().execute();
         
index 5b1bef1..41a5106 100644 (file)
@@ -300,6 +300,7 @@ public class FileDataStorageManager {
             cv.put(ProviderTableMeta.FILE_PUBLIC_LINK, file.getPublicLink());
             cv.put(ProviderTableMeta.FILE_PERMISSIONS, file.getPermissions());
             cv.put(ProviderTableMeta.FILE_REMOTE_ID, file.getRemoteId());
+            cv.put(ProviderTableMeta.FILE_UPDATE_THUMBNAIL, file.needsUpdateThumbnail());
 
             boolean existsByPath = fileExists(file.getRemotePath());
             if (existsByPath || fileExists(file.getFileId())) {
index e75404e..1d2cda8 100644 (file)
@@ -20,19 +20,30 @@ package com.owncloud.android.datamodel;
 import java.io.File;
 import java.lang.ref.WeakReference;
 
+import org.apache.commons.httpclient.HttpStatus;
+import org.apache.commons.httpclient.methods.GetMethod;
+
+import android.accounts.Account;
+import android.accounts.AccountManager;
 import android.content.res.Resources;
 import android.graphics.Bitmap;
-import android.graphics.BitmapFactory;
 import android.graphics.Bitmap.CompressFormat;
+import android.graphics.BitmapFactory;
 import android.graphics.drawable.BitmapDrawable;
 import android.graphics.drawable.Drawable;
 import android.media.ThumbnailUtils;
+import android.net.Uri;
 import android.os.AsyncTask;
-import android.util.TypedValue;
 import android.widget.ImageView;
 
 import com.owncloud.android.MainApp;
+import com.owncloud.android.R;
+import com.owncloud.android.lib.common.OwnCloudAccount;
+import com.owncloud.android.lib.common.OwnCloudClient;
+import com.owncloud.android.lib.common.OwnCloudClientManagerFactory;
+import com.owncloud.android.lib.common.accounts.AccountUtils.Constants;
 import com.owncloud.android.lib.common.utils.Log_OC;
+import com.owncloud.android.lib.resources.status.OwnCloudVersion;
 import com.owncloud.android.ui.adapter.DiskLruImageCache;
 import com.owncloud.android.utils.BitmapUtils;
 import com.owncloud.android.utils.DisplayUtils;
@@ -47,7 +58,8 @@ public class ThumbnailsCacheManager {
     
     private static final String TAG = ThumbnailsCacheManager.class.getSimpleName();
     
-    private static final String CACHE_FOLDER = "thumbnailCache"; 
+    private static final String CACHE_FOLDER = "thumbnailCache";
+    private static final String MINOR_SERVER_VERSION_FOR_THUMBS = "7.8.0";
     
     private static final Object mThumbnailsDiskCacheLock = new Object();
     private static DiskLruImageCache mThumbnailCache = null;
@@ -56,7 +68,9 @@ public class ThumbnailsCacheManager {
     private static final int DISK_CACHE_SIZE = 1024 * 1024 * 10; // 10MB
     private static final CompressFormat mCompressFormat = CompressFormat.JPEG;
     private static final int mCompressQuality = 70;
-    
+    private static OwnCloudClient mClient = null;
+    private static String mServerVersion = null;
+
     public static Bitmap mDefaultImg = 
             BitmapFactory.decodeResource(
                     MainApp.getAppContext().getResources(), 
@@ -65,10 +79,12 @@ public class ThumbnailsCacheManager {
 
     
     public static class InitDiskCacheTask extends AsyncTask<File, Void, Void> {
+
         @Override
         protected Void doInBackground(File... params) {
             synchronized (mThumbnailsDiskCacheLock) {
                 mThumbnailCacheStarting = true;
+
                 if (mThumbnailCache == null) {
                     try {
                         // Check if media is mounted or storage is built-in, if so, 
@@ -153,15 +169,17 @@ public class ThumbnailsCacheManager {
 
     public static class ThumbnailGenerationTask extends AsyncTask<OCFile, Void, Bitmap> {
         private final WeakReference<ImageView> mImageViewReference;
+        private static Account mAccount;
         private OCFile mFile;
         private FileDataStorageManager mStorageManager;
         
-        public ThumbnailGenerationTask(ImageView imageView, FileDataStorageManager storageManager) {
+        public ThumbnailGenerationTask(ImageView imageView, FileDataStorageManager storageManager, Account account) {
          // Use a WeakReference to ensure the ImageView can be garbage collected
             mImageViewReference = new WeakReference<ImageView>(imageView);
             if (storageManager == null)
                 throw new IllegalArgumentException("storageManager must not be NULL");
             mStorageManager = storageManager;
+            mAccount = account;
         }
 
         // Decode image in background.
@@ -170,6 +188,15 @@ public class ThumbnailsCacheManager {
             Bitmap thumbnail = null;
             
             try {
+                if (mAccount != null) {
+                    AccountManager accountMgr = AccountManager.get(MainApp.getAppContext());
+                    
+                    mServerVersion = accountMgr.getUserData(mAccount, Constants.KEY_OC_VERSION);
+                    OwnCloudAccount ocAccount = new OwnCloudAccount(mAccount, MainApp.getAppContext());
+                    mClient = OwnCloudClientManagerFactory.getDefaultSingleton().
+                            getClientFor(ocAccount, MainApp.getAppContext());
+                }
+                
                 mFile = params[0];
                 final String imageKey = String.valueOf(mFile.getRemoteId());
     
@@ -180,9 +207,8 @@ public class ThumbnailsCacheManager {
                 if (thumbnail == null || mFile.needsUpdateThumbnail()) { 
                     // Converts dp to pixel
                     Resources r = MainApp.getAppContext().getResources();
-                    int px = (int) Math.round(TypedValue.applyDimension(
-                            TypedValue.COMPLEX_UNIT_DIP, 150, r.getDisplayMetrics()
-                    ));
+                    
+                    int px = (int) Math.round(r.getDimension(R.dimen.file_icon_size));
                     
                     if (mFile.isDown()){
                         Bitmap bitmap = BitmapUtils.decodeSampledBitmapFromFile(
@@ -198,6 +224,36 @@ public class ThumbnailsCacheManager {
                             mStorageManager.saveFile(mFile);
                         }
     
+                    } else {
+                        // Download thumbnail from server
+                        if (mClient != null && mServerVersion != null) {
+                            OwnCloudVersion serverOCVersion = new OwnCloudVersion(mServerVersion);
+                            if (serverOCVersion.compareTo(new OwnCloudVersion(MINOR_SERVER_VERSION_FOR_THUMBS)) >= 0) {
+                                try {
+                                    int status = -1;
+
+                                    String uri = mClient.getBaseUri() + "/index.php/apps/files/api/v1/thumbnail/" + 
+                                            px + "/" + px + Uri.encode(mFile.getRemotePath(), "/");
+                                    Log_OC.d("Thumbnail", "URI: " + uri);
+                                    GetMethod get = new GetMethod(uri);
+                                    status = mClient.executeMethod(get);
+                                    if (status == HttpStatus.SC_OK) {
+                                        byte[] bytes = get.getResponseBody();
+                                        Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
+                                        thumbnail = ThumbnailUtils.extractThumbnail(bitmap, px, px);
+
+                                        // Add thumbnail to cache
+                                        if (thumbnail != null) {
+                                            addBitmapToCache(imageKey, thumbnail);
+                                        }
+                                    }
+                                } catch (Exception e) {
+                                    e.printStackTrace();
+                                }
+                            } else {
+                                Log_OC.d(TAG, "Server too old");
+                            }
+                        }
                     }
                 }
                 
index cdf1282..d61e678 100644 (file)
@@ -33,6 +33,7 @@ import org.apache.http.HttpStatus;
 import android.accounts.Account;
 import android.content.Context;
 import android.content.Intent;
+import android.util.Log;
 //import android.support.v4.content.LocalBroadcastManager;
 
 import com.owncloud.android.datamodel.FileDataStorageManager;
@@ -325,7 +326,7 @@ public class SynchronizeFolderOperation extends RemoteOperation {
     private void synchronizeData(ArrayList<Object> folderAndFiles, OwnCloudClient client) {
         // get 'fresh data' from the database
         mLocalFolder = mStorageManager.getFileByPath(mLocalFolder.getRemotePath());
-        
+
         // parse data from remote folder 
         OCFile remoteFolder = fillOCFile((RemoteFile)folderAndFiles.get(0));
         remoteFolder.setParentId(mLocalFolder.getParentId());
@@ -372,6 +373,10 @@ public class SynchronizeFolderOperation extends RemoteOperation {
                 if (remoteFile.isFolder()) {
                     remoteFile.setFileLength(localFile.getFileLength()); 
                         // TODO move operations about size of folders to FileContentProvider
+                } else if (mRemoteFolderChanged && remoteFile.isImage() &&
+                        remoteFile.getModificationTimestamp() != localFile.getModificationTimestamp()) {
+                    remoteFile.setNeedsUpdateThumbnail(true);
+                    Log.d(TAG, "Image " + remoteFile.getFileName() + " updated on the server");
                 }
                 remoteFile.setPublicLink(localFile.getPublicLink());
                 remoteFile.setShareByLink(localFile.isShareByLink());
index 9a2a0d3..e002efb 100644 (file)
@@ -82,10 +82,11 @@ public class FileListListAdapter extends BaseAdapter implements ListAdapter {
             Context context, \r
             ComponentsGetter transferServiceGetter\r
             ) {\r
-        \r
+\r
         mJustFolders = justFolders;\r
         mContext = context;\r
         mAccount = AccountUtils.getCurrentOwnCloudAccount(mContext);\r
+
         mTransferServiceGetter = transferServiceGetter;
         \r
         mAppPreferences = PreferenceManager\r
@@ -98,6 +99,7 @@ public class FileListListAdapter extends BaseAdapter implements ListAdapter {
         \r
         // initialise thumbnails cache on background thread\r
         new ThumbnailsCacheManager.InitDiskCacheTask().execute();\r
+\r
     }\r
     
     @Override\r
@@ -217,7 +219,7 @@ public class FileListListAdapter extends BaseAdapter implements ListAdapter {
                         if (ThumbnailsCacheManager.cancelPotentialWork(file, fileIcon)) {\r
                             final ThumbnailsCacheManager.ThumbnailGenerationTask task = \r
                                     new ThumbnailsCacheManager.ThumbnailGenerationTask(\r
-                                            fileIcon, mStorageManager\r
+                                            fileIcon, mStorageManager, mAccount\r
                                     );\r
                             if (thumbnail == null) {\r
                                 thumbnail = ThumbnailsCacheManager.mDefaultImg;\r