Code Monkey home page Code Monkey logo

androiddevicenames's Introduction

Android Device Names Build Status

Android Device Names is a 14 kB library that transforms the device model name into something people can understand. For example, a useless SM-N910W8 becomes Samsung Galaxy Note 4. Here's how to use it:

// returns "Samsung Galaxy Note 4"
DeviceNames.getDeviceName("SM-N910W8", "Unknown Device");

To get the name of the device you're currently running on, use

DeviceNames.getCurrentDeviceName("Unknown Device");

Currently, the library recognises about 400 devices. In case the device model is not in the list, a fallback is returned:

// returns "Unknown Device"
DeviceNames.getDeviceName("unknown_device_model", "Unknown Device");

Download

Download a jar file, get it via Gradle:

compile 'com.github.tslamic.adn:library:1.0'

Maven:

<dependency>
    <groupId>com.github.tslamic.adn</groupId>
    <artifactId>library</artifactId>
    <version>1.0</version>
</dependency>

or just copy-paste the DeviceNames class into your project.

How does it work?

The DeviceNames class is generated with a Python script. getDeviceName uses if-elif-else statements to determine the device name. Here's an excerpt:

public static String getDeviceName(String model, String fallback) {
    if (android.text.TextUtils.isEmpty(model)) {
        return fallback;
    }
    final char c = Character.toUpperCase(model.charAt(0));
    switch (c) {
        // before stuff
        case 'B':
            if ("bq_Aquaris_5".equals(model)) {
                return "bq Aquaris 5";
            } else if ("bq_Aquaris_5_HD".equals(model)) {
                return "bq Aquaris 5 HD";
            }
            break;
        case 'E':
            if ("EVO".equals(model)) {
                return "HTC Evo";
            }
            break;
        // after stuff
    }
    return fallback;
}

There's no memory overhead and performance is great. With over 400 device names, a battered Samsung Galaxy S3 handles the worst case scenario, according to Traceview, in less than 5 ms.

Acknowledgements

A big thank you to Meetup for inspiration and the device list.

License

Copyright 2015 tslamic

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

androiddevicenames's People

Contributors

tslamic avatar

Watchers

 avatar

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.