Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ interface DeviceInformation {

fun getStorageInformation(): StorageInformation

fun getDisplayInformation(): DisplayInformation
fun getDisplayInformation(context: Context): DisplayInformation

fun getSensorsInformation(): SensorsInformation

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import androidx.annotation.WorkerThread
import com.udfsoft.androidinfo.lib.command.GetOSInformationCommand
import com.udfsoft.androidinfo.lib.command.GetRAMInformationCommand
import com.udfsoft.androidinfo.lib.command.cpu.GetCpuInformationCommand
import com.udfsoft.androidinfo.lib.command.display.GetDisplayInformationCommand
import com.udfsoft.androidinfo.lib.command.sim.GetSIMCardInformationCommand
import com.udfsoft.androidinfo.lib.entity.*

Expand All @@ -23,11 +24,7 @@ object DeviceInformationFactory : DeviceInformation {
}

@RequiresPermission(
allOf = [
Manifest.permission.READ_PHONE_STATE,
Manifest.permission.READ_SMS,
"android.permission.READ_PHONE_NUMBERS"
]
allOf = [Manifest.permission.READ_PHONE_STATE, Manifest.permission.READ_SMS, "android.permission.READ_PHONE_NUMBERS"]
)
override fun getSIMCardInformation(context: Context): SIMCardInformation {
val getSIMCardInformationCommand = GetSIMCardInformationCommand(context)
Expand Down Expand Up @@ -65,8 +62,9 @@ object DeviceInformationFactory : DeviceInformation {
TODO("Not yet implemented")
}

override fun getDisplayInformation(): DisplayInformation {
TODO("Not yet implemented")
override fun getDisplayInformation(context: Context): DisplayInformation {
val getDisplayInformationCommand = GetDisplayInformationCommand(context)
return getDisplayInformationCommand(Unit)
}

override fun getSensorsInformation(): SensorsInformation {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package com.udfsoft.androidinfo.lib.command.display

import android.content.Context
import android.content.res.Configuration
import android.graphics.Rect
import android.os.Build
import android.util.DisplayMetrics
import android.util.Size
import android.view.Display
import android.view.WindowInsets
import android.view.WindowManager
import androidx.annotation.RequiresApi
import com.udfsoft.androidinfo.lib.command.CommandInterface
import com.udfsoft.androidinfo.lib.entity.DisplayInformation

class GetDisplayInformationCommand(val context: Context) :
CommandInterface<Unit, DisplayInformation> {

override fun invoke(param: Unit): DisplayInformation {
val windowManager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager

val display = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
context.display
} else {
windowManager.defaultDisplay
}

val displayId = display?.displayId
val appVsyncOffsetNanos = display?.appVsyncOffsetNanos
val displayName = display?.name
val rotation = display?.rotation

val isHdr = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
display?.isHdr
} else {
false
}

val screenRefreshRate = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
display?.mode?.refreshRate
} else {
null
}

val screenSize = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
getScreenSize(windowManager)
} else {
getScreenSizeOld(display)
}

val screenSizeCategory = getScreenSizeCategory()

return DisplayInformation(
screenSize,
screenSizeCategory,
rotation,
isHdr,
screenRefreshRate,
displayId,
displayName,
appVsyncOffsetNanos
)
}


private fun getScreenSizeOld(display: Display?): Size {
val metrics = DisplayMetrics()
display?.getMetrics(metrics)
val width = metrics.widthPixels
val height = metrics.heightPixels
return Size(width, height)
}

@RequiresApi(Build.VERSION_CODES.R)
private fun getScreenSize(windowManager: WindowManager): Size {
val metrics = windowManager.currentWindowMetrics
val windowInsets: WindowInsets = metrics.windowInsets
val insets = windowInsets.getInsetsIgnoringVisibility(
WindowInsets.Type.navigationBars() or WindowInsets.Type.displayCutout()
)

val insetsWidth: Int = insets.right + insets.left
val insetsHeight: Int = insets.top + insets.bottom

val bounds: Rect = metrics.bounds
return Size(
bounds.width() - insetsWidth, bounds.height() - insetsHeight
)
}

private fun getScreenSizeCategory(): String {
val screenSize =
context.resources.configuration.screenLayout and Configuration.SCREENLAYOUT_SIZE_MASK
return when (screenSize) {
Configuration.SCREENLAYOUT_SIZE_LARGE -> "Large screen"
Configuration.SCREENLAYOUT_SIZE_NORMAL -> "Normal screen"
Configuration.SCREENLAYOUT_SIZE_SMALL -> "Small screen"
else -> "Screen size is neither large, normal or small"
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
package com.udfsoft.androidinfo.lib.entity

class DisplayInformation {
import android.util.Size

}
data class DisplayInformation(
val screenSize: Size,
val screenSizeCategory: String,
val rotation: Int?,
val isHdr: Boolean?,
val screenRefreshRate: Float?,
val displayId: Int?,
val displayName: String?,
val appVsyncOffsetNanos: Long?
)
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ class MainFragment : Fragment(R.layout.fragment_main) {
viewModel.getSIMCardInformationLiveData().observe(viewLifecycleOwner) {
Log.d(TAG, it.toString())
}

viewModel.getDisplayInformationLiveData().observe(viewLifecycleOwner) {
Log.d(TAG, it.toString())
}
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class MainViewModel : ViewModel() {

private val simCardInformationLiveData = MutableLiveData<SIMCardInformation>()

private val displayInformationLiveData = MutableLiveData<DisplayInformation>()

@RequiresPermission(
allOf = [Manifest.permission.READ_PHONE_STATE, Manifest.permission.READ_SMS, "android.permission.READ_PHONE_NUMBERS"]
)
Expand All @@ -33,6 +35,7 @@ class MainViewModel : ViewModel() {
osInformationLiveData.postValue(DeviceInformationFactory.getOSInformation())
cpuInformationLiveData.postValue(DeviceInformationFactory.getCPUInformation())
simCardInformationLiveData.postValue(DeviceInformationFactory.getSIMCardInformation(context))
displayInformationLiveData.postValue(DeviceInformationFactory.getDisplayInformation(context))
}

fun getGeneralInformationLiveData() = generalInformationLiveData.toLiveData()
Expand All @@ -44,4 +47,6 @@ class MainViewModel : ViewModel() {
fun getCPUInformationLiveData() = cpuInformationLiveData.toLiveData()

fun getSIMCardInformationLiveData() = simCardInformationLiveData.toLiveData()

fun getDisplayInformationLiveData() = displayInformationLiveData.toLiveData()
}