From: tobiasKaminsky Date: Sat, 9 Aug 2014 08:19:50 +0000 (+0200) Subject: Thumbnail X-Git-Tag: oc-android-1.7.0_signed~163^2~8^2~6 X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/commitdiff_plain/d15e5cce063c60b37ec054fa4f7e325670b7d205?ds=inline;hp=--cc Thumbnail --- d15e5cce063c60b37ec054fa4f7e325670b7d205 diff --git a/.classpath b/.classpath index 7bc01d9a..fa15a286 100644 --- a/.classpath +++ b/.classpath @@ -1,9 +1,10 @@ - - + + + diff --git a/lint.xml b/lint.xml index ee0eead5..ce54dadc 100644 --- a/lint.xml +++ b/lint.xml @@ -1,3 +1,6 @@ + + + \ No newline at end of file diff --git a/src/com/owncloud/android/ui/adapter/FileListListAdapter.java b/src/com/owncloud/android/ui/adapter/FileListListAdapter.java index 4a30be66..88e64b17 100644 --- a/src/com/owncloud/android/ui/adapter/FileListListAdapter.java +++ b/src/com/owncloud/android/ui/adapter/FileListListAdapter.java @@ -17,10 +17,22 @@ */ package com.owncloud.android.ui.adapter; +import java.io.File; +import java.io.IOException; +import java.net.URLEncoder; import java.util.Vector; import android.accounts.Account; +import android.accounts.AuthenticatorException; +import android.accounts.OperationCanceledException; import android.content.Context; +import android.content.res.Resources; +import android.graphics.Bitmap; +import android.graphics.Bitmap.CompressFormat; +import android.graphics.BitmapFactory; +import android.media.ThumbnailUtils; +import android.os.AsyncTask; +import android.util.TypedValue; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -36,9 +48,21 @@ import com.owncloud.android.datamodel.FileDataStorageManager; import com.owncloud.android.datamodel.OCFile; import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder; import com.owncloud.android.files.services.FileUploader.FileUploaderBinder; +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.AccountNotFoundException; import com.owncloud.android.ui.activity.ComponentsGetter; import com.owncloud.android.utils.DisplayUtils; +import org.apache.http.HttpEntity; +import org.apache.http.HttpResponse; +import org.apache.http.auth.AuthScope; +import org.apache.http.auth.UsernamePasswordCredentials; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.DefaultHttpClient; +import org.apache.http.util.EntityUtils; + /** * This Adapter populates a ListView with all files and folders in an ownCloud @@ -57,11 +81,144 @@ public class FileListListAdapter extends BaseAdapter implements ListAdapter { private FileDataStorageManager mStorageManager; private Account mAccount; private ComponentsGetter mTransferServiceGetter; - + private final Object mDiskCacheLock = new Object(); + private DiskLruImageCache mDiskLruCache; + private boolean mDiskCacheStarting = true; + private static final int DISK_CACHE_SIZE = 1024 * 1024 * 10; // 10MB + private CompressFormat mCompressFormat = CompressFormat.JPEG; + private int mCompressQuality = 70; + private OwnCloudClient mClient; + public FileListListAdapter(Context context, ComponentsGetter transferServiceGetter) { mContext = context; mAccount = AccountUtils.getCurrentOwnCloudAccount(mContext); mTransferServiceGetter = transferServiceGetter; + + // Initialise disk cache on background thread + new InitDiskCacheTask().execute(); + } + + class InitDiskCacheTask extends AsyncTask { + @Override + protected Void doInBackground(File... params) { + synchronized (mDiskCacheLock) { + mDiskLruCache = new DiskLruImageCache(mContext, "thumbnailCache", DISK_CACHE_SIZE, mCompressFormat,mCompressQuality); + + try { + OwnCloudAccount ocAccount = new OwnCloudAccount(mAccount, mContext); + mClient = OwnCloudClientManagerFactory.getDefaultSingleton(). + getClientFor(ocAccount, mContext); + } catch (AccountNotFoundException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (AuthenticatorException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (OperationCanceledException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + mDiskCacheStarting = false; // Finished initialization + mDiskCacheLock.notifyAll(); // Wake any waiting threads + } + return null; + } + } + + class BitmapWorkerTask extends AsyncTask { + private final ImageView fileIcon; + + public BitmapWorkerTask(ImageView fileIcon) { + this.fileIcon = fileIcon; + } + + // Decode image in background. + @Override + protected Bitmap doInBackground(OCFile... params) { + OCFile file = params[0]; + final String imageKey = String.valueOf(file.getRemoteId()); + + // Check disk cache in background thread + Bitmap thumbnail = getBitmapFromDiskCache(imageKey); + + // Not found in disk cache + if (thumbnail == null) { + // Converts dp to pixel + Resources r = mContext.getResources(); + int px = (int) Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 150, r.getDisplayMetrics())); + + if (file.isDown()){ + Bitmap bitmap = BitmapFactory.decodeFile(file.getStoragePath()); + thumbnail = ThumbnailUtils.extractThumbnail(bitmap, px, px); + + // Add thumbnail to cache + addBitmapToCache(imageKey, thumbnail); + + } else { + // Download thumbnail from server + DefaultHttpClient httpclient = new DefaultHttpClient(); + try { + httpclient.getCredentialsProvider().setCredentials( + new AuthScope(mClient.getBaseUri().toString().replace("https://", ""), 443), + new UsernamePasswordCredentials(mClient.getCredentials().getUsername(), mClient.getCredentials().getAuthToken())); + + + // TODO change to user preview.png + HttpGet httpget = new HttpGet(mClient.getBaseUri() + "/ocs/v1.php/thumbnail?path=" + URLEncoder.encode(file.getRemotePath(), "UTF-8")); + HttpResponse response = httpclient.execute(httpget); + HttpEntity entity = response.getEntity(); + + if (entity != null) { + byte[] bytes = EntityUtils.toByteArray(entity); + 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(); + }finally { + httpclient.getConnectionManager().shutdown(); + } + } + } + return thumbnail; + } + + protected void onPostExecute(Bitmap bitmap){ + fileIcon.setImageBitmap(bitmap); + } + } + + public void addBitmapToCache(String key, Bitmap bitmap) { + synchronized (mDiskCacheLock) { + if (mDiskLruCache != null && mDiskLruCache.getBitmap(key) == null) { + mDiskLruCache.put(key, bitmap); + } + } + } + + public Bitmap getBitmapFromDiskCache(String key) { + synchronized (mDiskCacheLock) { + // Wait while disk cache is started from background thread + while (mDiskCacheStarting) { + try { + mDiskCacheLock.wait(); + } catch (InterruptedException e) {} + } + if (mDiskLruCache != null) { + return (Bitmap) mDiskLruCache.getBitmap(key); + } + } + return null; } @Override @@ -163,15 +320,27 @@ public class FileListListAdapter extends BaseAdapter implements ListAdapter { } checkBoxV.setVisibility(View.VISIBLE); } - + + // first set thumbnail according to Mimetype, prevents empty thumbnails fileIcon.setImageResource(DisplayUtils.getResourceId(file.getMimetype(), file.getFileName())); - + + // get Thumbnail if file is image + if (file.isImage()){ + // Thumbnail in Cache? + Bitmap thumbnail = getBitmapFromDiskCache(String.valueOf(file.getRemoteId())); + if (thumbnail != null){ + fileIcon.setImageBitmap(thumbnail); + } else { + // generate new Thumbnail + new BitmapWorkerTask(fileIcon).execute(file); + } + } + if (checkIfFileIsSharedWithMe(file)) { sharedWithMeIconV.setVisibility(View.VISIBLE); } } else { - fileSizeV.setVisibility(View.INVISIBLE); //fileSizeV.setText(DisplayUtils.bytesToHumanReadable(file.getFileLength())); lastModV.setVisibility(View.VISIBLE);