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

Commit

Permalink
WIP: update and fix include layout
Browse files Browse the repository at this point in the history
  • Loading branch information
jiesou committed Jun 30, 2024
1 parent cf75ba9 commit abdd72c
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 44 deletions.
5 changes: 4 additions & 1 deletion app/src/main/java/top/jiecs/screener/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.NavController
import androidx.navigation.findNavController
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.fragment.findNavController
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.setupActionBarWithNavController
Expand Down Expand Up @@ -33,7 +35,8 @@ class MainActivity : AppCompatActivity(), PreferenceFragmentCompat.OnPreferenceS
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)

val navController = findNavController(R.id.nav_host_fragment_activity_main)
val navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment_activity_main) as NavHostFragment
val navController = navHostFragment.navController

// Passing each menu ID as a set of Ids because each
// menu should be considered as top (same) level destinations.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class DisplayModeDialogFragment : DialogFragment() {
.setPositiveButton(getString(R.string.looks_fine), null)
.setNegativeButton(getString(R.string.undo_changes)) { _: DialogInterface, _: Int ->
val apiCaller = ApiCaller()
apiCaller.resetResolution(0)
apiCaller.resetResolution()
}
.create()
return dialog
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class ConfirmationDialogFragment : DialogFragment() {

negativeButton.setOnClickListener {
confirmCountdownJob.value?.cancel()
apiCaller.resetResolution(0)
apiCaller.resetResolution()
negativeButton.text =
getString(R.string.undo_changes, getString(R.string.undone))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class ResolutionFragment : Fragment() {
// This property is only valid between onCreateView and
// onDestroyView.
private val binding get() = _binding!!

private val resolutionViewModel: ResolutionViewModel by viewModels()
private val mainViewModel by activityViewModels<MainViewModel>()

Expand All @@ -39,15 +39,6 @@ class ResolutionFragment : Fragment() {
// Apply resolution still according to real-time value
private var stuckScaleValue = 0

private val userId: Int
get() {
val usersList = resolutionViewModel.usersList.value ?: return 0
val checkedChipId = binding.resolution_editor.chipGroup.checkedChipId

val user = usersList.getOrNull(checkedChipId - 1) ?: return 0
return user["id"] as Int
}

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
Expand All @@ -65,23 +56,22 @@ class ResolutionFragment : Fragment() {
mainViewModel.shizukuPermissionGranted.observe(viewLifecycleOwner) {
if (it) {
resolutionViewModel.fetchScreenResolution()
resolutionViewModel.fetchUsers()
}
}

resolutionViewModel.physicalResolutionMap.observe(viewLifecycleOwner) {
binding.textResolution.text = "Physical ${it?.get("height").toString()}x${
it?.get("width").toString()}; DPI ${it?.get("dpi").toString()}"
}
val textHeight = binding.textHeight.editText!!
val textWidth = binding.textWidth.editText!!
val textDpi = binding.textDpi.editText!!
val textHeight = binding.resolutionEditor.textHeight.editText!!
val textWidth = binding.resolutionEditor.textWidth.editText!!
val textDpi = binding.resolutionEditor.textDpi.editText!!
resolutionViewModel.resolutionMap.observe(viewLifecycleOwner) {
textHeight.setText(it?.get("height")?.toInt()?.toString())
textWidth.setText(it?.get("width")?.toInt()?.toString())
textDpi.setText(it?.get("dpi")?.toInt()?.toString())
}
val chipGroup = binding.chipGroup
val chipGroup = binding.resolutionEditor.chipGroup
resolutionViewModel.usersList.observe(viewLifecycleOwner) {
if (it.isEmpty()) return@observe
// for each user, create a chip to chip group
Expand All @@ -97,7 +87,7 @@ class ResolutionFragment : Fragment() {
firstChip.isChecked = true
}

binding.sliderScale.addOnChangeListener { _, value, fromUser ->
binding.resolutionEditor.sliderScale.addOnChangeListener { _, value, fromUser ->
value.toInt().let {
if (fromUser) stuckScaleValue = it
updateDpiEditorOrScaleSlider(scaleValue=it)
Expand Down Expand Up @@ -129,20 +119,19 @@ class ResolutionFragment : Fragment() {
apiCaller.applyResolution(
textHeight.text.toString().toInt(),
textWidth.text.toString().toInt(),
textDpi.text.toString().toInt(),
userId
textDpi.text.toString().toInt()
)
val navController = v?.findNavController()
navController?.navigate(R.id.action_nav_resolution_to_nav_resolution_confirmation)
}
binding.btReset.setOnClickListener {
apiCaller.resetResolution(userId)
apiCaller.resetResolution()
}
}

private fun updateDpiEditorOrScaleSlider(scaleValue: Int?) {
val scaledHeight = binding.textHeight.editText!!.text.toString().toFloatOrNull() ?: return
val scaledWidth = binding.textWidth.editText!!.text.toString().toFloatOrNull() ?: return
val scaledHeight = binding.resolutionEditor.textHeight.editText!!.text.toString().toFloatOrNull() ?: return
val scaledWidth = binding.resolutionEditor.textWidth.editText!!.text.toString().toFloatOrNull() ?: return
val physical = resolutionViewModel.physicalResolutionMap.value ?: return

// Calculate the DPI that keeps the display size proportionally scaled
Expand All @@ -153,32 +142,32 @@ class ResolutionFragment : Fragment() {

val baseDpi = physical["dpi"]!! * physicalAdjRatio
if (scaleValue === null) {
val scaledDpi = binding.textDpi.editText!!.text.toString().toFloatOrNull() ?: baseDpi
val scaledDpi = binding.resolutionEditor.textDpi.editText!!.text.toString().toFloatOrNull() ?: baseDpi
// scale_ratio = scaled_dpi / (physical_dpi * physical_adj_ratio)
val scaleRatio = scaledDpi / baseDpi
// 0.5 -> -50 ; 1 -> 0 ; 1.25 -> 25
val scaledValue = (scaleRatio - 1) * 100
// Round to two decimal places
// scale_ratio = ((scale_ratio * 100).roundToInt()) / 100
if (scaledValue < -50 || scaledValue > 50) {
binding.textDpi.error = "over limit"
binding.resolutionEditor.textDpi.error = "over limit"
return
} else {
binding.sliderScale.value = ((scaledValue / 5).roundToInt() * 5).toFloat()
binding.textDpi.error = null
binding.resolutionEditor.sliderScale.value = ((scaledValue / 5).roundToInt() * 5).toFloat()
binding.resolutionEditor.textDpi.error = null
}
} else {
// -50 -> 0.5 ; 0 -> 1 ; 25 -> 1.25
val scaleRatio = (scaleValue * 0.01 + 1).toFloat()
// scaled_dpi = physical_dpi * physical_adj_ratio * scale_ratio
val scaledDpi = (baseDpi * scaleRatio).roundToInt()
binding.textDpi.editText!!.setText(scaledDpi.toString())
binding.resolutionEditor.textDpi.editText!!.setText(scaledDpi.toString())
}
}

override fun onDestroyView() {
super.onDestroyView()
_binding = null
}

}
8 changes: 4 additions & 4 deletions app/src/main/java/top/jiecs/screener/units/ApiCaller.kt
Original file line number Diff line number Diff line change
Expand Up @@ -96,17 +96,17 @@ class ApiCaller {
return emptyMap()
}

fun applyResolution(height: Int, width: Int, dpi: Int, userId: Int) {
fun applyResolution(height: Int, width: Int, dpi: Int) {
HiddenApiBypass.invoke(iWindowManager::class.java, iWindowManager,
"setForcedDisplaySize", Display.DEFAULT_DISPLAY, width, height)
HiddenApiBypass.invoke(iWindowManager::class.java, iWindowManager,
"setForcedDisplayDensityForUser", Display.DEFAULT_DISPLAY, dpi, userId)
"setForcedDisplayDensityForUser", Display.DEFAULT_DISPLAY, dpi, 0)
}

fun resetResolution(userId: Int) {
fun resetResolution() {
HiddenApiBypass.invoke(iWindowManager::class.java, iWindowManager,
"clearForcedDisplaySize", Display.DEFAULT_DISPLAY)
HiddenApiBypass.invoke(iWindowManager::class.java, iWindowManager,
"clearForcedDisplayDensityForUser", Display.DEFAULT_DISPLAY, userId)
"clearForcedDisplayDensityForUser", Display.DEFAULT_DISPLAY, 0)
}
}
2 changes: 1 addition & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />

<fragment
<androidx.fragment.app.FragmentContainerView
android:layout_height="match_parent"
android:layout_width="match_parent"
app:layout_constraintTop_toTopOf="parent"
Expand Down
8 changes: 4 additions & 4 deletions app/src/main/res/layout/fragment_resolution.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/text_resolution" />
app:layout_constraintTop_toBottomOf="@id/text_resolution" />

<Button
android:id="@+id/bt_apply"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="@string/apply"
app:layout_constraintStart_toStartOf="@+id/text_resolution"
app:layout_constraintStart_toStartOf="@id/text_resolution"
app:layout_constraintTop_toBottomOf="@+id/resolution_editor" />

<Button
Expand All @@ -41,7 +41,7 @@
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:text="@string/reset"
app:layout_constraintStart_toEndOf="@+id/bt_apply"
app:layout_constraintTop_toTopOf="@+id/bt_apply" />
app:layout_constraintStart_toEndOf="@id/bt_apply"
app:layout_constraintTop_toTopOf="@id/bt_apply" />

</androidx.constraintlayout.widget.ConstraintLayout>
5 changes: 3 additions & 2 deletions app/src/main/res/layout/resolution_editor.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_height="match_parent"
android:id="@+id/resolution_editor_layout">

<com.google.android.material.textfield.TextInputLayout
android:layout_height="wrap_content"
Expand All @@ -11,7 +12,7 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="@+id/text_resolution"
app:layout_constraintTop_toTopOf="@id/resolution_editor_layout"
android:id="@+id/text_height">

<com.google.android.material.textfield.TextInputEditText
Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
id 'com.android.application' version '8.2.2' apply false
id 'com.android.library' version '8.2.2' apply false
id 'com.android.application' version '8.5.0' apply false
id 'com.android.library' version '8.5.0' apply false
id 'org.jetbrains.kotlin.android' version '1.8.21' apply false
}

Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.2-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip
networkTimeout=10000
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

0 comments on commit abdd72c

Please sign in to comment.