package com.yyrh.utils;

import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorManager;

import android.os.Build;

import android.text.TextUtils;
import android.util.Log;


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

public class InspectSimulator {

    private static String[] known_pipes = new String[]{"/dev/socket/qemud", "/dev/qemu_pipe"};

    public InspectSimulator() {
    }

    public static String isSimulator(Context context) {
        return !notHasBlueTooth() && !notHasLightSensorManager(context) && !isFeatures() && !checkIsNotRealPhone() && !checkPipes() ? "0" : "1";
    }



    public static boolean notHasBlueTooth() {
        BluetoothAdapter ba = BluetoothAdapter.getDefaultAdapter();
        if (ba == null) {
            return true;
        } else {
            String name = ba.getName();
            return TextUtils.isEmpty(name);
        }
    }

    public static Boolean notHasLightSensorManager(Context context) {
        SensorManager sensorManager = (SensorManager)context.getSystemService(context.SENSOR_SERVICE);
        Sensor sensor8 = sensorManager.getDefaultSensor(5);
        return sensor8 == null ? true : false;
    }

    public static boolean isFeatures() {
        return Build.FINGERPRINT.startsWith("generic") || Build.FINGERPRINT.toLowerCase().contains("vbox") || Build.FINGERPRINT.toLowerCase().contains("test-keys") || Build.MODEL.contains("google_sdk") || Build.MODEL.contains("Emulator") || Build.MODEL.contains("Android SDK built for x86") || Build.MANUFACTURER.contains("Genymotion") || Build.BRAND.startsWith("generic") && Build.DEVICE.startsWith("generic") || "google_sdk".equals(Build.PRODUCT);
    }

    public static boolean checkIsNotRealPhone() {
        String cpuInfo = readCpuInfo();
        return cpuInfo.contains("intel") || cpuInfo.contains("amd");
    }

    public static String readCpuInfo() {
        String result = "";

        try {
            String[] args = new String[]{"/system/bin/cat", "/proc/cpuinfo"};
            ProcessBuilder cmd = new ProcessBuilder(args);
            Process process = cmd.start();
            StringBuffer sb = new StringBuffer();
            String readLine = "";
            BufferedReader responseReader = new BufferedReader(new InputStreamReader(process.getInputStream(), "utf-8"));

            while((readLine = responseReader.readLine()) != null) {
                sb.append(readLine);
            }

            responseReader.close();
            result = sb.toString().toLowerCase();
        } catch (IOException var7) {
        }

        return result;
    }

    public static boolean checkPipes() {
        for(int i = 0; i < known_pipes.length; ++i) {
            String pipes = known_pipes[i];
            File qemu_socket = new File(pipes);
            if (qemu_socket.exists()) {
                Log.v("Result:", "Find pipes!");
                return true;
            }
        }

        Log.i("Result:", "Not Find pipes!");
        return false;
    }
}
