Skip to content

Commit

Permalink
Glimpse: Implement ACTION_SET_WALLPAPER
Browse files Browse the repository at this point in the history
Change-Id: I1a12a7a0a690ac887200bf08e03552b20d7a55f5
  • Loading branch information
SebaUbuntu committed Jan 31, 2024
1 parent 781d1f8 commit 49ae61c
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 22 deletions.
6 changes: 6 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@
<data android:mimeType="vnd.android.cursor.dir/video" />
</intent-filter>

<intent-filter>
<action android:name="android.intent.action.SET_WALLPAPER" />

<category android:name="android.intent.category.DEFAULT" />
</intent-filter>

</activity>

</application>
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/org/lineageos/glimpse/PickerActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class AlbumSelectorFragment : Fragment(R.layout.fragment_picker_album_selector)
private val albumsRecyclerView by getViewProperty<RecyclerView>(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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -63,11 +65,16 @@ class MediaSelectorFragment : Fragment(R.layout.fragment_picker_media_selector)
private val mediasRecyclerView by getViewProperty<RecyclerView>(R.id.mediasRecyclerView)
private val noMediaLinearLayout by getViewProperty<LinearLayout>(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 {
Expand Down Expand Up @@ -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(
Expand All @@ -267,19 +278,42 @@ 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()
}

/**
* Whether we can provide multiple items or only one.
* @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"
Expand Down
25 changes: 14 additions & 11 deletions app/src/main/java/org/lineageos/glimpse/utils/PickerUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,12 @@
<string name="media_info_location_loading_placeholder">Loading…</string>
<string name="media_info_location_open_with">View the location with</string>

<!-- View activity -->
<!-- Intents handling -->
<string name="intent_action_not_supported">Action not supported</string>
<string name="intent_media_not_found">Media not found</string>
<string name="intent_media_type_not_found">Media type not found</string>
<string name="intent_media_type_not_supported">Media type not supported</string>
<string name="intent_no_system_wallpaper_cropper_available">No system cropper available</string>

<!-- File actions -->
<string name="file_action_add_to_favorites">Add to favorites</string>
Expand Down

0 comments on commit 49ae61c

Please sign in to comment.