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