每一个SAPUI5应用在初始化的时候都会触发这个方法的调用。调用栈的上下文:sap.ui.define(['sap/ui/Device'],function(Device){可以看到在Device.js文件中,有两行代码初始化了默认的RangeSet://始终初始化默认媒体范围集Device.media.initRangeSet();Device.media.initRangeSet(RANGESETS["SAP_STANDARD_EXTENDED"]);Device.media._predefinedRangeSets是SAPUI5官网介绍的预定义RangeSet:defaultRangeSet:默认RangeSet为SAP_STANDARD,如下图1041行所示:两点确定三种设备类型:三种查询及对应的设备类型:这里是通过windownativeAPImatchMedia计算出来的。注册RangeSet变化的事件监听函数:if(oQuery.media.addEventListener){oQuery.media.addEventListener("change",oConfig.listener);sap.ui.Device.orientation:sap.ui.Device.os:在这里设置oReducedNavigator:varsetDefaultNavigator=function(){oReducedNavigator={userAgent:window.navigator.userAgent,platform:window.navigator.platform};//仅在导航器具有此属性的情况下才添加属性standalone}};先用正则表达式尝试匹配iOS系统:rPlatform=/\(([a-zA-Z]+);\s(?:[U]?[;]?)([\D]+)((?:[\d._]*))(?:.*[\)][^\d]*)([\d.]*)\s/;aMatches=userAgent.match(rPlatform);如果(aMatches){varrAppleDevices=/iPhone|iPad|iPod/;如果匹配不成功,尝试匹配Android://FirefoxonAndroidrPlatform=/\((Android)[\s]?([\d][.\d]*)?;.*Firefox\/[\d][.\d]*/;a匹配s=userAgent.match(rPlatform);if(aMatches){return({"name":OS.ANDROID,"versionStr":aMatches.length==3?aMatches[2]:""});}最后匹配DesktopOS:从userAgent字段可以知道是Windows10,X64架构:最后在Device对象中设置,方便其他代码消费:OS大写,模拟其他编程语言sap.ui中的枚举类型.Device.resize保存当前窗口的宽高。SAPUI5应用初始化时,会执行setResizeInfo(Device.resize);这一步执行时,Device对象的详细信息如下:使用windowsSize获取当前窗口大小:其实就是使用的windows全局对象的innerHeight和innerWidth属性。支持Detect实现源码:vardetectTouch=function(){return!!(('ontouchstart'inwindow)||(window.navigator.maxTouchPoints>0)||(window.DocumentTouch&&documentinstanceofwindow.DocumentTouch)||(window.TouchEvent&&Device.browser.firefox));};Device.support.touch=detectTouch();
