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 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.
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.
A big thank you to Meetup for inspiration and the device list.
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.