From: David A. Velasco Date: Thu, 28 Jun 2012 08:59:27 +0000 (+0200) Subject: Better policies to handle possible errors in remote MIME type values; avoid crashes... X-Git-Tag: oc-android-1.4.3~329 X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/commitdiff_plain/121a462bdf08707c2518a9c4a438d8d723c2fbf5?ds=inline;hp=--cc Better policies to handle possible errors in remote MIME type values; avoid crashes when MIME types is trying to be found for files without extension --- 121a462bdf08707c2518a9c4a438d8d723c2fbf5 diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 841ce234..b904bd6b 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -18,7 +18,7 @@ --> + android:versionName="0.1.138B" xmlns:android="http://schemas.android.com/apk/res/android"> diff --git a/src/eu/alefzero/owncloud/files/PhotoTakenBroadcastReceiver.java b/src/eu/alefzero/owncloud/files/PhotoTakenBroadcastReceiver.java index b6a0a604..93149426 100644 --- a/src/eu/alefzero/owncloud/files/PhotoTakenBroadcastReceiver.java +++ b/src/eu/alefzero/owncloud/files/PhotoTakenBroadcastReceiver.java @@ -111,9 +111,16 @@ public class PhotoTakenBroadcastReceiver extends BroadcastReceiver { Intent upload_intent = new Intent(context, InstantUploadService.class); Account account = new Account(account_name, AccountAuthenticator.ACCOUNT_TYPE); - String mimeType = MimeTypeMap.getSingleton() - .getMimeTypeFromExtension( - f.getName().substring(f.getName().lastIndexOf('.') + 1)); + String mimeType; + try { + mimeType = MimeTypeMap.getSingleton() + .getMimeTypeFromExtension( + f.getName().substring(f.getName().lastIndexOf('.') + 1)); + + } catch (IndexOutOfBoundsException e) { + Log.e(TAG, "Trying to find out MIME type of a file without extension: " + f.getName()); + mimeType = "application/octet-stream"; + } upload_intent.putExtra(InstantUploadService.KEY_ACCOUNT, account); upload_intent.putExtra(InstantUploadService.KEY_FILE_PATH, file_path); diff --git a/src/eu/alefzero/owncloud/files/services/FileUploader.java b/src/eu/alefzero/owncloud/files/services/FileUploader.java index 5d748c96..2da59925 100644 --- a/src/eu/alefzero/owncloud/files/services/FileUploader.java +++ b/src/eu/alefzero/owncloud/files/services/FileUploader.java @@ -167,10 +167,18 @@ public class FileUploader extends Service implements OnDatatransferProgressListe Log.d(TAG, "Will upload " + mTotalDataToSend + " bytes, with " + mLocalPaths.length + " files"); for (int i = 0; i < mLocalPaths.length; ++i) { - String mimeType = MimeTypeMap.getSingleton() - .getMimeTypeFromExtension( - mLocalPaths[i].substring(mLocalPaths[i] + + String mimeType; + try { + mimeType = MimeTypeMap.getSingleton() + .getMimeTypeFromExtension( + mLocalPaths[i].substring(mLocalPaths[i] .lastIndexOf('.') + 1)); + } catch (IndexOutOfBoundsException e) { + Log.e(TAG, "Trying to find out MIME type of a file without extension: " + mLocalPaths[i]); + mimeType = "application/octet-stream"; + } + mResult = false; mCurrentIndexUpload = i; if (wc.putFile(mLocalPaths[i], mRemotePaths[i], mimeType)) { diff --git a/src/eu/alefzero/owncloud/syncadapter/FileSyncAdapter.java b/src/eu/alefzero/owncloud/syncadapter/FileSyncAdapter.java index 8d76328d..7255e5d1 100644 --- a/src/eu/alefzero/owncloud/syncadapter/FileSyncAdapter.java +++ b/src/eu/alefzero/owncloud/syncadapter/FileSyncAdapter.java @@ -143,16 +143,7 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter { OCFile file = new OCFile(we.path()); file.setCreationTimestamp(we.createTimestamp()); file.setFileLength(we.contentLength()); - - // dvelasco; looks like server is not sending very precise mimeTypes; mp3 file results un application/oct - String filename = file.getFileName(); - String mimeType = MimeTypeMap.getSingleton() - .getMimeTypeFromExtension(filename.substring(filename.lastIndexOf('.') + 1)); - if (mimeType == null) - file.setMimetype(we.contentType()); - else - file.setMimetype(mimeType); - + file.setMimetype(we.contentType()); file.setModificationTimestamp(we.modifiedTimesamp()); file.setLastSyncDate(mCurrentSyncTime); return file; diff --git a/src/eu/alefzero/owncloud/ui/fragment/FileDetailFragment.java b/src/eu/alefzero/owncloud/ui/fragment/FileDetailFragment.java index 6b2aa86a..4509372b 100644 --- a/src/eu/alefzero/owncloud/ui/fragment/FileDetailFragment.java +++ b/src/eu/alefzero/owncloud/ui/fragment/FileDetailFragment.java @@ -39,6 +39,7 @@ import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; +import android.webkit.MimeTypeMap; import android.widget.Button; import android.widget.ImageView; import android.widget.TextView; @@ -236,20 +237,48 @@ public class FileDetailFragment extends SherlockFragment implements } catch (Throwable t) { preview.setVisibility(View.INVISIBLE); - Log.e(TAG, "Unexpected error while creating image preview " + mFile.getFileLength()); + Log.e(TAG, "Unexpected error while creating image preview " + mFile.getFileLength(), t); } downloadButton.setText(R.string.filedetails_open); downloadButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { - Intent i = new Intent(Intent.ACTION_VIEW); - i.setDataAndType(Uri.parse("file://"+mFile.getStoragePath()), mFile.getMimetype()); - i.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); + String storagePath = mFile.getStoragePath(); try { + Intent i = new Intent(Intent.ACTION_VIEW); + i.setDataAndType(Uri.parse("file://"+ storagePath), mFile.getMimetype()); + i.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); startActivity(i); - } catch (ActivityNotFoundException e) { - Toast.makeText(getActivity(), "There is no application to handle file " + mFile.getFileName(), Toast.LENGTH_SHORT).show(); + } catch (Throwable t) { + Log.e(TAG, "Fail when trying to open with the mimeType provided from the ownCloud server: " + mFile.getMimetype()); + boolean toastIt = true; + String mimeType = ""; + try { + Intent i = new Intent(Intent.ACTION_VIEW); + mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(storagePath.substring(storagePath.lastIndexOf('.') + 1)); + if (mimeType != mFile.getMimetype()) { + i.setDataAndType(Uri.parse("file://"+mFile.getStoragePath()), mimeType); + i.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); + startActivity(i); + toastIt = false; + } + + } catch (IndexOutOfBoundsException e) { + Log.e(TAG, "Trying to find out MIME type of a file without extension: " + storagePath); + + } catch (ActivityNotFoundException e) { + Log.e(TAG, "No activity found to handle: " + storagePath + " with MIME type " + mimeType + " obtained from extension"); + + } catch (Throwable th) { + Log.e(TAG, "Unexpected problem when opening: " + storagePath, th); + + } finally { + if (toastIt) { + Toast.makeText(getActivity(), "There is no application to handle file " + mFile.getFileName(), Toast.LENGTH_SHORT).show(); + } + } + } } }); diff --git a/src/eu/alefzero/webdav/WebdavEntry.java b/src/eu/alefzero/webdav/WebdavEntry.java index 032d996a..f4fbda45 100644 --- a/src/eu/alefzero/webdav/WebdavEntry.java +++ b/src/eu/alefzero/webdav/WebdavEntry.java @@ -52,6 +52,10 @@ public class WebdavEntry { prop = propSet.get(DavPropertyName.GETCONTENTTYPE); if (prop != null) { mContentType = (String) prop.getValue(); + // dvelasco: some builds of ownCloud server 4.0.x added a trailing ';' to the MIME type ; if looks fixed, but let's be cautious + if (mContentType.indexOf(";") >= 0) { + mContentType = mContentType.substring(0, mContentType.indexOf(";")); + } } else { mContentType = "DIR"; /*