Skip to content

Commit

Permalink
Glimpse: Allow specifying where to apply the wallpaper
Browse files Browse the repository at this point in the history
Change-Id: I655e88d4c00cdee1014d15e4880214617623929e
  • Loading branch information
luca020400 committed Feb 3, 2024
1 parent 185ea88 commit a18c53a
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 5 deletions.
26 changes: 22 additions & 4 deletions app/src/main/java/org/lineageos/glimpse/SetWallpaperActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
package org.lineageos.glimpse

import android.app.WallpaperManager
import android.net.Uri
import android.os.Bundle
import android.widget.ImageView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.google.android.material.button.MaterialButton
import com.google.android.material.dialog.MaterialAlertDialogBuilder

class SetWallpaperActivity : AppCompatActivity(R.layout.activity_set_wallpaper) {
// Views
Expand Down Expand Up @@ -43,11 +45,27 @@ class SetWallpaperActivity : AppCompatActivity(R.layout.activity_set_wallpaper)

// Set wallpaper
setWallpaperButton.setOnClickListener {
contentResolver.openInputStream(wallpaperUri)?.use {
wallpaperManager.setStream(it)
}
MaterialAlertDialogBuilder(this, R.style.Theme_Glimpse_SetWallpaperDialog)
.setTitle(R.string.set_wallpaper_dialog_title)
.setItems(R.array.set_wallpaper_items) { _, which ->
val flags = POSITION_TO_FLAG[which] ?: throw Exception("Invalid position")
setWallpaper(wallpaperUri, flags)
finish()
}.show()
}
}

finish()
private fun setWallpaper(uri: Uri, flags: Int) {
contentResolver.openInputStream(uri)?.use {
wallpaperManager.setStream(it, null, true, flags)
}
}

companion object {
private val POSITION_TO_FLAG = mapOf(
0 to WallpaperManager.FLAG_SYSTEM,
1 to WallpaperManager.FLAG_LOCK,
2 to (WallpaperManager.FLAG_SYSTEM or WallpaperManager.FLAG_LOCK)
)
}
}
21 changes: 21 additions & 0 deletions app/src/main/res/layout/set_wallpaper_dialog_item.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
SPDX-FileCopyrightText: 2019 The Android Open Source Project
SPDX-License-Identifier: Apache-2.0
-->

<!--
This layout file is used by the AlertDialog when displaying a list of items.
This layout file is inflated and used as the TextView to display individual
items.
-->
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
style="?attr/materialAlertDialogBodyTextStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:gravity="center_vertical"
android:minHeight="?attr/listPreferredItemHeightSmall"
android:paddingStart="?attr/listPreferredItemPaddingLeft"
android:paddingEnd="?attr/listPreferredItemPaddingRight" />
12 changes: 12 additions & 0 deletions app/src/main/res/values/arrays.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
SPDX-FileCopyrightText: 2024 The LineageOS Project
SPDX-License-Identifier: Apache-2.0
-->
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<string-array translatable="false" name="set_wallpaper_items">
<item>@string/set_wallpaper_home_screen</item>
<item>@string/set_wallpaper_lock_screen</item>
<item>@string/set_wallpaper_both</item>
</string-array>
</resources>
4 changes: 4 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,8 @@
<!-- Wallpaper -->
<string name="set_wallpaper_label">Wallpaper</string>
<string name="set_wallpaper">Set wallpaper</string>
<string name="set_wallpaper_dialog_title">Set wallpaper on</string>
<string name="set_wallpaper_home_screen">Home screen</string>
<string name="set_wallpaper_lock_screen">Lock screen</string>
<string name="set_wallpaper_both">Home and lock screen</string>
</resources>
7 changes: 6 additions & 1 deletion app/src/main/res/values/themes.xml
Original file line number Diff line number Diff line change
@@ -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:tools="http://schemas.android.com/tools">
Expand Down Expand Up @@ -99,4 +99,9 @@
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
</style>

<!-- Set wallpaper dialog theme -->
<style name="Theme.Glimpse.SetWallpaperDialog" parent="ThemeOverlay.Material3.MaterialAlertDialog.Centered">
<item name="listItemLayout">@layout/set_wallpaper_dialog_item</item>
</style>
</resources>

0 comments on commit a18c53a

Please sign in to comment.