From 6d7d379bf5972a7d88eccf835ffaf47c21f974b8 Mon Sep 17 00:00:00 2001 From: jiesou Date: Mon, 25 Dec 2023 20:17:40 +0800 Subject: [PATCH] get real resolution and dpi --- .../ui/resolution/ResolutionFragment.kt | 7 +-- .../ui/resolution/ResolutionViewModel.kt | 62 ++++++++++--------- 2 files changed, 36 insertions(+), 33 deletions(-) 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 a036881..8b75d57 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 @@ -33,8 +33,6 @@ class ResolutionFragment : Fragment() { // onDestroyView. private val binding get() = _binding!! - private lateinit var windowManager: WindowManager - private var userId = 0 companion object { @@ -53,14 +51,13 @@ class ResolutionFragment : Fragment() { _binding = FragmentResolutionBinding.inflate(inflater, container, false) val root: View = binding.root - windowManager = requireActivity().windowManager iWindowManager = asInterface("android.view.IWindowManager", "window") iUserManager = asInterface("android.os.IUserManager", "user") - resolutionViewModel.fetchScreenResolution(windowManager) + resolutionViewModel.fetchScreenResolution() resolutionViewModel.fetchUsers() val textView = binding.textResolution - resolutionViewModel.text.observe(viewLifecycleOwner) { + resolutionViewModel.screenInfoText.observe(viewLifecycleOwner) { textView.text = it } val textHeight = binding.textHeight.editText!! diff --git a/app/src/main/java/top/jiecs/screener/ui/resolution/ResolutionViewModel.kt b/app/src/main/java/top/jiecs/screener/ui/resolution/ResolutionViewModel.kt index e20d623..31249cd 100644 --- a/app/src/main/java/top/jiecs/screener/ui/resolution/ResolutionViewModel.kt +++ b/app/src/main/java/top/jiecs/screener/ui/resolution/ResolutionViewModel.kt @@ -12,53 +12,59 @@ import android.view.WindowManager import android.util.Log import java.lang.reflect.Method import java.lang.reflect.Field +import android.graphics.Point import org.lsposed.hiddenapibypass.HiddenApiBypass import top.jiecs.screener.ui.resolution.ResolutionFragment class ResolutionViewModel : ViewModel() { - private val _text = MutableLiveData().apply { - value = "Physical Resolution Override" + val screenInfoText: MutableLiveData by lazy { + MutableLiveData() } - val text: LiveData = _text - - val resolutionMap: MutableLiveData> by lazy { MutableLiveData>() } - fun fetchScreenResolution(windowManager: WindowManager) { - val metrics = windowManager.currentWindowMetrics.bounds - - // TODO: get now Physical and Override size + fun fetchScreenResolution() { + val windowManager = ResolutionFragment.iWindowManager + + val physical_size = Point() + HiddenApiBypass.invoke(windowManager::class.java, windowManager, "getInitialDisplaySize", Display.DEFAULT_DISPLAY, physical_size) + val physical_dpi = HiddenApiBypass.invoke(windowManager::class.java, windowManager, "getInitialDisplayDensity", Display.DEFAULT_DISPLAY) as Int + screenInfoText.value = "Physical ${physical_size.y}x${physical_size.x}; DPI ${physical_dpi.toString()}" + + val override_size = Point() + HiddenApiBypass.invoke(windowManager::class.java, windowManager, "getBaseDisplaySize", Display.DEFAULT_DISPLAY, override_size) + val override_dpi = HiddenApiBypass.invoke(windowManager::class.java, windowManager, "getBaseDisplayDensity", Display.DEFAULT_DISPLAY) as Int resolutionMap.value = mapOf( - "height" to metrics.height(), - "width" to metrics.width(), - "dpi" to 520) + "height" to override_size.y, + "width" to override_size.x, + "dpi" to override_dpi.toString()) } val usersList: MutableLiveData>> by lazy { MutableLiveData>>() } - fun fetchUsers() { + fun fetchUsers() { val userManager = ResolutionFragment.iUserManager - - val users = HiddenApiBypass.invoke(userManager::class.java, userManager, "getUsers", true, true, true) as List<*> - Log.d("users", users.toString()) - - val userInfoFields = HiddenApiBypass.getInstanceFields(Class.forName("android.content.pm.UserInfo")) as List + try { + val users = HiddenApiBypass.invoke(userManager::class.java, userManager, "getUsers", true, true, true) as List<*> + val userInfoFields = HiddenApiBypass.getInstanceFields(Class.forName("android.content.pm.UserInfo")) as List - val idField = userInfoFields.first { it.name == "id" } - val nameField = userInfoFields.first { it.name == "name" } - - val mappedUsers = users.map { userInfo -> - mapOf( - "id" to idField.get(userInfo)!!, - "name" to nameField.get(userInfo)!! - ) - } + val idField = userInfoFields.first { it.name == "id" } + val nameField = userInfoFields.first { it.name == "name" } + + val mappedUsers = users.map { userInfo -> + mapOf( + "id" to idField.get(userInfo)!!, + "name" to nameField.get(userInfo)!! + ) + } - usersList.value = mappedUsers + usersList.value = mappedUsers + } catch (e: Exception) { + Log.e("fetchUsers", e.message, e) + } } } \ No newline at end of file