Browse Source

Merge pull request #687 from owncloud/thumbnails_from_server

Get remote thumbnails from server (OC8)
David A. Velasco 10 years ago
parent
commit
183cd732d0

+ 4 - 4
res/layout/list_item.xml

@@ -32,16 +32,16 @@
 
         <ImageView
             android:id="@+id/imageView2"
-            android:layout_width="32dp"
-            android:layout_height="32dp"
+            android:layout_width="@dimen/file_icon_size"
+            android:layout_height="@dimen/file_icon_size"
             android:layout_gravity="center_vertical"
             android:layout_marginLeft="22dp"
             android:src="@drawable/local_file_indicator" />
 
         <ImageView
             android:id="@+id/imageView1"
-            android:layout_width="32dp"
-            android:layout_height="32dp"
+            android:layout_width="@dimen/file_icon_size"
+            android:layout_height="@dimen/file_icon_size"
             android:layout_gravity="center_vertical"
             android:layout_marginLeft="9dp"
             android:src="@drawable/ic_menu_archive" />

+ 21 - 0
res/values/dims.xml

@@ -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>

+ 1 - 1
src/com/owncloud/android/MainApp.java

@@ -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();
         

+ 1 - 0
src/com/owncloud/android/datamodel/FileDataStorageManager.java

@@ -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())) {

+ 64 - 8
src/com/owncloud/android/datamodel/ThumbnailsCacheManager.java

@@ -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");
+                            }
+                        }
                     }
                 }
                 

+ 6 - 1
src/com/owncloud/android/operations/SynchronizeFolderOperation.java

@@ -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());

+ 4 - 2
src/com/owncloud/android/ui/adapter/FileListListAdapter.java

@@ -82,10 +82,11 @@ public class FileListListAdapter extends BaseAdapter implements ListAdapter {
             Context context, 
             ComponentsGetter transferServiceGetter
             ) {
-        
+
         mJustFolders = justFolders;
         mContext = context;
         mAccount = AccountUtils.getCurrentOwnCloudAccount(mContext);
+
         mTransferServiceGetter = transferServiceGetter;
         
         mAppPreferences = PreferenceManager
@@ -98,6 +99,7 @@ public class FileListListAdapter extends BaseAdapter implements ListAdapter {
         
         // initialise thumbnails cache on background thread
         new ThumbnailsCacheManager.InitDiskCacheTask().execute();
+
     }
     
     @Override
@@ -217,7 +219,7 @@ public class FileListListAdapter extends BaseAdapter implements ListAdapter {
                         if (ThumbnailsCacheManager.cancelPotentialWork(file, fileIcon)) {
                             final ThumbnailsCacheManager.ThumbnailGenerationTask task = 
                                     new ThumbnailsCacheManager.ThumbnailGenerationTask(
-                                            fileIcon, mStorageManager
+                                            fileIcon, mStorageManager, mAccount
                                     );
                             if (thumbnail == null) {
                                 thumbnail = ThumbnailsCacheManager.mDefaultImg;