Skip to content

Instantly share code, notes, and snippets.

@illuzor illuzor/WallpaperFragment.kt Secret
Created Sep 28, 2018

Embed
What would you like to do?
private fun setWallpaper() {
val setIntent = Intent(Intent.ACTION_SET_WALLPAPER)
if (activity!!.packageManager.resolveActivity(setIntent, PackageManager.MATCH_DEFAULT_ONLY) == null) {
val dialog = ProgressDialog()
dialog.isCancelable = false
dialog.title = getString(R.string.setting_up_wallpaper)
dialog.show(fragmentManager, "")
runInBackground {
WallpaperManager.getInstance(activity).setStream(FileInputStream(imageFile))
runInMainThread {
dialog.dismiss()
toast(R.string.wallpaper_set)
}
}
} else {
setIntent.action = Intent.ACTION_ATTACH_DATA
setIntent.addCategory(Intent.CATEGORY_DEFAULT)
setIntent.data = imageFileUri
val activities = activity!!.packageManager.queryIntentActivities(setIntent, PackageManager.MATCH_DEFAULT_ONLY)
for (activity in activities) {
getActivity()!!.grantUriPermission(activity.activityInfo.packageName, imageFileUri, Intent.FLAG_GRANT_READ_URI_PERMISSION)
}
startActivityForResult(Intent.createChooser(setIntent, getString(R.string.set_as)), SETTING_REQUEST_CODE)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.