Better policies to handle possible errors in remote MIME type values; avoid crashes...
[pub/Android/ownCloud.git] / src / eu / alefzero / owncloud / ui / fragment / FileDetailFragment.java
index 6b2aa86..4509372 100644 (file)
@@ -39,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.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
 import android.widget.Button;\r
 import android.widget.ImageView;\r
 import android.widget.TextView;\r
@@ -236,20 +237,48 @@ public class FileDetailFragment extends SherlockFragment implements
                     \r
                 } catch (Throwable t) {\r
                     preview.setVisibility(View.INVISIBLE);\r
                     \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
                 }\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
-                        i.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);\r
+                        String storagePath = mFile.getStoragePath();\r
                         try {\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
                             \r
                             startActivity(i);\r
                             \r
-                        } catch (ActivityNotFoundException e) {\r
-                            Toast.makeText(getActivity(), "There is no application to handle file " + mFile.getFileName(), Toast.LENGTH_SHORT).show();\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
                         }\r
                     }\r
                 });\r