feat: use wallpaper

This commit is contained in:
AnranYus
2024-06-29 20:55:32 +08:00
parent ab82ef5ff6
commit f706720a37
2 changed files with 73 additions and 39 deletions
@@ -1,10 +1,12 @@
package com.lsp.view.common
import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.content.pm.ResolveInfo
import android.graphics.Bitmap
import android.net.Uri
import android.os.Build
import android.os.Environment
import androidx.core.content.FileProvider
@@ -17,49 +19,21 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.io.File
data class ImageInfo(val path: String, val name: String, val uri: Uri)
@SuppressLint("QueryPermissionsNeeded")
fun share(url: String, context: Context) {
CoroutineScope(Dispatchers.IO).launch {
val imageLoader = ImageLoader.Builder(context)
.build()
val imageRequest = ImageRequest.Builder(context).data(url).allowHardware(true).build()
val cacheImage = imageLoader.execute(imageRequest)
val bitmap = cacheImage.drawable?.toBitmap()
val path = File("${context.cacheDir}/image")
val split = url.split("/")
val fileName = split[split.size - 1]
if (!path.exists())
path.mkdirs()
var file = File("${context.cacheDir}/image/$fileName")
val downloadFile =
File("${Environment.getExternalStorageDirectory()}/${Environment.DIRECTORY_PICTURES}/LspMake/$fileName")
if (downloadFile.exists()) {
file = downloadFile
} else if (!file.exists()) {
file.outputStream().apply {
bitmap?.compress(Bitmap.CompressFormat.PNG, 100, this)
}
}
val imageUri = FileProvider.getUriForFile(
context,
"com.lsp.view.fileprovider",
file
)
val imageInfo = getImageInfo(url, context)
context.grantUriPermission(
context.packageName,
imageUri,
imageInfo.uri,
Intent.FLAG_GRANT_WRITE_URI_PERMISSION
)
val shareIntent = Intent(Intent.ACTION_SEND)
shareIntent.type = "image/*"
shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri)
shareIntent.putExtra(Intent.EXTRA_TITLE, fileName)
shareIntent.putExtra(Intent.EXTRA_STREAM, imageInfo.uri)
shareIntent.putExtra(Intent.EXTRA_TITLE, imageInfo.name)
val intent = Intent.createChooser(shareIntent, R.string.title_share.toString())
val resInfoList: List<ResolveInfo> = if (Build.VERSION.SDK_INT < 33) {
context.packageManager
@@ -74,11 +48,55 @@ fun share(url: String, context: Context) {
val packageName = resolveInfo.activityInfo.packageName
context.grantUriPermission(
packageName,
imageUri,
imageInfo.uri,
Intent.FLAG_GRANT_WRITE_URI_PERMISSION or Intent.FLAG_GRANT_READ_URI_PERMISSION
)
}
context.startActivity(intent)
}
}
fun setWallpaper(url: String,context: Context) {
CoroutineScope(Dispatchers.IO).launch {
val imageInfo = getImageInfo(url, context)
val intent = Intent(Intent.ACTION_ATTACH_DATA)
intent.setDataAndType(imageInfo.uri, "image/*")
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
context.startActivity(intent)
}
}
suspend fun getImageInfo(url: String, context: Context): ImageInfo {
val imageLoader = ImageLoader.Builder(context)
.build()
val imageRequest = ImageRequest.Builder(context).data(url).allowHardware(true).build()
val cacheImage = imageLoader.execute(imageRequest)
val bitmap = cacheImage.drawable?.toBitmap()
val path = File("${context.cacheDir}/image")
val split = url.split("/")
val fileName = split[split.size - 1]
if (!path.exists())
path.mkdirs()
var file = File("${context.cacheDir}/image/$fileName")
val downloadFile =
File("${Environment.getExternalStorageDirectory()}/${Environment.DIRECTORY_PICTURES}/LspMake/$fileName")
if (downloadFile.exists()) {
file = downloadFile
} else if (!file.exists()) {
file.outputStream().apply {
bitmap?.compress(Bitmap.CompressFormat.PNG, 100, this)
}
}
val imageUri = FileProvider.getUriForFile(
context,
"com.lsp.view.fileprovider",
file
)
return ImageInfo(path = file.path, name = fileName, uri = imageUri)
}
@@ -30,6 +30,7 @@ import androidx.compose.ui.res.painterResource
import androidx.navigation.NavController
import coil.compose.AsyncImage
import com.lsp.view.R
import com.lsp.view.common.setWallpaper
import com.lsp.view.common.share
import com.lsp.view.ui.compose.PostViewModel
@@ -82,7 +83,7 @@ fun DetailScreen(navController: NavController, viewModel: PostViewModel) {
actions = {
IconButton(onClick = {
viewModel.actionDownload()
Toast.makeText(context,"Start download", Toast.LENGTH_SHORT).show()
Toast.makeText(context, "Start download", Toast.LENGTH_SHORT).show()
}) {
Icon(
painter = painterResource(id = R.drawable.ic_twotone_arrow_downward_24),
@@ -91,10 +92,25 @@ fun DetailScreen(navController: NavController, viewModel: PostViewModel) {
}
IconButton(onClick = {
uiState.selectPost?.let {
share(it.sampleUrl,context)
share(it.sampleUrl, context)
}
}) {
Icon(imageVector = Icons.Default.Share, contentDescription = "Share", tint = Color.White)
Icon(
imageVector = Icons.Default.Share,
contentDescription = "Share",
tint = Color.White
)
}
IconButton(onClick = {
uiState.selectPost?.let {
setWallpaper(it.sampleUrl, context)
}
}) {
Icon(
painter = painterResource(id = R.drawable.ic_twotone_wallpaper_24),
contentDescription = "Use to wallpaper",
tint = Color.White
)
}
},
)