mirror of
https://github.com/FossifyOrg/Gallery.git
synced 2024-11-23 13:08:00 +01:00
allow selecting which wallpaper should be changed on Nougat+
This commit is contained in:
parent
a56bbbd54a
commit
9b5a52f66a
2 changed files with 31 additions and 3 deletions
|
@ -43,7 +43,7 @@ ext {
|
||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
implementation 'com.simplemobiletools:commons:3.0.21'
|
implementation 'com.simplemobiletools:commons:3.0.22'
|
||||||
implementation 'com.davemorrissey.labs:subsampling-scale-image-view:3.8.0'
|
implementation 'com.davemorrissey.labs:subsampling-scale-image-view:3.8.0'
|
||||||
implementation 'com.theartofdev.edmodo:android-image-cropper:2.4.0'
|
implementation 'com.theartofdev.edmodo:android-image-cropper:2.4.0'
|
||||||
implementation 'com.android.support:multidex:1.0.2'
|
implementation 'com.android.support:multidex:1.0.2'
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
package com.simplemobiletools.gallery.activities
|
package com.simplemobiletools.gallery.activities
|
||||||
|
|
||||||
|
import android.annotation.SuppressLint
|
||||||
import android.app.Activity
|
import android.app.Activity
|
||||||
import android.app.WallpaperManager
|
import android.app.WallpaperManager
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
|
@ -8,8 +9,11 @@ import android.net.Uri
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.view.Menu
|
import android.view.Menu
|
||||||
import android.view.MenuItem
|
import android.view.MenuItem
|
||||||
|
import com.simplemobiletools.commons.dialogs.RadioGroupDialog
|
||||||
import com.simplemobiletools.commons.extensions.isActivityDestroyed
|
import com.simplemobiletools.commons.extensions.isActivityDestroyed
|
||||||
|
import com.simplemobiletools.commons.extensions.isNougatPlus
|
||||||
import com.simplemobiletools.commons.extensions.toast
|
import com.simplemobiletools.commons.extensions.toast
|
||||||
|
import com.simplemobiletools.commons.models.RadioItem
|
||||||
import com.simplemobiletools.gallery.R
|
import com.simplemobiletools.gallery.R
|
||||||
import com.theartofdev.edmodo.cropper.CropImageView
|
import com.theartofdev.edmodo.cropper.CropImageView
|
||||||
import kotlinx.android.synthetic.main.view_crop_image.*
|
import kotlinx.android.synthetic.main.view_crop_image.*
|
||||||
|
@ -17,6 +21,7 @@ import kotlinx.android.synthetic.main.view_crop_image.*
|
||||||
class SetWallpaperActivity : SimpleActivity(), CropImageView.OnCropImageCompleteListener {
|
class SetWallpaperActivity : SimpleActivity(), CropImageView.OnCropImageCompleteListener {
|
||||||
private val PICK_IMAGE = 1
|
private val PICK_IMAGE = 1
|
||||||
private var isLandscapeRatio = true
|
private var isLandscapeRatio = true
|
||||||
|
private var wallpaperFlag = -1
|
||||||
|
|
||||||
lateinit var uri: Uri
|
lateinit var uri: Uri
|
||||||
lateinit var wallpaperManager: WallpaperManager
|
lateinit var wallpaperManager: WallpaperManager
|
||||||
|
@ -70,7 +75,7 @@ class SetWallpaperActivity : SimpleActivity(), CropImageView.OnCropImageComplete
|
||||||
|
|
||||||
override fun onOptionsItemSelected(item: MenuItem): Boolean {
|
override fun onOptionsItemSelected(item: MenuItem): Boolean {
|
||||||
when (item.itemId) {
|
when (item.itemId) {
|
||||||
R.id.save -> crop_image_view.getCroppedImageAsync()
|
R.id.save -> confirmWallpaper()
|
||||||
R.id.rotate -> crop_image_view.rotateImage(90)
|
R.id.rotate -> crop_image_view.rotateImage(90)
|
||||||
R.id.portrait_aspect_ratio -> changeAspectRatio(false)
|
R.id.portrait_aspect_ratio -> changeAspectRatio(false)
|
||||||
R.id.landscape_aspect_ratio -> changeAspectRatio(true)
|
R.id.landscape_aspect_ratio -> changeAspectRatio(true)
|
||||||
|
@ -85,6 +90,24 @@ class SetWallpaperActivity : SimpleActivity(), CropImageView.OnCropImageComplete
|
||||||
invalidateOptionsMenu()
|
invalidateOptionsMenu()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressLint("InlinedApi")
|
||||||
|
private fun confirmWallpaper() {
|
||||||
|
if (isNougatPlus()) {
|
||||||
|
val items = arrayListOf(
|
||||||
|
RadioItem(WallpaperManager.FLAG_SYSTEM, getString(R.string.home_screen)),
|
||||||
|
RadioItem(WallpaperManager.FLAG_LOCK, getString(R.string.lock_screen)),
|
||||||
|
RadioItem(WallpaperManager.FLAG_SYSTEM or WallpaperManager.FLAG_LOCK, getString(R.string.home_and_lock_screen)))
|
||||||
|
|
||||||
|
RadioGroupDialog(this, items) {
|
||||||
|
wallpaperFlag = it as Int
|
||||||
|
crop_image_view.getCroppedImageAsync()
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
crop_image_view.getCroppedImageAsync()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressLint("NewApi")
|
||||||
override fun onCropImageComplete(view: CropImageView?, result: CropImageView.CropResult) {
|
override fun onCropImageComplete(view: CropImageView?, result: CropImageView.CropResult) {
|
||||||
if (isActivityDestroyed())
|
if (isActivityDestroyed())
|
||||||
return
|
return
|
||||||
|
@ -97,7 +120,12 @@ class SetWallpaperActivity : SimpleActivity(), CropImageView.OnCropImageComplete
|
||||||
val ratio = wantedHeight / bitmap.height.toFloat()
|
val ratio = wantedHeight / bitmap.height.toFloat()
|
||||||
val wantedWidth = (bitmap.width * ratio).toInt()
|
val wantedWidth = (bitmap.width * ratio).toInt()
|
||||||
try {
|
try {
|
||||||
wallpaperManager.setBitmap(Bitmap.createScaledBitmap(bitmap, wantedWidth, wantedHeight, true))
|
val scaledBitmap = Bitmap.createScaledBitmap(bitmap, wantedWidth, wantedHeight, true)
|
||||||
|
if (isNougatPlus()) {
|
||||||
|
wallpaperManager.setBitmap(scaledBitmap, null, true, wallpaperFlag)
|
||||||
|
} else {
|
||||||
|
wallpaperManager.setBitmap(scaledBitmap)
|
||||||
|
}
|
||||||
setResult(Activity.RESULT_OK)
|
setResult(Activity.RESULT_OK)
|
||||||
} catch (e: OutOfMemoryError) {
|
} catch (e: OutOfMemoryError) {
|
||||||
toast(R.string.out_of_memory_error)
|
toast(R.string.out_of_memory_error)
|
||||||
|
|
Loading…
Reference in a new issue