move OpenWith in extension

This commit is contained in:
tibbi 2016-12-04 12:07:22 +01:00
parent 56ba895302
commit 72e56796b9
2 changed files with 24 additions and 16 deletions

View file

@ -20,6 +20,7 @@ import com.simplemobiletools.gallery.*
import com.simplemobiletools.gallery.adapters.MyPagerAdapter
import com.simplemobiletools.gallery.dialogs.CopyDialog
import com.simplemobiletools.gallery.dialogs.RenameFileDialog
import com.simplemobiletools.gallery.extensions.openWith
import com.simplemobiletools.gallery.extensions.setAsWallpaper
import com.simplemobiletools.gallery.extensions.shareMedium
import com.simplemobiletools.gallery.fragments.ViewPagerFragment
@ -131,7 +132,7 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View
true
}
R.id.menu_open_with -> {
openWith()
openWith(getCurrentFile())
true
}
R.id.menu_share -> {
@ -195,18 +196,6 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View
}
}
private fun openWith() {
val intent = Intent(Intent.ACTION_VIEW)
intent.setDataAndType(Uri.fromFile(getCurrentFile()), getCurrentMedium().getMimeType())
val chooser = Intent.createChooser(intent, getString(R.string.open_with))
if (intent.resolveActivity(packageManager) != null) {
startActivity(chooser)
} else {
toast(R.string.no_app_found)
}
}
private fun showProperties() {
PropertiesDialog(this, getCurrentFile().absolutePath, false)
}

View file

@ -40,9 +40,7 @@ fun Activity.shareMedia(media: List<Medium>) {
fun Activity.setAsWallpaper(file: File) {
val intent = Intent(Intent.ACTION_ATTACH_DATA)
val uri = Uri.fromFile(file)
var mimeType = getMimeType(uri.toString())
if (mimeType.isEmpty()) mimeType = "image/jpeg"
intent.setDataAndType(uri, mimeType)
intent.setDataAndType(uri, getImageMimeType(uri))
val chooser = Intent.createChooser(intent, getString(R.string.set_as_wallpaper_with))
if (intent.resolveActivity(packageManager) != null) {
@ -51,3 +49,24 @@ fun Activity.setAsWallpaper(file: File) {
toast(R.string.no_wallpaper_setter_found)
}
}
fun Activity.openWith(file: File) {
val intent = Intent(Intent.ACTION_VIEW)
val uri = Uri.fromFile(file)
intent.setDataAndType(uri, getImageMimeType(uri))
val chooser = Intent.createChooser(intent, getString(R.string.open_with))
if (intent.resolveActivity(packageManager) != null) {
startActivity(chooser)
} else {
toast(R.string.no_app_found)
}
}
fun getImageMimeType(uri: Uri): String {
val mimeType = getMimeType(uri.toString())
return if (mimeType.isNotEmpty())
mimeType
else
"image/jpeg"
}