From cb622e5dbc7e70672dcd7e2ba51c0a888f873ef5 Mon Sep 17 00:00:00 2001 From: matsuo Date: Wed, 28 Sep 2016 01:16:55 +0900 Subject: [PATCH 1/4] Add open URL file feature --- .../android/files/FileOperationsHelper.java | 126 ++++++++++++++---- 1 file changed, 101 insertions(+), 25 deletions(-) mode change 100644 => 100755 src/com/owncloud/android/files/FileOperationsHelper.java diff --git a/src/com/owncloud/android/files/FileOperationsHelper.java b/src/com/owncloud/android/files/FileOperationsHelper.java old mode 100644 new mode 100755 index fa5e1683c7..a525a89331 --- a/src/com/owncloud/android/files/FileOperationsHelper.java +++ b/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("(.+)"); + + 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); + Uri uri = Uri.parse("file://" + encodedStoragePath); - 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 - ); + 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 launchables = mFileActivity.getPackageManager(). queryIntentActivities(openFileWithIntent, PackageManager.GET_INTENT_FILTERS); From 89a14e87553aa98d5e0b194090403e5e4d2144fe Mon Sep 17 00:00:00 2001 From: matsuo Date: Sat, 1 Oct 2016 00:21:15 +0900 Subject: [PATCH 2/4] Improved performance and simpler source code --- .../android/files/FileOperationsHelper.java | 58 +++++-------------- 1 file changed, 16 insertions(+), 42 deletions(-) diff --git a/src/com/owncloud/android/files/FileOperationsHelper.java b/src/com/owncloud/android/files/FileOperationsHelper.java index a525a89331..0b7f1cabf1 100755 --- a/src/com/owncloud/android/files/FileOperationsHelper.java +++ b/src/com/owncloud/android/files/FileOperationsHelper.java @@ -73,26 +73,24 @@ public class FileOperationsHelper { /// Identifier of operation in progress which result shouldn't be lost private long mWaitingForOpId = Long.MAX_VALUE; + private static final Pattern mPatternUrl = Pattern.compile("^URL=(.+)$"); + private static final Pattern mPatternString = Pattern.compile("(.+)"); + public FileOperationsHelper(FileActivity fileActivity) { mFileActivity = fileActivity; } - /** - * Windows internet shortcut file .url - * Ubuntu internet shortcut file .desktop - */ - @Nullable - private String getUrlFromUrlFile(String file) { + @Nullable + private String getUrlFromFile(String storagePath, Pattern pattern) { String url = null; - Pattern p = Pattern.compile("^URL=(.+)$"); try { - FileReader fr = new FileReader(file); + FileReader fr = new FileReader(storagePath); BufferedReader br = new BufferedReader(fr); String line; while ((line = br.readLine()) != null) { - Matcher m = p.matcher(line); + Matcher m = pattern.matcher(line); if (m.find()) { url = m.group(1); break; @@ -100,40 +98,13 @@ public class FileOperationsHelper { } br.close(); fr.close(); - } catch (IOException ex) { + } catch (IOException e) { + Log_OC.d(TAG, e.getMessage()); return null; } return url; - } - - /** - * mac internet shortcut file .webloc - */ - @Nullable - private String getUrlFromWeblocFile(String file) { - String url = null; - Pattern p = Pattern.compile("(.+)"); - - 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; @@ -141,9 +112,12 @@ public class FileOperationsHelper { if (lastIndexOfDot >= 0) { String fileExt = storagePath.substring(lastIndexOfDot + 1); if (fileExt.equalsIgnoreCase("url") ||fileExt.equalsIgnoreCase("desktop")) { - url = getUrlFromUrlFile(storagePath); + // Windows internet shortcut file .url + // Ubuntu internet shortcut file .desktop + url = getUrlFromFile(storagePath, mPatternUrl); } else if (fileExt.equalsIgnoreCase("webloc")) { - url = getUrlFromWeblocFile(storagePath); + // mac internet shortcut file .webloc + url = getUrlFromFile(storagePath, mPatternString); } } if (url == null) { From 29002e5a247363a07a78df2f6cb6aaddc1daefc2 Mon Sep 17 00:00:00 2001 From: matsuo Date: Thu, 6 Oct 2016 00:49:13 +0900 Subject: [PATCH 3/4] close() in finally block --- .../android/files/FileOperationsHelper.java | 24 +++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/com/owncloud/android/files/FileOperationsHelper.java b/src/com/owncloud/android/files/FileOperationsHelper.java index 0b7f1cabf1..38ebae8920 100755 --- a/src/com/owncloud/android/files/FileOperationsHelper.java +++ b/src/com/owncloud/android/files/FileOperationsHelper.java @@ -84,9 +84,11 @@ public class FileOperationsHelper { private String getUrlFromFile(String storagePath, Pattern pattern) { String url = null; + FileReader fr = null; + BufferedReader br = null; try { - FileReader fr = new FileReader(storagePath); - BufferedReader br = new BufferedReader(fr); + fr = new FileReader(storagePath); + br = new BufferedReader(fr); String line; while ((line = br.readLine()) != null) { @@ -96,11 +98,25 @@ public class FileOperationsHelper { break; } } - br.close(); - fr.close(); } catch (IOException e) { Log_OC.d(TAG, e.getMessage()); return null; + } finally { + if (br != null) { + try { + br.close(); + } catch (IOException e) { + Log_OC.d(TAG, "Error closing buffered reader for URL file", e); + } + } + + if (br != null) { + try { + fr.close(); + } catch (IOException e) { + Log_OC.d(TAG, "Error closing file reader for URL file", e); + } + } } return url; } From a634be95ca9f3d287fb6a1752d3b9c2ea65eaa84 Mon Sep 17 00:00:00 2001 From: AndyScherzinger Date: Wed, 5 Oct 2016 22:30:12 +0200 Subject: [PATCH 4/4] fixed null check --- src/com/owncloud/android/files/FileOperationsHelper.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/com/owncloud/android/files/FileOperationsHelper.java b/src/com/owncloud/android/files/FileOperationsHelper.java index 38ebae8920..c6d3e7b96f 100755 --- a/src/com/owncloud/android/files/FileOperationsHelper.java +++ b/src/com/owncloud/android/files/FileOperationsHelper.java @@ -110,7 +110,7 @@ public class FileOperationsHelper { } } - if (br != null) { + if (fr != null) { try { fr.close(); } catch (IOException e) {