Initial version of FileOperation to encapsulate all events regarding
[pub/Android/ownCloud.git] / src / eu / alefzero / owncloud / ui / fragment / FileDetailFragment.java
index 0397e5b..9bcf9de 100644 (file)
@@ -22,6 +22,7 @@ import java.util.List;
 import android.accounts.Account;\r
 import android.accounts.AccountManager;\r
 import android.app.ActionBar.LayoutParams;\r
+import android.content.ActivityNotFoundException;\r
 import android.content.BroadcastReceiver;\r
 import android.content.Context;\r
 import android.content.Intent;\r
@@ -38,6 +39,7 @@ import android.view.LayoutInflater;
 import android.view.View;\r
 import android.view.View.OnClickListener;\r
 import android.view.ViewGroup;\r
+import android.webkit.MimeTypeMap;\r
 import android.widget.Button;\r
 import android.widget.ImageView;\r
 import android.widget.TextView;\r
@@ -150,6 +152,7 @@ public class FileDetailFragment extends SherlockFragment implements
         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
+        v.setEnabled(false);\r
         getActivity().startService(i);\r
     }\r
 \r
@@ -235,19 +238,48 @@ public class FileDetailFragment extends SherlockFragment implements
                     \r
                 } catch (Throwable t) {\r
                     preview.setVisibility(View.INVISIBLE);\r
-                    Log.e(TAG, "Unexpected error while creating image preview " + mFile.getFileLength());\r
+                    Log.e(TAG, "Unexpected error while creating image preview " + mFile.getFileLength(), t);\r
                 }\r
                 downloadButton.setText(R.string.filedetails_open);\r
                 downloadButton.setOnClickListener(new OnClickListener() {\r
                     @Override\r
                     public void onClick(View v) {\r
-                        Intent i = new Intent(Intent.ACTION_VIEW);\r
-                        i.setDataAndType(Uri.parse("file://"+mFile.getStoragePath()), mFile.getMimetype());\r
-                        List list = getActivity().getPackageManager().queryIntentActivities(i, PackageManager.MATCH_DEFAULT_ONLY);\r
-                        if (list.size() > 0) {\r
+                        String storagePath = mFile.getStoragePath();\r
+                        try {\r
+                            Intent i = new Intent(Intent.ACTION_VIEW);\r
+                            i.setDataAndType(Uri.parse("file://"+ storagePath), mFile.getMimetype());\r
+                            i.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);\r
                             startActivity(i);\r
-                        } else {\r
-                            Toast.makeText(getActivity(), "There is no application to handle file " + mFile.getFileName(), Toast.LENGTH_SHORT).show();\r
+                            \r
+                        } catch (Throwable t) {\r
+                            Log.e(TAG, "Fail when trying to open with the mimeType provided from the ownCloud server: " + mFile.getMimetype());\r
+                            boolean toastIt = true; \r
+                            String mimeType = "";\r
+                            try {\r
+                                Intent i = new Intent(Intent.ACTION_VIEW);\r
+                                mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(storagePath.substring(storagePath.lastIndexOf('.') + 1));\r
+                                if (mimeType != mFile.getMimetype()) {\r
+                                    i.setDataAndType(Uri.parse("file://"+mFile.getStoragePath()), mimeType);\r
+                                    i.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);\r
+                                    startActivity(i);\r
+                                    toastIt = false;\r
+                                }\r
+                                \r
+                            } catch (IndexOutOfBoundsException e) {\r
+                                Log.e(TAG, "Trying to find out MIME type of a file without extension: " + storagePath);\r
+                                \r
+                            } catch (ActivityNotFoundException e) {\r
+                                Log.e(TAG, "No activity found to handle: " + storagePath + " with MIME type " + mimeType + " obtained from extension");\r
+                                \r
+                            } catch (Throwable th) {\r
+                                Log.e(TAG, "Unexpected problem when opening: " + storagePath, th);\r
+                                \r
+                            } finally {\r
+                                if (toastIt) {\r
+                                    Toast.makeText(getActivity(), "There is no application to handle file " + mFile.getFileName(), Toast.LENGTH_SHORT).show();\r
+                                }\r
+                            }\r
+                            \r
                         }\r
                     }\r
                 });\r
@@ -314,24 +346,24 @@ public class FileDetailFragment extends SherlockFragment implements
     }\r
     \r
     /**\r
-     * In ownCloud 3.0.3 and 4.0.0 there is a bug that SabreDAV does not return\r
+     * In ownCloud 3.X.X and 4.X.X there is a bug that SabreDAV does not return\r
      * the time that the file was created. There is a chance that this will\r
      * be fixed in future versions. Use this method to check if this version of\r
      * ownCloud has this fix.\r
-     * @return True, if ownCloud the ownCloud version is > 3.0.4 and 4.0.1\r
+     * @return True, if ownCloud the ownCloud version is supporting creationg time\r
      */\r
     private boolean ocVersionSupportsTimeCreated(){\r
-        if(mIntent != null){\r
+       /* if(mIntent != null){\r
             Account ocAccount = mIntent.getParcelableExtra(FileDownloader.EXTRA_ACCOUNT);\r
             if(ocAccount != null){\r
                 AccountManager accManager = (AccountManager) getActivity().getSystemService(Context.ACCOUNT_SERVICE);\r
                 OwnCloudVersion ocVersion = new OwnCloudVersion(accManager\r
                         .getUserData(ocAccount, AccountAuthenticator.KEY_OC_VERSION));\r
-                if(ocVersion.compareTo(new OwnCloudVersion(0x030004)) >= 0 || ocVersion.compareTo(new OwnCloudVersion(0x040001)) >= 0){\r
+                if(ocVersion.compareTo(new OwnCloudVersion(0x030000)) < 0) {\r
                     return true;\r
                 }\r
             }\r
-        }\r
+        }*/\r
         return false;\r
     }\r
 \r
@@ -342,8 +374,14 @@ public class FileDetailFragment extends SherlockFragment implements
     private class DownloadFinishReceiver extends BroadcastReceiver {\r
         @Override\r
         public void onReceive(Context context, Intent intent) {\r
-            ((OCFile)mIntent.getParcelableExtra(EXTRA_FILE)).setStoragePath(intent.getStringExtra(FileDownloader.EXTRA_FILE_PATH));\r
-            updateFileDetails(mIntent);\r
+            getView().findViewById(R.id.fdDownloadBtn).setEnabled(true);\r
+            if (intent.getAction().equals(FileDownloader.BAD_DOWNLOAD_MESSAGE)) {\r
+                Toast.makeText(context, R.string.downloader_download_failed , Toast.LENGTH_SHORT).show();\r
+                \r
+            } else if (intent.getAction().equals(FileDownloader.DOWNLOAD_FINISH_MESSAGE)) {\r
+                ((OCFile)mIntent.getParcelableExtra(EXTRA_FILE)).setStoragePath(intent.getStringExtra(FileDownloader.EXTRA_FILE_PATH));\r
+                updateFileDetails(mIntent);\r
+            }\r
         }\r
         \r
     }\r