From abdd72c26484a968d3216f7ee35d37466192d890 Mon Sep 17 00:00:00 2001 From: jiesou Date: Sun, 30 Jun 2024 18:04:41 +0800 Subject: [PATCH] WIP: update and fix include layout --- .../java/top/jiecs/screener/MainActivity.kt | 5 ++- .../displaymode/DisplayModeDialogFragment.kt | 2 +- .../resolution/ConfirmationDialogFragment.kt | 2 +- .../ui/resolution/ResolutionFragment.kt | 43 +++++++------------ .../top/jiecs/screener/units/ApiCaller.kt | 8 ++-- app/src/main/res/layout/activity_main.xml | 2 +- .../main/res/layout/fragment_resolution.xml | 8 ++-- app/src/main/res/layout/resolution_editor.xml | 5 ++- build.gradle | 4 +- gradle/wrapper/gradle-wrapper.properties | 2 +- 10 files changed, 37 insertions(+), 44 deletions(-) diff --git a/app/src/main/java/top/jiecs/screener/MainActivity.kt b/app/src/main/java/top/jiecs/screener/MainActivity.kt index a1eea9a..140cafb 100644 --- a/app/src/main/java/top/jiecs/screener/MainActivity.kt +++ b/app/src/main/java/top/jiecs/screener/MainActivity.kt @@ -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 @@ -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. diff --git a/app/src/main/java/top/jiecs/screener/ui/displaymode/DisplayModeDialogFragment.kt b/app/src/main/java/top/jiecs/screener/ui/displaymode/DisplayModeDialogFragment.kt index 4e07a93..f8c4734 100644 --- a/app/src/main/java/top/jiecs/screener/ui/displaymode/DisplayModeDialogFragment.kt +++ b/app/src/main/java/top/jiecs/screener/ui/displaymode/DisplayModeDialogFragment.kt @@ -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 diff --git a/app/src/main/java/top/jiecs/screener/ui/resolution/ConfirmationDialogFragment.kt b/app/src/main/java/top/jiecs/screener/ui/resolution/ConfirmationDialogFragment.kt index 8653635..647be9d 100644 --- a/app/src/main/java/top/jiecs/screener/ui/resolution/ConfirmationDialogFragment.kt +++ b/app/src/main/java/top/jiecs/screener/ui/resolution/ConfirmationDialogFragment.kt @@ -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)) } diff --git a/app/src/main/java/top/jiecs/screener/ui/resolution/ResolutionFragment.kt b/app/src/main/java/top/jiecs/screener/ui/resolution/ResolutionFragment.kt index f8a8b8b..75118fa 100644 --- a/app/src/main/java/top/jiecs/screener/ui/resolution/ResolutionFragment.kt +++ b/app/src/main/java/top/jiecs/screener/ui/resolution/ResolutionFragment.kt @@ -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() @@ -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?, @@ -65,7 +56,6 @@ class ResolutionFragment : Fragment() { mainViewModel.shizukuPermissionGranted.observe(viewLifecycleOwner) { if (it) { resolutionViewModel.fetchScreenResolution() - resolutionViewModel.fetchUsers() } } @@ -73,15 +63,15 @@ class ResolutionFragment : Fragment() { 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 @@ -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) @@ -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 @@ -153,7 +142,7 @@ 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 @@ -161,18 +150,18 @@ class ResolutionFragment : Fragment() { // 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()) } } @@ -180,5 +169,5 @@ class ResolutionFragment : Fragment() { super.onDestroyView() _binding = null } - + } diff --git a/app/src/main/java/top/jiecs/screener/units/ApiCaller.kt b/app/src/main/java/top/jiecs/screener/units/ApiCaller.kt index e3b32c3..528b419 100644 --- a/app/src/main/java/top/jiecs/screener/units/ApiCaller.kt +++ b/app/src/main/java/top/jiecs/screener/units/ApiCaller.kt @@ -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) } } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 57f449f..8f3431a 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -23,7 +23,7 @@ app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" /> - + app:layout_constraintTop_toBottomOf="@id/text_resolution" />