cleaning up MediaActivity

This commit is contained in:
tibbi 2016-11-19 16:07:44 +01:00
parent 4d8b494c68
commit c6a8ff4b2f
6 changed files with 132 additions and 153 deletions

View File

@ -2,9 +2,7 @@ package com.simplemobiletools.gallery
import android.app.Activity import android.app.Activity
import android.content.Context import android.content.Context
import android.content.Intent
import android.content.res.Resources import android.content.res.Resources
import android.net.Uri
import android.os.Build import android.os.Build
import android.util.DisplayMetrics import android.util.DisplayMetrics
import android.util.TypedValue import android.util.TypedValue
@ -12,20 +10,9 @@ import android.view.KeyCharacterMap
import android.view.KeyEvent import android.view.KeyEvent
import android.view.ViewConfiguration import android.view.ViewConfiguration
import android.webkit.MimeTypeMap import android.webkit.MimeTypeMap
import com.simplemobiletools.filepicker.extensions.*
import com.simplemobiletools.gallery.models.Medium
import java.io.File
import java.util.*
class Utils { class Utils {
companion object { companion object {
fun getFilename(context: Context, path: String): String {
val humanized = context.humanizePath(path)
return humanized.substring(humanized.lastIndexOf("/") + 1)
}
fun showToast(context: Context, resId: Int) = context.toast(resId)
fun getActionBarHeight(context: Context, res: Resources): Int { fun getActionBarHeight(context: Context, res: Resources): Int {
val tv = TypedValue() val tv = TypedValue()
var height = 0 var height = 0
@ -75,8 +62,6 @@ class Utils {
} }
} }
fun hasStoragePermission(context: Context) = context.hasStoragePermission()
fun getMimeType(url: String): String { fun getMimeType(url: String): String {
val extension = MimeTypeMap.getFileExtensionFromUrl(url) val extension = MimeTypeMap.getFileExtensionFromUrl(url)
return if (extension != null) { return if (extension != null) {
@ -84,25 +69,5 @@ class Utils {
} else } else
"" ""
} }
fun shareMedium(medium: Medium, activity: Activity) {
val shareTitle = activity.resources.getString(R.string.share_via)
val file = File(medium.path)
val uri = Uri.fromFile(file)
Intent().apply {
action = Intent.ACTION_SEND
putExtra(Intent.EXTRA_STREAM, uri)
type = medium.getMimeType()
activity.startActivity(Intent.createChooser(this, shareTitle))
}
}
fun needsStupidWritePermissions(context: Context, path: String) = context.needsStupidWritePermissions(path)
fun getFileDocument(context: Context, path: String, treeUri: String) = context.getFileDocument(path, treeUri)
fun scanPath(context: Context, path: String) = context.scanPath(path) {}
fun scanFiles(context: Context, files: ArrayList<File>) = context.scanFiles(files) {}
} }
} }

View File

@ -34,8 +34,8 @@ class MainActivity : SimpleActivity(), SwipeRefreshLayout.OnRefreshListener, Get
private val PICK_WALLPAPER = 3 private val PICK_WALLPAPER = 3
private var mSnackbar: Snackbar? = null private var mSnackbar: Snackbar? = null
lateinit var mDirs: MutableList<Directory> lateinit var mDirs: ArrayList<Directory>
lateinit var mToBeDeleted: MutableList<String> lateinit var mToBeDeleted: ArrayList<String>
private var mIsSnackbarShown = false private var mIsSnackbarShown = false
private var mIsPickImageIntent = false private var mIsPickImageIntent = false
@ -311,7 +311,7 @@ class MainActivity : SimpleActivity(), SwipeRefreshLayout.OnRefreshListener, Get
} }
override fun onTouch(v: View, event: MotionEvent): Boolean { override fun onTouch(v: View, event: MotionEvent): Boolean {
if (mIsSnackbarShown && event.action == MotionEvent.ACTION_MOVE) { if (mIsSnackbarShown) {
deleteDirs() deleteDirs()
} }

View File

@ -3,32 +3,33 @@ package com.simplemobiletools.gallery.activities
import android.app.Activity import android.app.Activity
import android.app.WallpaperManager import android.app.WallpaperManager
import android.content.Intent import android.content.Intent
import android.database.Cursor
import android.graphics.Bitmap import android.graphics.Bitmap
import android.graphics.Color import android.graphics.Color
import android.media.MediaScannerConnection
import android.net.Uri import android.net.Uri
import android.os.Bundle import android.os.Bundle
import android.provider.MediaStore import android.provider.MediaStore
import android.support.design.widget.CoordinatorLayout
import android.support.design.widget.Snackbar import android.support.design.widget.Snackbar
import android.support.v4.widget.SwipeRefreshLayout import android.support.v4.widget.SwipeRefreshLayout
import android.util.Log import android.util.Log
import android.view.Menu import android.view.Menu
import android.view.MenuItem import android.view.MenuItem
import android.view.MotionEvent
import android.view.View import android.view.View
import android.widget.AdapterView import android.widget.AdapterView
import com.bumptech.glide.Glide import com.bumptech.glide.Glide
import com.bumptech.glide.request.animation.GlideAnimation import com.bumptech.glide.request.animation.GlideAnimation
import com.bumptech.glide.request.target.SimpleTarget import com.bumptech.glide.request.target.SimpleTarget
import com.simplemobiletools.filepicker.asynctasks.CopyMoveTask import com.simplemobiletools.filepicker.asynctasks.CopyMoveTask
import com.simplemobiletools.filepicker.extensions.scanPath import com.simplemobiletools.filepicker.extensions.*
import com.simplemobiletools.fileproperties.dialogs.PropertiesDialog import com.simplemobiletools.fileproperties.dialogs.PropertiesDialog
import com.simplemobiletools.gallery.Constants import com.simplemobiletools.gallery.Constants
import com.simplemobiletools.gallery.R import com.simplemobiletools.gallery.R
import com.simplemobiletools.gallery.Utils
import com.simplemobiletools.gallery.adapters.MediaAdapter import com.simplemobiletools.gallery.adapters.MediaAdapter
import com.simplemobiletools.gallery.dialogs.ChangeSortingDialog import com.simplemobiletools.gallery.dialogs.ChangeSortingDialog
import com.simplemobiletools.gallery.dialogs.CopyDialog import com.simplemobiletools.gallery.dialogs.CopyDialog
import com.simplemobiletools.gallery.extensions.getHumanizedFilename
import com.simplemobiletools.gallery.extensions.shareMedium
import com.simplemobiletools.gallery.models.Medium import com.simplemobiletools.gallery.models.Medium
import kotlinx.android.synthetic.main.activity_media.* import kotlinx.android.synthetic.main.activity_media.*
import java.io.File import java.io.File
@ -36,29 +37,30 @@ import java.io.IOException
import java.util.* import java.util.*
import java.util.regex.Pattern import java.util.regex.Pattern
class MediaActivity : SimpleActivity(), AdapterView.OnItemClickListener/*, GridView.MultiChoiceModeListener, GridView.OnTouchListener*/, SwipeRefreshLayout.OnRefreshListener { class MediaActivity : SimpleActivity(), AdapterView.OnItemClickListener, View.OnTouchListener, SwipeRefreshLayout.OnRefreshListener {
companion object { companion object {
private val TAG = MediaActivity::class.java.simpleName private val TAG = MediaActivity::class.java.simpleName
private var mSnackbar: Snackbar? = null private var mSnackbar: Snackbar? = null
lateinit var mToBeDeleted: MutableList<String> lateinit var mToBeDeleted: ArrayList<String>
lateinit var mMedia: MutableList<Medium> lateinit var mMedia: ArrayList<Medium>
private var mPath = "" private var mPath = ""
private var mIsSnackbarShown = false private var mIsSnackbarShown = false
private var mIsGetImageIntent = false private var mIsGetImageIntent = false
private var mIsGetVideoIntent = false private var mIsGetVideoIntent = false
private var mIsGetAnyIntent = false private var mIsGetAnyIntent = false
private var mSelectedItemsCnt = 0
} }
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
setContentView(R.layout.activity_media) setContentView(R.layout.activity_media)
mIsGetImageIntent = intent.getBooleanExtra(Constants.GET_IMAGE_INTENT, false) intent.apply {
mIsGetVideoIntent = intent.getBooleanExtra(Constants.GET_VIDEO_INTENT, false) mIsGetImageIntent = getBooleanExtra(Constants.GET_IMAGE_INTENT, false)
mIsGetAnyIntent = intent.getBooleanExtra(Constants.GET_ANY_INTENT, false) mIsGetVideoIntent = getBooleanExtra(Constants.GET_VIDEO_INTENT, false)
mIsGetAnyIntent = getBooleanExtra(Constants.GET_ANY_INTENT, false)
}
media_holder.setOnRefreshListener(this) media_holder.setOnRefreshListener(this)
mPath = intent.getStringExtra(Constants.DIRECTORY) mPath = intent.getStringExtra(Constants.DIRECTORY)
@ -77,7 +79,7 @@ class MediaActivity : SimpleActivity(), AdapterView.OnItemClickListener/*, GridV
} }
private fun tryloadGallery() { private fun tryloadGallery() {
if (Utils.hasStoragePermission(applicationContext)) { if (hasStoragePermission()) {
initializeGallery() initializeGallery()
} else { } else {
finish() finish()
@ -91,17 +93,16 @@ class MediaActivity : SimpleActivity(), AdapterView.OnItemClickListener/*, GridV
} }
mMedia = newMedia mMedia = newMedia
if (isDirEmpty) if (isDirEmpty())
return return
val adapter = MediaAdapter(this, mMedia) val adapter = MediaAdapter(this, mMedia)
media_grid.adapter = adapter media_grid.adapter = adapter
media_grid.onItemClickListener = this media_grid.onItemClickListener = this
/*mGridView!!.setMultiChoiceModeListener(this) media_grid.setOnTouchListener(this)
mGridView!!.setOnTouchListener(this)*/
mIsSnackbarShown = false mIsSnackbarShown = false
val dirName = Utils.getFilename(this, mPath) val dirName = getHumanizedFilename(mPath)
title = dirName title = dirName
} }
@ -111,7 +112,6 @@ class MediaActivity : SimpleActivity(), AdapterView.OnItemClickListener/*, GridV
val isFolderHidden = mConfig.getIsFolderHidden(mPath) val isFolderHidden = mConfig.getIsFolderHidden(mPath)
menu.findItem(R.id.hide_folder).isVisible = !isFolderHidden menu.findItem(R.id.hide_folder).isVisible = !isFolderHidden
menu.findItem(R.id.unhide_folder).isVisible = isFolderHidden menu.findItem(R.id.unhide_folder).isVisible = isFolderHidden
return true return true
} }
@ -171,7 +171,7 @@ class MediaActivity : SimpleActivity(), AdapterView.OnItemClickListener/*, GridV
} }
} }
private fun getMedia(): MutableList<Medium> { private fun getMedia(): ArrayList<Medium> {
val media = ArrayList<Medium>() val media = ArrayList<Medium>()
val invalidFiles = ArrayList<File>() val invalidFiles = ArrayList<File>()
for (i in 0..1) { for (i in 0..1) {
@ -185,14 +185,16 @@ class MediaActivity : SimpleActivity(), AdapterView.OnItemClickListener/*, GridV
uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI
} }
val where = "${MediaStore.Images.Media.DATA} like ? " val where = "${MediaStore.Images.Media.DATA} LIKE ? "
val args = arrayOf("$mPath%") val args = arrayOf("$mPath%")
val columns = arrayOf(MediaStore.Images.Media.DATA, MediaStore.Images.Media.DATE_MODIFIED) val columns = arrayOf(MediaStore.Images.Media.DATA, MediaStore.Images.Media.DATE_MODIFIED)
val cursor = contentResolver.query(uri, columns, where, args, null)
val pattern = "${Pattern.quote(mPath)}/[^/]*" val pattern = "${Pattern.quote(mPath)}/[^/]*"
var cursor: Cursor? = null
if (cursor != null) { try {
if (cursor.moveToFirst()) { cursor = contentResolver.query(uri, columns, where, args, null)
if (cursor != null && cursor.moveToFirst()) {
val pathIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA) val pathIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA)
do { do {
val curPath = cursor.getString(pathIndex) ?: continue val curPath = cursor.getString(pathIndex) ?: continue
@ -209,31 +211,30 @@ class MediaActivity : SimpleActivity(), AdapterView.OnItemClickListener/*, GridV
} }
} while (cursor.moveToNext()) } while (cursor.moveToNext())
} }
cursor.close() } finally {
cursor?.close()
} }
} }
Medium.sorting = mConfig.sorting Medium.sorting = mConfig.sorting
Collections.sort(media) media.sort()
Utils.scanFiles(applicationContext, invalidFiles) scanFiles(invalidFiles) {}
return media return media
} }
private val isDirEmpty: Boolean private fun isDirEmpty(): Boolean {
get() { return if (mMedia.size <= 0) {
if (mMedia.size <= 0) {
deleteDirectoryIfEmpty() deleteDirectoryIfEmpty()
finish() finish()
return true true
} } else
return false false
} }
private fun shareMedia() { private fun shareMedia() {
val selectedMedia = selectedMedia val selectedMedia = getSelectedMedia()
if (selectedMedia.size <= 1) { if (selectedMedia.size <= 1) {
Utils.shareMedium(selectedMedia[0], this) shareMedium(selectedMedia[0])
} else { } else {
shareMedia(selectedMedia) shareMedia(selectedMedia)
} }
@ -241,30 +242,25 @@ class MediaActivity : SimpleActivity(), AdapterView.OnItemClickListener/*, GridV
private fun shareMedia(media: List<Medium>) { private fun shareMedia(media: List<Medium>) {
val shareTitle = resources.getString(R.string.share_via) val shareTitle = resources.getString(R.string.share_via)
val intent = Intent() Intent().apply {
intent.action = Intent.ACTION_SEND_MULTIPLE action = Intent.ACTION_SEND_MULTIPLE
intent.type = "image/* video/*" type = "image/* video/*"
val uris = ArrayList<Uri>(media.size) val uris = ArrayList<Uri>(media.size)
for (medium in media) { media.map { File(it.path) }
val file = File(medium.path) .mapTo(uris) { Uri.fromFile(it) }
uris.add(Uri.fromFile(file))
putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris)
startActivity(Intent.createChooser(this, shareTitle))
}
} }
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris) private fun getSelectedMedia(): List<Medium> {
startActivity(Intent.createChooser(intent, shareTitle))
}
private val selectedMedia: List<Medium>
get() {
val media = ArrayList<Medium>()
val items = media_grid.checkedItemPositions val items = media_grid.checkedItemPositions
val cnt = items.size() val cnt = items.size()
for (i in 0..cnt - 1) { val media = (0..cnt - 1)
if (items.valueAt(i)) { .filter { items.valueAt(it) }
val id = items.keyAt(i) .map { mMedia[items.keyAt(it)] }
media.add(mMedia[id])
}
}
return media return media
} }
@ -272,7 +268,7 @@ class MediaActivity : SimpleActivity(), AdapterView.OnItemClickListener/*, GridV
if (isShowingPermDialog(File(mPath))) if (isShowingPermDialog(File(mPath)))
return return
Utils.showToast(this, R.string.deleting) toast(R.string.deleting)
val items = media_grid.checkedItemPositions val items = media_grid.checkedItemPositions
val cnt = items.size() val cnt = items.size()
var deletedCnt = 0 var deletedCnt = 0
@ -294,10 +290,9 @@ class MediaActivity : SimpleActivity(), AdapterView.OnItemClickListener/*, GridV
if (mMedia.isEmpty()) { if (mMedia.isEmpty()) {
deleteFiles() deleteFiles()
} else { } else {
val coordinator = findViewById(R.id.coordinator_layout) as CoordinatorLayout
val res = resources val res = resources
val msg = res.getQuantityString(R.plurals.files_deleted, cnt, cnt) val msg = res.getQuantityString(R.plurals.files_deleted, cnt, cnt)
mSnackbar = Snackbar.make(coordinator, msg, Snackbar.LENGTH_INDEFINITE) mSnackbar = Snackbar.make(coordinator_layout, msg, Snackbar.LENGTH_INDEFINITE)
mSnackbar!!.apply { mSnackbar!!.apply {
setAction(res.getString(R.string.undo), undoDeletion) setAction(res.getString(R.string.undo), undoDeletion)
setActionTextColor(Color.WHITE) setActionTextColor(Color.WHITE)
@ -322,12 +317,11 @@ class MediaActivity : SimpleActivity(), AdapterView.OnItemClickListener/*, GridV
for (delPath in mToBeDeleted) { for (delPath in mToBeDeleted) {
val file = File(delPath) val file = File(delPath)
if (file.exists()) { if (file.exists()) {
if (Utils.needsStupidWritePermissions(this, delPath)) { if (needsStupidWritePermissions(delPath)) {
if (isShowingPermDialog(file)) if (isShowingPermDialog(file))
return return
val document = Utils.getFileDocument(this, delPath, mConfig.treeUri) if (getFileDocument(delPath, mConfig.treeUri).delete()) {
if (document.delete()) {
wereFilesDeleted = true wereFilesDeleted = true
} }
} else { } else {
@ -338,8 +332,7 @@ class MediaActivity : SimpleActivity(), AdapterView.OnItemClickListener/*, GridV
} }
if (wereFilesDeleted) { if (wereFilesDeleted) {
val deletedPaths = mToBeDeleted.toTypedArray() scanPaths(mToBeDeleted) {
MediaScannerConnection.scanFile(applicationContext, deletedPaths, null) { path, uri ->
if (mMedia.isEmpty()) { if (mMedia.isEmpty()) {
finish() finish()
} }
@ -357,67 +350,63 @@ class MediaActivity : SimpleActivity(), AdapterView.OnItemClickListener/*, GridV
} }
private fun updateGridView() { private fun updateGridView() {
if (!isDirEmpty) { if (!isDirEmpty()) {
val adapter = media_grid.adapter as MediaAdapter (media_grid.adapter as MediaAdapter).updateItems(mMedia)
adapter.updateItems(mMedia)
} }
} }
private fun showProperties() { private fun showProperties() {
val selectedMedia = selectedMedia val selectedMedia = getSelectedMedia()
if (selectedMedia.size == 1) { if (selectedMedia.size == 1) {
PropertiesDialog(this, selectedMedia[0].path, false) PropertiesDialog(this, selectedMedia[0].path, false)
} else { } else {
val paths = ArrayList<String>(selectedMedia.size) val paths = ArrayList<String>(selectedMedia.size)
for (medium in selectedMedia) { selectedMedia.mapTo(paths) { it.path }
paths.add(medium.path)
}
PropertiesDialog(this, paths, false) PropertiesDialog(this, paths, false)
} }
} }
private val isSetWallpaperIntent: Boolean private fun isSetWallpaperIntent() = intent.getBooleanExtra(Constants.SET_WALLPAPER_INTENT, false)
get() = intent.getBooleanExtra(Constants.SET_WALLPAPER_INTENT, false)
private fun displayCopyDialog() { private fun displayCopyDialog() {
val files = ArrayList<File>()
val items = media_grid.checkedItemPositions val items = media_grid.checkedItemPositions
val cnt = items.size() val cnt = items.size()
for (i in 0..cnt - 1) { val files = (0..cnt - 1)
if (items.valueAt(i)) { .filter { items.valueAt(it) }
val id = items.keyAt(i) .map { items.keyAt(it) }
files.add(File(mMedia[id].path)) .mapTo(ArrayList<File>()) { File(mMedia[it].path) }
}
}
CopyDialog(this, files, object : CopyMoveTask.CopyMoveListener { CopyDialog(this, files, object : CopyMoveTask.CopyMoveListener {
override fun copySucceeded(deleted: Boolean, copiedAll: Boolean) { override fun copySucceeded(deleted: Boolean, copiedAll: Boolean) {
val msgId: Int
if (deleted) { if (deleted) {
refreshDir() refreshDir()
msgId = if (copiedAll) R.string.moving_success else R.string.moving_success_partial toast(if (copiedAll) R.string.moving_success else R.string.moving_success_partial)
} else { } else {
msgId = if (copiedAll) R.string.copying_success else R.string.copying_success_partial toast(if (copiedAll) R.string.copying_success else R.string.copying_success_partial)
} }
Utils.showToast(applicationContext, msgId)
} }
override fun copyFailed() { override fun copyFailed() {
Utils.showToast(applicationContext, R.string.copy_move_failed) toast(R.string.copy_move_failed)
} }
}) })
} }
override fun onItemClick(parent: AdapterView<*>, view: View, position: Int, id: Long) { override fun onItemClick(parent: AdapterView<*>, view: View, position: Int, id: Long) {
val curItemPath = mMedia[position].path val curItemPath = mMedia[position].path
if (isSetWallpaperIntent) { if (isSetWallpaperIntent()) {
Utils.showToast(this, R.string.setting_wallpaper) toast(R.string.setting_wallpaper)
val wantedWidth = wallpaperDesiredMinimumWidth val wantedWidth = wallpaperDesiredMinimumWidth
val wantedHeight = wallpaperDesiredMinimumHeight val wantedHeight = wallpaperDesiredMinimumHeight
val ratio = wantedWidth.toFloat() / wantedHeight val ratio = wantedWidth.toFloat() / wantedHeight
Glide.with(this).load(File(curItemPath)).asBitmap().override((wantedWidth * ratio).toInt(), wantedHeight).fitCenter().into(object : SimpleTarget<Bitmap>() { Glide.with(this)
.load(File(curItemPath))
.asBitmap()
.override((wantedWidth * ratio).toInt(), wantedHeight)
.fitCenter()
.into(object : SimpleTarget<Bitmap>() {
override fun onResourceReady(bitmap: Bitmap?, glideAnimation: GlideAnimation<in Bitmap>?) { override fun onResourceReady(bitmap: Bitmap?, glideAnimation: GlideAnimation<in Bitmap>?) {
try { try {
WallpaperManager.getInstance(applicationContext).setBitmap(bitmap) WallpaperManager.getInstance(applicationContext).setBitmap(bitmap)
@ -430,14 +419,16 @@ class MediaActivity : SimpleActivity(), AdapterView.OnItemClickListener/*, GridV
} }
}) })
} else if (mIsGetImageIntent || mIsGetVideoIntent || mIsGetAnyIntent) { } else if (mIsGetImageIntent || mIsGetVideoIntent || mIsGetAnyIntent) {
val result = Intent() Intent().apply {
result.data = Uri.parse(curItemPath) data = Uri.parse(curItemPath)
setResult(Activity.RESULT_OK, result) setResult(Activity.RESULT_OK, this)
}
finish() finish()
} else { } else {
val intent = Intent(this, ViewPagerActivity::class.java) Intent(this, ViewPagerActivity::class.java).apply {
intent.putExtra(Constants.MEDIUM, curItemPath) putExtra(Constants.MEDIUM, curItemPath)
startActivity(intent) startActivity(this)
}
} }
} }
@ -489,7 +480,7 @@ class MediaActivity : SimpleActivity(), AdapterView.OnItemClickListener/*, GridV
override fun onDestroyActionMode(mode: ActionMode) { override fun onDestroyActionMode(mode: ActionMode) {
mSelectedItemsCnt = 0 mSelectedItemsCnt = 0
} }*/
override fun onTouch(v: View, event: MotionEvent): Boolean { override fun onTouch(v: View, event: MotionEvent): Boolean {
if (mIsSnackbarShown) { if (mIsSnackbarShown) {
@ -497,7 +488,7 @@ class MediaActivity : SimpleActivity(), AdapterView.OnItemClickListener/*, GridV
} }
return false return false
}*/ }
override fun onRefresh() { override fun onRefresh() {
refreshDir() refreshDir()

View File

@ -23,6 +23,7 @@ import com.simplemobiletools.gallery.Utils
import com.simplemobiletools.gallery.adapters.MyPagerAdapter import com.simplemobiletools.gallery.adapters.MyPagerAdapter
import com.simplemobiletools.gallery.dialogs.CopyDialog import com.simplemobiletools.gallery.dialogs.CopyDialog
import com.simplemobiletools.gallery.dialogs.RenameFileDialog import com.simplemobiletools.gallery.dialogs.RenameFileDialog
import com.simplemobiletools.gallery.extensions.shareMedium
import com.simplemobiletools.gallery.fragments.ViewPagerFragment import com.simplemobiletools.gallery.fragments.ViewPagerFragment
import com.simplemobiletools.gallery.models.Medium import com.simplemobiletools.gallery.models.Medium
import kotlinx.android.synthetic.main.activity_medium.* import kotlinx.android.synthetic.main.activity_medium.*
@ -142,7 +143,7 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View
true true
} }
R.id.menu_share -> { R.id.menu_share -> {
shareMedium() shareMedium(getCurrentMedium())
true true
} }
R.id.menu_delete -> { R.id.menu_delete -> {
@ -254,10 +255,6 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View
super.onActivityResult(requestCode, resultCode, resultData) super.onActivityResult(requestCode, resultCode, resultData)
} }
private fun shareMedium() {
Utils.shareMedium(getCurrentMedium(), this)
}
private fun askConfirmDelete() { private fun askConfirmDelete() {
ConfirmationDialog(this, listener = object : ConfirmationDialog.OnConfirmedListener { ConfirmationDialog(this, listener = object : ConfirmationDialog.OnConfirmedListener {
override fun onConfirmed() { override fun onConfirmed() {

View File

@ -0,0 +1,20 @@
package com.simplemobiletools.gallery.extensions
import android.app.Activity
import android.content.Intent
import android.net.Uri
import com.simplemobiletools.gallery.R
import com.simplemobiletools.gallery.models.Medium
import java.io.File
fun Activity.shareMedium(medium: Medium) {
val shareTitle = resources.getString(R.string.share_via)
val file = File(medium.path)
val uri = Uri.fromFile(file)
Intent().apply {
action = Intent.ACTION_SEND
putExtra(Intent.EXTRA_STREAM, uri)
type = medium.getMimeType()
startActivity(Intent.createChooser(this, shareTitle))
}
}

View File

@ -4,6 +4,7 @@ import android.content.Context
import android.database.Cursor import android.database.Cursor
import android.net.Uri import android.net.Uri
import android.provider.MediaStore import android.provider.MediaStore
import com.simplemobiletools.filepicker.extensions.humanizePath
fun Context.getRealPathFromURI(uri: Uri): String? { fun Context.getRealPathFromURI(uri: Uri): String? {
var cursor: Cursor? = null var cursor: Cursor? = null
@ -19,3 +20,8 @@ fun Context.getRealPathFromURI(uri: Uri): String? {
cursor?.close() cursor?.close()
} }
} }
fun Context.getHumanizedFilename(path: String): String {
val humanized = humanizePath(path)
return humanized.substring(humanized.lastIndexOf("/") + 1)
}