-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
63a4b18
commit 7007192
Showing
10 changed files
with
103 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 49 additions & 25 deletions
74
library/src/main/java/com/andreacioccarelli/billingprotector/utils/RootUtils.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,69 @@ | ||
package com.andreacioccarelli.billingprotector.utils | ||
|
||
import java.io.BufferedReader | ||
import java.io.IOException | ||
import java.io.InputStreamReader | ||
|
||
/** | ||
* Created by andrea on 2018/Jul. | ||
* Part of the package com.andreacioccarelli.billingprotector.utils | ||
*/ | ||
object RootUtils { | ||
val path: String | ||
get() { | ||
return try { | ||
val process = Runtime.getRuntime().exec("which su") | ||
internal object RootUtils { | ||
|
||
val output = StringBuffer() | ||
val buffer = BufferedReader(InputStreamReader(process.inputStream)) | ||
/** | ||
* Requests the root binary path by using unix which command, that | ||
* returns the path of a specified executable, in this case, su | ||
* | ||
* This is usually placed in directories like /system/bin, | ||
* /system/xbin, /su, etc.. | ||
* | ||
* While checking the path, root access is not requested or logged | ||
* | ||
* @return The path of the binary, or an empty string if | ||
* nothing is found in the host system | ||
* */ | ||
internal fun extractPath(): String { | ||
val inspectionCommand = "which su" | ||
|
||
var l: String? | ||
return try { | ||
val process = Runtime.getRuntime().exec(inspectionCommand) | ||
|
||
do { | ||
l = buffer.readLine() | ||
val outputBuffer = StringBuffer() | ||
val bufferedReader = BufferedReader(InputStreamReader(process.inputStream)) | ||
|
||
if (l == null) | ||
break | ||
var line: String? | ||
|
||
output.append(l) | ||
} while (true) | ||
do { | ||
line = bufferedReader.readLine() | ||
|
||
output.toString() | ||
} catch (io: Exception) { | ||
"" | ||
} | ||
if (line == null) | ||
break | ||
|
||
outputBuffer.append(line) | ||
} while (true) | ||
|
||
outputBuffer.toString() | ||
} catch (io: IOException) { | ||
"" | ||
} catch (nil: NullPointerException) { | ||
"" | ||
} | ||
} | ||
|
||
fun hasRootAccess(): Boolean { | ||
val path = path | ||
if (path.isEmpty()) return false | ||
if (path == "") return false | ||
if (!path.contains("/")) return false | ||
if (!path.contains("su")) return false | ||
|
||
return true | ||
/** | ||
* Determines wheter or not the device has root access by | ||
* analyzing the path of the su binary. | ||
* | ||
* @return A Boolean representing the device root state | ||
* */ | ||
internal fun hasRootAccess(): Boolean { | ||
val path = extractPath() | ||
return when { | ||
path.isEmpty() -> false | ||
!path.contains("/") -> false | ||
!path.contains("su") -> false | ||
else -> true | ||
} | ||
} | ||
} |