com.u2apple.rt.util.AndroidDeviceUtils.java Source code

Java tutorial

Introduction

Here is the source code for com.u2apple.rt.util.AndroidDeviceUtils.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.u2apple.rt.util;

import com.u2apple.rt.constant.Constants;
import com.u2apple.rt.model.AndroidDevice;
import com.u2apple.rt.model.AndroidDeviceRanking;
import com.u2apple.rt.tool.RecognitionTool;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.lang3.StringUtils;

/**
 *
 * @author Adam
 */
public final class AndroidDeviceUtils {

    private static final Properties brandProps;

    static {
        brandProps = new Properties();
        try {
            //init brands.
            InputStream is = RecognitionTool.class.getResourceAsStream(Constants.BRAND_CONF);
            brandProps.load(is);
        } catch (IOException ex) {
            Logger.getLogger(RecognitionTool.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private AndroidDeviceUtils() {

    }

    public static List<AndroidDeviceRanking> parse(List<AndroidDevice> devices) {
        List<AndroidDeviceRanking> androidDevices = new ArrayList<>();
        if (devices != null && !devices.isEmpty()) {
            for (AndroidDevice device : devices) {
                androidDevices.add(new AndroidDeviceRanking(device));
            }
        }
        return androidDevices;
    }

    public static String getBrandByProductId(String productId) {
        String brand = null;
        if (StringUtils.isNotBlank(productId)) {
            brand = StringUtils.substringBefore(productId, "-");
        }
        return brand;
    }

    public static String getProductName(String brand, String model) {
        if (StringUtils.isNotBlank(brand) && StringUtils.isNotBlank(model)) {
            String productName;
            if (StringUtils.containsIgnoreCase(model, brand)) {
                int index = model.toLowerCase().indexOf(brand.toLowerCase());
                productName = model.substring(index + brand.length());
            } else {
                productName = model;
            }
            //Remove prefix -.
            productName = StringUtils.removeStart(productName, "-");
            productName = StringUtils.removeStart(productName, "_");
            return productName.replace("_", "-").trim();
        } else {
            return null;
        }
    }

    private static String formatModel(String model) {
        return model == null ? null
                : model.replaceAll("-", "").replaceAll("\\s", "").replaceAll("_", "").replaceAll("\\(", "")
                        .replaceAll("\\)", "").trim();
    }

    public static String getProductId(String brand, String model) {
        String productId = null;
        if (StringUtils.isNotBlank(brand) && StringUtils.isNotBlank(model)) {
            brand = brand.toLowerCase();
            model = model.toLowerCase();
            String modelWithoutBrand;
            if (StringUtils.containsIgnoreCase(model, brand)) {
                modelWithoutBrand = StringUtils.substringAfter(model, brand);
            } else {
                modelWithoutBrand = model;
            }

            //?model.
            if ("samsung".equalsIgnoreCase(brand) && modelWithoutBrand.contains("-")) {
                int index = modelWithoutBrand.indexOf("-");
                //When "-" is the last char.
                if (index < modelWithoutBrand.length() - 1) {
                    modelWithoutBrand = modelWithoutBrand.substring(index + 1);
                }
            }

            String formattedModel = formatModel(modelWithoutBrand);
            //??
            if ("vivo".equalsIgnoreCase(brand)) {
                productId = "bbk-vivo" + formattedModel;
            } else {
                productId = brand + "-" + formattedModel;
            }
        }
        return productId;
    }

    public static String formatBrand(String brand) {
        String formattedBrand = brand;
        if (brand != null) {
            String humanBrand = brandProps.getProperty(brand.toLowerCase());
            if (humanBrand != null) {
                formattedBrand = humanBrand;
            }
        }
        return formattedBrand;
    }
}