
package com.reactlibrary;

import android.util.Log;

import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.Callback;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;

public class RNJailRootModule extends ReactContextBaseJavaModule {

  private static String TAG = RNJailRootModule.class.getName();

  private static Boolean isRooted;

  public RNJailRootModule(ReactApplicationContext reactContext) {
    super(reactContext);
  }

  @ReactMethod
  public void getStatus(Callback successCallback) {
    successCallback.invoke(null, isRooted);
  }

  @ReactMethod
  public void check() {
    isRooted = checkRootMethod1() || checkRootMethod2() || checkRootMethod3() || checkRootMethod4();
  }

  private static boolean checkRootMethod1() {
    String buildTags = android.os.Build.TAGS;
    return buildTags != null && buildTags.contains("test-keys");
  }

  private static boolean checkRootMethod2() {
    String[] paths = { "/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 (String path : paths) {
      if (new File(path).exists()) return true;
    }
    return false;
  }

  private static boolean checkRootMethod3() {
    Process process = null;
    try {
      process = Runtime.getRuntime().exec(new String[] { "/system/xbin/which", "su" });
      BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
      return in.readLine() != null;
    } catch (Throwable t) {
      return false;
    } finally {
      if (process != null) process.destroy();
    }
  }

  public boolean checkRootMethod4() {
    return new ExecShell().executeCommand(SHELL_CMD.check_su_binary)!=null;
  }

  public enum SHELL_CMD {
    check_su_binary(new String[] { "/system/xbin/which", "su" });

    String[] command;

    SHELL_CMD(String[] command) {
      this.command = command;
    }
  }

  public class ExecShell {
    public ArrayList<String> executeCommand(SHELL_CMD shellCmd) {
      String line = null;
      Process localProcess = null;
      ArrayList<String> fullResponse = new ArrayList<String>();

      try {
        localProcess = Runtime.getRuntime().exec(shellCmd.command);
      } catch (Exception e) {
        return null;
      }

      BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
              localProcess.getOutputStream()));
      BufferedReader in = new BufferedReader(new InputStreamReader(
              localProcess.getInputStream()));

      try {
        while ((line = in.readLine()) != null) {
          Log.d(TAG, "--> Line received: " + line);
          fullResponse.add(line);
        }
      } catch (Exception e) {
        e.printStackTrace();
      }

      Log.d(TAG, "--> Full response was: " + fullResponse);
      return fullResponse;
    }
  }


  @Override
  public String getName() {
    return "RNJailRoot";
  }
}