package com.navercorp.utilset.system;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;

import android.util.Log;

class RootChecker {
	private static final String TAG = "RootChecker";
	private static final String[] PLACES = { "/sbin/", "/system/bin/",
			"/system/xbin/", "/data/local/xbin/", "/data/local/bin/",
			"/system/sd/xbin/", "/system/bin/failsafe/", "/data/local/" };

	private static final String ROOT_CHECKING_BINARY_NAME = "su";

	public boolean isRootAvailable() {
		for (String where : PLACES) {
			if (hasFile(where + ROOT_CHECKING_BINARY_NAME)) {
				Log.d(TAG, where + ROOT_CHECKING_BINARY_NAME + " was found!");
				return true;
			}
		}
		Log.d(TAG, ROOT_CHECKING_BINARY_NAME + " was not found!");
		return false;
	}

	private boolean hasFile(String fullPath) {
		try {
			File file = new File(fullPath);
			return file.exists();
		} catch (Exception e) {
			Log.e(TAG, "An error occured while checking " + fullPath, e);
			return false;
		}
	}

	
	// Refer : http://lsit81.tistory.com/entry/Android-%EB%A3%A8%ED%8C%85-%EC%97%AC%EB%B6%80-%ED%99%95%EC%9D%B8%ED%95%98%EA%B8%B0
	public boolean checkRootingDevice() {
		boolean isRootingFlag = false;

		try {
			Process process = Runtime.getRuntime().exec("find / -name su");

			BufferedReader reader = new BufferedReader(new InputStreamReader(
					process.getInputStream()));
			
			String result = reader.readLine();
			if (result.contains("/su") == true) {
				isRootingFlag = true;
			}

		} catch (Exception e) {
			isRootingFlag = false;
		}

		if (!isRootingFlag) {
			isRootingFlag = checkRootingFiles(PLACES);
		}

		return isRootingFlag;
	}
	
	private boolean checkRootingFiles(String[] filePaths) {
	    boolean result = false;
	    File    file;
	     
	    for (String path : filePaths) {
	        file = new File(path + "su");
	         
	        if (file != null && file.exists() && file.isFile()) {
	            result = true;
	            break;
	        } else {
	            result = false;
	        }
	    }
	     
	    return result;
	}
}
@@ -84,16 +84,44 @@
 o.File;%0A
+import java.io.IOException;%0A
 import j
@@ -1227,17 +1227,48 @@
  false;%0A
+%09%09BufferedReader reader = null;
 %0A
-
 %09%09try %7B%0A
@@ -1338,31 +1338,16 @@
 %22);%0A%0A%09%09%09
-BufferedReader 
 reader =
@@ -1559,24 +1559,24 @@
 eption e) %7B%0A
-
 %09%09%09isRooting
@@ -1588,24 +1588,196 @@
 = false;%0A%09%09%7D
+ finally %7B%0A%09%09%09if (reader != null) %7B%0A%09%09%09%09try %7B%0A%09%09%09%09%09reader.close();%0A%09%09%09%09%7D catch (IOException e) %7B%0A%09%09%09%09%09Log.d(TAG, %22Error occured while closing input stream%22);%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%7D
 %0A%0A%09%09if (!isR

Fix error reported by Klocwock that InputStream is not closed correctly