Merge branch 'master' of gitorious.org:owncloud/android-devel
[pub/Android/ownCloud.git] / src / eu / alefzero / owncloud / ui / fragment / FileDetailFragment.java
index d2f5d10..ee69555 100644 (file)
  */\r
 package eu.alefzero.owncloud.ui.fragment;\r
 \r
+import java.util.List;\r
+\r
 import android.accounts.Account;\r
 import android.accounts.AccountManager;\r
+import android.app.ActionBar.LayoutParams;\r
 import android.content.BroadcastReceiver;\r
 import android.content.Context;\r
 import android.content.Intent;\r
 import android.content.IntentFilter;\r
+import android.content.pm.PackageManager;\r
 import android.graphics.Bitmap;\r
 import android.graphics.BitmapFactory;\r
+import android.graphics.BitmapFactory.Options;\r
+import android.graphics.Path.FillType;\r
 import android.net.Uri;\r
 import android.os.Bundle;\r
 import android.util.Log;\r
@@ -40,10 +46,10 @@ import android.widget.Toast;
 import com.actionbarsherlock.app.SherlockFragment;\r
 \r
 import eu.alefzero.owncloud.DisplayUtils;\r
-import eu.alefzero.owncloud.FileDownloader;\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
@@ -141,7 +147,9 @@ public class FileDetailFragment extends SherlockFragment implements
         Intent i = new Intent(getActivity(), FileDownloader.class);\r
         i.putExtra(FileDownloader.EXTRA_ACCOUNT,\r
                 mIntent.getParcelableExtra(FileDownloader.EXTRA_ACCOUNT));\r
-        i.putExtra(FileDownloader.EXTRA_FILE_PATH, mFile.getRemotePath());\r
+        i.putExtra(FileDownloader.EXTRA_REMOTE_PATH, mFile.getRemotePath());\r
+        i.putExtra(FileDownloader.EXTRA_FILE_PATH, mFile.getURLDecodedRemotePath());\r
+        i.putExtra(FileDownloader.EXTRA_FILE_SIZE, mFile.getFileLength());\r
         getActivity().startService(i);\r
     }\r
 \r
@@ -190,14 +198,31 @@ public class FileDetailFragment extends SherlockFragment implements
             \r
             // Update preview\r
             if (mFile.getStoragePath() != null) {\r
+                ImageView preview = (ImageView) getView().findViewById(R.id.fdPreview);\r
                 try {\r
                     if (mFile.getMimetype().startsWith("image/")) {\r
-                        ImageView preview = (ImageView) getView().findViewById(\r
-                                R.id.fdPreview);\r
-                        Bitmap bmp = BitmapFactory.decodeFile(mFile.getStoragePath());\r
+                        BitmapFactory.Options options = new Options();\r
+                        options.inScaled = true;\r
+                        options.inMutable = false;\r
+                        options.inPreferQualityOverSpeed = false;\r
+                        options.inPurgeable = true;\r
+\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
+\r
+                            bmp = BitmapFactory.decodeFile(mFile.getStoragePath(), options);\r
+                        }\r
                         preview.setImageBitmap(bmp);\r
                     }\r
                 } catch (OutOfMemoryError e) {\r
+                    preview.setVisibility(View.INVISIBLE);\r
                     Log.e(TAG, "Out of memory occured for file with size " + mFile.getFileLength());\r
                 }\r
                 downloadButton.setText(R.string.filedetails_open);\r
@@ -206,7 +231,12 @@ public class FileDetailFragment extends SherlockFragment implements
                     public void onClick(View v) {\r
                         Intent i = new Intent(Intent.ACTION_VIEW);\r
                         i.setDataAndType(Uri.parse("file://"+mFile.getStoragePath()), mFile.getMimetype());\r
-                        startActivity(i);\r
+                        List list = getActivity().getPackageManager().queryIntentActivities(i, PackageManager.MATCH_DEFAULT_ONLY);\r
+                        if (list.size() > 0) {\r
+                            startActivity(i);\r
+                        } else {\r
+                            Toast.makeText(getActivity(), "There is no application to handle file " + mFile.getFileName(), Toast.LENGTH_SHORT).show();\r
+                        }\r
                     }\r
                 });\r
             } else {\r
@@ -300,9 +330,10 @@ public class FileDetailFragment extends SherlockFragment implements
     private class DownloadFinishReceiver extends BroadcastReceiver {\r
         @Override\r
         public void onReceive(Context context, Intent intent) {\r
-            updateFileDetails();\r
+            ((OCFile)mIntent.getParcelableExtra(EXTRA_FILE)).setStoragePath(intent.getStringExtra(FileDownloader.EXTRA_FILE_PATH));\r
+            updateFileDetails(mIntent);\r
         }\r
-\r
+        \r
     }\r
 \r
 }\r