Skip to content
This repository has been archived by the owner on Dec 22, 2024. It is now read-only.

Commit

Permalink
try to add res-scale-dpi async
Browse files Browse the repository at this point in the history
  • Loading branch information
jiesou committed Dec 29, 2023
1 parent fa488a4 commit 58c4069
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ import android.content.Context
import android.view.Display
import android.view.WindowManager
import android.content.DialogInterface
import android.text.TextWatcher
import android.text.Editable
import java.lang.CharSequence
import android.os.Handler
import android.os.Looper
import kotlin.math.pow
Expand All @@ -36,6 +39,8 @@ class ResolutionFragment : Fragment() {
// onDestroyView.
private val binding get() = _binding!!

private lateinit var resolutionViewModel: ResolutionViewModel

private var userId = 0

companion object {
Expand All @@ -48,7 +53,7 @@ class ResolutionFragment : Fragment() {
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
val resolutionViewModel =
resolutionViewModel =
ViewModelProvider(this).get(ResolutionViewModel::class.java)

_binding = FragmentResolutionBinding.inflate(inflater, container, false)
Expand Down Expand Up @@ -97,25 +102,38 @@ class ResolutionFragment : Fragment() {
userId = currentUser["id"] as Int
}
binding.silderScale.addOnChangeListener { _, value, _ ->
// 0 -> 1; 25 -> 1.25
val scale_ratio = value * 0.01 + 1
val base_height = textHeight.text.toString().toFloatOrNull() ?: return@addOnChangeListener
val base_width = textWidth.text.toString().toFloatOrNull() ?: return@addOnChangeListener
val physical = resolutionViewModel.physicalResolutionMap.value ?: return@addOnChangeListener

// calculate the DPI that keeps the display size proportionally scaled
// get the ratio of virtual to physical resolution diagonal (pythagorean theorem)
// base_physical_ratio = √(h²+w²/ph²+pw²)
val base_physical_ratio = sqrt((base_height.pow(2) + base_width.pow(2)) /
(physical["height"]!!.pow(2) + physical["width"]!!.pow(2)))

// scaled_dpi = pdpi * base_physical_ratio * scale_ratio
val scaled_dpi = (physical["dpi"]!! * base_physical_ratio * scale_ratio).roundToInt()

Log.d("scaled_dpi", scaled_dpi.toString())

if (scaled_dpi != 0) binding.textDpi.editText!!.setText(scaled_dpi.toString())
val scaled_height = textHeight.text.toString().toFloatOrNull() ?: return@addOnChangeListener
val scaled_width = textWidth.text.toString().toFloatOrNull() ?: return@addOnChangeListener
// -50 -> 0.5 ; 0 -> 1 ; 25 -> 1.25
val scale_ratio = (value * 0.01 + 1).toFloat()
updateDpiEditorOrScaleSilder(scaled_height, scaled_width, scale_ratio)
}
val hw_text_watcher = object : TextWatcher {
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {
Log.d("screener", s.toString())
super.beforeTextChanged(s, start, count, after)
}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {}
override fun afterTextChanged(s: Editable) {
val physical = resolutionViewModel.physicalResolutionMap.value ?: return
val aspect_ratio = physical["height"]!! / physical["width"]!!
when (s) {
textHeight.text -> {
val equal_ratio_width = s.toString().toInt() / aspect_ratio
textWidth.setText(equal_ratio_width.roundToInt().toString())
}
textWidth.text -> {
val equal_ratio_height = s.toString().toInt() * aspect_ratio
textHeight.setText(equal_ratio_height.roundToInt().toString())
}
}
val scaled_height = textHeight.text.toString().toFloatOrNull() ?: return
val scaled_width = textWidth.text.toString().toFloatOrNull() ?: return
updateDpiEditorOrScaleSilder(scaled_height, scaled_width, scale_ratio=null)
}
}
textHeight.addTextChangedListener(hw_text_watcher)
textWidth.addTextChangedListener(hw_text_watcher)
binding.btApply.setOnClickListener {
applyResolution(textHeight.text.toString().toInt(),
textWidth.text.toString().toInt(),
Expand Down Expand Up @@ -186,6 +204,33 @@ class ResolutionFragment : Fragment() {
HiddenApiBypass.invoke(iWindowManager::class.java, iWindowManager,
"clearForcedDisplayDensityForUser", Display.DEFAULT_DISPLAY, userId)
}

fun updateDpiEditorOrScaleSilder(scaled_height: Float, scaled_width: Float, scale_ratio: Float?) {
val physical = resolutionViewModel.physicalResolutionMap.value ?: return

// Calculate the DPI that keeps the display size proportionally scaled
// Get the ratio of virtual to physical resolution diagonal (pythagorean theorem)
// physical_adj_ratio = √(h²+w²/ph²+pw²)
val physical_adj_ratio = sqrt((scaled_height.pow(2) + scaled_width.pow(2)) /
(physical["height"]!!.pow(2) + physical["width"]!!.pow(2)))

val base_dpi = physical["dpi"]!! * physical_adj_ratio

if (scale_ratio === null) {
val scaled_dpi = binding.textDpi.editText!!.text.toString().toInt()
// scale_ratio = scaled_dpi / (physical_dpi * physical_adj_ratio)
var scale_ratio = scaled_dpi / base_dpi
// 0.5 -> -50 ; 1 -> 0 ; 1.25 -> 25
scale_ratio = (scale_ratio - 1) * 100
// Round to two decimal places
// scale_ratio = ((scale_ratio * 100).roundToInt()) / 100
binding.silderScale.value = scale_ratio
} else {
// scaled_dpi = physical_dpi * physical_adj_ratio * scale_ratio
val scaled_dpi = (base_dpi * scale_ratio).roundToInt()
binding.textDpi.editText!!.setText(scaled_dpi.toString())
}
}

override fun onDestroyView() {
super.onDestroyView()
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/res/layout/fragment_resolution.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@
app:layout_constraintStart_toEndOf="@+id/text_scale"
app:layout_constraintEnd_toEndOf="@+id/text_resolution"
android:value="0"
android:valueFrom="-30"
android:valueTo="150"
android:valueFrom="-50"
android:valueTo="50"
app:layout_constraintBottom_toBottomOf="@+id/text_scale"
android:layout_marginTop="8dp"
android:stepSize="5"
Expand Down

0 comments on commit 58c4069

Please sign in to comment.