From f69228be7161aa54b17e633f9d75a6d1fa7db1b5 Mon Sep 17 00:00:00 2001 From: tibbi Date: Mon, 10 Sep 2018 21:12:29 +0200 Subject: [PATCH] allow opening a file as different type with "Open as" --- .../filemanager/adapters/ItemsAdapter.kt | 25 +++++++++++++------ .../filemanager/extensions/Activity.kt | 19 ++++++++++---- .../filemanager/helpers/Constants.kt | 8 ++++++ 3 files changed, 39 insertions(+), 13 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/filemanager/adapters/ItemsAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/filemanager/adapters/ItemsAdapter.kt index 1fc2d90e..86d05652 100644 --- a/app/src/main/kotlin/com/simplemobiletools/filemanager/adapters/ItemsAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/filemanager/adapters/ItemsAdapter.kt @@ -13,22 +13,20 @@ import com.bumptech.glide.load.engine.DiskCacheStrategy import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions import com.bumptech.glide.request.RequestOptions import com.simplemobiletools.commons.adapters.MyRecyclerViewAdapter -import com.simplemobiletools.commons.dialogs.ConfirmationDialog -import com.simplemobiletools.commons.dialogs.FilePickerDialog -import com.simplemobiletools.commons.dialogs.PropertiesDialog -import com.simplemobiletools.commons.dialogs.RenameItemDialog +import com.simplemobiletools.commons.dialogs.* import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.helpers.CONFLICT_OVERWRITE import com.simplemobiletools.commons.helpers.CONFLICT_SKIP import com.simplemobiletools.commons.helpers.OTG_PATH import com.simplemobiletools.commons.models.FileDirItem +import com.simplemobiletools.commons.models.RadioItem import com.simplemobiletools.commons.views.FastScroller import com.simplemobiletools.commons.views.MyRecyclerView import com.simplemobiletools.filemanager.R import com.simplemobiletools.filemanager.activities.SimpleActivity import com.simplemobiletools.filemanager.dialogs.CompressAsDialog import com.simplemobiletools.filemanager.extensions.* -import com.simplemobiletools.filemanager.helpers.RootHelpers +import com.simplemobiletools.filemanager.helpers.* import com.simplemobiletools.filemanager.interfaces.ItemOperationsListener import com.stericson.RootTools.RootTools import kotlinx.android.synthetic.main.list_item.view.* @@ -65,6 +63,7 @@ class ItemsAdapter(activity: SimpleActivity, var fileDirItems: MutableList copyPath() R.id.cab_set_as -> setAs() R.id.cab_open_with -> openWith() - R.id.cab_open_as -> openAsText() + R.id.cab_open_as -> openAs() R.id.cab_copy_to -> copyMoveTo(true) R.id.cab_move_to -> copyMoveTo(false) R.id.cab_compress -> compressSelection() @@ -224,8 +223,18 @@ class ItemsAdapter(activity: SimpleActivity, var fileDirItems: MutableList) { sharePathsIntent(paths, BuildConfig.APPLICATION_ID) } -fun Activity.tryOpenPathIntent(path: String, forceChooser: Boolean, openAsText: Boolean = false) { +fun Activity.tryOpenPathIntent(path: String, forceChooser: Boolean, openAsType: Int = OPEN_AS_DEFAULT) { if (!forceChooser && path.endsWith(".apk", true)) { val uri = if (isNougatPlus()) { FileProvider.getUriForFile(this, "${BuildConfig.APPLICATION_ID}.provider", File(path)) @@ -35,13 +36,21 @@ fun Activity.tryOpenPathIntent(path: String, forceChooser: Boolean, openAsText: } } } else { - openPath(path, forceChooser, openAsText) + openPath(path, forceChooser, openAsType) } } -fun Activity.openPath(path: String, forceChooser: Boolean, openAsText: Boolean = false) { - val mimeType = if (openAsText) "text/plain" else "" - openPathIntent(path, forceChooser, BuildConfig.APPLICATION_ID, mimeType) +fun Activity.openPath(path: String, forceChooser: Boolean, openAsType: Int = OPEN_AS_DEFAULT) { + openPathIntent(path, forceChooser, BuildConfig.APPLICATION_ID, getMimeType(openAsType)) +} + +private fun getMimeType(type: Int) = when (type) { + OPEN_AS_DEFAULT -> "" + OPEN_AS_TEXT -> "text/*" + OPEN_AS_IMAGE -> "image/*" + OPEN_AS_AUDIO -> "audio/*" + OPEN_AS_VIDEO -> "video/*" + else -> "*/*" } fun Activity.setAs(path: String) { diff --git a/app/src/main/kotlin/com/simplemobiletools/filemanager/helpers/Constants.kt b/app/src/main/kotlin/com/simplemobiletools/filemanager/helpers/Constants.kt index 7a5c8eda..91c5f815 100644 --- a/app/src/main/kotlin/com/simplemobiletools/filemanager/helpers/Constants.kt +++ b/app/src/main/kotlin/com/simplemobiletools/filemanager/helpers/Constants.kt @@ -10,3 +10,11 @@ const val SORT_FOLDER_PREFIX = "sort_folder_" const val TEMPORARILY_SHOW_HIDDEN = "temporarily_show_hidden" const val IS_ROOT_AVAILABLE = "is_root_available" const val ENABLE_ROOT_ACCESS = "enable_root_access" + +// open as +const val OPEN_AS_DEFAULT = 0 +const val OPEN_AS_TEXT = 1 +const val OPEN_AS_IMAGE = 2 +const val OPEN_AS_AUDIO = 3 +const val OPEN_AS_VIDEO = 4 +const val OPEN_AS_OTHER = 5