From 679deb424ab100d8dd379ea55f1fff4217823fb2 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sun, 4 Dec 2016 12:25:54 +0100 Subject: [PATCH] add more action buttons to PhotoVideoActivity used by third parties --- .../gallery/activities/PhotoVideoActivity.kt | 28 ++++++++++--------- .../gallery/extensions/activity.kt | 13 ++------- .../gallery/extensions/uri.kt | 12 ++++++++ app/src/main/res/menu/photo_video_menu.xml | 9 ++++++ 4 files changed, 38 insertions(+), 24 deletions(-) create mode 100644 app/src/main/kotlin/com/simplemobiletools/gallery/extensions/uri.kt diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/PhotoVideoActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/PhotoVideoActivity.kt index 30bd020ac..8cdb06175 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/PhotoVideoActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/PhotoVideoActivity.kt @@ -9,6 +9,9 @@ import android.view.Menu import android.view.MenuItem import com.simplemobiletools.gallery.MEDIUM import com.simplemobiletools.gallery.R +import com.simplemobiletools.gallery.extensions.openWith +import com.simplemobiletools.gallery.extensions.setAsWallpaper +import com.simplemobiletools.gallery.extensions.shareMedium import com.simplemobiletools.gallery.fragments.PhotoFragment import com.simplemobiletools.gallery.fragments.VideoFragment import com.simplemobiletools.gallery.fragments.ViewPagerFragment @@ -19,6 +22,7 @@ open class PhotoVideoActivity : SimpleActivity(), ViewPagerFragment.FragmentClic private var mIsFullScreen = false lateinit var mUri: Uri lateinit var mFragment: ViewPagerFragment + lateinit var mMedium: Medium companion object { var mIsVideo = false @@ -41,8 +45,8 @@ open class PhotoVideoActivity : SimpleActivity(), ViewPagerFragment.FragmentClic val bundle = Bundle() val file = File(mUri.toString()) - val medium = Medium(file.name, mUri.toString(), mIsVideo, 0, file.length()) - bundle.putSerializable(MEDIUM, medium) + mMedium = Medium(file.name, mUri.toString(), mIsVideo, 0, file.length()) + bundle.putSerializable(MEDIUM, mMedium) if (savedInstanceState == null) { mFragment = if (mIsVideo) VideoFragment() else PhotoFragment() @@ -73,24 +77,22 @@ open class PhotoVideoActivity : SimpleActivity(), ViewPagerFragment.FragmentClic override fun onOptionsItemSelected(item: MenuItem): Boolean { return when (item.itemId) { + R.id.menu_set_as_wallpaper -> { + setAsWallpaper(File(mMedium.path)) + true + } + R.id.menu_open_with -> { + openWith(File(mMedium.path)) + true + } R.id.menu_share -> { - shareMedium() + shareMedium(mMedium) true } else -> super.onOptionsItemSelected(item) } } - private fun shareMedium() { - val shareTitle = resources.getString(R.string.share_via) - Intent().apply { - action = Intent.ACTION_SEND - putExtra(Intent.EXTRA_STREAM, mUri) - type = if (mIsVideo) "video/*" else "image/*" - startActivity(Intent.createChooser(this, shareTitle)) - } - } - override fun fragmentClicked() { mIsFullScreen = !mIsFullScreen if (mIsFullScreen) { diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/activity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/activity.kt index a0fe1d753..5352d2453 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/activity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/activity.kt @@ -3,7 +3,6 @@ package com.simplemobiletools.gallery.extensions import android.app.Activity import android.content.Intent import android.net.Uri -import com.simplemobiletools.filepicker.extensions.getMimeType import com.simplemobiletools.filepicker.extensions.toast import com.simplemobiletools.gallery.R import com.simplemobiletools.gallery.REQUEST_SET_WALLPAPER @@ -40,7 +39,7 @@ fun Activity.shareMedia(media: List) { fun Activity.setAsWallpaper(file: File) { val intent = Intent(Intent.ACTION_ATTACH_DATA) val uri = Uri.fromFile(file) - intent.setDataAndType(uri, getImageMimeType(uri)) + intent.setDataAndType(uri, uri.getImageMimeType()) val chooser = Intent.createChooser(intent, getString(R.string.set_as_wallpaper_with)) if (intent.resolveActivity(packageManager) != null) { @@ -53,7 +52,7 @@ fun Activity.setAsWallpaper(file: File) { fun Activity.openWith(file: File) { val intent = Intent(Intent.ACTION_VIEW) val uri = Uri.fromFile(file) - intent.setDataAndType(uri, getImageMimeType(uri)) + intent.setDataAndType(uri, uri.getImageMimeType()) val chooser = Intent.createChooser(intent, getString(R.string.open_with)) if (intent.resolveActivity(packageManager) != null) { @@ -62,11 +61,3 @@ fun Activity.openWith(file: File) { toast(R.string.no_app_found) } } - -fun getImageMimeType(uri: Uri): String { - val mimeType = getMimeType(uri.toString()) - return if (mimeType.isNotEmpty()) - mimeType - else - "image/jpeg" -} diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/uri.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/uri.kt new file mode 100644 index 000000000..ec86cb6ff --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/uri.kt @@ -0,0 +1,12 @@ +package com.simplemobiletools.gallery.extensions + +import android.net.Uri +import com.simplemobiletools.filepicker.extensions.getMimeType + +fun Uri.getImageMimeType(): String { + val mimeType = getMimeType(toString()) + return if (mimeType.isNotEmpty()) + mimeType + else + "image/jpeg" +} diff --git a/app/src/main/res/menu/photo_video_menu.xml b/app/src/main/res/menu/photo_video_menu.xml index 65e9d5e91..9537315f3 100644 --- a/app/src/main/res/menu/photo_video_menu.xml +++ b/app/src/main/res/menu/photo_video_menu.xml @@ -6,4 +6,13 @@ android:icon="@mipmap/share" android:title="@string/share" app:showAsAction="ifRoom"/> + +