Drusantia
3/21/2019 - 9:48 AM

ScreenTypeHelper

Helpers to get device original orientation, force lock the orientation to the device's original rotation - portrait for phone, small tablet, landscape for large tablet, glass or whatever the device says it's original orientation/rotation is.

import android.content.Context
import android.content.pm.ActivityInfo
import android.content.res.Configuration
import android.view.Surface
import android.view.WindowManager
import androidx.appcompat.app.AppCompatActivity

private const val TAG = "ScreenTypeHelper"

enum class ScreenOrientationType { PORTRAIT, PORTRAIT_REVERSE, LANDSCAPE, LANDSCAPE_REVERSE, UNKNOWN }

/** @return the default orientation of the device */
fun AppCompatActivity.getDeviceDefaultScreenOrientation(): ScreenOrientationType {
    with(applicationContext.getSystemService(Context.WINDOW_SERVICE) as WindowManager) {
        val config = resources.configuration
        val rotation = defaultDisplay.rotation

        return when {
            config?.orientation == Configuration.ORIENTATION_PORTRAIT
                    && rotation == Surface.ROTATION_0 -> ScreenOrientationType.PORTRAIT

            config?.orientation == Configuration.ORIENTATION_PORTRAIT
                    && rotation == Surface.ROTATION_180 -> ScreenOrientationType.PORTRAIT_REVERSE

            config?.orientation == Configuration.ORIENTATION_LANDSCAPE
                    && rotation == Surface.ROTATION_0 -> ScreenOrientationType.LANDSCAPE

            config?.orientation == Configuration.ORIENTATION_LANDSCAPE
                    && rotation == Surface.ROTATION_180 -> ScreenOrientationType.LANDSCAPE_REVERSE

            else -> ScreenOrientationType.UNKNOWN
        }
    }
}

/** Locks the screen orientation to the device's default orientation */
fun AppCompatActivity.forceDefaultScreenOrientation() {
    requestedOrientation = when (getDeviceDefaultScreenOrientation()) {
        ScreenOrientationType.PORTRAIT -> ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
        ScreenOrientationType.PORTRAIT_REVERSE -> ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT
        ScreenOrientationType.LANDSCAPE -> ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
        ScreenOrientationType.LANDSCAPE_REVERSE -> ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
        else -> {
            // Logging.Log(TAG, "Couldn't determine screen orientation. Forcing rotation to Portrait.")
            ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
        }
    }
}

fun AppCompatActivity.forcePortraitOrientationForPortraitDefaultDevice() {
    if (arrayOf(ScreenOrientationType.PORTRAIT, ScreenOrientationType.PORTRAIT_REVERSE).contains(getDeviceDefaultScreenOrientation())) {
        requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
    }
}
import android.content.Context
import android.content.pm.ActivityInfo
import android.content.res.Configuration
import android.support.v7.app.AppCompatActivity
import android.view.Surface
import android.view.WindowManager

enum class ScreenOrientationType { Portrait, PortraitReverse, Landscape, LandscapeReverse, Unknown }

fun getScreenOrientationType(activity: AppCompatActivity): ScreenOrientationType =
    with(activity.applicationContext?.getSystemService(Context.WINDOW_SERVICE) as WindowManager) {
        val config = activity.resources?.configuration
        val rotation = defaultDisplay.rotation

        return when {
            config?.orientation == Configuration.ORIENTATION_PORTRAIT
            && rotation == Surface.ROTATION_0 -> ScreenOrientationType.Portrait

            config?.orientation == Configuration.ORIENTATION_PORTRAIT
            && rotation == Surface.ROTATION_180 -> ScreenOrientationType.PortraitReverse

            config?.orientation == Configuration.ORIENTATION_LANDSCAPE
            && rotation == Surface.ROTATION_0 -> ScreenOrientationType.Landscape

            config?.orientation == Configuration.ORIENTATION_LANDSCAPE
            && rotation == Surface.ROTATION_180 -> ScreenOrientationType.LandscapeReverse

            else -> ScreenOrientationType.Unknown
        }
    }

fun forceDefaultScreenOrientation(activity: AppCompatActivity) =
    with(activity) {
        requestedOrientation = when(getScreenOrientationType(this)) {
            ScreenOrientationType.Portrait -> ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
            ScreenOrientationType.PortraitReverse -> ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT
            ScreenOrientationType.Landscape -> ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
            ScreenOrientationType.LandscapeReverse -> ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
            else -> {
                // Logging.Log(TAG, "Couldn't determine screen orientation. Forcing rotation to Portrait.")
                ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
            }
        }
    }
import android.content.Context
import android.content.pm.PackageManager

/** IMPORTANT: this exact string must be provided in the AndroidManifest.xml for metadata key. */
const val COPYRIGHT_FILE_NAME_METADATA_KEY = "copyrightTextFileName"

/** IMPORTANT: this string must match the preference xml file's name (without extension) */
const val PREFERENCES_RESOURCE_FILE_NAME = "preferences"

fun Context.getMetaData() = packageManager.getApplicationInfo(packageName, PackageManager.GET_META_DATA).metaData
fun Context.getRawResourceId(fileName: String) = resources.getIdentifier(fileName, "raw", packageName)
fun Context.getXmlResourceId(fileName: String) = resources.getIdentifier(fileName, "xml", packageName)
fun Context.getMetaDataValue(name: String): String = getMetaData()?.getString(name) ?: throw IllegalArgumentException("No metadata found with name \"$name\"")