Skip to content

Commit

Permalink
[Fix] Use lock for isSuiAvailable() and refactor code a bit.
Browse files Browse the repository at this point in the history
  • Loading branch information
zhanghai committed Feb 6, 2022
1 parent 4e19c61 commit 8b9ff15
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,20 @@ object LibSuFileServiceLauncher {
)
}

val isSuAvailable: Boolean
get() =
try {
Runtime.getRuntime().exec("su --version")
true
} catch (e: IOException) {
// java.io.IOException: Cannot run program "su": error=2, No such file or directory
false
}
fun isSuAvailable(): Boolean =
try {
Runtime.getRuntime().exec("su --version")
true
} catch (e: IOException) {
// java.io.IOException: Cannot run program "su": error=2, No such file or directory
false
}

@Throws(RemoteFileSystemException::class)
fun launchService(): IRemoteFileService {
synchronized(lock) {
// libsu won't call back when su isn't available.
if (!isSuAvailable) {
if (!isSuAvailable()) {
throw RemoteFileSystemException("Root isn't available")
}
return runBlocking {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ lateinit var rootContext: Context private set

object RootFileService : RemoteFileService(
RemoteInterface {
if (SuiFileServiceLauncher.isSuiAvailable) {
if (SuiFileServiceLauncher.isSuiAvailable()) {
SuiFileServiceLauncher.launchService()
} else {
LibSuFileServiceLauncher.launchService()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ object SuiFileServiceLauncher {

private var isSuiIntialized = false

val isSuiAvailable: Boolean
@ChecksSdkIntAtLeast(api = Build.VERSION_CODES.M)
get() {
@ChecksSdkIntAtLeast(api = Build.VERSION_CODES.M)
fun isSuiAvailable(): Boolean {
synchronized(lock) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
return false
}
Expand All @@ -43,12 +43,13 @@ object SuiFileServiceLauncher {
}
return Sui.isSui()
}
}

@RequiresApi(Build.VERSION_CODES.M)
@Throws(RemoteFileSystemException::class)
fun launchService(): IRemoteFileService {
synchronized(lock) {
if (!isSuiAvailable) {
if (!isSuiAvailable()) {
throw RemoteFileSystemException("Sui isn't available")
}
if (Shizuku.checkSelfPermission() != PackageManager.PERMISSION_GRANTED) {
Expand Down

0 comments on commit 8b9ff15

Please sign in to comment.