Merge remote-tracking branch 'upstream/develop' into resizedImages
authortobiasKaminsky <tobias@kaminsky.me>
Wed, 1 Jul 2015 17:16:42 +0000 (19:16 +0200)
committertobiasKaminsky <tobias@kaminsky.me>
Wed, 1 Jul 2015 17:16:42 +0000 (19:16 +0200)
1  2 
AndroidManifest.xml
owncloud-android-library
src/com/owncloud/android/datamodel/ThumbnailsCacheManager.java
src/com/owncloud/android/files/FileOperationsHelper.java
src/com/owncloud/android/ui/adapter/FileListListAdapter.java
src/com/owncloud/android/ui/fragment/OCFileListFragment.java
src/com/owncloud/android/ui/preview/PreviewImageActivity.java
src/com/owncloud/android/ui/preview/PreviewImageFragment.java
src/com/owncloud/android/ui/preview/PreviewImagePagerAdapter.java

Simple merge
@@@ -1,1 -1,1 +1,1 @@@
- Subproject commit 9e761387a0b406402684571f28c36c2d6d2b6301
 -Subproject commit f5fbca24becbb01660abe2a7013c1b536ea8a301
++Subproject commit d5aa05bc0ca126626536e035459a33b13eeaf0fd
@@@ -224,17 -205,10 +222,17 @@@ public class ThumbnailsCacheManager 
                      if (mFile instanceof OCFile){
                          tagId = String.valueOf(((OCFile)mFile).getFileId());
                      } else if (mFile instanceof File){
-                         tagId = String.valueOf(((File)mFile).hashCode());
+                         tagId = String.valueOf(mFile.hashCode());
                      }
                      if (String.valueOf(imageView.getTag()).equals(tagId)) {
 +                        if (mProgressWheelRef != null) {
 +                            final ProgressBar progressWheel = mProgressWheelRef.get();
 +                            if (progressWheel != null) {
 +                                progressWheel.setVisibility(View.GONE);
 +                            }
 +                        }
                          imageView.setImageBitmap(bitmap);
 +                        imageView.setVisibility(View.VISIBLE);
                      }
                  }
              }
          private int getThumbnailDimension(){
              // Converts dp to pixel
              Resources r = MainApp.getAppContext().getResources();
-             return (int) Math.round(r.getDimension(R.dimen.file_icon_size_grid));
+             return Math.round(r.getDimension(R.dimen.file_icon_size_grid));
          }
  
 -        private Bitmap doOCFileInBackground() {
 +        private Point getScreenDimension(){
 +            WindowManager wm = (WindowManager) MainApp.getAppContext().getSystemService(Context.WINDOW_SERVICE);
 +            Display display = wm.getDefaultDisplay();
 +            Point test = new Point();
 +            display.getSize(test);
 +            return test;
 +        }
 +
 +        private Bitmap doOCFileInBackground(Boolean isThumbnail) {
 +            Bitmap thumbnail = null;
              OCFile file = (OCFile)mFile;
  
 -            final String imageKey = String.valueOf(file.getRemoteId());
 +            // distinguish between thumbnail and resized image
 +            String temp = String.valueOf(file.getRemoteId());
 +            if (isThumbnail){
 +                temp = "t" + temp;
 +            } else {
 +                temp = "r" + temp;
 +            }
 +
 +            final String imageKey = temp;
  
              // Check disk cache in background thread
 -            Bitmap thumbnail = getBitmapFromDiskCache(imageKey);
 +            thumbnail = getBitmapFromDiskCache(imageKey);
  
              // Not found in disk cache
              if (thumbnail == null || file.needsUpdateThumbnail()) {
  
                  } 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) {
+                     OwnCloudVersion serverOCVersion = AccountUtils.getServerVersion(mAccount);
+                     if (mClient != null && serverOCVersion != null) {
+                         if (serverOCVersion.supportsRemoteThumbnails()) {
                              try {
-                                 int status = -1;
                                  String uri = mClient.getBaseUri() + "" +
                                          "/index.php/apps/files/api/v1/thumbnail/" +
 -                                        px + "/" + px + Uri.encode(file.getRemotePath(), "/");
 +                                        pxW + "/" + pxH + Uri.encode(file.getRemotePath(), "/");
                                  Log_OC.d("Thumbnail", "URI: " + uri);
                                  GetMethod get = new GetMethod(uri);
-                                 status = mClient.executeMethod(get);
+                                 int status = mClient.executeMethod(get);
                                  if (status == HttpStatus.SC_OK) {
 -                                    thumbnail = ThumbnailUtils.extractThumbnail(bitmap, px, px);
+ //                                    byte[] bytes = get.getResponseBody();
+ //                                    Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0,
+ //                                            bytes.length);
+                                     InputStream inputStream = get.getResponseBodyAsStream();
+                                     Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
-                                     Log_OC.d("Thumbnail", type + " size of " + file.getRemotePath() + ": " + bytes.length);
++                                    thumbnail = ThumbnailUtils.extractThumbnail(bitmap, pxW, pxH);
 +                                    byte[] bytes = get.getResponseBody();
 +
 +                                    String type = "";
 +                                    if (mIsThumbnail){
 +                                        type = "Thumbnail";
 +                                    } else {
 +                                        type = "Resized image";
 +                                    }
-                                     Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0,
-                                             bytes.length);
++                                    Log_OC.d("Thumbnail",
++                                            type + " size of " + file.getRemotePath()
++                                                 + ": " + bytes.length);
 +
++                                    // bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
 +
 +                                    if (mIsThumbnail) {
 +                                        thumbnail = ThumbnailUtils.extractThumbnail(bitmap, pxW, pxH);
 +                                    } else {
 +                                        thumbnail = bitmap;
 +                                    }
  
                                      // Add thumbnail to cache
                                      if (thumbnail != null) {
@@@ -24,18 -24,15 +24,18 @@@ package com.owncloud.android.files
  import org.apache.http.protocol.HTTP;
  
  import android.accounts.Account;
- import android.accounts.AccountManager;
  import android.content.Intent;
 +import android.graphics.Bitmap;
  import android.net.Uri;
  import android.support.v4.app.DialogFragment;
  import android.webkit.MimeTypeMap;
  import android.widget.Toast;
  
 +import com.owncloud.android.MainApp;
  import com.owncloud.android.R;
+ import com.owncloud.android.authentication.AccountUtils;
  import com.owncloud.android.datamodel.OCFile;
 +import com.owncloud.android.datamodel.ThumbnailsCacheManager;
  import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
  import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
  
@@@ -389,7 -414,13 +414,8 @@@ public class PreviewImageActivity exten
          } else {
              OCFile currentFile = mPreviewImagePagerAdapter.getFileAt(position); 
              getSupportActionBar().setTitle(currentFile.getFileName());
+             mDrawerToggle.setDrawerIndicatorEnabled(false);
 -            if (!currentFile.isDown()) {
 -                if (!mPreviewImagePagerAdapter.pendingErrorAt(position)) {
 -                    requestForDownload(currentFile);
 -                }
 -            }
 -
++            
              // Call to reset image zoom to initial state
              ((PreviewImagePagerAdapter) mViewPager.getAdapter()).resetZoom();
          }
@@@ -46,13 -40,8 +40,10 @@@ import android.widget.ImageView
  import android.widget.ProgressBar;
  import android.widget.TextView;
  
- import com.actionbarsherlock.view.Menu;
- import com.actionbarsherlock.view.MenuInflater;
- import com.actionbarsherlock.view.MenuItem;
 +import com.owncloud.android.MainApp;
  import com.owncloud.android.R;
  import com.owncloud.android.datamodel.OCFile;
 +import com.owncloud.android.datamodel.ThumbnailsCacheManager;
  import com.owncloud.android.files.FileMenuFilter;
  import com.owncloud.android.lib.common.utils.Log_OC;
  import com.owncloud.android.ui.dialog.ConfirmationDialogFragment;
@@@ -91,22 -81,30 +85,32 @@@ public class PreviewImageFragment exten
      
      private LoadBitmapTask mLoadBitmapTask = null;
  
-     
      /**
-      * Creates a fragment to preview an image.
-      * 
-      * When 'imageFile' or 'ocAccount' are null
-      * 
-      * @param fileToDetail              An {@link OCFile} to preview as an image in the fragment
-      * @param ocAccount                 An ownCloud account; needed to start downloads
-      * @param ignoreFirstSavedState     Flag to work around an unexpected behaviour of {@link FragmentStatePagerAdapter}; TODO better solution 
+      * Public factory method to create a new fragment that previews an image.
+      *
+      * Android strongly recommends keep the empty constructor of fragments as the only public
+      * constructor, and
+      * use {@link #setArguments(Bundle)} to set the needed arguments.
+      *
+      * This method hides to client objects the need of doing the construction in two steps.
+      *
+      * @param imageFile                 An {@link OCFile} to preview as an image in the fragment
+      * @param ignoreFirstSavedState     Flag to work around an unexpected behaviour of
+      *                                  {@link FragmentStatePagerAdapter}
+      *                                  ; TODO better solution
       */
-     public PreviewImageFragment(OCFile fileToDetail, Account ocAccount, boolean ignoreFirstSavedState, boolean showResizedImage) {
-         super(fileToDetail);
-         mAccount = ocAccount;
-         mShowResizedImage = showResizedImage;
-         mIgnoreFirstSavedState = ignoreFirstSavedState;
 -    public static PreviewImageFragment newInstance(OCFile imageFile, boolean ignoreFirstSavedState){
++    public static PreviewImageFragment newInstance(OCFile imageFile, boolean ignoreFirstSavedState,
++            boolean showResizedImage){
+         PreviewImageFragment frag = new PreviewImageFragment();
++        frag.mShowResizedImage = showResizedImage;
+         Bundle args = new Bundle();
+         args.putParcelable(ARG_FILE, imageFile);
+         args.putBoolean(ARG_IGNORE_FIRST, ignoreFirstSavedState);
+         frag.setArguments(args);
+         return frag;
      }
      
      
      /**
          if (getFile() == null) {
              throw new IllegalStateException("Instanced with a NULL OCFile");
          }
-         if (mAccount == null) {
-             throw new IllegalStateException("Instanced with a NULL ownCloud Account");
 -        if (!getFile().isDown()) {
 -            throw new IllegalStateException("There is no local file to preview");
--        }
      }
          
  
      public void onStart() {
          super.onStart();
          if (getFile() != null) {
 -            mLoadBitmapTask = new LoadBitmapTask(mImageView, mMessageView, mProgressWheel);
 -            //mLoadBitmapTask.execute(new String[]{getFile().getStoragePath()});
 -            mLoadBitmapTask.execute(getFile().getStoragePath());
 +            mImageView.setTag(getFile().getFileId());
 +
 +            if (mShowResizedImage){
 +                Bitmap thumbnail = ThumbnailsCacheManager.getBitmapFromDiskCache(
 +                        String.valueOf("r" + getFile().getRemoteId())
 +                );
 +
 +                if (thumbnail != null && !getFile().needsUpdateThumbnail()){
 +                    mProgressWheel.setVisibility(View.GONE);
 +                    mImageView.setImageBitmap(thumbnail);
-                     mImageView.setBitmap(thumbnail);
 +                    mImageView.setVisibility(View.VISIBLE);
 +                    mBitmap  = thumbnail;
 +                } else {
 +                // generate new Thumbnail
 +                    if (ThumbnailsCacheManager.cancelPotentialWork(getFile(), mImageView)) {
 +                        final ThumbnailsCacheManager.ThumbnailGenerationTask task =
 +                                new ThumbnailsCacheManager.ThumbnailGenerationTask(
-                                         mImageView, mContainerActivity.getStorageManager(), mAccount, mProgressWheel
-                                 );
++                                        mImageView, mContainerActivity.getStorageManager(),
++                                        mContainerActivity.getStorageManager().getAccount(),
++                                        mProgressWheel);
 +                        if (thumbnail == null) {
 +                            thumbnail = ThumbnailsCacheManager.mDefaultImg;
 +                        }
 +                        final ThumbnailsCacheManager.AsyncDrawable asyncDrawable =
 +                                new ThumbnailsCacheManager.AsyncDrawable(
 +                                        MainApp.getAppContext().getResources(),
 +                                        thumbnail,
 +                                        task
 +                                );
 +                        mImageView.setImageDrawable(asyncDrawable);
 +                        task.execute(getFile(), false);
 +                    }
 +            }
 +            } else {
 +                mLoadBitmapTask = new LoadBitmapTask(mImageView, mMessageView, mProgressWheel);
-                 mLoadBitmapTask.execute(new String[]{getFile().getStoragePath()});
++                mLoadBitmapTask.execute(getFile().getStoragePath());
 +            }
          }
      }
      
@@@ -104,13 -103,18 +106,16 @@@ public class PreviewImagePagerAdapter e
          OCFile file = mImageFiles.get(i);
          Fragment fragment = null;
          if (file.isDown()) {
-             fragment = new PreviewImageFragment(file, mAccount, mObsoletePositions.contains(Integer.valueOf(i)), false);
+             fragment = PreviewImageFragment.newInstance(file,
 -                    mObsoletePositions.contains(Integer.valueOf(i)));
++                    mObsoletePositions.contains(Integer.valueOf(i)), false);
+             
          } else if (mDownloadErrors.contains(Integer.valueOf(i))) {
-             fragment = new FileDownloadFragment(file, mAccount, true);
+             fragment = FileDownloadFragment.newInstance(file, mAccount, true);
              ((FileDownloadFragment)fragment).setError(true);
              mDownloadErrors.remove(Integer.valueOf(i));
 -            
          } else {
-          fragment = new PreviewImageFragment(file, mAccount, mObsoletePositions.contains(Integer.valueOf(i)), true);
 -            fragment = FileDownloadFragment.newInstance(
 -                    file, mAccount, mObsoletePositions.contains(Integer.valueOf(i))
 -            );
++            fragment = PreviewImageFragment.newInstance(file,
++                    mObsoletePositions.contains(Integer.valueOf(i)), true);
          }
          mObsoletePositions.remove(Integer.valueOf(i));
          return fragment;