From 49ae61c4e4751a065b8cadbca84c3baaad2dfb20 Mon Sep 17 00:00:00 2001 From: Sebastiano Barezzi Date: Wed, 31 Jan 2024 18:57:09 +0100 Subject: [PATCH] Glimpse: Implement ACTION_SET_WALLPAPER Change-Id: I1a12a7a0a690ac887200bf08e03552b20d7a55f5 --- app/src/main/AndroidManifest.xml | 6 +++ .../org/lineageos/glimpse/PickerActivity.kt | 3 +- .../fragments/picker/AlbumSelectorFragment.kt | 2 +- .../fragments/picker/MediaSelectorFragment.kt | 50 ++++++++++++++++--- .../lineageos/glimpse/utils/PickerUtils.kt | 25 ++++++---- app/src/main/res/values/strings.xml | 3 +- 6 files changed, 67 insertions(+), 22 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 61b62023..def60f31 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -97,6 +97,12 @@ + + + + + + diff --git a/app/src/main/java/org/lineageos/glimpse/PickerActivity.kt b/app/src/main/java/org/lineageos/glimpse/PickerActivity.kt index 8ee5e6fe..3460d119 100644 --- a/app/src/main/java/org/lineageos/glimpse/PickerActivity.kt +++ b/app/src/main/java/org/lineageos/glimpse/PickerActivity.kt @@ -45,7 +45,7 @@ class PickerActivity : AppCompatActivity(R.layout.activity_picker) { return } - val mimeType = PickerUtils.translateMimeType(intent.type) ?: run { + val mimeType = PickerUtils.translateMimeType(intent) ?: run { Toast.makeText( this, R.string.intent_media_type_not_supported, Toast.LENGTH_SHORT ).show() @@ -79,6 +79,7 @@ class PickerActivity : AppCompatActivity(R.layout.activity_picker) { private val supportedIntentActions = listOf( Intent.ACTION_GET_CONTENT, Intent.ACTION_PICK, + Intent.ACTION_SET_WALLPAPER, ) } } diff --git a/app/src/main/java/org/lineageos/glimpse/fragments/picker/AlbumSelectorFragment.kt b/app/src/main/java/org/lineageos/glimpse/fragments/picker/AlbumSelectorFragment.kt index a780f960..8298d8e4 100644 --- a/app/src/main/java/org/lineageos/glimpse/fragments/picker/AlbumSelectorFragment.kt +++ b/app/src/main/java/org/lineageos/glimpse/fragments/picker/AlbumSelectorFragment.kt @@ -43,7 +43,7 @@ class AlbumSelectorFragment : Fragment(R.layout.fragment_picker_album_selector) private val albumsRecyclerView by getViewProperty(R.id.albumsRecyclerView) // Intent data - private val mimeType by lazy { PickerUtils.translateMimeType(activity?.intent?.type) } + private val mimeType by lazy { PickerUtils.translateMimeType(activity?.intent) } // Recyclerview private val albumThumbnailAdapter by lazy { diff --git a/app/src/main/java/org/lineageos/glimpse/fragments/picker/MediaSelectorFragment.kt b/app/src/main/java/org/lineageos/glimpse/fragments/picker/MediaSelectorFragment.kt index 83bb550c..d87b829a 100644 --- a/app/src/main/java/org/lineageos/glimpse/fragments/picker/MediaSelectorFragment.kt +++ b/app/src/main/java/org/lineageos/glimpse/fragments/picker/MediaSelectorFragment.kt @@ -6,6 +6,7 @@ package org.lineageos.glimpse.fragments.picker import android.app.Activity +import android.app.WallpaperManager import android.content.ClipData import android.content.Intent import android.os.Bundle @@ -15,6 +16,7 @@ import android.view.MenuItem import android.view.View import android.view.ViewGroup import android.widget.LinearLayout +import android.widget.Toast import androidx.core.os.bundleOf import androidx.core.view.ViewCompat import androidx.core.view.WindowInsetsCompat @@ -63,11 +65,16 @@ class MediaSelectorFragment : Fragment(R.layout.fragment_picker_media_selector) private val mediasRecyclerView by getViewProperty(R.id.mediasRecyclerView) private val noMediaLinearLayout by getViewProperty(R.id.noMediaLinearLayout) + // System services + private val wallpaperManager by lazy { + requireContext().getSystemService(WallpaperManager::class.java) + } + // Arguments private val bucketId by lazy { arguments?.getInt(KEY_BUCKET_ID) } // Intent data - private val mimeType by lazy { PickerUtils.translateMimeType(activity?.intent?.type) } + private val mimeType by lazy { PickerUtils.translateMimeType(activity?.intent) } // Recyclerview private val thumbnailAdapter by lazy { @@ -242,13 +249,17 @@ class MediaSelectorFragment : Fragment(R.layout.fragment_picker_media_selector) * @param medias The selected medias */ private fun sendResult(vararg medias: MediaStoreMedia) { - activity?.let { - it.setResult( + val activity = activity ?: return + val intent = activity.intent ?: return + + when (intent.action) { + Intent.ACTION_GET_CONTENT, + Intent.ACTION_PICK -> activity.setResult( Activity.RESULT_OK, Intent().apply { if (allowMultipleSelection) { clipData = ClipData.newUri( - it.contentResolver, "", medias.first().uri + activity.contentResolver, "", medias.first().uri ).also { clipData -> for (media in 1 until medias.size) { clipData.addItem( @@ -267,9 +278,27 @@ class MediaSelectorFragment : Fragment(R.layout.fragment_picker_media_selector) flags = Intent.FLAG_GRANT_READ_URI_PERMISSION } ) + Intent.ACTION_SET_WALLPAPER -> { + require(medias.size == 1) { + "More than one media provided when only one was requested" + } - it.finish() + runCatching { + wallpaperManager.getCropAndSetWallpaperIntent( + medias.first().uri + ) + }.getOrNull()?.also { + activity.startActivity(it) + } ?: Toast.makeText( + activity, + R.string.intent_no_system_wallpaper_cropper_available, + Toast.LENGTH_LONG, + ).show() + } + else -> throw Exception("Unknown action") } + + activity.finish() } /** @@ -277,9 +306,14 @@ class MediaSelectorFragment : Fragment(R.layout.fragment_picker_media_selector) * @see Intent.EXTRA_ALLOW_MULTIPLE */ private val allowMultipleSelection: Boolean - get() = activity?.intent?.extras?.getBoolean( - Intent.EXTRA_ALLOW_MULTIPLE, false - ) ?: false + get() = activity?.intent?.let { intent -> + when (intent.action) { + Intent.ACTION_GET_CONTENT -> intent.extras?.getBoolean( + Intent.EXTRA_ALLOW_MULTIPLE, false + ) + else -> false + } + } ?: false companion object { private const val KEY_BUCKET_ID = "bucket_id" diff --git a/app/src/main/java/org/lineageos/glimpse/utils/PickerUtils.kt b/app/src/main/java/org/lineageos/glimpse/utils/PickerUtils.kt index 2a116b67..1613984b 100644 --- a/app/src/main/java/org/lineageos/glimpse/utils/PickerUtils.kt +++ b/app/src/main/java/org/lineageos/glimpse/utils/PickerUtils.kt @@ -15,20 +15,23 @@ object PickerUtils { private const val MIME_TYPE_ANY = "*/*" /** - * Fix-up a MIME type coming from an [Intent]. - * @param mimeType A MIME type coming from an [Intent] + * Get a fixed up MIME type from an [Intent]. + * @param intent An [Intent] * @return A simpler MIME type, null if not supported */ - fun translateMimeType(mimeType: String?) = (mimeType ?: MIME_TYPE_ANY).let { - when (it) { - MediaStore.Images.Media.CONTENT_TYPE -> MIME_TYPE_IMAGE_ANY - MediaStore.Video.Media.CONTENT_TYPE -> MIME_TYPE_VIDEO_ANY - else -> when { - it == MIME_TYPE_ANY - || it.startsWith("image/") - || it.startsWith("video/") -> it + fun translateMimeType(intent: Intent?) = when (intent?.action) { + Intent.ACTION_SET_WALLPAPER -> MIME_TYPE_IMAGE_ANY + else -> (intent?.type ?: MIME_TYPE_ANY).let { + when (it) { + MediaStore.Images.Media.CONTENT_TYPE -> MIME_TYPE_IMAGE_ANY + MediaStore.Video.Media.CONTENT_TYPE -> MIME_TYPE_VIDEO_ANY + else -> when { + it == MIME_TYPE_ANY + || it.startsWith("image/") + || it.startsWith("video/") -> it - else -> null + else -> null + } } } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index f37c6fd2..1e620cab 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -81,11 +81,12 @@ Loading… View the location with - + Action not supported Media not found Media type not found Media type not supported + No system cropper available Add to favorites