瀏覽代碼

Merge pull request #1190 from owncloud/1182_Open_With_Fix

Fix for #1182 on Android 5
Maria Asensio 9 年之前
父節點
當前提交
567355eed0
共有 2 個文件被更改,包括 37 次插入5 次删除
  1. 1 0
      res/values/strings.xml
  2. 36 5
      src/com/owncloud/android/files/FileOperationsHelper.java

+ 1 - 0
res/values/strings.xml

@@ -74,6 +74,7 @@
     <string name="file_list_seconds_ago">seconds ago</string>
     <string name="file_list_empty">Nothing in here. Upload something!</string>
     <string name="file_list_loading">Loading&#8230;</string>
+    <string name="file_list_no_app_for_file_type">No App found for file type!</string>
     <string name="local_file_list_empty">There are no files in this folder.</string>
     <string name="filedetails_select_file">Tap on a file to display additional information.</string>
     <string name="filedetails_size">Size:</string>

+ 36 - 5
src/com/owncloud/android/files/FileOperationsHelper.java

@@ -22,7 +22,11 @@
 package com.owncloud.android.files;
 
 import android.accounts.Account;
+import android.content.ActivityNotFoundException;
+import android.content.Context;
 import android.content.Intent;
+import android.content.pm.PackageManager;
+import android.content.pm.ResolveInfo;
 import android.net.Uri;
 import android.support.v4.app.DialogFragment;
 import android.webkit.MimeTypeMap;
@@ -43,6 +47,8 @@ import com.owncloud.android.ui.dialog.ShareLinkToDialog;
 
 import org.apache.http.protocol.HTTP;
 
+import java.util.List;
+
 /**
  *
  */
@@ -86,21 +92,46 @@ public class FileOperationsHelper {
                     );
                 }
             }
-            
-            Intent chooserIntent;
+
+            Intent openFileWithIntent;
             if (intentForGuessedMimeType != null) {
-                chooserIntent = Intent.createChooser(intentForGuessedMimeType, mFileActivity.getString(R.string.actionbar_open_with));
+                openFileWithIntent = intentForGuessedMimeType;
             } else {
-                chooserIntent = Intent.createChooser(intentForSavedMimeType, mFileActivity.getString(R.string.actionbar_open_with));
+                openFileWithIntent = intentForSavedMimeType;
             }
 
-            mFileActivity.startActivity(chooserIntent);
+            List<ResolveInfo> launchables = mFileActivity.getPackageManager().
+                    queryIntentActivities(openFileWithIntent, PackageManager.GET_INTENT_FILTERS);
+
+            if(launchables != null && launchables.size() > 0) {
+                try {
+                    mFileActivity.startActivity(
+                            Intent.createChooser(
+                                    openFileWithIntent, mFileActivity.getString(R.string.actionbar_open_with)
+                            )
+                    );
+                } catch (ActivityNotFoundException anfe) {
+                    showNoAppForFileTypeToast(mFileActivity.getApplicationContext());
+                }
+            } else {
+                showNoAppForFileTypeToast(mFileActivity.getApplicationContext());
+            }
 
         } else {
             Log_OC.wtf(TAG, "Trying to open a NULL OCFile");
         }
     }
 
+    /**
+     * Displays a toast stating that no application could be found to open the file.
+     *
+     * @param context the context to be able to show a toast.
+     */
+    private void showNoAppForFileTypeToast(Context context) {
+        Toast.makeText(context,
+                R.string.file_list_no_app_for_file_type, Toast.LENGTH_SHORT)
+                .show();
+    }
 
     public void shareFileWithLink(OCFile file) {