0
点赞
收藏
分享

微信扫一扫

【JAVA】JAVA检测移动端IOS、Android工具类

elvinyang 2022-07-28 阅读 104


目录

​​1. 移动端工具类​​

​​2. 移动端枚举类​​

​​参考资料​​

1. 移动端工具类

/* *******************************************
// LICENSE INFORMATION
// The code, "Detecting Smartphones Using PHP"
// by Anthony Hand, is licensed under a Creative Commons
// Attribution 3.0 United States License.
//
// Updated 01 March 2010 by Bryan J Swift
// - Remove un-needed if statements instead just returning the boolean
// inside the if clause
//
// Updated 14 December 2009 by A Hand
// - Added the method for detecting BlackBerry Touch
// devices like the Storm 1 and 2.
//
// Updated 5 December 2009 by A Hand
// - Fixed the DetectPalmOS method. It should filter
// out WebOS devices.
//
// Updated 8 November 2009 by A Hand
// - Added the deviceWebOS variable.
// - Added Palm's WebOS to the DetectPalmOS method.
// - Created a new method to check for Palm's WebOS devices.
// - Added Palm's WebOS to the DetectTierIphone method.
//
// Updated 4 April 2009 by A Hand
// - Changed the name of the class from DetectSmartPhone to UAgentInfo.
// New name is more consistent with PHP and JavaScript classes.
// - Added a method to detect Opera Mobile and Mini. Opera Mobile is new.
// - Updated the algorithm for detecting a Sony Mylo.
// - Added Android to the DetectTierIphone method.
// - Updated comments for Detect Tier methods.
//
// Updated 22 January 2009
// - Ported to Java from the PHP code by
// Satish Kumar Nookala, javasatish@yahoo.com
//
// Anthony Hand, ahand@hand-interactive.com
// Web: www.hand-interactive.com
//
// License info: http://creativecommons.org/licenses/by/3.0/us/
//
// This code is provided AS IS with no expressed or implied warranty.
// You have the right to use this code or any portion of it
// so long as you provide credit toward myself as the original author.
//
// *******************************************
*/

import com.easylive.mgs.gateway.enums.MobilePlatform;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* The DetectSmartPhone class encapsulates information about
* a browser's connection to your web site.
* You can use it to find out whether the browser asking for
* your site's content is probably running on a mobile device.
* The methods were written so you can be as granular as you want.
* For example, enquiring whether it's as specific as an iPod Touch or
* as general as a smartphone class device.
* The object's methods return true, or false.
*/
public class UAgentInfo {

// OS Detection

private static final Pattern IOS_PATTERN = Pattern.compile("(.+) (.+) rv:([^(]+) \\((.+); .+os (.+)");
private static final Pattern ANDROID_PATTERN = Pattern.compile("; android (.+); (.+)\\); (.+) android v(.+)", Pattern.CASE_INSENSITIVE);
private static final Pattern ANDROID_WEB_PATTERN = Pattern.compile("; android (.+); (.+)\\).+;web (.+) android v(.+)", Pattern.CASE_INSENSITIVE);
private static final Logger logger = LoggerFactory.getLogger(UAgentInfo.class);
// User-Agent and Accept HTTP request headers
private String userAgent = "";
private String appVersion = "";
private String appName = "";
private String osVersion = "";
private String deviceModel = "";
private String buildType = "";
private String buildVersion = "";
private MobilePlatform deviceType = MobilePlatform.UNKNOWN;

/**
* Initialize the userAgent and httpAccept variables
*
* @param userAgent the User-Agent header
*/
public UAgentInfo(String userAgent) {
init(userAgent);
}

/**
* Initialize the userAgent and httpAccept variables by getting the headers
* from the HttpServletRequest
*
* @param request the HttpServletRequest to get the header information from
*/
public UAgentInfo(HttpServletRequest request) {
String uagent = request.getHeader("User-Agent");
String newua = null;
try {
newua = new String(uagent.getBytes("iso8859-1"), StandardCharsets.UTF_8);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}

init(newua);
}

private void init(String userAgent) {
if (userAgent != null) {
this.userAgent = userAgent.toLowerCase();
}
this.detection();

if( this.appVersion.length() > 0 ) {
String[] versions = this.appVersion.split("\\.");
if( versions.length > 3) {
this.appVersion = String.format("%s.%s.%s", versions[0],versions[1],versions[2]);
}
}
}

private void detection() {
Matcher androidMatcher = ANDROID_PATTERN.matcher(userAgent);
if (androidMatcher.find()) {
buildAndroidInfo(androidMatcher);
return;
}

Matcher iosMatcher = IOS_PATTERN.matcher(userAgent);
if (iosMatcher.find()) {
this.appName = iosMatcher.group(1);
this.appVersion = iosMatcher.group(2);
this.osVersion = iosMatcher.group(5);
this.deviceModel = iosMatcher.group(4);
this.buildVersion = iosMatcher.group(3);
String[] builds = this.buildVersion.split(" ");
if (builds.length == 2) {
this.buildType = builds[1];
this.buildVersion = builds[0];
}
this.deviceType = MobilePlatform.IOS;
return;
}

Matcher webAndroidMatcher = ANDROID_WEB_PATTERN.matcher(userAgent);
if( webAndroidMatcher.find() ) {
buildAndroidInfo(webAndroidMatcher);
} else {
this.appVersion = "0";
this.deviceType = MobilePlatform.UNKNOWN;
}
}

private void buildAndroidInfo(Matcher androidMatcher) {
this.deviceType = MobilePlatform.ANDROID;

this.appName = androidMatcher.group(3);
this.appVersion = androidMatcher.group(4);
this.osVersion = androidMatcher.group(1);
this.deviceModel = androidMatcher.group(2);

String[] builds = this.appVersion.split("-");
if (builds.length >= 2) {
if (builds[builds.length - 1].equals("mp")) {
String[] _subbuilds = this.appVersion.split("_");
if (_subbuilds.length == 2) {
String[] __subbuilds = _subbuilds[0].split(".");
this.buildType = _subbuilds[1].substring(0, _subbuilds[1].length() - 3);
if (__subbuilds.length >= 3) {
this.appVersion = __subbuilds[0] + "." + __subbuilds[1] + "." + __subbuilds[2];
if (__subbuilds.length >= 4) {
this.buildVersion = __subbuilds[3];
}
}

}
} else {
this.buildType = builds[1];
String[] __subbuilds = builds[0].split(".");
if (__subbuilds.length >= 3) {
this.appVersion = __subbuilds[0] + "." + __subbuilds[1] + "." + __subbuilds[2];
if (__subbuilds.length >= 4) {
this.buildVersion = __subbuilds[3];
}
}
}
}
}

public MobilePlatform getDeviceType() {
return this.deviceType;
}

public String getAppName() {
return this.appName;
}
public String getAppVersion() {
return this.appVersion;
}

public String getUserAgent() {
return userAgent;
}

public String getOsVersion() {
return osVersion;
}

public String getDeviceModel() {
return deviceModel;
}

public String getBuildType() {
return buildType;
}

public String getBuildVersion() {
return buildVersion;
}
}

2. 移动端枚举类

/**
* 客户端平台
*/
public enum MobilePlatform {

UNKNOWN("unknown"),
ANDROID("android"),
IOS("ios"),
PC("pc");

private final String name;

MobilePlatform(final String newValue) {
name = newValue;
}

public String getName() {
return name;
}

public static MobilePlatform fromName(String name) {
if( name == null )
return UNKNOWN;
name = name.toLowerCase();

if( IOS.getName().equals(name))
return IOS;
if( ANDROID.getName().equals(name))
return ANDROID;
if( PC.getName().equals(name))
return PC;
return UNKNOWN;
}
}

参考资料

​​Detecting Mobile Devices Using Java :: Hand Interactive Resources​​

举报

相关推荐

0 条评论