package com.android.launcher3.util;

import android.os.Build;
import android.text.TextUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Method;

public class SystemUtils {

    private static final String TAG = "SystemUtils";

    public static String getDeviceBrand() {
        return Build.BRAND;
    }

    /**
     * 是否是小米
     *
     * @return
     */
    public static boolean isMIUI() {
        return getDeviceBrand().toUpperCase().equals("XIAOMI") || getDeviceBrand().toUpperCase().equals("MEITU");
    }


    /**
     * 是否是华为
     *
     * @return
     */
    public static boolean isHW() {
        return getDeviceBrand().toUpperCase().equals("HUAWEI");
    }


    public static boolean isHONOR() {
        return getDeviceBrand().toUpperCase().equals("HONOR");
    }

    /**
     * 是否是魅族
     *
     * @return
     */
    public static boolean isMEIZU() {
        return getDeviceBrand().toUpperCase().equals("MEIZU");
    }

    /**
     * 是否是OPPO
     *
     * @return
     */
    public static boolean isOPPO() {
        return getDeviceBrand().toUpperCase().equals("OPPO");
    }

    /**
     * 是否是VIVO
     *
     * @return
     */
    public static boolean isVIVO() {
        return getDeviceBrand().toUpperCase().equals("VIVO");
    }

    /**
     * 是否是三星
     *
     * @return
     */
    public static boolean isSAMSUNG() {
        return getDeviceBrand().toUpperCase().equals("SAMSUNG");
    }

    public static boolean isNOKIA() {
        return getDeviceBrand().toUpperCase().equals("NOKIA");
    }

    /**
     * 获取MIUI版本
     *
     * @return
     */
    public static String getMIUIVersion() {
        String versionCode = "";
        String manufacturer = Build.MANUFACTURER;
        String model = Build.MODEL;
        if (!TextUtils.isEmpty(manufacturer) && manufacturer.equals("Xiaomi")) {
            versionCode = getSystemProperty("ro.miui.ui.version.name");
        }
        return versionCode;
    }

    /**
     * 获取EMUI版本
     *
     * @return
     */
    public static String getEMUI() {
        Class<?> classType = null;
        String buildVersion = null;
        try {
            classType = Class.forName("android.os.SystemProperties");
            Method getMethod = classType.getDeclaredMethod("get", new Class<?>[]{String.class});
            buildVersion = (String) getMethod.invoke(classType, new Object[]{"ro.build.version.emui"});
        } catch (Exception e) {
            e.printStackTrace();
        }
        return buildVersion;
    }

    public static String getSystemProperty(String propName) {
        String line;
        BufferedReader input = null;
        try {
            Process p = Runtime.getRuntime().exec("getprop " + propName);
            input = new BufferedReader(new InputStreamReader(p.getInputStream()), 1024);
            line = input.readLine();
            input.close();
        } catch (IOException ex) {
            return null;
        } finally {
            if (input != null) {
                try {
                    input.close();
                } catch (IOException e) {
                }
            }
        }
        return line;
    }

    /**
     * 获取当前手机系统版本号
     *
     * @return  系统版本号
     */
    public static String getSystemVersion() {
        return android.os.Build.VERSION.RELEASE;
    }

    public static int getSystemVersionCode(){
        return android.os.Build.VERSION.SDK_INT;
    }


}
