Skip to content

Commit

Permalink
Correctly scale to full screen and first step towards custom scaling (#…
Browse files Browse the repository at this point in the history
…1442)

Correctly scale to full screen and first step towards custom scaling
  • Loading branch information
corbinlc authored Oct 8, 2021
1 parent 9ffbc46 commit 3dc67f3
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 30 deletions.
5 changes: 5 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ android {
vectorDrawables.useSupportLibrary = true
buildConfigField 'boolean', 'ENABLE_PLAY_SERVICES', 'true'

//these are used to help determine the resolution
buildConfigField 'boolean', 'FORCE_PORTRAIT_GEOMETRY', 'true'
buildConfigField 'int', "MAX_DIMENSION", "1280"
buildConfigField 'int', "MIN_DIMENSION", "360"

// Ignore Sentry packages that cause lint failure
lintOptions {
lintConfig file("$projectDir/src/main/resources/lint.xml")
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/tech/ula/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ class MainActivity : AppCompatActivity(), SessionListFragment.SessionSelection,
val windowManager = applicationContext.getSystemService(Context.WINDOW_SERVICE) as WindowManager

val orientation = applicationContext.resources.configuration.orientation
deviceDimensions.saveDeviceDimensions(windowManager, DisplayMetrics(), orientation)
deviceDimensions.saveDeviceDimensions(windowManager, DisplayMetrics(), orientation, defaultSharedPreferences)
session.geometry = deviceDimensions.getScreenResolution()
}

Expand Down
69 changes: 43 additions & 26 deletions app/src/main/java/tech/ula/utils/DeviceDimensions.kt
Original file line number Diff line number Diff line change
@@ -1,42 +1,59 @@
package tech.ula.utils

import android.content.res.Configuration
import android.graphics.Point
import android.content.SharedPreferences
import android.os.Build
import android.util.DisplayMetrics
import android.view.WindowManager
import tech.ula.BuildConfig

class DeviceDimensions {
private var height = 720
private var width = 1480
private var height = 720f
private var width = 1480f
private var scaling = 1f

fun saveDeviceDimensions(windowManager: WindowManager, displayMetrics: DisplayMetrics, orientation: Int) {
val navBarSize = getNavigationBarSize(windowManager)
fun saveDeviceDimensions(windowManager: WindowManager, displayMetrics: DisplayMetrics, orientation: Int, sharedPreferences: SharedPreferences) {
var scalingMin = 1f
var scalingMax = 1f
windowManager.defaultDisplay.getRealMetrics(displayMetrics)
height = displayMetrics.heightPixels
width = displayMetrics.widthPixels
windowManager.defaultDisplay.getMetrics(displayMetrics)
height = displayMetrics.heightPixels.toFloat()
width = displayMetrics.widthPixels.toFloat()

when (orientation) {
Configuration.ORIENTATION_PORTRAIT -> if (navBarSize.y > 0) height += navBarSize.y
Configuration.ORIENTATION_LANDSCAPE -> if (navBarSize.x > 0) width += navBarSize.x
else -> return
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
val displayCutout = windowManager.defaultDisplay.cutout
if (displayCutout != null) {
height -= displayCutout.safeInsetBottom + displayCutout.safeInsetTop
width -= displayCutout.safeInsetLeft + displayCutout.safeInsetRight
}
}
}

fun getScreenResolution(): String {
return when (height > width) {
true -> "${height}x$width"
false -> "${width}x$height"
if (sharedPreferences.getBoolean("pref_custom_scaling_enabled", false)) {
scaling = sharedPreferences.getString("pref_scaling", "1.0")!!.toFloat()
} else {
if (height > width) {
scalingMax = height / BuildConfig.MAX_DIMENSION
scalingMin = width / BuildConfig.MIN_DIMENSION
} else {
scalingMax = width / BuildConfig.MAX_DIMENSION
scalingMin = height / BuildConfig.MIN_DIMENSION
}
if (scalingMin < 1f)
scalingMin = 1f
if (scalingMax < 1f)
scalingMax = 1f
if (scalingMax < scalingMin)
scaling = scalingMax
else
scaling = scalingMin
}
}

private fun getNavigationBarSize(windowManager: WindowManager): Point {
val display = windowManager.defaultDisplay
val appSize = Point()
val screenSize = Point()
display.getSize(appSize)
display.getRealSize(screenSize)
height = height / scaling
width = width / scaling
}

return Point(screenSize.x - appSize.x, screenSize.y - appSize.y)
fun getScreenResolution(): String {
return when ((height > width) && BuildConfig.FORCE_PORTRAIT_GEOMETRY) {
true -> "${height.toInt()}x${width.toInt()}"
false -> "${width.toInt()}x${height.toInt()}"
}
}
}
3 changes: 3 additions & 0 deletions app/src/main/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -281,5 +281,8 @@
<string name="pref_clear_auto_start_title">Autostart Einstellungen löschen</string>
<string name="pref_clear_auto_start_summary">Keine App mehr automatisch starten, bis wieder aktiviert wird.</string>
<string name="auto_start_checkbox">Autostart aktivieren</string>
<string name="pref_custom_scaling_enabled_title">Benuterdefinierten Skalierfaktor verwenden</string>
<string name="pref_custom_scaling_enabled_summary">Erlaubt das Einstellen eines benutzerdefinierten Skalierfaktors, um die Display Auflösung der VNC Sitzung zu verringern.</string>
<string name="pref_scaling_title">Skalierfaktor festlegen</string>

</resources>
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@
<string name="pref_default_landing_page_message">Select the page you would like to open the app with.</string>
<string name="pref_clear_auto_start_title">Clear Auto Start Settings</string>
<string name="pref_clear_auto_start_summary">Stops any app from auto starting until enabled again.</string>
<string name="pref_custom_scaling_enabled_title">Use Custom Graphical Scaling Factor</string>
<string name="pref_custom_scaling_enabled_summary">Allows specifying a custom scaling factor to decrease the resolution of the VNC session.</string>
<string name="pref_scaling_title">Set Custom Scaling Factor</string>
<string name="pref_proot_category">PRoot Preferences</string>
<string name="pref_proot_debugging_enabled_title">PRoot Debugging Logs Enabled</string>
<string name="pref_proot_debugging_enabled_summary">Only necessary if having problems with the app. Log file stored at\n/mnt/sdcard/PRoot_Debug_Log</string>
Expand Down
13 changes: 13 additions & 0 deletions app/src/main/res/xml/preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,19 @@
android:title="@string/pref_clear_auto_start_title"
android:summary="@string/pref_clear_auto_start_summary"
app:iconSpaceReserved="false"/>
<CheckBoxPreference
android:key="pref_custom_scaling_enabled"
android:title="@string/pref_custom_scaling_enabled_title"
android:summary="@string/pref_custom_scaling_enabled_summary"
android:defaultValue="false"
app:iconSpaceReserved="false"/>
<EditTextPreference
android:dependency="pref_custom_scaling_enabled"
android:key="pref_scaling"
android:title="@string/pref_scaling_title"
android:inputType="number"
android:defaultValue="1"
app:iconSpaceReserved="false"/>
<CheckBoxPreference
android:key="pref_opt_in"
android:title="@string/opt_in_preference"
Expand Down
15 changes: 12 additions & 3 deletions app/src/test/java/tech/ula/utils/DeviceDimensionsTest.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package tech.ula.utils

import android.content.SharedPreferences
import android.content.res.Configuration
import android.util.DisplayMetrics
import android.view.Display
Expand All @@ -11,6 +12,7 @@ import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mock
import org.mockito.junit.MockitoJUnitRunner
import tech.ula.BuildConfig

@RunWith(MockitoJUnitRunner::class)
class DeviceDimensionsTest {
Expand All @@ -27,6 +29,9 @@ class DeviceDimensionsTest {
@Mock
lateinit var mockDisplay: Display

@Mock
lateinit var mockSharedPreferences: SharedPreferences

@Before
fun setup() {
mockDeviceDimensions = DeviceDimensions()
Expand All @@ -41,15 +46,19 @@ class DeviceDimensionsTest {
@Test
fun `Device dimensions that are taller in height will have the height value first`() {
setDimensions(mockDisplayMetrics, width = 100, height = 200)
mockDeviceDimensions.saveDeviceDimensions(mockWindowManager, mockDisplayMetrics, Configuration.ORIENTATION_PORTRAIT)
mockDeviceDimensions.saveDeviceDimensions(mockWindowManager, mockDisplayMetrics, Configuration.ORIENTATION_PORTRAIT, mockSharedPreferences)
val geometry = mockDeviceDimensions.getScreenResolution()
assertEquals(geometry, "200x100")
if (BuildConfig.FORCE_PORTRAIT_GEOMETRY) {
assertEquals(geometry, "200x100")
} else {
assertEquals(geometry, "100x200")
}
}

@Test
fun `Device dimensions that are longer in width will have the width value first`() {
setDimensions(mockDisplayMetrics, width = 300, height = 200)
mockDeviceDimensions.saveDeviceDimensions(mockWindowManager, mockDisplayMetrics, Configuration.ORIENTATION_PORTRAIT)
mockDeviceDimensions.saveDeviceDimensions(mockWindowManager, mockDisplayMetrics, Configuration.ORIENTATION_PORTRAIT, mockSharedPreferences)
val geometry = mockDeviceDimensions.getScreenResolution()
assertEquals(geometry, "300x200")
}
Expand Down

0 comments on commit 3dc67f3

Please sign in to comment.