Glimpse: Allow setting a wallpaper from intent

Change-Id: Ia22805cdb6d0d0178e50c252147b6956563259df
diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml
index def60f3..71e8f03 100644
--- a/app/src/main/AndroidManifest.xml
+++ b/app/src/main/AndroidManifest.xml
@@ -1,6 +1,6 @@
 <?xml version="1.0" encoding="utf-8"?>
 <!--
-     SPDX-FileCopyrightText: 2023 The LineageOS Project
+     SPDX-FileCopyrightText: 2023-2024 The LineageOS Project
      SPDX-License-Identifier: Apache-2.0
 -->
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
@@ -17,6 +17,7 @@
         android:maxSdkVersion="32" />
     <uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />
     <uses-permission android:name="android.permission.READ_MEDIA_VIDEO" />
+    <uses-permission android:name="android.permission.SET_WALLPAPER" />
 
     <application
         android:name=".GlimpseApplication"
@@ -105,6 +106,22 @@
 
         </activity>
 
+        <activity
+            android:name=".SetWallpaperActivity"
+            android:label="@string/set_wallpaper_label"
+            android:configChanges="orientation|screenLayout|screenSize|smallestScreenSize|keyboardHidden"
+            android:exported="true">
+
+            <intent-filter>
+                <action android:name="android.intent.action.ATTACH_DATA" />
+
+                <category android:name="android.intent.category.DEFAULT" />
+
+                <data android:mimeType="image/*" />
+            </intent-filter>
+
+        </activity>
+
     </application>
 
 </manifest>
diff --git a/app/src/main/java/org/lineageos/glimpse/SetWallpaperActivity.kt b/app/src/main/java/org/lineageos/glimpse/SetWallpaperActivity.kt
new file mode 100644
index 0000000..d3d5df6
--- /dev/null
+++ b/app/src/main/java/org/lineageos/glimpse/SetWallpaperActivity.kt
@@ -0,0 +1,53 @@
+/*
+ * SPDX-FileCopyrightText: 2024 The LineageOS Project
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+package org.lineageos.glimpse
+
+import android.app.WallpaperManager
+import android.os.Bundle
+import android.widget.ImageView
+import android.widget.Toast
+import androidx.appcompat.app.AppCompatActivity
+import com.google.android.material.button.MaterialButton
+
+class SetWallpaperActivity : AppCompatActivity(R.layout.activity_set_wallpaper) {
+    // Views
+    private val wallpaperImageView by lazy { findViewById<ImageView>(R.id.wallpaperImageView)!! }
+    private val setWallpaperButton by lazy { findViewById<MaterialButton>(R.id.setWallpaperButton)!! }
+
+    // System services
+    private val wallpaperManager by lazy { getSystemService(WallpaperManager::class.java) }
+
+    override fun onCreate(savedInstanceState: Bundle?) {
+        super.onCreate(savedInstanceState)
+
+        // Load wallpaper from intent
+        val wallpaperUri = intent.data ?: run {
+            Toast.makeText(this, R.string.intent_media_not_found, Toast.LENGTH_LONG).show()
+            finish()
+            return
+        }
+
+        // Check if the wallpaper can be changed
+        if (!wallpaperManager.isWallpaperSupported || !wallpaperManager.isSetWallpaperAllowed) {
+            Toast.makeText(
+                this, R.string.intent_wallpaper_cannot_be_changed, Toast.LENGTH_LONG
+            ).show()
+            finish()
+            return
+        }
+
+        wallpaperImageView.setImageURI(wallpaperUri)
+
+        // Set wallpaper
+        setWallpaperButton.setOnClickListener {
+            contentResolver.openInputStream(wallpaperUri)?.use {
+                wallpaperManager.setStream(it)
+            }
+
+            finish()
+        }
+    }
+}
diff --git a/app/src/main/res/layout/activity_set_wallpaper.xml b/app/src/main/res/layout/activity_set_wallpaper.xml
new file mode 100644
index 0000000..10a5d96
--- /dev/null
+++ b/app/src/main/res/layout/activity_set_wallpaper.xml
@@ -0,0 +1,24 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+     SPDX-FileCopyrightText: 2024 The LineageOS Project
+     SPDX-License-Identifier: Apache-2.0
+-->
+<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:tools="http://schemas.android.com/tools"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"
+    tools:context=".SetWallpaperActivity">
+
+    <ImageView
+        android:id="@+id/wallpaperImageView"
+        android:layout_width="match_parent"
+        android:layout_height="match_parent" />
+
+    <Button
+        android:id="@+id/setWallpaperButton"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_gravity="bottom|center_horizontal"
+        android:text="@string/set_wallpaper" />
+
+</FrameLayout>
diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml
index 1e620ca..aa4b682 100644
--- a/app/src/main/res/values/strings.xml
+++ b/app/src/main/res/values/strings.xml
@@ -1,6 +1,6 @@
 <?xml version="1.0" encoding="utf-8"?>
 <!--
-     SPDX-FileCopyrightText: 2023 The LineageOS Project
+     SPDX-FileCopyrightText: 2023-2024 The LineageOS Project
      SPDX-License-Identifier: Apache-2.0
 -->
 <resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
@@ -87,6 +87,7 @@
     <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>
+    <string name="intent_wallpaper_cannot_be_changed">Wallpaper can\'t be changed</string>
 
     <!-- File actions -->
     <string name="file_action_add_to_favorites">Add to favorites</string>
@@ -112,4 +113,8 @@
     <string name="pick_a_video">Pick a video</string>
     <string name="pick_a_media">Pick a media</string>
     <string name="picker_done">Done</string>
+
+    <!-- Wallpaper -->
+    <string name="set_wallpaper_label">Wallpaper</string>
+    <string name="set_wallpaper">Set wallpaper</string>
 </resources>