mirror of
https://github.com/SimpleMobileTools/Simple-File-Manager.git
synced 2025-06-05 22:09:15 +02:00
add file sharing
This commit is contained in:
@ -1,5 +1,7 @@
|
|||||||
package com.simplemobiletools.filemanager.adapters
|
package com.simplemobiletools.filemanager.adapters
|
||||||
|
|
||||||
|
import android.content.Intent
|
||||||
|
import android.net.Uri
|
||||||
import android.support.v7.view.ActionMode
|
import android.support.v7.view.ActionMode
|
||||||
import android.support.v7.widget.RecyclerView
|
import android.support.v7.widget.RecyclerView
|
||||||
import android.view.*
|
import android.view.*
|
||||||
@ -13,6 +15,7 @@ import com.simplemobiletools.filemanager.R
|
|||||||
import com.simplemobiletools.filemanager.activities.SimpleActivity
|
import com.simplemobiletools.filemanager.activities.SimpleActivity
|
||||||
import com.simplemobiletools.filepicker.extensions.formatSize
|
import com.simplemobiletools.filepicker.extensions.formatSize
|
||||||
import com.simplemobiletools.filepicker.extensions.isGif
|
import com.simplemobiletools.filepicker.extensions.isGif
|
||||||
|
import com.simplemobiletools.filepicker.extensions.toast
|
||||||
import com.simplemobiletools.filepicker.models.FileDirItem
|
import com.simplemobiletools.filepicker.models.FileDirItem
|
||||||
import com.simplemobiletools.fileproperties.dialogs.PropertiesDialog
|
import com.simplemobiletools.fileproperties.dialogs.PropertiesDialog
|
||||||
import kotlinx.android.synthetic.main.list_item.view.*
|
import kotlinx.android.synthetic.main.list_item.view.*
|
||||||
@ -40,6 +43,10 @@ class ItemsAdapter(val activity: SimpleActivity, val mItems: List<FileDirItem>,
|
|||||||
showProperties()
|
showProperties()
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
R.id.cab_share -> {
|
||||||
|
shareFiles()
|
||||||
|
true
|
||||||
|
}
|
||||||
else -> false
|
else -> false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -70,6 +77,33 @@ class ItemsAdapter(val activity: SimpleActivity, val mItems: List<FileDirItem>,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun shareFiles() {
|
||||||
|
val selectedItems = getSelectedMedia().filterNot { it.isDirectory }
|
||||||
|
val uris = ArrayList<Uri>(selectedItems.size)
|
||||||
|
selectedItems.mapTo(uris) { Uri.fromFile(File(it.path)) }
|
||||||
|
|
||||||
|
if (uris.isEmpty()) {
|
||||||
|
activity.toast(R.string.no_files_selected)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
val shareTitle = activity.resources.getString(R.string.share_via)
|
||||||
|
Intent().apply {
|
||||||
|
action = Intent.ACTION_SEND_MULTIPLE
|
||||||
|
putExtra(Intent.EXTRA_SUBJECT, activity.resources.getString(R.string.shared_files))
|
||||||
|
putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris)
|
||||||
|
type = "*/*"
|
||||||
|
activity.startActivity(Intent.createChooser(this, shareTitle))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun getSelectedMedia(): List<FileDirItem> {
|
||||||
|
val positions = multiSelector.selectedPositions
|
||||||
|
val selectedMedia = ArrayList<FileDirItem>(positions.size)
|
||||||
|
positions.forEach { selectedMedia.add(mItems[it]) }
|
||||||
|
return selectedMedia
|
||||||
|
}
|
||||||
|
|
||||||
override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ViewHolder {
|
override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ViewHolder {
|
||||||
val view = LayoutInflater.from(parent?.context).inflate(R.layout.list_item, parent, false)
|
val view = LayoutInflater.from(parent?.context).inflate(R.layout.list_item, parent, false)
|
||||||
return ViewHolder(activity, view, itemClick)
|
return ViewHolder(activity, view, itemClick)
|
||||||
|
@ -24,7 +24,6 @@ import com.simplemobiletools.filepicker.asynctasks.CopyMoveTask
|
|||||||
import com.simplemobiletools.filepicker.extensions.*
|
import com.simplemobiletools.filepicker.extensions.*
|
||||||
import com.simplemobiletools.filepicker.models.FileDirItem
|
import com.simplemobiletools.filepicker.models.FileDirItem
|
||||||
import com.simplemobiletools.filepicker.views.RecyclerViewDivider
|
import com.simplemobiletools.filepicker.views.RecyclerViewDivider
|
||||||
import com.simplemobiletools.fileproperties.dialogs.PropertiesDialog
|
|
||||||
import kotlinx.android.synthetic.main.items_fragment.*
|
import kotlinx.android.synthetic.main.items_fragment.*
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.util.*
|
import java.util.*
|
||||||
@ -179,26 +178,7 @@ class ItemsFragment : android.support.v4.app.Fragment(), AdapterView.OnItemClick
|
|||||||
return type + "/*"
|
return type + "/*"
|
||||||
}
|
}
|
||||||
|
|
||||||
/*override fun onItemCheckedStateChanged(mode: ActionMode, position: Int, id: Long, checked: Boolean) {
|
/*override fun onPrepareActionMode(mode: ActionMode, menu: Menu): Boolean {
|
||||||
if (checked) {
|
|
||||||
mSelectedItemsCnt++
|
|
||||||
} else {
|
|
||||||
mSelectedItemsCnt--
|
|
||||||
}
|
|
||||||
|
|
||||||
if (mSelectedItemsCnt > 0) {
|
|
||||||
mode.title = mSelectedItemsCnt.toString()
|
|
||||||
}
|
|
||||||
|
|
||||||
mode.invalidate()
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onCreateActionMode(mode: ActionMode, menu: Menu): Boolean {
|
|
||||||
mode.menuInflater.inflate(R.menu.cab, menu)
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onPrepareActionMode(mode: ActionMode, menu: Menu): Boolean {
|
|
||||||
val menuItem = menu.findItem(R.id.cab_rename)
|
val menuItem = menu.findItem(R.id.cab_rename)
|
||||||
menuItem.isVisible = mSelectedItemsCnt == 1
|
menuItem.isVisible = mSelectedItemsCnt == 1
|
||||||
return true
|
return true
|
||||||
@ -210,8 +190,6 @@ class ItemsFragment : android.support.v4.app.Fragment(), AdapterView.OnItemClick
|
|||||||
displayRenameDialog()
|
displayRenameDialog()
|
||||||
mode.finish()
|
mode.finish()
|
||||||
}
|
}
|
||||||
R.id.cab_properties -> displayPropertiesDialog()
|
|
||||||
R.id.cab_share -> shareFiles()
|
|
||||||
R.id.cab_copy -> {
|
R.id.cab_copy -> {
|
||||||
displayCopyDialog()
|
displayCopyDialog()
|
||||||
mode.finish()
|
mode.finish()
|
||||||
@ -226,54 +204,6 @@ class ItemsFragment : android.support.v4.app.Fragment(), AdapterView.OnItemClick
|
|||||||
return true
|
return true
|
||||||
}*/
|
}*/
|
||||||
|
|
||||||
private fun shareFiles() {
|
|
||||||
val itemIndexes = getSelectedItemIndexes()
|
|
||||||
if (itemIndexes.isEmpty())
|
|
||||||
return
|
|
||||||
|
|
||||||
val uris = ArrayList<Uri>(itemIndexes.size)
|
|
||||||
itemIndexes.map { File(mItems[it].path) }
|
|
||||||
.filterNot { it.isDirectory }
|
|
||||||
.mapTo(uris) { Uri.fromFile(it) }
|
|
||||||
|
|
||||||
if (uris.isEmpty()) {
|
|
||||||
context.toast(R.string.no_files_selected)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
val shareTitle = resources.getString(R.string.share_via)
|
|
||||||
Intent().apply {
|
|
||||||
action = Intent.ACTION_SEND_MULTIPLE
|
|
||||||
putExtra(Intent.EXTRA_SUBJECT, resources.getString(R.string.shared_files))
|
|
||||||
putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris)
|
|
||||||
type = "*/*"
|
|
||||||
startActivity(Intent.createChooser(this, shareTitle))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun displayPropertiesDialog() {
|
|
||||||
val itemIndexes = getSelectedItemIndexes()
|
|
||||||
if (itemIndexes.isEmpty())
|
|
||||||
return
|
|
||||||
|
|
||||||
if (itemIndexes.size == 1) {
|
|
||||||
showOneItemProperties()
|
|
||||||
} else {
|
|
||||||
showMultipleItemProperties(itemIndexes)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun showOneItemProperties() {
|
|
||||||
val item = getSelectedItem() ?: return
|
|
||||||
PropertiesDialog(activity, item.path, mConfig.showHidden)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun showMultipleItemProperties(itemIndexes: List<Int>) {
|
|
||||||
val paths = ArrayList<String>(itemIndexes.size)
|
|
||||||
itemIndexes.mapTo(paths) { mItems[it].path }
|
|
||||||
PropertiesDialog(activity, paths, mConfig.showHidden)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun displayRenameDialog() {
|
private fun displayRenameDialog() {
|
||||||
val item = getSelectedItem() ?: return
|
val item = getSelectedItem() ?: return
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user