Code
Helper code related to live wallpapers.
LiveWallpaperUtil.kt
package com.hanmajid.androidnotebook
import android.app.WallpaperInfo
import android.app.WallpaperManager
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.service.wallpaper.WallpaperService
import androidx.core.content.ContextCompat.startActivity
/**
* Live wallpaper-related utility object.
*/
object LiveWallpaperUtil {
/**
* Launch an Activity for user to choose live wallpaper.
*/
fun launchLiveWallpaperChooserActivity(context: Context) {
startActivity(context, Intent(WallpaperManager.ACTION_LIVE_WALLPAPER_CHOOSER), null)
}
/**
* Get all live wallpapers that are available in the device.
*
* To use this method, you need to specify this inside your AndroidManifest.xml:
* ```
* <queries>
* <intent>
* <action android:name="android.service.wallpaper.WallpaperService" />
* </intent>
* </queries>
* ```
*/
fun getAllLiveWallpapers(context: Context): List<WallpaperInfo> {
return context.packageManager.queryIntentServices(
Intent(WallpaperService.SERVICE_INTERFACE),
PackageManager.GET_META_DATA,
).map {
WallpaperInfo(context, it)
}
}
/**
* Launch a preview of [liveWallpaper], allowing user to confirm to change to that live
* wallpaper.
*/
fun changeLiveWallpaper(context: Context, liveWallpaper: WallpaperInfo) {
startActivity(
context,
Intent(WallpaperManager.ACTION_CHANGE_LIVE_WALLPAPER).apply {
putExtra(
WallpaperManager.EXTRA_LIVE_WALLPAPER_COMPONENT,
liveWallpaper.component
)
},
null,
)
}
}