reuse some third party intents from the Commons library

This commit is contained in:
tibbi 2017-10-29 20:54:29 +01:00
parent 9b675ba6d4
commit f7fc5c06c9
4 changed files with 37 additions and 82 deletions

View file

@ -37,7 +37,7 @@ android {
}
dependencies {
compile 'com.simplemobiletools:commons:2.33.2'
compile 'com.simplemobiletools:commons:2.33.8'
compile 'com.bignerdranch.android:recyclerview-multiselect:0.2'
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"

View file

@ -1,10 +1,8 @@
package com.simplemobiletools.filemanager.adapters
import android.content.Intent
import android.graphics.drawable.Drawable
import android.net.Uri
import android.os.Build
import android.os.TransactionTooLargeException
import android.support.v7.view.ActionMode
import android.support.v7.widget.RecyclerView
import android.util.SparseArray
@ -29,6 +27,7 @@ import com.simplemobiletools.filemanager.dialogs.CompressAsDialog
import com.simplemobiletools.filemanager.extensions.config
import com.simplemobiletools.filemanager.extensions.isPathOnRoot
import com.simplemobiletools.filemanager.extensions.isZipFile
import com.simplemobiletools.filemanager.extensions.shareUris
import com.stericson.RootTools.RootTools
import kotlinx.android.synthetic.main.list_item.view.*
import java.io.Closeable
@ -171,35 +170,9 @@ class ItemsAdapter(val activity: SimpleActivity, var mItems: MutableList<FileDir
val selectedItems = getSelectedMedia()
val uris = ArrayList<Uri>(selectedItems.size)
selectedItems.forEach {
val file = File(it.path)
addFileUris(file, uris)
}
if (uris.isEmpty()) {
activity.toast(R.string.no_files_selected)
return
}
val shareTitle = activity.resources.getString(R.string.share_via)
Intent().apply {
if (uris.size <= 1) {
action = Intent.ACTION_SEND
putExtra(Intent.EXTRA_STREAM, uris.first())
} else {
action = Intent.ACTION_SEND_MULTIPLE
putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris)
}
type = uris.getMimeType()
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
try {
activity.startActivity(Intent.createChooser(this, shareTitle))
} catch (e: TransactionTooLargeException) {
activity.toast(R.string.maximum_share_reached)
} catch (exception: Exception) {
activity.showErrorToast(exception.cause.toString())
}
addFileUris(File(it.path), uris)
}
activity.shareUris(uris)
}
private fun addFileUris(file: File, uris: ArrayList<Uri>) {

View file

@ -0,0 +1,26 @@
package com.simplemobiletools.filemanager.extensions
import android.app.Activity
import android.net.Uri
import com.simplemobiletools.commons.extensions.openFile
import com.simplemobiletools.commons.extensions.shareUri
import com.simplemobiletools.commons.extensions.shareUris
import com.simplemobiletools.filemanager.BuildConfig
import java.util.*
fun Activity.shareUri(uri: Uri) {
shareUri(uri, BuildConfig.APPLICATION_ID)
}
fun Activity.shareUris(uris: ArrayList<Uri>) {
if (uris.size == 1) {
shareUri(uris.first())
} else {
shareUris(uris, BuildConfig.APPLICATION_ID)
}
}
fun Activity.openFile(uri: Uri, forceChooser: Boolean) {
openFile(uri, forceChooser, BuildConfig.APPLICATION_ID)
}

View file

@ -1,7 +1,5 @@
package com.simplemobiletools.filemanager.fragments
import android.content.ActivityNotFoundException
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.os.Parcelable
@ -12,12 +10,13 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.simplemobiletools.commons.dialogs.StoragePickerDialog
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.REAL_FILE_PATH
import com.simplemobiletools.commons.extensions.deleteFiles
import com.simplemobiletools.commons.extensions.getFilenameFromPath
import com.simplemobiletools.commons.extensions.toast
import com.simplemobiletools.commons.extensions.updateTextColors
import com.simplemobiletools.commons.models.FileDirItem
import com.simplemobiletools.commons.views.Breadcrumbs
import com.simplemobiletools.commons.views.MyScalableRecyclerView
import com.simplemobiletools.filemanager.BuildConfig
import com.simplemobiletools.filemanager.R
import com.simplemobiletools.filemanager.activities.MainActivity
import com.simplemobiletools.filemanager.activities.SimpleActivity
@ -25,6 +24,7 @@ import com.simplemobiletools.filemanager.adapters.ItemsAdapter
import com.simplemobiletools.filemanager.dialogs.CreateNewItemDialog
import com.simplemobiletools.filemanager.extensions.config
import com.simplemobiletools.filemanager.extensions.isPathOnRoot
import com.simplemobiletools.filemanager.extensions.openFile
import com.simplemobiletools.filemanager.helpers.RootHelpers
import com.stericson.RootTools.RootTools
import kotlinx.android.synthetic.main.items_fragment.*
@ -224,48 +224,12 @@ class ItemsFragment : Fragment(), ItemsAdapter.ItemOperationsListener, Breadcrum
if (isGetContentIntent) {
(activity as MainActivity).pickedPath(path)
} else {
fileClicked(path)
val file = File(path)
activity.openFile(Uri.fromFile(file), false)
}
}
}
private fun fileClicked(path: String) {
val file = File(path)
val mimeType = path.getMimeTypeFromPath()
val uri = context.getFilePublicUri(file, BuildConfig.APPLICATION_ID)
Intent(Intent.ACTION_VIEW).apply {
setDataAndType(uri, mimeType)
flags = Intent.FLAG_ACTIVITY_NEW_TASK
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
if (context.isNougatPlus()) {
putExtra(REAL_FILE_PATH, Uri.fromFile(file))
}
try {
startActivity(this)
} catch (e: ActivityNotFoundException) {
if (!tryGenericMimeType(this, mimeType, file)) {
activity.toast(R.string.no_app_found)
}
}
}
}
private fun tryGenericMimeType(intent: Intent, mimeType: String, file: File): Boolean {
val uri = context.getFilePublicUri(file, BuildConfig.APPLICATION_ID)
val genericMimeType = getGenericMimeType(mimeType)
intent.setDataAndType(uri, genericMimeType)
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
return try {
startActivity(intent)
true
} catch (e: ActivityNotFoundException) {
false
}
}
private fun createNewItem() {
CreateNewItemDialog(activity as SimpleActivity, currentPath) {
if (it) {
@ -274,14 +238,6 @@ class ItemsFragment : Fragment(), ItemsAdapter.ItemOperationsListener, Breadcrum
}
}
private fun getGenericMimeType(mimeType: String): String {
if (!mimeType.contains("/"))
return mimeType
val type = mimeType.substring(0, mimeType.indexOf("/"))
return "$type/*"
}
override fun breadcrumbClicked(id: Int) {
if (id == 0) {
StoragePickerDialog(activity, currentPath) {