Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import android.content.Context;

import android.content.res.Configuration;

import android.os.Build;
import android.util.DisplayMetrics;

import java.util.LinkedHashMap;

import java.util.Map;

public class Main {
    public static String getScreenInfos(Context context) {
        Map<String, String> infos = new LinkedHashMap<>();

        DisplayMetrics dm = context.getResources().getDisplayMetrics();
        int densityDpi = dm.densityDpi;
        float density = dm.density;
        int height = dm.heightPixels;
        int width = dm.widthPixels;

        infos.put("Screen", String.valueOf(width + " x " + height));
        infos.put("Density / DPI", String.valueOf(densityDpi) + " / " + getHumanReadableDpi(density));
        infos.put("Current orientation", getHumanReadableOrientation(context));
        infos.put("Display", Build.DISPLAY);

        StringBuilder sb = new StringBuilder();

        sb.append("<table class=\"table table-striped\">");
        sb.append("<tbody>");

        for (Map.Entry<String, String> deviceInfo : infos.entrySet()) {
            sb.append("<tr><th>");
            sb.append(deviceInfo.getKey());
            sb.append("</th><td>");
            sb.append(deviceInfo.getValue());
            sb.append("</td></tr>");
        }
        sb.append("<tbody>");
        sb.append("</table>");

        return sb.toString();
    }

    private static String getHumanReadableDpi(float density) {
        String densityName = "unknown";

        if (density == 0.75) {
            densityName = "ldpi";
        } else if (density == 1.0) {
            densityName = "mdpi";
        } else if (density == 1.5) {
            densityName = "hdpi";
        } else if (density == 2.0) {
            densityName = "xhdpi";
        } else if (density == 3.0) {
            densityName = "xxhdpi";
        } else if (density == 4.0) {
            densityName = "xxxhdpi";
        } else if (density < 0.75) {
            densityName = "lower then ldpi";
        } else if (density > 4.0) {
            densityName = "higher then xxxhdpi";
        }

        return densityName;
    }

    private static String getHumanReadableOrientation(Context context) {
        switch (context.getResources().getConfiguration().orientation) {
        case Configuration.ORIENTATION_LANDSCAPE:
            return "Landscape";
        case Configuration.ORIENTATION_PORTRAIT:
            return "Portrait";
        default:
            return "unknown";
        }
    }
}