获取手机品牌工具类

package com.xsw.model.fonts.utils;

import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.Signature;
import android.os.Build;
import android.os.Environment;
import android.text.TextUtils;
import android.util.DisplayMetrics;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.security.interfaces.RSAPublicKey;
import java.util.ArrayList;
import java.util.Properties;

/**
 * Created by 247321453 on 2016/7/17.
 */
public class OSUtils {
    private static final String KEY_EMUI_VERSION_CODE = "ro.build.version.emui";
    private static final String KEY_MIUI_VERSION_CODE = "ro.miui.ui.version.code";
    private static final String KEY_MIUI_VERSION_NAME = "ro.miui.ui.version.name";
    private static final String KEY_MIUI_INTERNAL_STORAGE = "ro.miui.internal.storage";
    private static final String KEY_EMUI_API_LEVEL_CODE = "ro.build.hw_emui_api_level";
    /**
     * FixBUG 
     * Mephone: tony.amin on: 2017/2/9 17:03
     * TODO: replace this line with your comment
     */
    private static final String KEY_MIUI_VERSION_INCREMENTAL = "ro.build.version.incremental";
    private static final String KEY_BUILD_DISPLAY_ID = "ro.build.display.id";
    private static final String KER_VIVO_BUILD_DISPLAY_ID = "ro.vivo.os.build.display.id";
    private static final String KEY_OPPO_ROM_VERSION = "ro.build.version.opporom";
    private boolean isMiui_8_1 = false;
    private boolean isOppoRom_V3 = false;
    private boolean funtouch;
    private boolean colorUi;
    private String miuiVersionName;
    /********* End of Mephone: tony.amin ***********/

    private static final OSUtils sOSUtils = new OSUtils();
    private boolean emui;
    private boolean miui;
    private boolean flyme;
    private String miuiVersion;
    private static int level = 0;
    private OSUtils() {
        Properties properties;
        try {
            properties = new Properties();
            properties.load(new FileInputStream(new File(Environment.getRootDirectory(), "build.prop")));
        } catch (IOException e) {
            properties = null;
        }
        if (properties != null) {
//          emui = !TextUtils.isEmpty(properties.getProperty(KEY_EMUI_VERSION_CODE));
            emui = isEMUI();
            miuiVersion = properties.getProperty(KEY_MIUI_VERSION_CODE);
            miuiVersionName = properties.getProperty(KEY_MIUI_VERSION_NAME);
            miui = !TextUtils.isEmpty(miuiVersion) || !TextUtils.isEmpty(properties.getProperty(KEY_MIUI_VERSION_NAME))
                    || !TextUtils.isEmpty(properties.getProperty(KEY_MIUI_INTERNAL_STORAGE));

            /**
             * FixBUG
             * Mephone: tony.amin on: 2017/2/9 17:04
             * TODO: replace this line with your comment
             */
            String incremental = properties.getProperty(KEY_MIUI_VERSION_INCREMENTAL);
            if (miui && !TextUtils.isEmpty(incremental)) {
                isMiui_8_1 = incremental.contains("V8.1");
            }
            String oppo_rom = properties.getProperty(KEY_OPPO_ROM_VERSION);
            if (!TextUtils.isEmpty(oppo_rom)) {
                colorUi = true;
            }
            if (TextUtils.equals(oppo_rom, "V3.0")) {
                isOppoRom_V3 = true;
            }
            String displayId = properties.getProperty(KEY_BUILD_DISPLAY_ID);
            if (!TextUtils.isEmpty(displayId) && displayId.toLowerCase().contains("flyme")) {
                flyme = true;
            }
            String vivoDisplayId = properties.getProperty(KER_VIVO_BUILD_DISPLAY_ID);
            if (!TextUtils.isEmpty(vivoDisplayId) && vivoDisplayId.toLowerCase().contains("funtouch")) {
                funtouch = true;
            }
            android.util.Log.e("yubang","funtouch=" + funtouch);
            /********* End of Mephone: tony.amin ***********/
        }
    }

    public static boolean isSamsung(Context context){
        PackageManager pm = context.getPackageManager();
        try {
            PackageInfo info = pm.getPackageInfo("com.android.thememanager", 0);
            return getReceiver(info.applicationInfo.sourceDir);
        } catch (Exception e) {
            if (getSingInfo(context, "com.android.settings")
                    .equals("c986384a3e1f2fb206670e78ef232215c0d26f45a22728db99a44da11c35ac33a71fe071c4a2d6825a9b4c88b333ed96f3c5e6c666d60f3ee94c490885abcf8dc660f707aabc77ead3e2d0d8aee8108c15cd260f2e85042c28d2f292daa3c6da0c7bf2391db7841aade8fdf0c9d0defcf77124e6d2de0a9e0d2da746c3670e4ffcdc85b701bb4744861b96ff7311da3603c5a10336e55ffa34b4353eedc85f51015e1518c67e309e39f87639ff178107f109cd18411a6077f26964b6e63f8a70b9619db04306a323c1a1d23af867e19f14f570ffe573d0e3a0c2b30632aaec3173380994be1e341e3a90bd2e4b615481f46db39ea83816448ec35feb1735c1f3")) {
                return true;
            }
        }
        return false;
    }

    private static String getSingInfo(Context mContext, String packageName) {
        String key = "";
        try {
            PackageInfo packageInfo = mContext.getPackageManager()
                    .getPackageInfo(packageName, PackageManager.GET_SIGNATURES);
            Signature[] signs = packageInfo.signatures;
            Signature sign = signs[0];
            key = parseSignature(sign.toByteArray());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return key;
    }

    private static String parseSignature(byte[] signature) {
        String key = "";
        try {
            CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
            X509Certificate cert = (X509Certificate) certFactory.generateCertificate(new ByteArrayInputStream(signature));
            RSAPublicKey pk = (RSAPublicKey) cert.getPublicKey();
            key = pk.getModulus().toString(16);
        } catch (CertificateException e) {
            e.printStackTrace();
        }
        return key;
    }

    public static boolean getReceiver(final String apkPath) {
        if (apkPath == null)
            return false;
        File file = new File(apkPath);
        if (file.exists()) {
            String PATH_PackageParser = "android.content.pm.PackageParser";
            // 反射得到pkgParserCls对象并实例化,有参数
            try {
                Class<?> pkgParserCls = Class.forName(PATH_PackageParser);
                Class<?>[] typeArgs = { String.class };
                Constructor<?> pkgParserCt = pkgParserCls.getConstructor(typeArgs);
                Object[] valueArgs = { apkPath };
                Object pkgParser = pkgParserCt.newInstance(valueArgs);

                DisplayMetrics metrics = new DisplayMetrics();
                metrics.setToDefaults();
                typeArgs = new Class<?>[] { File.class, String.class, DisplayMetrics.class, int.class };
                Method pkgParser_parsePackageMtd = pkgParserCls.getDeclaredMethod("parsePackage", typeArgs);

                valueArgs = new Object[] { new File(apkPath), apkPath, metrics, 0 };

                // 执行pkgParser_parsePackageMtd方法并返回
                Object pkgParserPkg = pkgParser_parsePackageMtd.invoke(pkgParser, valueArgs);

                // 从返回的对象得到名为"applicationInfo"的字段对象
                if (pkgParserPkg != null) {
                    Field PkgParserActivityFld = pkgParserPkg.getClass().getDeclaredField("receivers");
                    @SuppressWarnings("unchecked")
                    ArrayList<Object> PkgParserActivitys = (ArrayList<Object>) PkgParserActivityFld.get(pkgParserPkg);
                    for (int i = 0; i < PkgParserActivitys.size(); i++) {
                        Object receiver = PkgParserActivitys.get(i);
                        if (receiver != null) {
                            Field ActivityInfoFld = receiver.getClass().getDeclaredField("info");
                            ActivityInfo activityInfo = (ActivityInfo) ActivityInfoFld.get(receiver);
                            if (activityInfo.name.equals("com.android.settings.flipfont.FontListProgressActivity"))
                                return true;
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }
        return false;
    }

    public static OSUtils getInstance() {
        return sOSUtils;
    }

    public String getMiuiVersion() {
        return miuiVersion;
    }

    public boolean isEmui() {
        return emui;
    }

    public boolean isMiui() {
        return miui;
    }

    public boolean isFlyme() {
        return flyme;
    }

    private boolean hasFlyme() {
        try {
            final Method method = Build.class.getMethod("hasSmartBar");
            return method != null;
        } catch (final Exception e) {
            return false;
        }
    }

    /**
     * FixBUG
     * Mephone: tony.amin on: 2017/2/9 17:05
     * TODO: replace this line with your comment
     *
     */
    public boolean isOppo() {
        return colorUi;
    }

    public boolean isMiui_8_1() {
        return isMiui_8_1;
    }

    public boolean isOppoRom_V3() {
        return isOppoRom_V3;
    }

    public boolean isVovi() {
        return funtouch;
    }

    public String getMiuiVersionName() {
        return miuiVersionName;
    }

    public static boolean isEMUI() {
        return getEmuiLeval() >0 ? true :false;
    }

    public static int getEmuiLeval() {
        // Finals 2016-6-14 如果获取过了就不用再获取了,因为读取配置文件很慢
        if (level > 0) {
            return level;
        }
        Properties properties = new Properties();
        File propFile = new File(Environment.getRootDirectory(), "build.prop");
        FileInputStream fis = null;
        if (propFile != null && propFile.exists()) {
            try {
                fis = new FileInputStream(propFile);
                properties.load(fis);
                fis.close();
                fis = null;
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (fis != null) {
                    try {
                        fis.close();
                    } catch (Exception e2) {
                        e2.printStackTrace();
                    }
                }
            }
        }
        if (properties.containsKey(KEY_EMUI_API_LEVEL_CODE)) {
            String valueString = properties.getProperty(KEY_EMUI_API_LEVEL_CODE);
            try {
                level = Integer.parseInt(valueString);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return level;
    }
    /********* End of Mephone: tony.amin ***********/

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值