Merge remote-tracking branch 'remotes/upstream/resizedImages' into beta
authortobiasKaminsky <tobias@kaminsky.me>
Sat, 21 Nov 2015 08:50:57 +0000 (09:50 +0100)
committertobiasKaminsky <tobias@kaminsky.me>
Sat, 21 Nov 2015 08:50:57 +0000 (09:50 +0100)
1  2 
src/com/owncloud/android/datamodel/ThumbnailsCacheManager.java
src/com/owncloud/android/ui/preview/PreviewImageFragment.java

@@@ -24,8 -24,6 +24,8 @@@ package com.owncloud.android.datamodel
  import java.io.File;
  import java.io.InputStream;
  import java.lang.ref.WeakReference;
 +import java.net.FileNameMap;
 +import java.net.URLConnection;
  
  import org.apache.commons.httpclient.HttpStatus;
  import org.apache.commons.httpclient.methods.GetMethod;
@@@ -37,10 -35,8 +37,10 @@@ import android.content.res.Resources
  import android.graphics.Bitmap;
  import android.graphics.Bitmap.CompressFormat;
  import android.graphics.BitmapFactory;
 +import android.graphics.Canvas;
  import android.graphics.Point;
  import android.graphics.Canvas;
 +import android.graphics.Paint;
  import android.graphics.drawable.BitmapDrawable;
  import android.graphics.drawable.ColorDrawable;
  import android.graphics.drawable.Drawable;
@@@ -64,7 -60,6 +64,7 @@@ import com.owncloud.android.lib.resourc
  import com.owncloud.android.ui.adapter.DiskLruImageCache;
  import com.owncloud.android.utils.BitmapUtils;
  import com.owncloud.android.utils.DisplayUtils;
 +import com.owncloud.android.utils.FileStorageUtils;
  
  /**
   * Manager for concurrent access to thumbnails cache.
@@@ -152,32 -147,6 +152,32 @@@ public class ThumbnailsCacheManager 
          return null;
      }
  
 +    /**
 +     * Sets max size of cache
 +     * @param maxSize in MB
 +     * @return
 +     */
 +    public static boolean setMaxSize(long maxSize){
 +        if (mThumbnailCache != null){
 +            mThumbnailCache.setMaxSize(maxSize * 1024 * 1024);
 +            return true;
 +        } else {
 +            return false;
 +        }
 +    }
 +
 +    /**
 +     * Shows max cache size
 +     * @return max cache size in MB.
 +     */
 +    public static long getMaxSize(){
 +        if (mThumbnailCache != null) {
 +            return mThumbnailCache.getMaxSize() / 1024 / 1024;
 +        } else {
 +            return -1l;
 +        }
 +    }
 +
      public static class ThumbnailGenerationTask extends AsyncTask<Object, Void, Bitmap> {
          private final WeakReference<ImageView> mImageViewReference;
          private WeakReference<ProgressBar> mProgressWheelRef;
                  
                  if (mFile instanceof OCFile) {
                      thumbnail = doOCFileInBackground(mIsThumbnail);
 +
 +                    if (((OCFile) mFile).isVideo()){
 +                        thumbnail = addVideoOverlay(thumbnail);
 +                    }
                  }  else if (mFile instanceof File) {
                      thumbnail = doFileInBackground(mIsThumbnail);
 -                } else {
 -                    // do nothing
 +
 +                    String url = ((File) mFile).getAbsolutePath();
 +                    String mMimeType = FileStorageUtils.getMimeTypeFromName(url);
 +
 +                    if (mMimeType != null && mMimeType.startsWith("video/")){
 +                        thumbnail = addVideoOverlay(thumbnail);
 +                    }
 +                //} else {  do nothing
                  }
  
                  }catch(Throwable t){
                              }
                          }
                          imageView.setImageBitmap(bitmap);
-                         imageView.setVisibility(View.VISIBLE);
+                         // imageView.setVisibility(View.VISIBLE);
                      }
                  }
              }
                  }
  
                  if (file.isDown()) {
 -                    Bitmap bitmap = BitmapUtils.decodeSampledBitmapFromFile(
 +                    Bitmap tempBitmap = BitmapUtils.decodeSampledBitmapFromFile(
                              file.getStoragePath(), pxW, pxH);
 +                    Bitmap bitmap = ThumbnailUtils.extractThumbnail(tempBitmap, pxW, pxH);
  
                      if (bitmap != null) {
                          // Handle PNG
                              bitmap = handlePNG(bitmap, pxW);
                          }
  
 -                        thumbnail = addThumbnailToCache(imageKey, bitmap, file.getStoragePath(), pxW, pxH);
 +                        thumbnail = addThumbnailToCache(imageKey, bitmap,
 +                                                        file.getStoragePath(), pxW, pxH);
  
                          file.setNeedsUpdateThumbnail(false);
                          mStorageManager.saveFile(file);
                      if (mClient != null && serverOCVersion != null) {
                          if (serverOCVersion.supportsRemoteThumbnails()) {
                              try {
 -                                String uri = mClient.getBaseUri() + "" +
 -                                        "/index.php/apps/files/api/v1/thumbnail/" +
 -                                        pxW + "/" + pxH + Uri.encode(file.getRemotePath(), "/");
 -                                Log_OC.d("Thumbnail", "URI: " + uri);
 -                                GetMethod get = new GetMethod(uri);
 -                                int status = mClient.executeMethod(get);
 -                                if (status == HttpStatus.SC_OK) {
 -                                    InputStream inputStream = get.getResponseBodyAsStream();
 -                                    Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
 -                                    thumbnail = ThumbnailUtils.extractThumbnail(bitmap, pxW, pxH);
 -                                    byte[] bytes = get.getResponseBody();
 -
 -                                    if (mIsThumbnail) {
 +                                if (mIsThumbnail) {
 +                                    String uri = mClient.getBaseUri() + "" +
 +                                            "/index.php/apps/files/api/v1/thumbnail/" +
 +                                            pxW + "/" + pxH + Uri.encode(file.getRemotePath(), "/");
 +                                    Log_OC.d("Thumbnail", "Download URI: " + uri);
 +                                    GetMethod get = new GetMethod(uri);
 +                                    int status = mClient.executeMethod(get);
 +                                    if (status == HttpStatus.SC_OK) {
 +                                        InputStream inputStream = get.getResponseBodyAsStream();
 +                                        Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                                          thumbnail = ThumbnailUtils.extractThumbnail(bitmap, pxW, pxH);
                                      } else {
 -                                        thumbnail = bitmap;
 +                                        Log_OC.d(TAG, "Status: " + status);
                                      }
 -
 -                                    // Handle PNG
 -                                    if (file.getMimetype().equalsIgnoreCase("image/png")) {
 -                                        thumbnail = handlePNG(thumbnail, pxW);
 +                                } else {
 +                                    String gallery = "";
 +                                    if (serverOCVersion.supportsNativeGallery()){
 +                                        gallery = "gallery";
 +                                    } else {
 +                                        gallery = "galleryplus";
                                      }
  
 -                                    // Add thumbnail to cache
 -                                    if (thumbnail != null) {
 -                                        addBitmapToCache(imageKey, thumbnail);
 +                                    String uri = mClient.getBaseUri() +
 +                                            "/index.php/apps/" + gallery + "/api/preview/" + Integer.parseInt(file.getRemoteId().substring(0,8)) +
 +                                            "/" + pxW + "/" + pxH;
 +                                    Log_OC.d("Thumbnail", "FileName: " + file.getFileName() + " Download URI: " + uri);
 +                                    GetMethod get = new GetMethod(uri);
 +                                    int status = mClient.executeMethod(get);
 +                                    if (status == HttpStatus.SC_OK) {
 +                                        InputStream inputStream = get.getResponseBodyAsStream();
 +                                        Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
 +                                        // Download via gallery app
 +                                        thumbnail = bitmap;
                                      }
                                  }
 +
 +                                // Handle PNG
 +                                if (thumbnail != null && file.getMimetype().equalsIgnoreCase("image/png")) {
 +                                    thumbnail = handlePNG(thumbnail, pxW);
 +                                }
 +
 +                                // Add thumbnail to cache
 +                                if (thumbnail != null) {
 +                                    addBitmapToCache(imageKey, thumbnail);
 +                                }
                              } catch (Exception e) {
                                  e.printStackTrace();
                              }
          return null;
      }
  
 +    public static Bitmap addVideoOverlay(Bitmap thumbnail){
 +        Bitmap playButton = BitmapFactory.decodeResource(MainApp.getAppContext().getResources(),
 +                R.drawable.view_play);
 +
 +        Bitmap resizedPlayButton = Bitmap.createScaledBitmap(playButton,
 +                (int) (thumbnail.getWidth() * 0.3),
 +                (int) (thumbnail.getHeight() * 0.3), true);
 +
 +        Bitmap resultBitmap = Bitmap.createBitmap(thumbnail.getWidth(),
 +                thumbnail.getHeight(),
 +                Bitmap.Config.ARGB_8888);
 +
 +        Canvas c = new Canvas(resultBitmap);
 +
 +        // compute visual center of play button, according to resized image
 +        int x1 = resizedPlayButton.getWidth();
 +        int y1 = resizedPlayButton.getHeight() / 2;
 +        int x2 = 0;
 +        int y2 = resizedPlayButton.getWidth();
 +        int x3 = 0;
 +        int y3 = 0;
 +
 +        double ym = ( ((Math.pow(x3,2) - Math.pow(x1,2) + Math.pow(y3,2) - Math.pow(y1,2)) *
 +                    (x2 - x1)) - (Math.pow(x2,2) - Math.pow(x1,2) + Math.pow(y2,2) -
 +                    Math.pow(y1,2)) * (x3 - x1) )  /  (2 * ( ((y3 - y1) * (x2 - x1)) -
 +                    ((y2 - y1) * (x3 - x1)) ));
 +        double xm = ( (Math.pow(x2,2) - Math.pow(x1,2)) + (Math.pow(y2,2) - Math.pow(y1,2)) -
 +                    (2*ym*(y2 - y1)) ) / (2*(x2 - x1));
 +
 +        // offset to top left
 +        double ox = - xm;
 +        double oy = thumbnail.getHeight() - ym;
 +
 +
 +        c.drawBitmap(thumbnail, 0, 0, null);
 +
 +        Paint p = new Paint();
 +        p.setAlpha(230);
 +
 +        c.drawBitmap(resizedPlayButton, (float) ((thumbnail.getWidth() / 2) + ox),
 +                                        (float) ((thumbnail.getHeight() / 2) - ym), p);
 +
 +        return resultBitmap;
 +    }
 +
      public static class AsyncDrawable extends BitmapDrawable {
          private final WeakReference<ThumbnailGenerationTask> bitmapWorkerTaskReference;
  
@@@ -208,17 -208,29 +208,29 @@@ public class PreviewImageFragment exten
              mImageView.setTag(getFile().getFileId());
  
              if (mShowResizedImage){
-                 Bitmap thumbnail = ThumbnailsCacheManager.getBitmapFromDiskCache(
-                         String.valueOf("r" + getFile().getRemoteId())
-                 );
+                 Bitmap resizedImage = ThumbnailsCacheManager.getBitmapFromDiskCache(
+                         String.valueOf("r" + getFile().getRemoteId()));
  
-                 if (thumbnail != null && !getFile().needsUpdateThumbnail()){
+                 if (resizedImage != null && !getFile().needsUpdateThumbnail()){
                      mProgressWheel.setVisibility(View.GONE);
-                     mImageView.setImageBitmap(thumbnail);
+                     mImageView.setImageBitmap(resizedImage);
                      mImageView.setVisibility(View.VISIBLE);
-                     mBitmap  = thumbnail;
+                     mBitmap  = resizedImage;
                  } else {
-                     // generate new Thumbnail
+                     // show thumbnail while loading resized image
+                     Bitmap thumbnail = ThumbnailsCacheManager.getBitmapFromDiskCache(
+                             String.valueOf("t" + getFile().getRemoteId()));
+                     if (thumbnail != null){
+                         mImageView.setImageBitmap(thumbnail);
+                         mProgressWheel.setVisibility(View.VISIBLE);
+                         mImageView.setVisibility(View.VISIBLE);
+                         mBitmap = thumbnail;
+                     } else {
+                         thumbnail = ThumbnailsCacheManager.mDefaultImg;
+                     }
+                     // generate new resized image
                      if (ThumbnailsCacheManager.cancelPotentialWork(getFile(), mImageView) &&
                          mContainerActivity.getStorageManager() != null) {
                          final ThumbnailsCacheManager.ThumbnailGenerationTask task =
                                          mImageView, mContainerActivity.getStorageManager(),
                                          mContainerActivity.getStorageManager().getAccount(),
                                          mProgressWheel);
-                         if (thumbnail == null) {
-                             thumbnail = ThumbnailsCacheManager.mDefaultImg;
+                         if (resizedImage == null) {
+                             resizedImage = thumbnail;
                          }
                          final ThumbnailsCacheManager.AsyncDrawable asyncDrawable =
                                  new ThumbnailsCacheManager.AsyncDrawable(
                                          MainApp.getAppContext().getResources(),
-                                         thumbnail,
+                                         resizedImage,
                                          task
                                  );
                          mImageView.setImageDrawable(asyncDrawable);
      public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
          super.onCreateOptionsMenu(menu, inflater);
          inflater.inflate(R.menu.file_actions_menu, menu);
 +
 +//        MenuItem item = menu.findItem(R.id.action_set_as_wallpaper);
 +//        item.setVisible(getFile().isDown());
      }
  
      /**
                  mContainerActivity.getFileOperationsHelper().toggleFavorite(getFile(), false);
                  return true;
              }
 +            case R.id.action_set_as_wallpaper:{
 +                mContainerActivity.getFileOperationsHelper().setPictureAs(getFile());
 +                return true;
 +            }
              default:
                  return false;
          }
                      imageView.setBackground(backrepeat);
                  }
  
 -                imageView.setImageBitmap(bitmap);
 +                if (result.ocFile.getMimetype().equalsIgnoreCase("image/gif")){
 +                    imageView.setGifImage(result.ocFile);
 +                } else {
 +                    imageView.setImageBitmap(bitmap);
 +                }
 +
                  imageView.setVisibility(View.VISIBLE);
                  mBitmap  = bitmap;  // needs to be kept for recycling when not useful
              }