feat: use wallpaper
This commit is contained in:
@@ -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,8 +19,55 @@ 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 imageInfo = getImageInfo(url, context)
|
||||
context.grantUriPermission(
|
||||
context.packageName,
|
||||
imageInfo.uri,
|
||||
Intent.FLAG_GRANT_WRITE_URI_PERMISSION
|
||||
)
|
||||
val shareIntent = Intent(Intent.ACTION_SEND)
|
||||
shareIntent.type = "image/*"
|
||||
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
|
||||
.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY)
|
||||
} else {
|
||||
context.packageManager.queryIntentActivities(
|
||||
intent,
|
||||
PackageManager.ResolveInfoFlags.of(PackageManager.MATCH_DEFAULT_ONLY.toLong())
|
||||
)
|
||||
}
|
||||
for (resolveInfo in resInfoList) {
|
||||
val packageName = resolveInfo.activityInfo.packageName
|
||||
context.grantUriPermission(
|
||||
packageName,
|
||||
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()
|
||||
@@ -33,7 +82,6 @@ fun share(url: String, context: Context) {
|
||||
if (!path.exists())
|
||||
path.mkdirs()
|
||||
|
||||
|
||||
var file = File("${context.cacheDir}/image/$fileName")
|
||||
val downloadFile =
|
||||
File("${Environment.getExternalStorageDirectory()}/${Environment.DIRECTORY_PICTURES}/LspMake/$fileName")
|
||||
@@ -49,36 +97,6 @@ fun share(url: String, context: Context) {
|
||||
context,
|
||||
"com.lsp.view.fileprovider",
|
||||
file
|
||||
|
||||
)
|
||||
context.grantUriPermission(
|
||||
context.packageName,
|
||||
imageUri,
|
||||
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)
|
||||
val intent = Intent.createChooser(shareIntent, R.string.title_share.toString())
|
||||
val resInfoList: List<ResolveInfo> = if (Build.VERSION.SDK_INT < 33) {
|
||||
context.packageManager
|
||||
.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY)
|
||||
} else {
|
||||
context.packageManager.queryIntentActivities(
|
||||
intent,
|
||||
PackageManager.ResolveInfoFlags.of(PackageManager.MATCH_DEFAULT_ONLY.toLong())
|
||||
)
|
||||
}
|
||||
for (resolveInfo in resInfoList) {
|
||||
val packageName = resolveInfo.activityInfo.packageName
|
||||
context.grantUriPermission(
|
||||
packageName,
|
||||
imageUri,
|
||||
Intent.FLAG_GRANT_WRITE_URI_PERMISSION or Intent.FLAG_GRANT_READ_URI_PERMISSION
|
||||
)
|
||||
}
|
||||
context.startActivity(intent)
|
||||
}
|
||||
|
||||
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
|
||||
)
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user