Android Rom识别各类品牌

版权声明:转载请@我原创地址 https://blog.csdn.net/weixin_39706415/article/details/84968366


import android.annotation.SuppressLint;
import android.app.AppOpsManager;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.Log;

import com.xinlu.vkeeper.R;
import com.xinlu.vkeeper.views.CommomDialog;

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

/**
 * The type Rom.
 */
public class Rom {

    private static final String TAG = "Rom";

    /**
     * The constant ROM_MIUI.
     */
    public static final String ROM_MIUI = "MIUI";
    /**
     * The constant ROM_EMUI.
     */
    public static final String ROM_EMUI = "EMUI";
    /**
     * The constant ROM_FLYME.
     */
    public static final String ROM_FLYME = "FLYME";
    /**
     * The constant ROM_OPPO.
     */
    public static final String ROM_OPPO = "OPPO";
    /**
     * The constant ROM_SMARTISAN.
     */
    public static final String ROM_SMARTISAN = "SMARTISAN";
    /**
     * The constant ROM_VIVO.
     */
    public static final String ROM_VIVO = "VIVO";
    /**
     * The constant ROM_QIKU.
     */
    public static final String ROM_QIKU = "QIKU";
    /**
     * The constant ROM_SAMSUNG.
     */
    public static final String ROM_SAMSUNG = "SAMSUNG";
    /**
     * The constant ROM_ZTE.
     */
    public static final String ROM_ZTE = "ZTE";
    private static final String KEY_VERSION_MIUI = "ro.miui.ui.version.name";
    private static final String KEY_VERSION_EMUI = "ro.build.version.emui";
    private static final String KEY_VERSION_OPPO = "ro.build.version.opporom";
    private static final String KEY_VERSION_SMARTISAN = "ro.smartisan.version";
    private static final String KEY_VERSION_VIVO = "ro.vivo.os.version";
    private static String sName;
    private static String sVersion;
    private Context context;

    /**
     * Is emui boolean.
     *
     * @return the boolean
     */
    public  boolean isEmui() {
        return check(ROM_EMUI);
    }

    /**
     * Is miui boolean.
     *
     * @return the boolean
     */
    public  boolean isMiui() {
        return check(ROM_MIUI);
    }

    /**
     * Is vivo boolean.
     *
     * @return the boolean
     */
    public  boolean isVivo() {
        return check(ROM_VIVO);
    }

    /**
     * Is oppo boolean.
     *
     * @return the boolean
     */
    public  boolean isOppo() {
        return check(ROM_OPPO);
    }

    /**
     * Is flyme boolean.
     *
     * @return the boolean
     */
    public  boolean isFlyme() {
        return check(ROM_FLYME);
    }

    /**
     * Is sam sung boolean.
     *
     * @return the boolean
     */
    public  boolean isSamSung() {
        return check(ROM_SAMSUNG);
    }

    /**
     * Is zte boolean.
     *
     * @return the boolean
     */
    public  boolean isZTE() {
        return check(ROM_ZTE)||check("中兴");
    }


    /**
     * Is 360 boolean.
     *
     * @return the boolean
     */
    public  boolean is360() {
        return check(ROM_QIKU) || check("360");
    }

    /**
     * Is smartisan boolean.
     *
     * @return the boolean
     */
    public  boolean isSmartisan() {
        return check(ROM_SMARTISAN);
    }



    private  Rom rom = null;
    @SuppressLint("StaticFieldLeak")

            private static Rom rom1 = null;


    /**
     * Gets instance.
     *
     * @return the instance
     */
    public  static Rom getInstances() {
        if (rom1 == null) {
            rom1 = new Rom();
        }
        return rom1;
    }

    /**
     * Gets instance.
     *
     * @return the instance
     */
    public  Rom getInstance() {
        if (rom == null) {
            rom = new Rom();
        }
        return rom;
    }

    /**
     * Gets name.
     *
     * @return the name
     */
    public  String getName() {
        if (sName == null) {
            check("");
        }
        return sName;
    }

    /**
     * Gets version.
     *
     * @return the version
     */
    public  String getVersion() {
        if (sVersion == null) {
            check("");
        }
        return sVersion;
    }

    /**
     * Check boolean.
     *
     * @param rom the rom
     * @return the boolean
     */
    public  boolean check(String rom) {
        if (sName != null) {
            return sName.equals(rom);
        }

        if (!TextUtils.isEmpty(sVersion = getProp(KEY_VERSION_MIUI))) {
            sName = ROM_MIUI;
        } else if (!TextUtils.isEmpty(sVersion = getProp(KEY_VERSION_EMUI))) {
            sName = ROM_EMUI;
        } else if (!TextUtils.isEmpty(sVersion = getProp(KEY_VERSION_OPPO))) {
            sName = ROM_OPPO;
        } else if (!TextUtils.isEmpty(sVersion = getProp(KEY_VERSION_VIVO))) {
            sName = ROM_VIVO;
        } else if (!TextUtils.isEmpty(sVersion = getProp(KEY_VERSION_SMARTISAN))) {
            sName = ROM_SMARTISAN;
        } else {
            sVersion = Build.DISPLAY;
            if (sVersion.toUpperCase().contains(ROM_FLYME)) {
                sName = ROM_FLYME;
            } else {
                sVersion = Build.UNKNOWN;
                sName = Build.MANUFACTURER.toUpperCase();
            }
        }
        return sName.equals(rom);
    }

    /**
     * Gets prop.
     *
     * @param name the name
     * @return the prop
     */
    public  String getProp(String name) {
        String line = null;
        BufferedReader input = null;
        try {
            Process p = Runtime.getRuntime().exec("getprop " + name);
            input = new BufferedReader(new InputStreamReader(p.getInputStream()), 1024);
            line = input.readLine();
            input.close();
        } catch (IOException ex) {
            Log.e(TAG, "Unable to read prop " + name, ex);
            return null;
        } finally {
            if (input != null) {
                try {
                    input.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return line;
    }

    /**
     * 检测VPN拨号手机通知提示
     *
     * @param context the context
     */
    public void checkVpnPhoneRom(Context context) {
        this.context = context;
        if (!isNotificationEnabled(context)) {
            if (isEmui()) {
                VpnNotificationDialogShow("华为手机需要打开\n1.优先通知\n2.允许快速查看");
            } else if (isVivo()) {
                VpnNotificationDialogShow("vivo手机需要打开权限\n1.允许通知");
            } else if (isMiui()) {
                VpnNotificationDialogShow("小米手机需要打开\n1.视为优先通知\n2.允许快速查看\n3.设为重要通知");
            } else if (isOppo()) {
                VpnNotificationDialogShow("oppo手机需要前往通知管理打开权限\n1.允许通知");
            } else if (isFlyme()) {
                VpnNotificationDialogShow("魅族手机需要打开\n1.允许通知\n2.优先显示\n3.悬浮通知");
            } else {
                VpnNotificationDialogShow("当前手机需前往通知管理打开权限\n1.允许通知");
            }
        }
    }
    /**
     * vpn通知权限弹框提示
     */
    private void VpnNotificationDialogShow(final String name) {
        new CommomDialog(context, R.style.dialogs, "VPN拨号需要手动开启通知全部权限,否则会导致频繁掉线。\n请点击开启通知权限。\n" + name, new CommomDialog.OnCloseListener() {

            @Override
            public void onClick(Dialog dialog, boolean confirm) {
                if (confirm) {
                    checkRom(name);
                    dialog.dismiss();
                    SPUtils.getInstance().setLocalString("notification","notification","notification");
                } else {
                    dialog.dismiss();
                }
            }
        }).setTitle("温馨提示").setTitleColor(false).setPositiveButton("立即开启").setNegativeButton("我知道了").show();
    }

    /**
     * 检查rom跳转的界面
     */
    private void checkRom(String name) {
        if (name.contains("oppo") || name.contains("vivo") || name.contains("当前")) {
            startSetting();
        } else {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                Intent intent = new Intent();
                intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
                intent.putExtra("app_package", context.getPackageName());
                intent.putExtra("app_uid", context.getApplicationInfo().uid);
                context.startActivity(intent);
            } else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
                Intent intent = new Intent();
                intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                intent.addCategory(Intent.CATEGORY_DEFAULT);
                intent.setData(Uri.parse("package:" + context.getPackageName()));
                context. startActivity(intent);
            }
        }
    }

    /**
     * 跳转设置界面
     */
    private void startSetting() {
        Intent localIntent = new Intent();
        localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        localIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
        localIntent.setData(Uri.fromParts("package", context.getPackageName(), null));
        context.startActivity(localIntent);
    }

    /**
     * 获取通知栏权限是否开启
     */
    private static final String CHECK_OP_NO_THROW = "checkOpNoThrow";
    private static final String OP_POST_NOTIFICATION = "OP_POST_NOTIFICATION";
    @SuppressLint("NewApi")
    private boolean isNotificationEnabled(Context context) {
        AppOpsManager mAppOps = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
        ApplicationInfo appInfo = context.getApplicationInfo();
        String pkg = context.getApplicationContext().getPackageName();
        int uid = appInfo.uid;
        Class appOpsClass = null;
      /* Context.APP_OPS_MANAGER */
        try {
            appOpsClass = Class.forName(AppOpsManager.class.getName());
            Method checkOpNoThrowMethod = appOpsClass.getMethod(CHECK_OP_NO_THROW, Integer.TYPE, Integer.TYPE,
                    String.class);
            Field opPostNotificationValue = appOpsClass.getDeclaredField(OP_POST_NOTIFICATION);
            int value = (Integer) opPostNotificationValue.get(Integer.class);
            return ((Integer) checkOpNoThrowMethod.invoke(mAppOps, value, uid, pkg) == AppOpsManager.MODE_ALLOWED);
        } catch (ClassNotFoundException | NoSuchMethodException | InvocationTargetException | NoSuchFieldException | IllegalAccessException e) {
            e.printStackTrace();
        }
        return false;
    }


}

猜你喜欢

转载自blog.csdn.net/weixin_39706415/article/details/84968366
今日推荐