Code Monkey home page Code Monkey logo

device-detector's Introduction

Device Detector

build release maven central license

The Universal Device Detection library that parses User Agents and detects devices (desktop, tablet, mobile, tv, cars, console, etc.), clients (browsers, feed readers, media players, PIMs, ...), operating systems, brands and models.

This project is a Java adaptation of Matomo Device Detector.

Usage

Using DeviceDetector with Maven is quite easy. Just add the following dependency in your project's pom.xml:

<dependency>
	<groupId>io.github.mngsk</groupId>
	<artifactId>device-detector</artifactId>
	<version>1.0.10</version>
</dependency>

And use some code like this one:

import io.github.mngsk.devicedetector.DeviceDetector;
import io.github.mngsk.devicedetector.DeviceDetector.DeviceDetectorBuilder;

DeviceDetector dd = new DeviceDetectorBuilder().build();

// Assuming `request` is an instance of HttServletRequest
String userAgent = request.getHeader("User-Agent");

Detection detection = dd.detect(userAgent);
System.out.println(detection.getDevice().map(d -> d.toString()).orElse("unknown"));
System.out.println(detection.getOperatingSystem().map(d -> d.toString()).orElse("unknown"));
System.out.println(detection.getClient().map(d -> d.toString()).orElse("unknown"));

if (detection.getDevice().isPresent()) {
	System.out.println(detection.getDevice().get().getType()); // bot, browser, feed reader...
	System.out.println(detection.getDevice().get().getBrand().orElse("unknown"));
	System.out.println(detection.getDevice().get().getModel().orElse("unknown"));
}

Instead of using the full power of DeviceDetector it might in some cases be better to use only specific parsers. If you aim to check if a given user agent is a bot and don't require any of the other information, you can enable only the bot parser.

import io.github.mngsk.devicedetector.DeviceDetector;
import io.github.mngsk.devicedetector.DeviceDetector.DeviceDetectorBuilder;

DeviceDetector dd = new DeviceDetectorBuilder().disableEverything().enableBots().build();

Detection detection = dd.detect(userAgent);

if (detection.isBot()) {
	System.out.println(detection.getClient().get().getName().get());
}

Caching

Contrary to the original PHP implementation, this project does not include any caching mechanism. Consider using an specialized tool for that purpose.

Contributing

Please note that we use the fixture files directly from the original PHP project; if you want to contribute or fix a regex, please consider making a pull request on that project, so that all derivative projects -including this one- can benefit from it.

On the other hand, contributions of java code are very welcome.

Tests

This project uses the YAML fixture files of the original PHP version. To run the tests, execute mvn test from the project's root directory.

device-detector's People

Contributors

dependabot[bot] avatar mngsk avatar nxtdev01 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

device-detector's Issues

Dependency to org.apache.commons.lang3.StringUtils

Hi!

The dependency to org.apache.commons.lang3.StringUtils is not specified in the pom.xml file.
It is used in the AbstractParser last line of code:

  protected String buildVersion(String version, Matcher matcher) {
    version = buildByMatch(version, matcher);
    if (version == null) {
      return null;
    }

    version = version.replace("_", ".");
    return StringUtils.strip(version, " .");
  }

All the best!

Unable Capture platform deatils using User Agent in Spring Boot for in Some Mobile Device

I'm facing an issue with capturing device details for Xiaomi phones using the user agent.

using the following user agent:
Mozilla/5.0 (Linux; Android 12; 2109119DI) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Mobile Safari/537.36

I have followed the following link: https://github.com/mngsk/device-detector

My code is like:

   `` DeviceDetector dd = new DeviceDetectorBuilder().build();
    Detection detection = dd.detect(userAgent);
    System.out.println(detection.getDevice());``

in this I'm getting null value for the device, the model,brand

{ "model": null, "type": "smartphone", "brand": null }

Expected Output is:
{ id: "XI", type: "smartphone", brand: "Xiaomi", model: "Mi 11 Lite 5G NE" }

please help me to solve this issue

using Java Version: JDK11

performance

Hello, after using it, I found that each Java parsing takes about 20ms, but PHP parsing can be completed within about 5ms, Java performance is too poor, I wonder if you have noticed the performance problem, looking forward to your recovery, thank you

last version

The latest version of Java does not seem to be very accurate, I wonder if it has not been updated.But the latest version of PHP is accurate. Could you update Java as well? Thank you

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.