Skip to content

Commit

Permalink
添加了申请Shizuku的权限申请
Browse files Browse the repository at this point in the history
将部分序列化的bean和service keep
  • Loading branch information
huaweikai committed Apr 18, 2024
1 parent 639f800 commit a030364
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 30 deletions.
6 changes: 5 additions & 1 deletion app/proguard-rules.pro
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,8 @@

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
#-renamesourcefileattribute SourceFile
-keep class hua.dy.image.service.IFileExplorerService { *; }
-keep class hua.dy.image.service.FileExplorerService
-keep class hua.dy.image.bean.FileBean { *; }
-keep class hua.dy.image.bean.ImageBean { *; }
Binary file modified app/release/app-release.apk
Binary file not shown.
2 changes: 1 addition & 1 deletion app/release/output-metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"type": "SINGLE",
"filters": [],
"attributes": [],
"versionCode": 4,
"versionCode": 5,
"versionName": "0.3",
"outputFile": "app-release.apk"
}
Expand Down
8 changes: 1 addition & 7 deletions app/src/main/java/hua/dy/image/DyImgApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,13 @@ import android.util.Log
import hua.dy.image.utils.FileExplorerServiceManager
import hua.dy.image.utils.ShizukuUtils
import rikka.shizuku.Shizuku
import rikka.shizuku.ShizukuProvider
import rikka.sui.Sui

class DyImgApplication: Application() {

companion object {
private val isSui = Sui.init(BuildConfig.APPLICATION_ID)
}

override fun onCreate() {
super.onCreate()
Shizuku.addBinderReceivedListener {
if (ShizukuUtils.isShizukuAvailable) {
if (ShizukuUtils.isShizukuAvailable && ShizukuUtils.isShizukuPermission) {
FileExplorerServiceManager.bindService()
}
}
Expand Down
23 changes: 15 additions & 8 deletions app/src/main/java/hua/dy/image/ui/Home.kt
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,11 @@ import coil.decode.ImageDecoderDecoder
import coil.request.ImageRequest
import coil.transform.RoundedCornersTransformation
import hua.dy.image.SharedDialog
import hua.dy.image.app.DyAppBean
import hua.dy.image.bean.ImageBean
import hua.dy.image.bean.isGif
import hua.dy.image.utils.GetDyPermission
import hua.dy.image.utils.SortBottomDialog
import hua.dy.image.utils.dp2Px
import hua.dy.image.utils.hasDyPermission
import hua.dy.image.utils.screenHeightPx
import hua.dy.image.utils.sortValue
import hua.dy.image.viewmodel.DyImageViewModel
Expand All @@ -86,11 +84,7 @@ fun Home() {
val imageData = viewModel.allImages.collectAsLazyPagingItems()

var permissionState by remember {
mutableStateOf(hasDyPermission(DyAppBean.packageName))
}

if (!permissionState) {
GetDyPermission()
mutableStateOf(viewModel.hasPermission)
}

val dialogState = remember {
Expand Down Expand Up @@ -138,7 +132,7 @@ fun Home() {
modifier = Modifier
.padding(end = 16.dp)
.clickable {
val permission = hasDyPermission(DyAppBean.packageName)
val permission = viewModel.hasPermission
permissionState = permission
if (permission) {
viewModel.refreshDyImages()
Expand Down Expand Up @@ -312,6 +306,19 @@ fun Home() {
)
}

if (!permissionState) {
GetDyPermission(viewModel.needShizuku) { isGanted, isShizuku ->
if (isShizuku) {
if (isGanted) {
viewModel.bindService()
} else {
viewModel.needShizuku = false
permissionState = false
}
}
}
}

}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,12 @@ import android.content.ComponentName
import android.content.ServiceConnection
import android.os.IBinder
import android.util.Log
import androidx.documentfile.provider.DocumentFile
import hua.dy.image.BuildConfig
import hua.dy.image.DyImgApplication
import hua.dy.image.app.DY_FILE_PATH
import hua.dy.image.app.DyAppBean
import hua.dy.image.service.FileExplorerService
import hua.dy.image.service.IFileExplorerService
import rikka.shizuku.Shizuku
import rikka.shizuku.Shizuku.UserServiceArgs
import splitties.init.appCtx
import java.io.File


object FileExplorerServiceManager {
Expand All @@ -30,11 +25,6 @@ object FileExplorerServiceManager {
Log.d(TAG, "onServiceConnected: ")
isBind = true
FileExplorerService.service = IFileExplorerService.Stub.asInterface(service)
runCatching {
FileExplorerService.service?.listFiles(DY_FILE_PATH)?.forEach {
Log.e("TAG", "name ${it}")
}
}
}

override fun onServiceDisconnected(name: ComponentName) {
Expand All @@ -47,6 +37,7 @@ object FileExplorerServiceManager {
fun bindService() {
Log.d(TAG, "bindService: isBind = " + isBind)
if (!isBind) {
Log.e("TAG", "startBindService")
Shizuku.bindUserService(USER_SERVICE_ARGS, SERVICE_CONNECTION)
}
}
Expand Down
45 changes: 42 additions & 3 deletions app/src/main/java/hua/dy/image/utils/PathUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,64 @@
package hua.dy.image.utils

import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.widget.Toast
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
import androidx.core.net.toUri
import androidx.documentfile.provider.DocumentFile
import hua.dy.image.app.AppBean
import hua.dy.image.app.DyAppBean
import hua.dy.image.bean.FileBean
import rikka.shizuku.Shizuku
import splitties.init.appCtx

const val ANDROID_SAF_PATH = "content://com.android.externalstorage.documents/document/primary%3AAndroid%2Fdata%2F"

@Composable
fun GetDyPermission(
appBean: AppBean = DyAppBean
needShizuku: Boolean,
appBean: AppBean = DyAppBean,
callBack: (isGanted: Boolean, isShizuku: Boolean) -> Unit
) {
if (ShizukuUtils.isShizukuAvailable && needShizuku) {
GetShizukuPermission(callBack)
} else {
GetSafPermission(appBean)
}
}

@Composable
private fun GetShizukuPermission(
callBack: (isGanted: Boolean, isShizuku: Boolean) -> Unit
) {
val listener = Shizuku.OnRequestPermissionResultListener { requestCode, grantResult ->
if (requestCode != 0) return@OnRequestPermissionResultListener
if (grantResult == PackageManager.PERMISSION_GRANTED) {
callBack.invoke(true, true)
Toast.makeText(appCtx, "权限获取成功", Toast.LENGTH_SHORT).show()
} else {
callBack.invoke(false, true)
Toast.makeText(appCtx, "权限获取失败", Toast.LENGTH_SHORT).show()
}
}
DisposableEffect(Unit) {
Shizuku.addRequestPermissionResultListener(listener)
onDispose {
Shizuku.removeRequestPermissionResultListener(listener)
}
}
LaunchedEffect(Unit) {
Shizuku.requestPermission(0)
}
}

@Composable
private fun GetSafPermission(
appBean: AppBean
) {
var packShared by SharedPreferenceEntrust(appBean.packageName, "")
val launcher = rememberLauncherForActivityResult(ActivityResultContracts.OpenDocumentTree()) { uri ->
Expand All @@ -46,7 +86,6 @@ fun GetDyPermission(
}

fun hasDyPermission(packageName: String): Boolean {
if (ShizukuUtils.isShizukuPermission) return true
val permissionUris = appCtx.contentResolver.persistedUriPermissions.map {
it.uri.toString().split("data%2F", ignoreCase = true).last()
}
Expand Down
17 changes: 17 additions & 0 deletions app/src/main/java/hua/dy/image/viewmodel/DyImageViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ import hua.dy.image.bean.PNG
import hua.dy.image.db.dyImageDao
import hua.dy.image.service.FileExplorerService
import hua.dy.image.utils.APP_SHARED_PROVIDER_TOP_PATH
import hua.dy.image.utils.FileExplorerServiceManager
import hua.dy.image.utils.ShizukuUtils
import hua.dy.image.utils.hasDyPermission
import hua.dy.image.utils.scanDyImages
import hua.dy.image.utils.scanDyImagesWithShizuku
import hua.dy.image.utils.sortValue
Expand Down Expand Up @@ -145,4 +147,19 @@ class DyImageViewModel: ViewModel() {
}
}

fun bindService() {
FileExplorerServiceManager.bindService()
}

val hasPermission: Boolean
get() {
return if (ShizukuUtils.isShizukuAvailable) {
ShizukuUtils.isShizukuPermission
} else {
hasDyPermission(DyAppBean.packageName)
}
}

var needShizuku: Boolean = true

}

0 comments on commit a030364

Please sign in to comment.