com.mruddy.devdataviewer.DevDataListFragment.java Source code

Java tutorial

Introduction

Here is the source code for com.mruddy.devdataviewer.DevDataListFragment.java

Source

// The MIT License (MIT)
//
// Copyright (c) 2014 mruddy
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package com.mruddy.devdataviewer;

import java.util.LinkedList;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.res.Configuration;
import android.os.Build;
import android.support.v4.app.ListFragment;
import android.util.DisplayMetrics;
import android.util.SparseArray;
import android.view.Display;
import android.view.Surface;
import android.view.View;
import android.widget.ArrayAdapter;

public class DevDataListFragment extends ListFragment {
    private static final SparseArray<String> DENSITY_BUCKETS = new SparseArray<String>(6);
    private static final SparseArray<String> ROTATION = new SparseArray<String>(4);
    private static final SparseArray<String> ORIENTATION = new SparseArray<String>(3);
    private static final SparseArray<String> SCREEN_SIZE_BUCKETS = new SparseArray<String>(5);
    static {
        DevDataListFragment.initMaps();
    }

    @SuppressLint("InlinedApi")
    private static void initMaps() {
        DevDataListFragment.DENSITY_BUCKETS.append(DisplayMetrics.DENSITY_LOW, "LDPI");
        DevDataListFragment.DENSITY_BUCKETS.append(DisplayMetrics.DENSITY_MEDIUM, "MDPI");
        DevDataListFragment.DENSITY_BUCKETS.append(DisplayMetrics.DENSITY_HIGH, "HDPI");
        DevDataListFragment.DENSITY_BUCKETS.append(DisplayMetrics.DENSITY_XHIGH, "XHDPI");
        DevDataListFragment.DENSITY_BUCKETS.append(DisplayMetrics.DENSITY_XXHIGH, "XXHDPI");
        DevDataListFragment.DENSITY_BUCKETS.append(DisplayMetrics.DENSITY_XXXHIGH, "XXXHDPI");
        DevDataListFragment.ROTATION.append(Surface.ROTATION_0, "0");
        DevDataListFragment.ROTATION.append(Surface.ROTATION_90, "90");
        DevDataListFragment.ROTATION.append(Surface.ROTATION_180, "180");
        DevDataListFragment.ROTATION.append(Surface.ROTATION_270, "270");
        DevDataListFragment.ORIENTATION.append(Configuration.ORIENTATION_UNDEFINED, "undefined");
        DevDataListFragment.ORIENTATION.append(Configuration.ORIENTATION_PORTRAIT, "portrait");
        DevDataListFragment.ORIENTATION.append(Configuration.ORIENTATION_LANDSCAPE, "landscape");
        DevDataListFragment.SCREEN_SIZE_BUCKETS.append(Configuration.SCREENLAYOUT_SIZE_UNDEFINED, "undefined");
        DevDataListFragment.SCREEN_SIZE_BUCKETS.append(Configuration.SCREENLAYOUT_SIZE_SMALL, "small");
        DevDataListFragment.SCREEN_SIZE_BUCKETS.append(Configuration.SCREENLAYOUT_SIZE_NORMAL, "normal");
        DevDataListFragment.SCREEN_SIZE_BUCKETS.append(Configuration.SCREENLAYOUT_SIZE_LARGE, "large");
        DevDataListFragment.SCREEN_SIZE_BUCKETS.append(Configuration.SCREENLAYOUT_SIZE_XLARGE, "xlarge");
    }

    /**
     * @return
     */
    @SuppressLint("NewApi")
    private List<String> getDevDataList() {
        final List<String> result = new LinkedList<String>();
        final Display display = this.getActivity().getWindowManager().getDefaultDisplay();
        final DisplayMetrics appAreaDisplayMetrics = new DisplayMetrics();
        display.getMetrics(appAreaDisplayMetrics);
        result.add("API Level: " + Build.VERSION.SDK_INT);
        result.add("Android Release: " + Build.VERSION.RELEASE);
        if (Build.VERSION.SDK_INT >= 17) {
            final DisplayMetrics fullDisplayMetrics = new DisplayMetrics();
            display.getRealMetrics(fullDisplayMetrics);
            result.add("Full Width (px): " + fullDisplayMetrics.widthPixels);
            result.add("Full Height (px): " + fullDisplayMetrics.heightPixels);
            result.add("Full Width (dp): " + ((int) (fullDisplayMetrics.widthPixels / fullDisplayMetrics.density)));
            result.add(
                    "Full Height (dp): " + ((int) (fullDisplayMetrics.heightPixels / fullDisplayMetrics.density)));
        }
        result.add("App Area Width (px): " + appAreaDisplayMetrics.widthPixels);
        result.add("App Area Height (px): " + appAreaDisplayMetrics.heightPixels);
        result.add("App Area Width (dp): "
                + ((int) (appAreaDisplayMetrics.widthPixels / appAreaDisplayMetrics.density)));
        result.add("App Area Height (dp): "
                + ((int) (appAreaDisplayMetrics.heightPixels / appAreaDisplayMetrics.density)));
        result.add("dp Scaling Factor: " + appAreaDisplayMetrics.density);
        final String densityBucket = DevDataListFragment.DENSITY_BUCKETS.get(appAreaDisplayMetrics.densityDpi);
        result.add("Density Bucket: " + (null != densityBucket ? densityBucket : "other") + " ("
                + appAreaDisplayMetrics.densityDpi + ")");
        result.add("Actual xdpi: " + appAreaDisplayMetrics.xdpi);
        result.add("Actual ydpi: " + appAreaDisplayMetrics.ydpi);
        if (Build.VERSION.SDK_INT >= 8) {
            final String rotation = DevDataListFragment.ROTATION.get(display.getRotation());
            result.add("Rotation: " + (null != rotation ? rotation : "other (" + display.getRotation() + ")"));
        }
        final String orientation = DevDataListFragment.ORIENTATION
                .get(this.getResources().getConfiguration().orientation);
        result.add("Orientation: " + (null != orientation ? orientation
                : "other (" + this.getResources().getConfiguration().orientation + ")"));
        final int screenSizeKey = this.getResources().getConfiguration().screenLayout
                & Configuration.SCREENLAYOUT_SIZE_MASK;
        final String screenSizeBucket = DevDataListFragment.SCREEN_SIZE_BUCKETS.get(screenSizeKey);
        result.add(
                "Size Bucket: " + (null != screenSizeBucket ? screenSizeBucket : "other (" + screenSizeKey + ")"));
        return result;
    }

    /**
     * @see android.support.v4.app.Fragment#onResume()
     */
    @Override
    public void onResume() {
        super.onResume();
        this.setListAdapter(new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_list_item_1,
                this.getDevDataList()));
        this.getListView().setScrollbarFadingEnabled(false);
        this.getListView().setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
    }
}