Android常见ROM类型识别MD

android应用开发有时候需要对常见的定制ROM做区分(尤其是国内的手机,譬如MIUI,Flyme,EMUI等等),但苦于android没有通用的对ROM类型进行判断的api,因此在对定制ROM做适配优化的时候带来了困难。本文主要讲述的是如何实现对常见ROM的区分。

原理:在Android系统中有一个类似Windows系统注册表的文件build.prop。这个文件内定义了系统初始(或永久)的一些参数属性、功能的开放等。通过调整/增加参数可以达到较调系统性能偏重点和附加功能开启的作用。

常见build.prop参数表(以CM为例):

# begin build properties (开始设置系统性能)
    # autogenerated (通过设置形成系统信息)
    ro.=GRI40 (版本ID)
    ro.build.=GRJ22 (版本号)
    ro.build.version.incremental=eng.buildbot.20110619.060228 (版本增量)
    ro.build.version.sdk=10 (sdk版本)
    ro.build.version.codename=REL (版本代号)
    ro.build.version.release=2.3.4 (Android 2.3.4系统)
    ro.build.date=Sun Jun 19 06:02:58 UTC 2011 (制作者及制作时间)
    ro.build.date.utc=0
    ro.build.type=user (编译模式,如user,userdebug,eng,test模式)
    ro.build.user=buildbot (编译账户)
    ro.build.host=bb1 (编译主机系统)
    ro.build.tags=test-keys (编译标签)
        
    ro.product.model=HTC Wildfire (HTC内部手机代号)
    ro.product.brand=htc_wwe (手机品牌)
    ro.product.name=htc_buzz (手机正式名称)
    ro.product.device=buzz (采用的设备)
    ro.product.board=buzz (采用的处理器)
    ro.product.cpu.abi=armeabi-v6j (cpu的版本)
    ro.product.cpu.abi2=armeabi (cpu的品牌)
    ro.product.manufacturer=HTC (手机制造商)
    ro.product.locale.language=zh (手机默认语言)
    ro.product.locale.region=CN (地区语言)
    ro.wifi.channels= (WIFI连接的渠道)
    ro.board.platform=msm7k (主板平台)
    # ro.build.product is obsolete; use ro.product.device (旧代码ro.build.product,使用代码ro.product.device)
    ro.build.product=buzz (建立产品)
    # Do not try to parse ro.build.description or .fingerprint (不要试图修改description和fingerprint)
    ro.build.description=passion-user 2.3.3 GRI40 102588 release-keys (用户的KEY)
    ro.build.fingerprint=google/passion/passion:2.3.3/GRI40/102588:user/release-keys (系统指纹)
    # end build properties (性能代码完毕)
            #
            # system.prop for buzz (系统技术支持由BUZZ提供)
            #
            # Density in DPI of the LCD of this board. This is used to scale the UI (高密度的液晶的DPI板。这是用来大规模UI的)
            # appropriately. If this property is not defined, the default value is 160 dpi. (appropriately.如果这个属性没有定义,缺省值是160 dpi的分辨率)
    ro.sf.lcd_density=240 (显示屏分辨率,数值越大分辨率越底,240就是800*480的)
            
    # View configuration for QVGA. (屏幕的设置)
    view.fading_edge_length=8
    view.touch_slop=15 (触摸屏灵敏度,数值越大越灵敏)
    view.minimum_fling_velocity=25 (滑动速度)
    view.scroll_friction=0.008 (滑动误差)
            # RIL specific configuration. (特定设置)
    rild.libpath=/system/lib/libhtc_
    ro.ril.ecc.HTC-WWE=999
    ro.ril.ecc.HTC-ELL=92,93,94
    ro.ril.enable.a52.HTC-ITA=1
    ro.ril.enable.a53.HTC-ITA=1
    ro.ril.enable.a52=0
    ro.ril.enable.a53=1
    ro.ril.vmail.23415=1571,BT
    ro.ril.hsdpa.category=8 (hsdpa全称High Speed Downlink Packet Access中文意思:高速下行分组接入,设置的数越大传输越快)
    ro.ril.htcmaskw1.bitmask=429496
    ro.ril.htcmaskw1=14449
    ro.ril.def.agps.mode=2 (打开AGPS服务支持,可改为ro.ril.def.agps.mode=0 改后能省电但GPS定位速度会变慢)
    ro.ril.gprsclass=12 (GPRS设置)
            # For HSDPA low throughput (HSDPA低输量)
    ro.ril.disable.power.collapse=1 (关闭电源)
            # Modify MMS APN retry timer from 5s to 2s. (修改短信的APN设置5秒为2秒)
    ro.gsm.2nd_data_retry_config=max_retries=3, 2000, 2000, 2000
            # Time between scans in seconds. Keep it high to minimize battery drain.(扫描在几秒之内,可降低用电量)
            # This only affects the case in which there are remembered access points, (这个修改仅能影响此文件)
            # but none are in range.(但是没有一项是在范围内的)
    
    wifi.interface=eth0 (WIFI界面)
    wifi.supplicant_scan_interval=45 (WIFI扫描间隔时间,这里设置是45秒。把这个时间设置长点能省电)
            # Mobile data interfaces (移动数据的接口)
    mobiledata.interfaces=rmnet0,rmnet1,rmnet2
    
    # Allow or deny tethering. (允许和拒绝绑定)
    ro.tether.denied=false
            # Default network type. (默认的网络类型)
            # 0 => WCDMA Preferred. (0=WCDMA优先)
    ro.telephony.default_network=0
            # Enable Google-specific location features, (谷歌特定地点的设置)
            # like NetworkLocationProvider and LocationCollector.(如网络服务器提供商和服务器位置)
    ro.c o m.google.locationfeatures=1
            # The OpenGL ES API level that is natively supported by this device. (开放式绘图介面)
            # This is a 16.16 fixed point number. (界面有16个点,16个不动点数量)
    ro.opengles.version=65536 (开放式绘图介面参数)
            # Disable fs check on boot by default. (开机时默认禁用FS检查)
    sys.checkfs.fat=false
            # Performance settings. (性能设置)
    dalvik.vm.execution-mode=int:jit
    dalvik.vm.heapsize=24m (虚拟内存大小,可设置为16m或24m或32m或48m)
    persist.sys.use_dithering=1
    persist.sys.purgeable_assets=1
            # Increase SKIA decode memory capability for progressive jpg file.
            ro.media.dec.jpeg.memcap=20000000
            #
            # ADDITIONAL_BUILD_PROPERTIES (其他性能设置)
    no_require_sim=true (手机卡保护设置)
    ro.rommanager.developerid=cyanogenmodnightly (固件管理器开发者是CM大神)
    ro.url.legal=http://www./intl/%s/mobile/android/basic/phone-legal.html
    ro.url.legal.android_privacy=http://www]/intl/%s/mobile/android/basic/privacy.html
    ro.com.google.clientidbase=android-google (谷歌客户身份)
    ro.com.android.wifi-watchlist=GoogleGuest (WIFI用户名单)
    ro.setupwizard.enterprise_mode=1 (默认情景模式)
    ro.com.android.dateformat=MM-dd-yyyy (默认时间格式,改为yyyy-MM-dd,显示效果就是XXXX年XX月XX日)
    ro.com.android.dataroaming=false (漫游设置)
    ro.config.ringtone=Playa.ogg (默认铃声设置,文件在/system/media/audio/ringtones 把喜欢的铃声放这里,比如123. MP3放入ringtones文件夹中,这里代码改为ro.config.ringtone=123. mp3)
    ro.config.notification_sound=regulus.ogg (默认提示音,文件在/system/media/audio/notifications 修改方法同上)
    ro.config.alarm_alert=Alarm_Beep_03.ogg (默认闹铃,文件在/system/media/audio/alarms 修改方法同上)
    ro.modversion=CyanogenMod-7-06192011-NIGHTLY-buzz (版本信息,改这个能让你大名出现系统关于中,改为ro.modversion=xxxxx)
    ro.setupwizard.mode=OPTIONAL (安装向导模式)
    net.bt.name=Android (系统名称)
    dalvik.vm.stack-trace-file=/data/anr/traces.txt

其实这些配置参数是以键值对的方式存放,一般情况下厂商定制ROM的时候会在定制的ROM里面加入ROM特有的标识(当然不排除有些ROM在不同版本中使用不同的版本标识),我们可以根据这些ROM标识对常见的ROM进行识别。
关键代码如下:

public class BuildProperties {
    private static BuildProperties ourInstance;

    public static BuildProperties getInstance() throws IOException {
        if (ourInstance == null) ourInstance = new BuildProperties();
        return ourInstance;

    }

    private final Properties properties;

    private BuildProperties() throws IOException {
        properties = new Properties();
        properties.load(new FileInputStream(new File(Environment.getRootDirectory(), "build.prop")));
    }

    public boolean containsKey(final Object key) {
        return properties.containsKey(key);
    }

    public boolean containsValue(final Object value) {
        return properties.containsValue(value);
    }

    public String getProperty(final String name) {
        return properties.getProperty(name);
    }

    public String getProperty(final String name, final String defaultValue) {
        return properties.getProperty(name, defaultValue);
    }

    public Set> entrySet() {
        return properties.entrySet();
    }

    public boolean isEmpty() {
        return properties.isEmpty();
    }

    public Enumeration keys() {
        return properties.keys();
    }

    public Set keySet() {
        return properties.keySet();
    }

    public int size() {
        return properties.size();
    }

    public Collection values() {
        return properties.values();
    }
}

ROM识别工具类:

public class OSUtils {
    public enum ROM_TYPE {

        MIUI_ROM,

        FLYME_ROM,

        EMUI_ROM,

        OTHER_ROM

    }

    /**
     * MIUI ROM标识
     * 

* "ro.miui.ui.version.code" -> "5" *

* "ro.miui.ui.version.name" -> "V7" *

* "ro.miui.has_handy_mode_sf" -> "1" *

* "ro.miui.has_real_blur" -> "1" *

*

*

* Flyme ROM标识 *

* "ro.build.user" -> "flyme" *

* "persist.sys.use.flyme.icon" -> "true" *

* "ro.flyme.published" -> "true" *

* "ro.build.display.id" -> "Flyme OS 5.1.2.0U" *

* "ro.meizu.setupwizard.flyme" -> "true" *

*

*

* EMUI ROM标识 *

* "ro.build.version.emui" -> "EmotionUI_1.6" */ //MIUI标识 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"; //EMUI标识 private static final String KEY_EMUI_VERSION_CODE = "ro.build.version.emui"; //Flyme标识 private static final String KEY_FLYME_ID_FALG_KEY = "ro.build.display.id"; private static final String KEY_FLYME_ID_FALG_VALUE_KEYWORD = "Flyme"; private static final String KEY_FLYME_ICON_FALG = "persist.sys.use.flyme.icon"; private static final String KEY_FLYME_SETUP_FALG = "ro.meizu.setupwizard.flyme"; private static final String KEY_FLYME_PUBLISH_FALG = "ro.flyme.published"; /** * @param * @return ROM_TYPE ROM类型的枚举 * @datecreate at 2016/5/11 0011 9:46 * @mehtodgetRomType * @description获取ROM类型,MIUI_ROM, *FLYME_ROM, * EMUI_ROM, * OTHER_ROM */ public static ROM_TYPE getRomType() { ROM_TYPE rom_type = ROM_TYPE.OTHER_ROM; try { BuildProperties buildProperties = BuildProperties.getInstance(); if (buildProperties.containsKey(KEY_EMUI_VERSION_CODE)) { return ROM_TYPE.EMUI_ROM; } if (buildProperties.containsKey(KEY_MIUI_VERSION_CODE) || buildProperties.containsKey(KEY_MIUI_VERSION_NAME)) { return ROM_TYPE.MIUI_ROM; } if (buildProperties.containsKey(KEY_FLYME_ICON_FALG) || buildProperties.containsKey(KEY_FLYME_SETUP_FALG) || buildProperties.containsKey(KEY_FLYME_PUBLISH_FALG)) { return ROM_TYPE.FLYME_ROM; } if (buildProperties.containsKey(KEY_FLYME_ID_FALG_KEY)) { String romName = buildProperties.getProperty(KEY_FLYME_ID_FALG_KEY); if (!TextUtils.isEmpty(romName) && romName.contains(KEY_FLYME_ID_FALG_VALUE_KEYWORD)) { return ROM_TYPE.FLYME_ROM; } } } catch (IOException e) { e.printStackTrace(); } return rom_type; } }

使用方法:

OSUtils.ROM_TYPE romType = = OSUtils.getRomType();

可能您需要对其他的ROM进行区分,那么只需三步:
一:使用BuildProperties获取到所有的key,遍历获取到所有的value(getProperty),或者直接找到build.prop文件。
二:找到定制ROM特征的标识(key/value)
三:增加ROM_TYPE枚举类型,getRomType方法加入识别比对即可。

以上内容用到了网上的参考文章,如果侵犯了您的权益请联系我:email:[email protected]

OK,今天就到这里。以后会经常写文章。如果您有什么问题或好的建议请联系我!

你可能感兴趣的:(Android常见ROM类型识别MD)