Change the system display size programmatically Android N

后端 未结 3 1548
误落风尘
误落风尘 2020-12-16 02:04

Background: Android N comes with a feature to change system Display Size from settings, in addition to the previously present feature of changi

3条回答
  •  心在旅途
    2020-12-16 02:52

    Just share my approach to tackle this requirement. I achieve this function by using the dirty Java reflection method - although it's not that elegant.

    The main reference source code files are:

    • ScreenZoomSettings.java (https://github.com/aosp-mirror/platform_packages_apps_settings/blob/master/src/com/android/settings/display/ScreenZoomSettings.java)
    • DisplayDensityUtils.java (http://androidxref.com/9.0.0_r3/xref/frameworks/base/packages/SettingsLib/src/com/android/settingslib/display/DisplayDensityUtils.java)
    • WindowManagerGlobal.java (http://androidxref.com/9.0.0_r3/xref/frameworks/base/core/java/android/view/WindowManagerGlobal.java)

    And, follow the below steps, then you can get the required control:

    1. Read ZoomScreenSettings.java's onCreate() and commit(). They demonstrate how to correctly get and set the density value into the framework.
    2. Read DisplayDensityUtils.java. It shows how to use WindowManagerService to control the system density. Because we can't get the instance of DisplayDensityUtils via reflection, we need to understand which WindowManagerService methods are utilized.
    3. Use reflection to get WindowManagerService's instance, and write a DisplayDensityUtils-like class into your project.
    // Where wm is short for window manager
    val wmGlobalClz = Class.forName("android.view.WindowManagerGlobal")
    val getWmServiceMethod = wmGlobalClz.getDeclaredMethod("getWindowManagerService")
    val wmService = getWmServiceMethod.invoke(wmGlobalClz)
    
    val wmInterfaceClz = Class.forName("android.view.IWindowManager")
    
    // Now, we already have the ability to do many things we want.
    // For instance, to get the default density value.
    val getInitialDisplayDensityMethod = wmInterfaceClz.getDeclaredMethod(
            "getInitialDisplayDensity", 
            Integer.TYPE
    )
    val defaultDensity = getInitialDisplayDensityMethod.invoke(
            wmService,
            Display.DEFAULT_DISPLAY
    ) as Int
    
    
    1. Set or get the density value with your DisplayDensityUtils-like class. One thing just to mention is that, if you want to pass an index value (e.g., 2 for large display size), please feed it to your DisplayDensityUtils-like class's mValues array to get the actual density value which is the right one to pass to the framework. Getting the current density index also applies the same concept.

提交回复
热议问题