Created
May 28, 2015 20:49
-
-
Save rafaelwkerr/c4bb578505224c0ca5c6 to your computer and use it in GitHub Desktop.
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
package utils; | |
import java.io.BufferedReader; | |
import java.io.File; | |
import java.io.InputStreamReader; | |
public class RootUtil { | |
public static boolean isDeviceRooted() { | |
return checkRootMethod1() || checkRootMethod2() || checkRootMethod3() || checkRootMethod4(); | |
} | |
private static boolean checkRootMethod1() { | |
String buildTags = android.os.Build.TAGS; | |
return buildTags != null && buildTags.contains("test-keys"); | |
} | |
@SuppressWarnings("findbugs:DMI_HARDCODED_ABSOLUTE_FILENAME") | |
private static boolean checkRootMethod2() { | |
return new File("/system/app/Superuser.apk").exists(); | |
} | |
private static boolean checkRootMethod3() { | |
String[] paths = { "/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" }; | |
for (String path : paths) { | |
if (new File(path).exists()) return true; | |
} | |
return false; | |
} | |
private static boolean checkRootMethod4() { | |
Process process = null; | |
BufferedReader in = null; | |
try { | |
process = Runtime.getRuntime().exec(new String[] { "/system/xbin/which", "su" }); | |
in = new BufferedReader(new InputStreamReader(process.getInputStream())); | |
if (in.readLine() != null) { | |
return true; | |
} | |
return false; | |
} catch (Throwable t) { | |
return false; | |
} finally { | |
try { | |
if (in != null) { | |
in.close(); | |
} | |
if (process != null) { | |
process.destroy(); | |
} | |
} catch (Throwable t) { | |
// ignore the damn thing!!! | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment