Back to project page android_device.
The source code is released under:
[Apache License](http://www.apache.org/licenses/): Version 2.0, January 2004 =============== ## TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION ## ### 1. Definitions. ### "License" sha...
If you think the Android project android_device listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
/* * ================================================================================================= * Copyright (C) 2013 - 2014 Martin Albedinsky [Wolf-ITechnologies] * ================================================================================================= * Licensed under the Apache License, Version 2.0 or later (further "License" only). * ------------------------------------------------------------------------------------------------- * You may use this file only in compliance with the License. More details and copy of this License * you may obtain at/*w ww . java 2 s . c o m*/ * * http://www.apache.org/licenses/LICENSE-2.0 * * You can redistribute, modify or publish any part of the code written within this file but as it * is described in the License, the software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES or CONDITIONS OF ANY KIND. * * See the License for the specific language governing permissions and limitations under the License. * ================================================================================================= */ package com.wit.android.device; import android.app.Activity; import android.content.pm.ActivityInfo; import android.support.annotation.NonNull; import android.util.DisplayMetrics; import android.view.Display; import android.view.Surface; /** * <h3>Interface Overview</h3> * todo: description * * @author Martin Albedinsky */ public interface Screen { /** * Constants =================================================================================== */ /** * Enums ======================================================================================= */ /** * <h3>Enum Overview</h3> * Represents density of an Android device's screen * * @author Martin Albedinsky * @see #resolve(float) */ public enum ScreenDensity { /** * Indicates that the screen density is unknown due to some error or the current screen data * are unavailable. */ UNKNOWN(0, 0), /** * <b>Low</b> density per inch: * <ul> * <li>density: <b>120</b></li> * <li>scale ratio: <b>.75</b></li> * </ul> */ LDPI(120.0f, 0.75f), /** * <b>Medium</b> density per inch. * <ul> * <li>density: <b>160</b></li> * <li>scale ratio: <b>1</b></li> * </ul> */ MDPI(160.0f, 1f), /** * <b>Height</b> density per inch. * <ul> * <li>density: <b>240</b></li> * <li>scale ratio: <b>1.5</b></li> * </ul> */ HDPI(240f, 1.5f), /** * <b>Extra-Height</b> density per inch. * <ul> * <li>density: <b>320</b></li> * <li>scale ratio: <b>2</b></li> * </ul> */ XHDPI(320f, 2.00f), /** * <b>Extra-Extra-Eeight</b> density per inch. * <ul> * <li>density: <b>480</b></li> * <li>scale ratio: <b>3</b></li> * </ul> */ XXHDPI(480f, 3f), /** * <b>Extra-Extra-Extra-Height</b> density per inch. * <ul> * <li>density: <b>640</b></li> * <li>scale ratio: <b>4</b></li> * </ul> */ XXXHDPI(640f, 4f); /** * The value of this screen density in DPI units. */ public final float value; /** * The value of scale ratio specific for this screen density. */ public final float scaleRatio; /** */ private static final float DENSITY_CHECK_OFFSET = 10f; /** * Creates a new instance of ScreenDensity with the given value and scale ratio. * * @param value Value of density. * @param scaleRatio Scale ratio for density. */ private ScreenDensity(float value, float scaleRatio) { this.value = value; this.scaleRatio = scaleRatio; } /** * Resolves an instance of ScreenDensity according to the given <var>densityDpi</var>. * * @param densityDpi The value {@link #value} of the desired density to resolve. * @return Resolved screen density instance or {@link ScreenDensity#UNKNOWN} if there is no * screen density with the requested density value. */ @NonNull public static ScreenDensity resolve(float densityDpi) { for (ScreenDensity density : values()) { if ((densityDpi + DENSITY_CHECK_OFFSET) >= density.value && (densityDpi - DENSITY_CHECK_OFFSET) <= density.value) { return density; } } return UNKNOWN; } } /** * <h3>Enum Overview</h3> * Represents type of an Android device's screen. * * @author Martin Albedinsky * @see #resolve(float, float) */ public enum ScreenType { /** * Indicates that the screen type is unknown due to some error or the current screen data are * unavailable. */ UNKNOWN(-1, -1), /** * Type indicating <b>Small</b> screen. * <h3>Native dimensions (in DP):</h3> * <ul> * <li><b>Native width: </b> <i>320</i></li> * <li><b>Native height: </b> <i>426</i></li> * </ul> */ SMALL(320f, 426f), /** * Type indicating <b>Normal</b> screen. * <h3>Native dimensions (in DP):</h3> * <ul> * <li><b>Native width: </b> <i>320</i></li> * <li><b>Native height: </b> <i>470</i></li> * </ul> */ NORMAL(320f, 470f), /** * Type indicating <b>Large</b> screen. * <h3>Native dimensions (in DP):</h3> * <ul> * <li><b>Native width: </b> <i>480</i></li> * <li><b>Native height: </b> <i>640</i></li> * </ul> */ LARGE(480f, 640f), /** * Type indicating <b>X-Large</b> screen. * <h3>Native dimensions (in DP):</h3> * <ul> * <li><b>Native width: </b> <i>720</i></li> * <li><b>Native height: </b> <i>960</i></li> * </ul> */ XLARGE(720f, 960f); /** * Reversed array with all ScreenType values. */ private static final ScreenType[] REVERSED_VALUES = { XLARGE, LARGE, NORMAL, SMALL, UNKNOWN }; /** * Native dimension in DP units. */ public final float nativeWidthDp, nativeHeightDp; /** * Creates a new instance of ScreenType with the specified native dimensions. * * @param nativeWidthDp Native width specific for this screen type. * @param nativeHeightDp Native height specific for this screen type. */ private ScreenType(float nativeWidthDp, float nativeHeightDp) { this.nativeWidthDp = nativeWidthDp; this.nativeHeightDp = nativeHeightDp; } /** * Resolves an instance of ScreenType according to the given native screen dimensions from the * current set of ScreenType values. * * @param widthDp The value of native screen width, <b>no current width</b>. * @param heightDp The value of native screen height, <b>no current height</b>. * @return Resolved screen type instance or {@link ScreenType#UNKNOWN} if there is no screen * type with the given dimensions specified. */ @NonNull public static ScreenType resolve(float widthDp, float heightDp) { for (ScreenType type : REVERSED_VALUES) { if (widthDp >= type.nativeWidthDp && heightDp >= type.nativeHeightDp) { return type; } } return UNKNOWN; } } /** * <h3>Enum Overview</h3> * Represents orientation of an Android device's screen. * <h3>Default device orientation and rotation:</h3> * <ul> * <li><b>phone:</b> {@link Screen.ScreenOrientation#PORTRAIT PORTRAIT} : {@link Screen.ScreenRotation#ROTATION_0 ROTATION_0}</li> * <li><b>tablet:</b> {@link Screen.ScreenOrientation#LANDSCAPE LANDSCAPE} : {@link Screen.ScreenRotation#ROTATION_0 ROTATION_0}</li> * </ul> * * @author Martin Albedinsky * @see Screen.ScreenRotation * @see #resolve(int) */ public enum ScreenOrientation { /** * System constant: <b>{@link android.content.pm.ActivityInfo#SCREEN_ORIENTATION_UNSPECIFIED}</b> */ UNSPECIFIED(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED), /** * System constant: <b>{@link android.content.pm.ActivityInfo#SCREEN_ORIENTATION_PORTRAIT}</b> */ PORTRAIT(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT), /** * System constant: <b>{@link android.content.pm.ActivityInfo#SCREEN_ORIENTATION_REVERSE_PORTRAIT}</b> */ REVERSE_PORTRAIT(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT), /** * System constant: <b>{@link android.content.pm.ActivityInfo#SCREEN_ORIENTATION_LANDSCAPE}</b> */ LANDSCAPE(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE), /** * System constant: <b>{@link android.content.pm.ActivityInfo#SCREEN_ORIENTATION_REVERSE_LANDSCAPE}</b> */ REVERSE_LANDSCAPE(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE), /** * System constant: <b>{@link android.content.pm.ActivityInfo#SCREEN_ORIENTATION_USER}</b> */ USER(ActivityInfo.SCREEN_ORIENTATION_USER), /** * System constant: <b>{@link android.content.pm.ActivityInfo#SCREEN_ORIENTATION_SENSOR}</b> */ SENSOR(ActivityInfo.SCREEN_ORIENTATION_SENSOR), /** * System constant: <b>{@link android.content.pm.ActivityInfo#SCREEN_ORIENTATION_SENSOR_LANDSCAPE}</b> */ SENSOR_LANDSCAPE(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE), /** * System constant: <b>{@link android.content.pm.ActivityInfo#SCREEN_ORIENTATION_SENSOR_PORTRAIT}</b> */ SENSOR_PORTRAIT(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT), /** * System constant: <b>{@link android.content.pm.ActivityInfo#SCREEN_ORIENTATION_FULL_SENSOR}</b> */ FULL_SENSOR(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR), /** * System constant: <b>{@link android.content.pm.ActivityInfo#SCREEN_ORIENTATION_BEHIND}</b> */ BEHIND(ActivityInfo.SCREEN_ORIENTATION_BEHIND), /** * This is only for internal purpose. */ SQUARE(0), /** * Current screen orientation. This is only for internal purpose. */ CURRENT(0); /** * The flag provided by {@link android.content.pm.ActivityInfo} for this screen orientation. */ public final int systemConstant; /** * Creates a new instance of ScreenOrientation with the given ActivityInfo flag. * * @param flag Id of status as flag provided by {@link android.content.pm.ActivityInfo}. */ private ScreenOrientation(int flag) { this.systemConstant = flag; } /** * Resolves an instance of ScreenOrientation according to the given <var>systemConstant</var> from * the current set of ScreenOrientation values. * * @param systemConstant The id ({@link #systemConstant}) of the desired screen orientation * to resolve. * @return Resolved screen orientation instance or {@link ScreenOrientation#UNSPECIFIED} if * there is no screen orientation with the requested constant. */ @NonNull public static ScreenOrientation resolve(int systemConstant) { for (ScreenOrientation orientation : values()) { if (orientation.systemConstant == systemConstant) { return orientation; } } return UNSPECIFIED; } } /** * <h3>Enum Overview</h3> * Represents rotation of an Android device's screen. * <p> * <b>Note: </b>there is difference between tablet default rotation and phone default rotation. * * @author Martin Albedinsky * @see Screen.ScreenOrientation * @see #resolve(int) */ public enum ScreenRotation { /** * Indicates that the screen rotation is unknown due to some error or the current screen data * are unavailable. */ UNKNOWN(-1, -1), /** * Rotation indicating <b>default</b> screen rotation by <b>0 degrees</b>: * <ul> * <li><b>phone:</b> {@link Screen.ScreenOrientation#PORTRAIT PORTRAIT} orientation</li> * <li><b>tablet:</b> {@link Screen.ScreenOrientation#LANDSCAPE LANDSCAPE} orientation</li> * </ul> * System constant: <b>{@link android.view.Surface#ROTATION_0}</b> */ ROTATION_0(0, Surface.ROTATION_0), /** * Rotation indicating screen rotation by <b>90 degrees</b>: * <ul> * <li><b>phone:</b> {@link Screen.ScreenOrientation#LANDSCAPE LANDSCAPE} orientation</li> * <li><b>tablet:</b> {@link Screen.ScreenOrientation#REVERSE_PORTRAIT REVERSE_PORTRAIT} orientation</li> * </ul> * System constant: <b>{@link android.view.Surface#ROTATION_90}</b> */ ROTATION_90(90, Surface.ROTATION_90), /** * Rotation indicating screen rotation by <b>180 degrees</b>: * <p> * <ul> * <li><b>phone:</b> {@link Screen.ScreenOrientation#REVERSE_PORTRAIT REVERSE_PORTRAIT} orientation</li> * <li><b>tablet:</b> {@link Screen.ScreenOrientation#REVERSE_LANDSCAPE REVERSE_LANDSCAPE} orientation</li> * </ul> * System constant: <b>{@link android.view.Surface#ROTATION_180}</b> */ ROTATION_180(180, Surface.ROTATION_180), /** * Rotation indicating screen rotation by <b>270 degrees</b>: * <ul> * <li><b>phone:</b> {@link Screen.ScreenOrientation#REVERSE_LANDSCAPE REVERSE_LANDSCAPE} orientation</li> * <li><b>tablet:</b> {@link Screen.ScreenOrientation#PORTRAIT PORTRAIT} orientation</li> * </ul> * System constant: <b>{@link android.view.Surface#ROTATION_270}</b> */ ROTATION_270(270, Surface.ROTATION_270); /** * The value of degrees specific for this screen rotation. */ public final int degrees; /** * The flag provided by {@link android.view.Surface} for this screen rotation. */ public final int systemConstant; /** * Creates a new instance of ScreenRotation with the given value of degrees. * * @param degreesValue The value of degrees specific for this screen rotation. * @param flag Id of rotation as flag provided by {@link android.view.Surface}. */ private ScreenRotation(int degreesValue, int flag) { this.degrees = degreesValue; this.systemConstant = flag; } /** * Resolves an instance of ScreenRotation according to the given <var>systemConstant</var> from * the current set of ScreenRotation values. * * @param systemConstant The id ({@link #systemConstant}) of the desired screen rotation to * resolve. * @return Resolved screen rotation instance or {@link ScreenRotation#UNKNOWN} if there is * no screen rotation with the requested constant. */ @NonNull public static ScreenRotation resolve(int systemConstant) { for (ScreenRotation rotation : values()) { if (rotation.systemConstant == systemConstant) { return rotation; } } return UNKNOWN; } } /** * Methods ===================================================================================== */ /** * Checks whether this Android device's screen is currently on or off. * * @return {@code True} if screen is on, {@code false} if is off. */ public boolean isOn(); /** * Returns default display of this Android device. * * @return The default display obtained by {@link android.view.WindowManager#getDefaultDisplay()}. */ public Display getDisplay(); /** * Returns the "default" width of this Android device's screen. * * @return Screen width in pixels. */ public int getWidth(); /** * Returns the "default" height of this Android device's screen. * * @return Screen height in pixels. */ public int getHeight(); /** * Returns the current width of this Android device's screen, which depends on the current screen * orientation. * * @return Screen current width in pixels. */ public int getCurrentWidth(); /** * Returns the current height of this Android device's screen, which depends on the current screen * orientation. * * @return Screen current height in pixels. */ public int getCurrentHeight(); /** * Returns the current display metrics of this Android device's screen. * * @return Display metrics. */ @NonNull public DisplayMetrics getMetrics(); /** * Returns the density of this Android device's screen. * * @return One of {@link Screen.ScreenDensity} values or {@link Screen.ScreenDensity#UNKNOWN UNKNOWN} * if the current screen data are unavailable. * @see #getRawDensity() */ @NonNull public ScreenDensity getDensity(); /** * Returns the raw density of this Android device's screen obtained from the screen metrics. * * @return Value of raw density * @see #getDensity() */ public int getRawDensity(); /** * Returns the type of this Android device's screen. * * @return One of {@link Screen.ScreenType} values or {@link Screen.ScreenType#UNKNOWN UNKNOWN} * if the current screen data are unavailable. */ @NonNull public ScreenType getType(); /** * Returns the default orientation of this Android device's screen. * * @return {@link Screen.ScreenOrientation#PORTRAIT PORTRAIT} for <b>phone</b>, * {@link Screen.ScreenOrientation#LANDSCAPE LANDSCAPE} for <b>tablet</b> * or {@link Screen.ScreenOrientation#UNSPECIFIED UNSPECIFIED} if the current screen data are * unavailable or orientation is unknown. */ @NonNull public ScreenOrientation getDefaultOrientation(); /** * Returns the current orientation of this Android device's screen. * * @return One of {@link Screen.ScreenOrientation} values or {@link Screen.ScreenOrientation#UNSPECIFIED UNSPECIFIED} * if the current screen data are unavailable or orientation is unknown. */ @NonNull public ScreenOrientation getCurrentOrientation(); /** * Returns the current rotation of this Android device's screen. * * @return One of {@link Screen.ScreenRotation} values or {@link Screen.ScreenRotation#UNKNOWN UNKNOWN} * if the current screen data are unavailable or orientation is unknown. */ @NonNull public ScreenRotation getCurrentRotation(); /** * Returns the diagonal distance of this Android device's screen. * * @param inInches {@code True} to calculate distance in inches, {@code false} in pixels. * @return Diagonal distance in the desired units. */ public float getDiagonalDistance(boolean inInches); /** * Returns the back-light brightness of this Android device's screen for the current application * window from the range <b>[0, 100]</b>. * * @param activity Currently visible activity. * @return The value of brightness from the specified range or negative number if the current * application window doesn't have set custom brightness value or the given activity or its window * is invalid. */ public int getBrightness(@NonNull Activity activity); /** * Sets the back-light brightness of this Android device's screen. Brightness will be applied only * for the current application window, not for the whole device. * <p> * Note, that the platform will automatically take care of changing the brightness as the user moves * in and out of the application with custom brightness. * * @param brightness The value of brightness for the application window from the range <b>[0, 100]</b>. * @param activity Currently visible activity. * @throws IllegalArgumentException When the given brightness value is out of the specified range. */ public void setBrightness(int brightness, @NonNull Activity activity); /** * Returns the back-light brightness of this Android device's screen. * * @return The value of system brightness as an Android user set it in the settings. */ public int getSystemBrightness(); /** * Requests the specified orientation as orientation of this Android device's screen for the given * activity. * * @param requestedOrientation Orientation which you would like to set up. * @param activity Currently visible activity. * @return {@code True} if orientation was successfully requested, {@code false} otherwise. * @see #isOrientationLocked() * @see #lockOrientation(android.app.Activity) */ public boolean requestOrientation(@NonNull ScreenOrientation requestedOrientation, @NonNull Activity activity); /** * Locks orientation of this Android device's screen at the current orientation for the given * activity. * * @param activity Currently visible activity. * @return {@code True} if orientation was locked, {@code false} otherwise. * @see #isOrientationLocked() * @see #unlockOrientation(android.app.Activity) * @see #requestOrientation(Screen.ScreenOrientation, android.app.Activity) */ public boolean lockOrientation(@NonNull Activity activity); /** * Unlocks orientation of this Android device's screen for the given activity. This will actually * requests {@link android.content.pm.ActivityInfo#SCREEN_ORIENTATION_USER} orientation. * * @param activity Currently visible activity. * @see #isOrientationLocked() * @see #lockOrientation(android.app.Activity) */ public void unlockOrientation(@NonNull Activity activity); /** * Returns flag indicating whether orientation of this Android device's screen is currently locked * or not. <b>Note, that this flag is useful only while you are within activity which requests to * lock/unlock screen orientation.</b> * * @return {@code True} if screen orientation is currently locked by one of {@link #lockOrientation(Activity)} * or {@link #requestOrientation(ScreenOrientation, Activity)}, {@code false} otherwise. * @see #lockOrientation(android.app.Activity) * @see #requestOrientation(Screen.ScreenOrientation, android.app.Activity) */ public boolean isOrientationLocked(); /** * Transforms raw pixel to density pixel. Transformation will be performed according to density * of this Android device's screen. * * @param pixel The pixel value which you want to transform to density pixel value. * @return Density pixel value transformed from the given pixel value. * @see #dpToPixel(int) */ public float pixelToDP(int pixel); /** * Transforms density pixel to raw pixel. Transformation will be performed according to density * of this Android device's screen. * * @param dp The density pixel value which you want to transform to pixel raw value. * @return Pixel value transformed from the given density pixel value. * @see #pixelToDP(int) */ public float dpToPixel(int dp); /** * Returns the value of density pixel for 1 pixel for this Android device's screen. * This value depends on density of this Android device's screen. * * @return Density pixel value of screen. * @see #dpToPixel(int) * @see #pixelToDP(int) */ public float getScreenDP(); /** * Returns refresh rate of this Android device's screen. * * @return Refresh rate in frames per second. * @see android.view.Display#getRefreshRate() */ public float getRefreshRate(); }