From 18f5e8a8493f0f09ce955f8190fc3bcd8ca9a39c Mon Sep 17 00:00:00 2001 From: Adrian Ulrich Date: Sun, 4 Oct 2015 16:32:30 +0200 Subject: [PATCH] Dispatch text and image files to external viewers --- .../android/vanilla/FileSystemAdapter.java | 28 +++++++++++++++++-- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/src/ch/blinkenlights/android/vanilla/FileSystemAdapter.java b/src/ch/blinkenlights/android/vanilla/FileSystemAdapter.java index d9f2d059..030a92c2 100644 --- a/src/ch/blinkenlights/android/vanilla/FileSystemAdapter.java +++ b/src/ch/blinkenlights/android/vanilla/FileSystemAdapter.java @@ -1,5 +1,6 @@ /* * Copyright (C) 2010, 2011 Christopher Eby + * Copyright (C) 2015 Adrian Ulrich * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -25,6 +26,7 @@ package ch.blinkenlights.android.vanilla; import android.content.Context; import android.content.Intent; import android.graphics.drawable.Drawable; +import android.net.Uri; import android.os.Environment; import android.os.FileObserver; import android.util.Log; @@ -37,6 +39,7 @@ import android.widget.TextView; import java.io.File; import java.io.FilenameFilter; import java.io.IOException; +import java.net.URLConnection; import java.util.Arrays; import java.util.Comparator; import java.util.regex.Pattern; @@ -308,10 +311,29 @@ public class FileSystemAdapter } @Override - public void onClick(View view) - { - Intent intent = createData((View)view.getParent()); + public void onClick(View view) { + Intent intent = createData((View)view.getParent()); + String path = intent.getStringExtra(LibraryAdapter.DATA_FILE); + String mimeGuess = URLConnection.guessContentTypeFromName(path); boolean isFolder = intent.getBooleanExtra(LibraryAdapter.DATA_EXPANDABLE, false); + + if (isFolder == false && mimeGuess != null && mimeGuess.matches("^(image|text)/.+")) { // Dispatch images and text to external apps + boolean fallthrough = false; + File file = new File(path); + Uri uri = Uri.fromFile(file); + + Intent extView = new Intent(Intent.ACTION_VIEW); + extView.setDataAndType(uri, mimeGuess); + try { + mActivity.startActivity(extView); + } catch (Exception ActivityNotFoundException) { + fallthrough = true; + } + + if (fallthrough == false) // activity was started + return; + } + if (isFolder) { mActivity.onItemExpanded(intent); } else {