Aperture: Replace Context.convertDpToPx with IntExt
Signed-off-by: Luca Stefani <luca.stefani.ge1@gmail.com>
Change-Id: Id4833b4b68e4af4b27a17a89497587204ebb715a
diff --git a/app/src/main/java/org/lineageos/aperture/ContextExt.kt b/app/src/main/java/org/lineageos/aperture/ContextExt.kt
deleted file mode 100644
index 70a2b3e..0000000
--- a/app/src/main/java/org/lineageos/aperture/ContextExt.kt
+++ /dev/null
@@ -1,15 +0,0 @@
-/*
- * Copyright (C) 2022 The LineageOS Project
- *
- * SPDX-License-Identifier: Apache-2.0
- */
-
-package org.lineageos.aperture
-
-import android.content.Context
-import kotlin.math.roundToInt
-
-internal fun Context.convertDpToPx(dp: Int): Int {
- val density = resources.displayMetrics.density
- return (dp * density).roundToInt()
-}
diff --git a/app/src/main/java/org/lineageos/aperture/IntExt.kt b/app/src/main/java/org/lineageos/aperture/IntExt.kt
new file mode 100644
index 0000000..ba3187d
--- /dev/null
+++ b/app/src/main/java/org/lineageos/aperture/IntExt.kt
@@ -0,0 +1,16 @@
+/*
+ * Copyright (C) 2022 The LineageOS Project
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+package org.lineageos.aperture
+
+import android.content.res.Resources.getSystem
+import kotlin.math.roundToInt
+
+val Int.px
+ get() = (this * getSystem().displayMetrics.density).roundToInt()
+
+val Int.dp
+ get() = (this / getSystem().displayMetrics.density).roundToInt()
diff --git a/app/src/main/java/org/lineageos/aperture/MainActivity.kt b/app/src/main/java/org/lineageos/aperture/MainActivity.kt
index cf88fb0..58e089e 100644
--- a/app/src/main/java/org/lineageos/aperture/MainActivity.kt
+++ b/app/src/main/java/org/lineageos/aperture/MainActivity.kt
@@ -272,7 +272,7 @@
CameraController.TAP_TO_FOCUS_STARTED -> {
viewFinderFocus.visibility = View.VISIBLE
handler.removeMessages(MSG_HIDE_FOCUS_RING)
- ValueAnimator.ofInt(convertDpToPx(0), convertDpToPx(8)).apply {
+ ValueAnimator.ofInt(0.px, 8.px).apply {
addUpdateListener { anim ->
viewFinderFocus.setPadding(anim.animatedValue as Int)
}
@@ -280,7 +280,7 @@
}
else -> {
handler.removeMessages(MSG_HIDE_FOCUS_RING)
- ValueAnimator.ofInt(convertDpToPx(8), convertDpToPx(0)).apply {
+ ValueAnimator.ofInt(8.px, 0.px).apply {
addUpdateListener { anim ->
viewFinderFocus.setPadding(anim.animatedValue as Int)
}
@@ -1076,14 +1076,14 @@
galleryButton.setPadding(0)
galleryButton.setImageBitmap(it)
} ?: run {
- galleryButton.setPadding(convertDpToPx(15))
+ galleryButton.setPadding(15.px)
galleryButton.setImageResource(R.drawable.ic_image)
}
} else if (keyguardManager.isKeyguardLocked) {
- galleryButton.setPadding(convertDpToPx(15))
+ galleryButton.setPadding(15.px)
galleryButton.setImageResource(R.drawable.ic_lock)
} else {
- galleryButton.setPadding(convertDpToPx(15))
+ galleryButton.setPadding(15.px)
galleryButton.setImageResource(R.drawable.ic_image)
}
}
@@ -1174,8 +1174,7 @@
private fun getThumbnail(uri: Uri?): Bitmap? {
return try {
uri?.let {
- val sizeInPx = convertDpToPx(75)
- contentResolver.loadThumbnail(it, Size(sizeInPx, sizeInPx), null)
+ contentResolver.loadThumbnail(it, Size(75.px, 75.px), null)
}
} catch (exception: Exception) {
Log.e(LOG_TAG, "${exception.message}")