多屏模式输入法可以正确切换屏幕展示原理剖析

2023-12-07 12:44

本文主要是介绍多屏模式输入法可以正确切换屏幕展示原理剖析,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

背景

hi,粉丝朋友们:
近期有个学员问到了一个输入法相关问题。刚好梳理了一下输入法相关的在多屏模式的一个展示流程,这里做个记录,也相当于深入理解窗口相关的一篇干货blog。

在这里插入图片描述
在这里插入图片描述
如上面两幅图展示,输入法可以自由自在显示在双屏的任意屏幕,那么下面主要就是来解密输入到底如何做到的自由双屏展示。

分析思路

如果对输入相关业务不知道的话,那么就只能考虑从窗口层面入手,一般输入法都是其实是一个dialog,这个类是SoftInputWindow
在这里插入图片描述一般输入法窗口显示到了其他屏幕,肯定相关的displayid是要变化的这里可以dumpsys window windows可以看出来
输入法窗口在副屏:

Window #1 Window{e79aca4 u0 InputMethod}:mDisplayId=2 rootTaskId=35 mSession=Session{d9973cd 1240:u0a10084} mClient=android.os.BinderProxy@e774f37mOwnerUid=10084 showForAllUsers=false package=com.android.inputmethod.latin appop=NONEmAttrs={(0,0)(fillxfill) gr=BOTTOM CENTER_VERTICAL sim={adjust=pan} ty=INPUT_METHOD fmt=TRANSPARENT wanim=0x1030056 receive insets ignoring z-orderfl=NOT_FOCUSABLE LAYOUT_IN_SCREEN SPLIT_TOUCH HARDWARE_ACCELERATED DRAWS_SYSTEM_BAR_BACKGROUNDSpfl=USE_BLAST FIT_INSETS_CONTROLLEDbhv=DEFAULTfitTypes=STATUS_BARS NAVIGATION_BARSfitSides=LEFT TOP RIGHT}Requested w=1120 h=2960 mLayoutSeq=64mIsImWindow=true mIsWallpaper=false mIsFloatingLayer=truemBaseLayer=131000 mSubLayer=0    mToken=WindowToken{8e22fba type=2011 android.os.Binder@8ee38e5}mViewVisibility=0x0 mHaveFrame=true mObscured=falsemGivenContentInsets=[0,1759][0,0] mGivenVisibleInsets=[0,1759][0,0]mTouchableInsets=3 mGivenInsetsPending=falsetouchable region=SkRegion((0,1759,1440,2792))mFullConfiguration={1.0 310mcc260mnc [en_US] ldltr sw411dp w411dp h797dp 560dpi nrml long port finger qwerty/v/v dpad/v winConfig={ mBounds=Rect(0, 0 - 1440, 2960) mAppBounds=Rect(0, 0 - 1440, 2792) mMaxBounds=Rect(0, 0 - 1440, 2960) mDisplayRotation=ROTATION_0 mWindowingMode=fullscreen mDisplayWindowingMode=fullscreen mActivityType=undefined mAlwaysOnTop=undefined mRotation=ROTATION_0} s.49 fontWeightAdjustment=0}mLastReportedConfiguration={1.0 310mcc260mnc [en_US] ldltr sw411dp w411dp h797dp 560dpi nrml long port finger qwerty/v/v dpad/v winConfig={ mBounds=Rect(0, 0 - 1440, 2960) mAppBounds=Rect(0, 0 - 1440, 2792) mMaxBounds=Rect(0, 0 - 1440, 2960) mDisplayRotation=ROTATION_0 mWindowingMode=fullscreen mDisplayWindowingMode=fullscreen mActivityType=undefined mAlwaysOnTop=undefined mRotation=ROTATION_0} s.49 fontWeightAdjustment=0}mHasSurface=true isReadyForDisplay()=true mWindowRemovalAllowed=falseFrames: parent=[0,0][1440,2960] display=[0,0][1440,2960] frame=[0,0][1440,2960] last=[0,0][1440,2960] insetsChanged=falsesurface=[0,0][0,0]ContainerAnimator:mLeash=Surface(name=Surface(name=e79aca4 InputMethod)/@0xd668dc2 - animation-leash of insets_animation)/@0xaeadaa0 mAnimationType=insets_animationAnimation: com.android.server.wm.InsetsSourceProvider$ControlAdapter@c5c0659ControlAdapter mCapturedLeash=Surface(name=Surface(name=e79aca4 InputMethod)/@0xd668dc2 - animation-leash of insets_animation)/@0xaeadaa0WindowStateAnimator{ef4021e InputMethod}:mSurface=Surface(name=InputMethod)/@0x7be3affSurface: shown=true layer=0 alpha=1.0 rect=(0.0,0.0)  transform=(1.0, 0.0, 0.0, 1.0)mDrawState=HAS_DRAWN       mLastHidden=falsemEnterAnimationPending=false      mSystemDecorRect=[0,0][0,0]mForceSeamlesslyRotate=false seamlesslyRotate: pending=null    isOnScreen=trueisVisible=truekeepClearAreas: restricted=[], unrestricted=[]

输入法在主屏幕:

Window #7 Window{ee26d8d u0 InputMethod}:mDisplayId=0 rootTaskId=1 mSession=Session{d9973cd 1240:u0a10084} mClient=android.os.BinderProxy@7424e24mOwnerUid=10084 showForAllUsers=false package=com.android.inputmethod.latin appop=NONEmAttrs={(0,0)(fillxfill) gr=BOTTOM CENTER_VERTICAL sim={adjust=pan} ty=INPUT_METHOD fmt=TRANSPARENT wanim=0x1030056 receive insets ignoring z-orderfl=NOT_FOCUSABLE LAYOUT_IN_SCREEN SPLIT_TOUCH HARDWARE_ACCELERATED DRAWS_SYSTEM_BAR_BACKGROUNDSpfl=USE_BLAST FIT_INSETS_CONTROLLEDbhv=DEFAULTfitTypes=STATUS_BARS NAVIGATION_BARSfitSides=LEFT TOP RIGHT}Requested w=1440 h=2960 mLayoutSeq=162mIsImWindow=true mIsWallpaper=false mIsFloatingLayer=truemBaseLayer=131000 mSubLayer=0    mToken=WindowToken{ba8dd3a type=2011 android.os.Binder@4152c65}mViewVisibility=0x0 mHaveFrame=true mObscured=falsemGivenContentInsets=[0,1675][0,0] mGivenVisibleInsets=[0,1675][0,0]mTouchableInsets=3 mGivenInsetsPending=falsetouchable region=SkRegion((0,1759,1440,2792))mFullConfiguration={1.0 310mcc260mnc [en_US] ldltr sw411dp w411dp h773dp 560dpi nrml long port finger qwerty/v/v dpad/v winConfig={ mBounds=Rect(0, 0 - 1440, 2960) mAppBounds=Rect(0, 0 - 1440, 2792) mMaxBounds=Rect(0, 0 - 1440, 2960) mDisplayRotation=ROTATION_0 mWindowingMode=fullscreen mDisplayWindowingMode=fullscreen mActivityType=undefined mAlwaysOnTop=undefined mRotation=ROTATION_0} s.24 fontWeightAdjustment=0}mLastReportedConfiguration={1.0 310mcc260mnc [en_US] ldltr sw411dp w411dp h773dp 560dpi nrml long port finger qwerty/v/v dpad/v winConfig={ mBounds=Rect(0, 0 - 1440, 2960) mAppBounds=Rect(0, 0 - 1440, 2792) mMaxBounds=Rect(0, 0 - 1440, 2960) mDisplayRotation=ROTATION_0 mWindowingMode=fullscreen mDisplayWindowingMode=fullscreen mActivityType=undefined mAlwaysOnTop=undefined mRotation=ROTATION_0} s.24 fontWeightAdjustment=0}mHasSurface=true isReadyForDisplay()=true mWindowRemovalAllowed=falseFrames: parent=[0,84][1440,2960] display=[0,84][1440,2960] frame=[0,84][1440,2960] last=[0,84][1440,2960] insetsChanged=falsesurface=[0,0][0,0]ContainerAnimator:mLeash=Surface(name=Surface(name=ee26d8d InputMethod)/@0x1dbd753 - animation-leash of insets_animation)/@0xf16d3fd mAnimationType=insets_animationAnimation: com.android.server.wm.InsetsSourceProvider$ControlAdapter@3fd88f2ControlAdapter mCapturedLeash=Surface(name=Surface(name=ee26d8d InputMethod)/@0x1dbd753 - animation-leash of insets_animation)/@0xf16d3fdWindowStateAnimator{fd93b43 InputMethod}:mAnimationIsEntrance=true      mSurface=Surface(name=InputMethod)/@0x85ed5c0Surface: shown=true layer=0 alpha=1.0 rect=(0.0,0.0)  transform=(1.0, 0.0, 0.0, 1.0)mDrawState=HAS_DRAWN       mLastHidden=falsemEnterAnimationPending=false      mSystemDecorRect=[0,0][0,0]mForceSeamlesslyRotate=false seamlesslyRotate: pending=null    isOnScreen=trueisVisible=truekeepClearAreas: restricted=[], unrestricted=[]

明显看到了displayId是变化了,而且window对象不是一个

这里可以考虑在SoftInputWindow的构造方法中加入相关堆栈。
在这里插入图片描述

日志堆栈及源码

上一个屏幕的输入法移除

systemserver发起inputmethodservice的destory

bringDownServiceLocked:4744, ActiveServices (com.android.server.am)
bringDownServiceIfNeededLocked:4566, ActiveServices (com.android.server.am)
removeConnectionLocked:4923, ActiveServices (com.android.server.am)
unbindServiceLocked:3164, ActiveServices (com.android.server.am)
unbindService:12670, ActivityManagerService (com.android.server.am)
unbindService:2079, ContextImpl (android.app)
unbindMainConnection:450, InputMethodBindingController (com.android.server.inputmethod)
unbindCurrentMethod:353, InputMethodBindingController (com.android.server.inputmethod)
startInputUncheckedLocked:2843, InputMethodManagerService (com.android.server.inputmethod)
startInputOrWindowGainedFocusInternalLocked:4156, InputMethodManagerService (com.android.server.inputmethod)
startInputOrWindowGainedFocusInternal:3863, InputMethodManagerService (com.android.server.inputmethod)
startInputOrWindowGainedFocus:3816, InputMethodManagerService (com.android.server.inputmethod)
onTransact:402, IInputMethodManager$Stub (com.android.internal.view)
onTransact:1957, InputMethodManagerService (com.android.server.inputmethod)
execTransactInternal:1280, Binder (android.os)
execTransact:1244, Binder (android.os)
新的输入法service启动
realStartServiceLocked:4373, ActiveServices (com.android.server.am)
bringUpServiceLocked:4228, ActiveServices (com.android.server.am)
bindServiceLocked:2956, ActiveServices (com.android.server.am)
bindServiceInstance:12653, ActivityManagerService (com.android.server.am)
bindServiceInstance:12609, ActivityManagerService (com.android.server.am)
bindServiceCommon:2035, ContextImpl (android.app)
bindServiceAsUser:1974, ContextImpl (android.app)
bindCurrentInputMethodService:466, InputMethodBindingController (com.android.server.inputmethod)
bindCurrentInputMethodServiceMainConnection:479, InputMethodBindingController (com.android.server.inputmethod)
bindCurrentMethod:400, InputMethodBindingController (com.android.server.inputmethod)
startInputUncheckedLocked:2845, InputMethodManagerService (com.android.server.inputmethod)
startInputOrWindowGainedFocusInternalLocked:4156, InputMethodManagerService (com.android.server.inputmethod)
startInputOrWindowGainedFocusInternal:3863, InputMethodManagerService (com.android.server.inputmethod)
startInputOrWindowGainedFocus:3816, InputMethodManagerService (com.android.server.inputmethod)
onTransact:402, IInputMethodManager$Stub (com.android.internal.view)
onTransact:1957, InputMethodManagerService (com.android.server.inputmethod)
execTransactInternal:1280, Binder (android.os)
execTransact:1244, Binder (android.os)

这里可以看出主要是一个跨进程调用发起老的输入法service要stop,destory新的service要进行onCreate,这些堆栈都是在systemserver进程打出来的。
是谁发起跨进程startInputOrWindowGainedFocus方法呢?当然是app端啊,输入焦点EditText那个app

App端发起启动输入法
startInputInner:2379, InputMethodManager (android.view.inputmethod)
startInput:665, InputMethodManager$DelegateImpl (android.view.inputmethod)
startInputAsyncOnWindowFocusGain:733, InputMethodManager$DelegateImpl (android.view.inputmethod)
onPostWindowFocus:141, ImeFocusController (android.view)
handleWindowFocusChanged:3699, ViewRootImpl (android.view)
-$$Nest$mhandleWindowFocusChanged:-1, ViewRootImpl (android.view)
handleMessageImpl:5538, ViewRootImpl$ViewRootHandler (android.view)
handleMessage:5452, ViewRootImpl$ViewRootHandler (android.view)
dispatchMessage:106, Handler (android.os)
loopOnce:201, Looper (android.os)
loop:288, Looper (android.os)
main:7898, ActivityThread (android.app)
invoke:-1, Method (java.lang.reflect)
run:548, RuntimeInit$MethodAndArgsCaller (com.android.internal.os)
main:936, ZygoteInit (com.android.internal.os)

可以看到在app ViewRootImpl的handleWindowFocusChanged() 方法发起的操作

SoftInputWindow创建堆栈

LatinIME这个service的onCreate创建

<init>:135, SoftInputWindow (android.inputmethodservice)
onCreate:1515, InputMethodService (android.inputmethodservice)
onCreate:608, LatinIME (com.android.inputmethod.latin)
handleCreateService:4515, ActivityThread (android.app)
-$$Nest$mhandleCreateService:-1, ActivityThread (android.app)
handleMessage:2162, ActivityThread$H (android.app)
dispatchMessage:106, Handler (android.os)
loopOnce:201, Looper (android.os)
loop:288, Looper (android.os)
main:7898, ActivityThread (android.app)
invoke:-1, Method (java.lang.reflect)
run:548, RuntimeInit$MethodAndArgsCaller (com.android.internal.os)
main:936, ZygoteInit (com.android.internal.os)
创建SoftInputWindow的Display确定

相关创建SoftInputWindow代码关键的是这个context,看看这个context的创建流程

frameworks/base/core/java/android/app/ActivityThread.java

 @UnsupportedAppUsageprivate void handleCreateService(CreateServiceData data) {
//省略大部分ContextImpl context = ContextImpl.getImpl(service.createServiceBaseContext(this, packageInfo));}

调用到了createServiceBaseContext方法

frameworks/base/core/java/android/window/WindowProviderService.java

/** @hide */
@Override
public final Context createServiceBaseContext(ActivityThread mainThread,LoadedApk packageInfo) {final Context context = super.createServiceBaseContext(mainThread, packageInfo);final Display display = context.getSystemService(DisplayManager.class).getDisplay(getInitialDisplayId());//这里是核心关键会创建displayreturn context.createTokenContext(mWindowToken, display);
}

这里可以看到主要核心就是context.getSystemService(DisplayManager.class)
.getDisplay(getInitialDisplayId())获取一个display,这个display也就是最后决定输入在哪个屏幕的关键
这里主要通过getInitialDisplayId获取到合适的displayId

frameworks/base/core/java/android/inputmethodservice/AbstractInputMethodService.java

    /** @hide */@Overridepublic final int getInitialDisplayId() {try {//这里主要调用到wms的getImeDisplayIdreturn WindowManagerGlobal.getWindowManagerService().getImeDisplayId();} catch (RemoteException e) {throw e.rethrowFromSystemServer();}}

下面看看wms的getImeDisplayId
frameworks/base/services/core/java/com/android/server/wm/WindowManagerService.java

@Overridepublic int getImeDisplayId() {synchronized (mGlobalLock) {//这里主要获取一下焦点的Display是谁final DisplayContent dc = mRoot.getTopFocusedDisplayContent();//这里有个getImePolicy获取,要等于DISPLAY_IME_POLICY_LOCAL才可以return dc.getImePolicy() == DISPLAY_IME_POLICY_LOCAL ? dc.getDisplayId(): DEFAULT_DISPLAY;}}

那现在就清楚了,输入法显示在哪个Display,是最后调用到了wms的中的getImeDisplayId方法,这个方法主要就是获取当前谁是focused的display,然后把displayid传递回去

总结

整个多屏输入法的流程涉及到了3个进程

在这里插入图片描述

    ---->焦点app发起startInputAsyncOnWindowFocusGain------>systemserver stop老的service,start新service-------->输入法进程创建新的service onCreate执行------->输入法创建带有display的context-------->创建对于输入法窗口带有新的display

更多framework干货获取相关可以 私聊+v(androidframework007)
点击这里 https://mp.weixin.qq.com/s/Qv8zjgQ0CkalKmvi8tMGaw
视频:https://www.bilibili.com/video/BV1ah411d7Y3
在这里插入图片描述

这篇关于多屏模式输入法可以正确切换屏幕展示原理剖析的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/465934

相关文章

ShardingProxy读写分离之原理、配置与实践过程

《ShardingProxy读写分离之原理、配置与实践过程》ShardingProxy是ApacheShardingSphere的数据库中间件,通过三层架构实现读写分离,解决高并发场景下数据库性能瓶... 目录一、ShardingProxy技术定位与读写分离核心价值1.1 技术定位1.2 读写分离核心价值二

深度解析Python中递归下降解析器的原理与实现

《深度解析Python中递归下降解析器的原理与实现》在编译器设计、配置文件处理和数据转换领域,递归下降解析器是最常用且最直观的解析技术,本文将详细介绍递归下降解析器的原理与实现,感兴趣的小伙伴可以跟随... 目录引言:解析器的核心价值一、递归下降解析器基础1.1 核心概念解析1.2 基本架构二、简单算术表达

Three.js构建一个 3D 商品展示空间完整实战项目

《Three.js构建一个3D商品展示空间完整实战项目》Three.js是一个强大的JavaScript库,专用于在Web浏览器中创建3D图形,:本文主要介绍Three.js构建一个3D商品展... 目录引言项目核心技术1. 项目架构与资源组织2. 多模型切换、交互热点绑定3. 移动端适配与帧率优化4. 可

深入浅出Spring中的@Autowired自动注入的工作原理及实践应用

《深入浅出Spring中的@Autowired自动注入的工作原理及实践应用》在Spring框架的学习旅程中,@Autowired无疑是一个高频出现却又让初学者头疼的注解,它看似简单,却蕴含着Sprin... 目录深入浅出Spring中的@Autowired:自动注入的奥秘什么是依赖注入?@Autowired

Java中如何正确的停掉线程

《Java中如何正确的停掉线程》Java通过interrupt()通知线程停止而非强制,确保线程自主处理中断,避免数据损坏,线程池的shutdown()等待任务完成,shutdownNow()强制中断... 目录为什么不强制停止为什么 Java 不提供强制停止线程的能力呢?如何用interrupt停止线程s

从原理到实战解析Java Stream 的并行流性能优化

《从原理到实战解析JavaStream的并行流性能优化》本文给大家介绍JavaStream的并行流性能优化:从原理到实战的全攻略,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的... 目录一、并行流的核心原理与适用场景二、性能优化的核心策略1. 合理设置并行度:打破默认阈值2. 避免装箱

深度剖析SpringBoot日志性能提升的原因与解决

《深度剖析SpringBoot日志性能提升的原因与解决》日志记录本该是辅助工具,却为何成了性能瓶颈,SpringBoot如何用代码彻底破解日志导致的高延迟问题,感兴趣的小伙伴可以跟随小编一起学习一下... 目录前言第一章:日志性能陷阱的底层原理1.1 日志级别的“双刃剑”效应1.2 同步日志的“吞吐量杀手”

MySQL中查询和展示LONGBLOB类型数据的技巧总结

《MySQL中查询和展示LONGBLOB类型数据的技巧总结》在MySQL中LONGBLOB是一种二进制大对象(BLOB)数据类型,用于存储大量的二进制数据,:本文主要介绍MySQL中查询和展示LO... 目录前言1. 查询 LONGBLOB 数据的大小2. 查询并展示 LONGBLOB 数据2.1 转换为十

Python中的filter() 函数的工作原理及应用技巧

《Python中的filter()函数的工作原理及应用技巧》Python的filter()函数用于筛选序列元素,返回迭代器,适合函数式编程,相比列表推导式,内存更优,尤其适用于大数据集,结合lamb... 目录前言一、基本概念基本语法二、使用方式1. 使用 lambda 函数2. 使用普通函数3. 使用 N

MyBatis-Plus 与 Spring Boot 集成原理实战示例

《MyBatis-Plus与SpringBoot集成原理实战示例》MyBatis-Plus通过自动配置与核心组件集成SpringBoot实现零配置,提供分页、逻辑删除等插件化功能,增强MyBa... 目录 一、MyBATis-Plus 简介 二、集成方式(Spring Boot)1. 引入依赖 三、核心机制