浏览代码

Add open URL file feature

matsuo 8 年之前
父节点
当前提交
cb622e5dbc
共有 1 个文件被更改,包括 102 次插入26 次删除
  1. 102 26
      src/com/owncloud/android/files/FileOperationsHelper.java

+ 102 - 26
src/com/owncloud/android/files/FileOperationsHelper.java

@@ -30,6 +30,7 @@ import android.content.pm.PackageManager;
 import android.content.pm.ResolveInfo;
 import android.net.Uri;
 import android.os.Parcelable;
+import android.support.annotation.Nullable;
 import android.support.v4.app.DialogFragment;
 import android.webkit.MimeTypeMap;
 import android.widget.Toast;
@@ -50,8 +51,13 @@ import com.owncloud.android.ui.activity.FileActivity;
 import com.owncloud.android.ui.activity.ShareActivity;
 import com.owncloud.android.ui.dialog.ShareLinkToDialog;
 
+import java.io.BufferedReader;
+import java.io.FileReader;
+import java.io.IOException;
 import java.util.Collection;
 import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
 /**
  *
@@ -71,42 +77,112 @@ public class FileOperationsHelper {
         mFileActivity = fileActivity;
     }
 
+    /**
+     * Windows internet shortcut file .url
+	 * Ubuntu internet shortcut file .desktop
+	 */
+    @Nullable
+    private String getUrlFromUrlFile(String file) {
+        String url = null;
+        Pattern p = Pattern.compile("^URL=(.+)$");
+
+        try {
+            FileReader fr = new FileReader(file);
+            BufferedReader br = new BufferedReader(fr);
+
+            String line;
+            while ((line = br.readLine()) != null) {
+                Matcher m = p.matcher(line);
+                if (m.find()) {
+                    url = m.group(1);
+                    break;
+                }
+            }
+            br.close();
+            fr.close();
+        } catch (IOException ex) {
+            return null;
+        }
+        return url;
+    }
+
+    /**
+     * mac internet shortcut file .webloc
+	 */
+    @Nullable
+    private String getUrlFromWeblocFile(String file) {
+        String url = null;
+        Pattern p = Pattern.compile("<string>(.+)</string>");
+
+        try {
+            FileReader fr = new FileReader(file);
+            BufferedReader br = new BufferedReader(fr);
+
+            String line;
+            while ((line = br.readLine()) != null) {
+                Matcher m = p.matcher(line);
+                if (m.find()) {
+                    url = m.group(1);
+                    break;
+                }
+            }
+            br.close();
+            fr.close();
+        } catch (IOException ex) {
+            return null;
+        }
+        return url;
+    }
+
+    @Nullable
+    private Intent createIntentFromFile(String storagePath) {
+        String url = null;
+        int lastIndexOfDot = storagePath.lastIndexOf('.');
+        if (lastIndexOfDot >= 0) {
+            String fileExt = storagePath.substring(lastIndexOfDot + 1);
+            if (fileExt.equalsIgnoreCase("url") ||fileExt.equalsIgnoreCase("desktop")) {
+                url = getUrlFromUrlFile(storagePath);
+            } else if (fileExt.equalsIgnoreCase("webloc")) {
+                url = getUrlFromWeblocFile(storagePath);
+            }
+        }
+        if (url == null) {
+            return null;
+        }
+        return new Intent(Intent.ACTION_VIEW, Uri.parse(url));
+    }
 
     public void openFile(OCFile file) {
         if (file != null) {
             String storagePath = file.getStoragePath();
             String encodedStoragePath = WebdavUtils.encodePath(storagePath);
-
-            Intent intentForSavedMimeType = new Intent(Intent.ACTION_VIEW);
-            intentForSavedMimeType.setDataAndType(Uri.parse("file://"+ encodedStoragePath),
-                    file.getMimetype());
-            intentForSavedMimeType.setFlags(
-                    Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION
-            );
-            
-            Intent intentForGuessedMimeType = null;
-            if (storagePath.lastIndexOf('.') >= 0) {
-                String guessedMimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(
-                        storagePath.substring(storagePath.lastIndexOf('.') + 1)
-                );
-                if (guessedMimeType != null && !guessedMimeType.equals(file.getMimetype())) {
-                    intentForGuessedMimeType = new Intent(Intent.ACTION_VIEW);
-                    intentForGuessedMimeType.setDataAndType(Uri.parse("file://" +
-                            encodedStoragePath), guessedMimeType);
-                    intentForGuessedMimeType.setFlags(
-                            Intent.FLAG_GRANT_READ_URI_PERMISSION |
-                                    Intent.FLAG_GRANT_WRITE_URI_PERMISSION
-                    );
+			Uri uri = Uri.parse("file://" + encodedStoragePath);
+
+            Intent openFileWithIntent = null;
+            int lastIndexOfDot = storagePath.lastIndexOf('.');
+            if (lastIndexOfDot >= 0) {
+                String fileExt = storagePath.substring(lastIndexOfDot + 1);
+                String guessedMimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(fileExt);
+                if (guessedMimeType != null) {
+                    openFileWithIntent = new Intent(Intent.ACTION_VIEW);
+                    openFileWithIntent.setDataAndType(uri, guessedMimeType);
                 }
             }
 
-            Intent openFileWithIntent;
-            if (intentForGuessedMimeType != null) {
-                openFileWithIntent = intentForGuessedMimeType;
-            } else {
-                openFileWithIntent = intentForSavedMimeType;
+            if(openFileWithIntent == null) {
+                openFileWithIntent = createIntentFromFile(storagePath);
+            }
+
+            if (openFileWithIntent == null) {
+                openFileWithIntent = new Intent(Intent.ACTION_VIEW);
+                openFileWithIntent.setDataAndType(uri, file.getMimetype());
             }
 
+            openFileWithIntent.setFlags(
+                    Intent.FLAG_GRANT_READ_URI_PERMISSION |
+							Intent.FLAG_GRANT_WRITE_URI_PERMISSION
+            );
+
             List<ResolveInfo> launchables = mFileActivity.getPackageManager().
                     queryIntentActivities(openFileWithIntent, PackageManager.GET_INTENT_FILTERS);