FileDetailFragment is not reused in double pane view to avoid problems with preview...
authorDavid A. Velasco <dvelasco@solidgear.es>
Thu, 5 Jul 2012 09:54:12 +0000 (11:54 +0200)
committerDavid A. Velasco <dvelasco@solidgear.es>
Thu, 5 Jul 2012 09:54:12 +0000 (11:54 +0200)
AndroidManifest.xml
src/eu/alefzero/owncloud/ui/activity/FileDisplayActivity.java
src/eu/alefzero/owncloud/ui/fragment/FileDetailFragment.java

index ac0ed71..aa9fb39 100644 (file)
@@ -18,7 +18,7 @@
  -->\r
 <manifest package="eu.alefzero.owncloud"\r
     android:versionCode="1"\r
-    android:versionName="0.1.145B" xmlns:android="http://schemas.android.com/apk/res/android">\r
+    android:versionName="0.1.146B" xmlns:android="http://schemas.android.com/apk/res/android">\r
 \r
     <uses-permission android:name="android.permission.GET_ACCOUNTS" />\r
     <uses-permission android:name="android.permission.USE_CREDENTIALS" />\r
index 74c2ee5..5245abf 100644 (file)
@@ -719,17 +719,11 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
         \r
         // If we are on a large device -> update fragment\r
         if (mDualPane) {\r
-            FileDetailFragment fileDetails = (FileDetailFragment) getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG);\r
-            if (fileDetails == null) {\r
-                // first selected file since the current directory was listed\r
-                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();\r
-                transaction.replace(R.id.file_details_container, new FileDetailFragment(file, AccountUtils.getCurrentOwnCloudAccount(this)), FileDetailFragment.FTAG);\r
-                transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);\r
-                transaction.commit();\r
-            } else {\r
-                // another select file\r
-                fileDetails.updateFileDetails(file, AccountUtils.getCurrentOwnCloudAccount(this));\r
-            }   \r
+            // buttons in the details view a are problematic when trying to reuse an existing fragment; create always a new one solves some of them, BUT no all; downloads are 'dangerous'\r
+            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();\r
+            transaction.replace(R.id.file_details_container, new FileDetailFragment(file, AccountUtils.getCurrentOwnCloudAccount(this)), FileDetailFragment.FTAG);\r
+            transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);\r
+            transaction.commit();\r
             \r
         } else {    // small or medium screen device -> new Activity\r
             Intent showDetailsIntent = new Intent(this, FileDetailActivity.class);\r
index 627c26a..0bf9564 100644 (file)
@@ -27,6 +27,8 @@ import android.content.IntentFilter;
 import android.graphics.Bitmap;\r
 import android.graphics.BitmapFactory;\r
 import android.graphics.BitmapFactory.Options;\r
+import android.graphics.drawable.BitmapDrawable;\r
+import android.graphics.drawable.Drawable;\r
 import android.net.Uri;\r
 import android.os.Bundle;\r
 import android.util.Log;\r
@@ -44,10 +46,8 @@ import com.actionbarsherlock.app.SherlockFragment;
 \r
 import eu.alefzero.owncloud.DisplayUtils;\r
 import eu.alefzero.owncloud.R;\r
-import eu.alefzero.owncloud.authenticator.AccountAuthenticator;\r
 import eu.alefzero.owncloud.datamodel.OCFile;\r
 import eu.alefzero.owncloud.files.services.FileDownloader;\r
-import eu.alefzero.owncloud.utils.OwnCloudVersion;\r
 \r
 /**\r
  * This Fragment is used to display the details about a file.\r
@@ -165,6 +165,17 @@ public class FileDetailFragment extends SherlockFragment implements
         getActivity().startService(i);\r
     }\r
 \r
+\r
+    /**\r
+     * Check if the fragment was created with an empty layout. An empty fragment can't show file details, must be replaced.\r
+     * \r
+     * @return  True when the fragment was created with the empty layout.\r
+     */\r
+    public boolean isEmpty() {\r
+        return mLayout == R.layout.file_details_empty;\r
+    }\r
+\r
+    \r
     /**\r
      * Can be used to get the file that is currently being displayed.\r
      * @return The file on the screen.\r
@@ -190,7 +201,8 @@ public class FileDetailFragment extends SherlockFragment implements
      */\r
     public void updateFileDetails() {\r
 \r
-        if (mFile != null && mLayout == R.layout.file_details_fragment) {\r
+        if (mFile != null && mAccount != null && mLayout == R.layout.file_details_fragment) {\r
+            \r
             Button downloadButton = (Button) getView().findViewById(R.id.fdDownloadBtn);\r
             // set file details\r
             setFilename(mFile.getFileName());\r
@@ -203,9 +215,10 @@ public class FileDetailFragment extends SherlockFragment implements
            \r
             setTimeModified(mFile.getModificationTimestamp());\r
             \r
-            // Update preview\r
             if (mFile.getStoragePath() != null) {\r
+                // Update preview\r
                 ImageView preview = (ImageView) getView().findViewById(R.id.fdPreview);\r
+                boolean previewIsSet = false;\r
                 try {\r
                     if (mFile.getMimetype().startsWith("image/")) {\r
                         BitmapFactory.Options options = new Options();\r
@@ -220,17 +233,23 @@ public class FileDetailFragment extends SherlockFragment implements
 \r
                         Bitmap bmp = BitmapFactory.decodeFile(mFile.getStoragePath(), options);\r
 \r
-                        int width = options.outWidth;\r
-                        int height = options.outHeight;\r
-                        int scale = 1;\r
-                        if (width >= 2048 || height >= 2048) {\r
-                            scale = (int) (Math.ceil(Math.max(height, width)/2048.));\r
-                            options.inSampleSize = scale;\r
-                            bmp.recycle();\r
+                        if (bmp != null) {\r
+                            int width = options.outWidth;\r
+                            int height = options.outHeight;\r
+                            int scale = 1;\r
+                            if (width >= 2048 || height >= 2048) {\r
+                                scale = (int) (Math.ceil(Math.max(height, width)/2048.));\r
+                                options.inSampleSize = scale;\r
+                                bmp.recycle();\r
 \r
-                            bmp = BitmapFactory.decodeFile(mFile.getStoragePath(), options);\r
+                                bmp = BitmapFactory.decodeFile(mFile.getStoragePath(), options);\r
+                            }\r
+                        }\r
+                        if (bmp != null) {\r
+                            //preview.setImageBitmap(bmp);\r
+                            preview.setImageDrawable(new BitmapDrawable(preview.getResources(), bmp));\r
+                            previewIsSet = true;\r
                         }\r
-                        preview.setImageBitmap(bmp);\r
                     }\r
                 } catch (OutOfMemoryError e) {\r
                     preview.setVisibility(View.INVISIBLE);\r
@@ -243,7 +262,13 @@ public class FileDetailFragment extends SherlockFragment implements
                 } catch (Throwable t) {\r
                     preview.setVisibility(View.INVISIBLE);\r
                     Log.e(TAG, "Unexpected error while creating image preview " + mFile.getFileLength(), t);\r
+                    \r
+                } finally {\r
+                    if (!previewIsSet) {\r
+                        resetPreview();\r
+                    }\r
                 }\r
+                // Change download button to open button\r
                 downloadButton.setText(R.string.filedetails_open);\r
                 downloadButton.setOnClickListener(new OnClickListener() {\r
                     @Override\r
@@ -290,10 +315,13 @@ public class FileDetailFragment extends SherlockFragment implements
             } else {\r
                 // Make download button effective\r
                 downloadButton.setOnClickListener(this);\r
+                // Be sure that preview image is reset; the fragment is reused when possible, a preview of other file could be there\r
+                resetPreview();\r
             }\r
         }\r
     }\r
     \r
+    \r
     /**\r
      * Updates the filename in view\r
      * @param filename to set\r
@@ -387,5 +415,16 @@ public class FileDetailFragment extends SherlockFragment implements
         \r
     }\r
     \r
+    \r
+    /**\r
+     * Make the preview image shows the ownCloud logo.\r
+     * \r
+     * To be called when setting a preview image is not possible.\r
+     */\r
+    private void resetPreview() {\r
+        ImageView preview = (ImageView) getView().findViewById(R.id.fdPreview);\r
+        preview.setImageDrawable(getResources().getDrawable(R.drawable.owncloud_logo));\r
+    }\r
+\r
 \r
 }\r