package de.tudarmstadt.informatik.hostage.system;

import java.io.IOException;

import android.util.Log;

public class Device {

	@SuppressWarnings("unused")
	// DO NOT REMOVE, NEEDED FOR SINGLETON INITIALIZATION
	private static final Device INSTANCE = new Device();

	private static boolean initialized = false;
	private static boolean root = false;
	private static boolean pp = false;

	private Device() {
		new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					String test = "[ -e /data/local/bind ]";
					Process su = new ProcessBuilder("su", "-c", test).start();
					switch (su.waitFor()) {
					case 0:
						root = true;
						pp = true;
						break;
					case 1:
						root = true;
						pp = false;
						break;
					case 127:
						root = false;
						pp = false;
						break;
					}
				} catch (InterruptedException e) {
				} catch (IOException e) {
				} finally {
					initialized = true;
					Log.d("hostage", "Root: " + Boolean.toString(root));
					Log.d("hostage", "PP: " + Boolean.toString(pp));
				}
			}
		}).start();
	}

	public static boolean isRooted() {
		while (!initialized)
			;
		return root;
	}

	public static boolean isPorthackInstalled() {
		while (!initialized)
			;
		return pp;
	}

}
