-
Notifications
You must be signed in to change notification settings - Fork 93
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
crazy
committed
Feb 6, 2019
1 parent
1c146bc
commit 5815d33
Showing
6 changed files
with
262 additions
and
5 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
53 changes: 53 additions & 0 deletions
53
kotlinextensions/src/main/java/com/crazylegend/kotlinextensions/root/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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.crazylegend.kotlinextensions.root | ||
|
||
import java.io.BufferedReader | ||
import java.io.File | ||
import java.io.InputStreamReader | ||
|
||
|
||
/** | ||
* Created by crazy on 2/6/19 to long live and prosper ! | ||
*/ | ||
|
||
object RootUtils { | ||
|
||
val isDeviceRooted: Boolean | ||
get() = checkRootMethod1() || checkRootMethod2() || checkRootMethod3() | ||
|
||
private fun checkRootMethod1(): Boolean { | ||
val buildTags = android.os.Build.TAGS | ||
return buildTags != null && buildTags.contains("test-keys") | ||
} | ||
|
||
private fun checkRootMethod2(): Boolean { | ||
val paths = arrayOf( | ||
"/system/app/Superuser.apk", | ||
"/sbin/su", | ||
"/system/bin/su", | ||
"/system/xbin/su", | ||
"/data/local/xbin/su", | ||
"/data/local/bin/su", | ||
"/system/sd/xbin/su", | ||
"/system/bin/failsafe/su", | ||
"/data/local/su", | ||
"/su/bin/su" | ||
) | ||
for (path in paths) { | ||
if (File(path).exists()) return true | ||
} | ||
return false | ||
} | ||
|
||
private fun checkRootMethod3(): Boolean { | ||
var process: Process? = null | ||
return try { | ||
process = Runtime.getRuntime().exec(arrayOf("/system/xbin/which", "su")) | ||
val bufferedReader = BufferedReader(InputStreamReader(process!!.inputStream)) | ||
bufferedReader.readLine() != null | ||
} catch (t: Throwable) { | ||
false | ||
} finally { | ||
process?.destroy() | ||
} | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
kotlinextensions/src/main/java/com/crazylegend/kotlinextensions/root/root.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 |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package com.crazylegend.kotlinextensions.root | ||
|
||
import java.io.BufferedReader | ||
import java.io.File | ||
import java.io.InputStreamReader | ||
|
||
|
||
/** | ||
* Created by crazy on 2/6/19 to long live and prosper ! | ||
*/ | ||
|
||
|
||
|
||
fun isRooted(): Boolean { | ||
|
||
// get from build info | ||
val buildTags = android.os.Build.TAGS | ||
if (buildTags != null && buildTags.contains("test-keys")) { | ||
return true | ||
} | ||
|
||
// check if /system/app/Superuser.apk is present | ||
try { | ||
val file = File("/system/app/Superuser.apk") | ||
if (file.exists()) { | ||
return true | ||
} | ||
} catch (e1: Exception) { | ||
// ignore | ||
} | ||
|
||
// try executing commands | ||
return (canExecuteCommand("/system/xbin/which su") | ||
|| canExecuteCommand("/system/bin/which su") || canExecuteCommand("which su")) | ||
} | ||
|
||
// executes a command on the system | ||
private fun canExecuteCommand(command: String): Boolean { | ||
return try { | ||
Runtime.getRuntime().exec(command) | ||
true | ||
} catch (e: Exception) { | ||
false | ||
} | ||
} | ||
|
||
fun isRootAvailable(): Boolean { | ||
for (pathDir in System.getenv("PATH")!!.split(":".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()) { | ||
if (File(pathDir, "su").exists()) { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
fun isRootGiven(): Boolean { | ||
if (isRootAvailable()) { | ||
var process: Process? = null | ||
try { | ||
process = Runtime.getRuntime().exec(arrayOf("su", "-c", "id")) | ||
val bufferedReader = BufferedReader(InputStreamReader(process!!.inputStream)) | ||
val output = bufferedReader.readLine() | ||
if (output != null && output.toLowerCase().contains("uid=0")) | ||
return true | ||
} catch (e: Exception) { | ||
e.printStackTrace() | ||
} finally { | ||
process?.destroy() | ||
} | ||
} | ||
|
||
return false | ||
} |
98 changes: 98 additions & 0 deletions
98
kotlinextensions/src/main/java/com/crazylegend/kotlinextensions/view/RateItDialogFragment.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 |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package com.crazylegend.kotlinextensions.view | ||
|
||
import android.app.Dialog | ||
import android.content.Context | ||
import android.content.Intent | ||
import android.os.Bundle | ||
import android.content.SharedPreferences | ||
import android.net.Uri | ||
import androidx.appcompat.app.AlertDialog | ||
import androidx.core.content.edit | ||
import androidx.fragment.app.DialogFragment | ||
import androidx.fragment.app.FragmentManager | ||
import com.crazylegend.kotlinextensions.R | ||
|
||
|
||
/** | ||
* Created by crazy on 2/6/19 to long live and prosper ! | ||
*/ | ||
class RateItDialogFragment : DialogFragment() { | ||
|
||
|
||
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { | ||
return AlertDialog.Builder(requireActivity()) | ||
.setTitle("Rate us ?") | ||
.setMessage("If you're enjoying this application, kindly give us a rate and support us.") | ||
.setPositiveButton("Rate us") { _, _ -> | ||
startActivity( | ||
Intent( | ||
Intent.ACTION_VIEW, | ||
Uri.parse("market://details?id=" + requireActivity().packageName) | ||
) | ||
) | ||
|
||
getSharedPreferences(requireActivity()).edit(true) { | ||
this.putBoolean(DISABLED, true) | ||
} | ||
dismiss() | ||
} | ||
.setNeutralButton( | ||
"Remind Me later" | ||
) { _, _ -> dismiss() } | ||
.setNegativeButton("Don't show again") { _, _ -> | ||
getSharedPreferences(requireActivity()).edit(true) { | ||
this.putBoolean(DISABLED, true) | ||
} | ||
dismiss() | ||
}.create() | ||
} | ||
|
||
companion object { | ||
private const val LAUNCHES_UNTIL_PROMPT = 10 | ||
private const val DAYS_UNTIL_PROMPT = 3 | ||
private const val MILLIS_UNTIL_PROMPT = DAYS_UNTIL_PROMPT * 24 * 60 * 60 * 1000 | ||
private const val PREF_NAME = "APP_RATER" | ||
private const val LAST_PROMPT = "LAST_PROMPT" | ||
private const val LAUNCHES = "LAUNCHES" | ||
private const val DISABLED = "DISABLED" | ||
|
||
fun show(context: Context, fragmentManager: FragmentManager) { | ||
var shouldShow = false | ||
val sharedPreferences = getSharedPreferences(context) | ||
val currentTime = System.currentTimeMillis() | ||
var lastPromptTime = sharedPreferences.getLong(LAST_PROMPT, 0) | ||
if (lastPromptTime == 0L) { | ||
lastPromptTime = currentTime | ||
sharedPreferences.edit(true) { | ||
this.putLong(LAST_PROMPT, lastPromptTime) | ||
} | ||
} | ||
|
||
if (!sharedPreferences.getBoolean(DISABLED, false)) { | ||
val launches = sharedPreferences.getInt(LAUNCHES, 0) + 1 | ||
if (launches > LAUNCHES_UNTIL_PROMPT) { | ||
if (currentTime > lastPromptTime + MILLIS_UNTIL_PROMPT) { | ||
shouldShow = true | ||
} | ||
} | ||
sharedPreferences.edit(true) { | ||
this.putInt(LAUNCHES, launches) | ||
} | ||
} | ||
|
||
if (shouldShow) { | ||
|
||
|
||
sharedPreferences.edit(true) { | ||
this.putInt(LAUNCHES, 0).putLong(LAST_PROMPT, System.currentTimeMillis()).apply() | ||
} | ||
RateItDialogFragment().show(fragmentManager, null) | ||
|
||
} | ||
} | ||
|
||
private fun getSharedPreferences(context: Context): SharedPreferences { | ||
return context.getSharedPreferences(PREF_NAME, 0) | ||
} | ||
} | ||
} |