-
Notifications
You must be signed in to change notification settings - Fork 410
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Correctly scale to full screen and first step towards custom scaling (#…
…1442) Correctly scale to full screen and first step towards custom scaling
- Loading branch information
Showing
7 changed files
with
80 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()}" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters