由于项目需求,简化移动App安装,需要在Server端判别客户端的类型:普通PC,Android,IOS,Tablet.
核心原理:解析Request对象,获取“User-Agent”字段对应的值
private static final String[] KNOWN_MOBILE_USER_AGENT_PREFIXES = new String[] {
"w3c ", "w3c-", "acs-", "alav", "alca", "amoi", "audi", "avan", "benq",
"bird", "blac", "blaz", "brew", "cell", "cldc", "cmd-", "dang", "doco",
"eric", "hipt", "htc_", "inno", "ipaq", "ipod", "jigs", "kddi", "keji",
"leno", "lg-c", "lg-d", "lg-g", "lge-", "lg/u", "maui", "maxo", "midp",
"mits", "mmef", "mobi", "mot-", "moto", "mwbp", "nec-", "newt", "noki",
"palm", "pana", "pant", "phil", "play", "port", "prox", "qwap", "sage",
"sams", "sany", "sch-", "sec-", "send", "seri", "sgh-", "shar", "sie-",
"siem", "smal", "smar", "sony", "sph-", "symb", "t-mo", "teli", "tim-",
"tosh", "tsm-", "upg1", "upsi", "vk-v", "voda", "wap-", "wapa", "wapi",
"wapp", "wapr", "webc", "winw", "winw", "xda ", "xda-" };
private static final String[] KNOWN_MOBILE_USER_AGENT_KEYWORDS = new String[] {
"blackberry", "webos", "ipod", "lge vx", "midp", "maemo", "mmp", "mobile",
"netfront", "hiptop", "nintendo DS", "novarra", "openweb", "opera mobi",
"opera mini", "palm", "psp", "phone", "smartphone", "symbian", "up.browser",
"up.link", "wap", "windows ce" };
private static final String[] KNOWN_TABLET_USER_AGENT_KEYWORDS = new String[] {
"ipad", "playbook", "hp-tablet", "kindle" };
public Device resolveDevice(HttpServletRequest request) {
String userAgent = request.getHeader("User-Agent");
// UserAgent keyword detection of Normal devices
if (userAgent != null) {
userAgent = userAgent.toLowerCase();
for (String keyword : normalUserAgentKeywords) {
if (userAgent.contains(keyword)) {
return resolveFallback(request);
}
}
}
// UserAgent keyword detection of Tablet devices
if (userAgent != null) {
userAgent = userAgent.toLowerCase();
// Android special case
if (userAgent.contains("android") && !userAgent.contains("mobile")) {
return resolveWithPlatform(DeviceType.TABLET, DevicePlatform.ANDROID);
}
// Apple special case
if (userAgent.contains("ipad")) {
return resolveWithPlatform(DeviceType.TABLET, DevicePlatform.IOS);
}
// Kindle Fire special case
if (userAgent.contains("silk") && !userAgent.contains("mobile")) {
return resolveWithPlatform(DeviceType.TABLET, DevicePlatform.UNKNOWN);
}
for (String keyword : tabletUserAgentKeywords) {
if (userAgent.contains(keyword)) {
return resolveWithPlatform(DeviceType.TABLET, DevicePlatform.UNKNOWN);
}
}
}
// UAProf detection
if (request.getHeader("x-wap-profile") != null || request.getHeader("Profile") != null) {
if (userAgent != null) {
// Android special case
if (userAgent.contains("android")) {
return resolveWithPlatform(DeviceType.MOBILE, DevicePlatform.ANDROID);
}
// Apple special case
if (userAgent.contains("iphone") || userAgent.contains("ipod") || userAgent.contains("ipad")) {
return resolveWithPlatform(DeviceType.MOBILE, DevicePlatform.IOS);
}
}
return resolveWithPlatform(DeviceType.MOBILE, DevicePlatform.UNKNOWN);
}
// User-Agent prefix detection
if (userAgent != null && userAgent.length() >= 4) {
String prefix = userAgent.substring(0, 4).toLowerCase();
if (mobileUserAgentPrefixes.contains(prefix)) {
return resolveWithPlatform(DeviceType.MOBILE, DevicePlatform.UNKNOWN);
}
}
// Accept-header based detection
String accept = request.getHeader("Accept");
if (accept != null && accept.contains("wap")) {
return resolveWithPlatform(DeviceType.MOBILE, DevicePlatform.UNKNOWN);
}
// UserAgent keyword detection for Mobile devices
if (userAgent != null) {
// Android special case
if (userAgent.contains("android")) {
return resolveWithPlatform(DeviceType.MOBILE, DevicePlatform.ANDROID);
}
// Apple special case
if (userAgent.contains("iphone") || userAgent.contains("ipod") || userAgent.contains("ipad")) {
return resolveWithPlatform(DeviceType.MOBILE, DevicePlatform.IOS);
}
for (String keyword : mobileUserAgentKeywords) {
if (userAgent.contains(keyword)) {
return resolveWithPlatform(DeviceType.MOBILE, DevicePlatform.UNKNOWN);
}
}
}
// OperaMini special case
@SuppressWarnings("rawtypes")
Enumeration headers = request.getHeaderNames();
while (headers.hasMoreElements()) {
String header = (String) headers.nextElement();
if (header.contains("OperaMini")) {
/*return LiteDevice.MOBILE_INSTANCE;*/
return resolveWithPlatform(DeviceType.MOBILE, DevicePlatform.UNKNOWN);
}
}
return resolveFallback(request);
}