Android 输入法窗口焦点获取流程(1),窗口和Session创建

基于Android9.x

目录

-1 窗口创建和WindowSession创建
       -1.1 窗口创建
           -1.1.1 attatch
       -1.2 WindowSession创建过程
           -1.2.1 getWindowSession
           -1.2.2 openSession
           -1.2.3 new Session
           -1.2.4 addClient
           -1.2.5 linkToDeath
           -1.2.6 binderDied
               -1.2.6.1 removeClient
               -1.2.6.2 killSessionLocked
-2 窗口创建和WindowSession创建依赖图
-3 总结

窗口创建和WindowSession创建

窗口创建

输入法#拉起流程#窗口初始化.png

attatch

 final void attach(Context context, ActivityThread aThread,
            Instrumentation instr, IBinder token, int ident,
            Application application, Intent intent, ActivityInfo info,
            CharSequence title, Activity parent, String id,
            NonConfigurationInstances lastNonConfigurationInstances,
            Configuration config, String referrer, IVoiceInteractor voiceInteractor,
            Window window, ActivityConfigCallback activityConfigCallback) {
        attachBaseContext(context);

        mFragments.attachHost(null /*parent*/);

        mWindow = new PhoneWindow(this, window, activityConfigCallback);
        mWindow.setWindowControllerCallback(this);
        mWindow.setCallback(this);
        mWindow.setOnWindowDismissedCallback(this);
        mWindow.getLayoutInflater().setPrivateFactory(this);
        //mWindow.getAttribute().softInputMode默认为0,因此不会执行softInputMode
        if (info.softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED) {
            mWindow.setSoftInputMode(info.softInputMode);
        }
    ...
}

该过程主要是完成PhoneWindow的创建,为布局内容的填充做准备;
跟输入法场景相关的是setSoftInputMode,不过,对于Activity,softInputMode = SOFT_INPUT_STATE_UNSPECIFIED;不会执行setSoftInputMode

WindowSession创建过程

getWindowSession.png

如上图,主要看getWindowSession方法

getWindowSession

    public static IWindowSession getWindowSession() {
        synchronized (WindowManagerGlobal.class) {
            //单例模式,每个进程只会初始化一次
            if (sWindowSession == null) {
                try {
                    //获取InputMethodManager对象
                    InputMethodManager imm = InputMethodManager.getInstance();
                     //获取IWindowManager对象
                    IWindowManager windowManager = getWindowManagerService();
                    sWindowSession = windowManager.openSession(
                            new IWindowSessionCallback.Stub() {
                                @Override
                                public void onAnimatorScaleChanged(float scale) {
                                    ValueAnimator.setDurationScale(scale);
                                }
                            },
                            imm.getClient(), imm.getInputContext());
                } catch (RemoteException e) {
                    throw e.rethrowFromSystemServer();
                }
            }
            return sWindowSession;
        }
    }

imm.getClient():InputMethodManager中的IInputMethodClient对象
imm.getInputContext():InputMethodManager中的IInputContext对象

InputMethodManager.getInstance

    public static InputMethodManager getInstance() {
        synchronized (InputMethodManager.class) {
            //单例模式,每个进程只会初始化一次
            if (sInstance == null) {
                try {
                    //单例模式创建InputMethodManager
                    sInstance = new InputMethodManager(Looper.getMainLooper());
                } catch (ServiceNotFoundException e) {
                    throw new IllegalStateException(e);
                }
            }
            return sInstance;
        }
    }

    InputMethodManager(Looper looper) throws ServiceNotFoundException {
        this(IInputMethodManager.Stub.asInterface(
                ServiceManager.getServiceOrThrow(Context.INPUT_METHOD_SERVICE)), looper);
    }

    InputMethodManager(IInputMethodManager service, Looper looper) {
        //远程IMMS服的本地对象
        mService = service;
        //application's main looper
        mMainLooper = looper;
        mH = new H(looper);
        mIInputContext = new ControlledInputConnectionWrapper(looper,
                mDummyInputConnection, this);
    }

mClient

 final IInputMethodClient.Stub mClient = new IInputMethodClient.Stub() 

通过以上Session创建过程,我们获取以下信息:

  • 每个应用进程持有一个IWindowSession对象
  • IWindowSession代表WMS端的Session对象,持有应用端的IWindowSessionCallback,IInputMethodClient和IInputContext对象
IWindowSessionCallback
mClient:IInputMethodClient
mIInputContext:ControlledInputConnectionWrapper

接着看下,wms端的创建过程:

openSession

WMS.JAVA

    @Override
    public IWindowSession openSession(IWindowSessionCallback callback, IInputMethodClient client,
            IInputContext inputContext) {
        if (client == null) throw new IllegalArgumentException("null client");
        if (inputContext == null) throw new IllegalArgumentException("null inputContext");
        //根据应用传递的参数,创建Session对象
        Session session = new Session(this, callback, client, inputContext);
        return session;
    }

new Session

/**
 * This class represents an active client session.  There is generally one
 * Session object per process that is interacting with the window manager.
 */

    public Session(WindowManagerService service, IWindowSessionCallback callback,
            IInputMethodClient client, IInputContext inputContext) {
        mService = service;
        mCallback = callback;
        mClient = client;
        //得到改应用进程的uid和pid
        mUid = Binder.getCallingUid();
        mPid = Binder.getCallingPid();
        mLastReportedAnimatorScale = service.getCurrentAnimatorScale();
        //check 应用操作系统窗口权限
        mCanAddInternalSystemWindow = service.mContext.checkCallingOrSelfPermission(
                INTERNAL_SYSTEM_WINDOW) == PERMISSION_GRANTED;
        mCanHideNonSystemOverlayWindows = service.mContext.checkCallingOrSelfPermission(
                HIDE_NON_SYSTEM_OVERLAY_WINDOWS) == PERMISSION_GRANTED;
        mCanAcquireSleepToken = service.mContext.checkCallingOrSelfPermission(DEVICE_POWER)
                == PERMISSION_GRANTED;
        mShowingAlertWindowNotificationAllowed = mService.mShowAlertWindowNotifications;
        mDragDropController = mService.mDragDropController;
        //获取IMMS服务的本地对象
        synchronized (mService.mWindowMap) {
            if (mService.mInputMethodManager == null && mService.mHaveInputMethods) {
                IBinder b = ServiceManager.getService(
                        Context.INPUT_METHOD_SERVICE);
                mService.mInputMethodManager = IInputMethodManager.Stub.asInterface(b);
            }
        }
        long ident = Binder.clearCallingIdentity();
        try {
            // Note: it is safe to call in to the input method manager
            // here because we are not holding our lock.
            if (mService.mInputMethodManager != null) {
                //调用IMMS的addClient方法,将应用信息与IMMS进行绑定
                mService.mInputMethodManager.addClient(client, inputContext,
                        mUid, mPid);
            } else {
                client.setUsingInputMethod(false);
            }
            //监听应用客户端是否死亡,死亡,则会调用
            client.asBinder().linkToDeath(this, 0);
        } catch (RemoteException e) {
            // The caller has died, so we can just forget about this.
            try {
                if (mService.mInputMethodManager != null) {
                    mService.mInputMethodManager.removeClient(client);
                }
            } catch (RemoteException ee) {
            }
        } finally {
            Binder.restoreCallingIdentity(ident);
        }
    }

主要代码逻辑如下:

  • 获取调用方,也就是应用进进程的uid,pid,连同应用传递的client和inputContext对象,传递到IMMS服务

  • 注册应用端进程死亡回调,在应用进程死亡时,调用IMMS的removeClient方法,解绑IMMS和应用的IInputMethodClient

addClient

IMMS.JAVA
    @Override
    public void addClient(IInputMethodClient client,
            IInputContext inputContext, int uid, int pid) {
        if (!calledFromValidUser()) {
            return;
        }
        synchronized (mMethodMap) {
            mClients.put(client.asBinder(), new ClientState(client,
                    inputContext, uid, pid));
        }
    }

将应用层的IInputMethodClient和IInputContext对象,存储在IMMS服务的本地map对象mClients中

linkToDeath

该过程client.asBinder().linkToDeath(this, 0);注册一个回到给应用层Binder,当进程死亡时,
会回调Session的binderDied方法,为应用程,Session和IMMS解绑

binderDied

@Override
Session.java

    public void binderDied() {
        // Note: it is safe to call in to the input method manager
        // here because we are not holding our lock.
        try {
            if (mService.mInputMethodManager != null) {
                //IMMS调用removeClient删除map中的记录
                mService.mInputMethodManager.removeClient(mClient);
            }
        } catch (RemoteException e) {
        }
        synchronized(mService.mWindowMap) {
            //应用端和Session解绑
            mClient.asBinder().unlinkToDeath(this, 0);
            mClientDead = true;
            //WMS删除关于改应用的Session对象,输入法应用服务未启动前,灭有Session被创建;创建过程,后续输入法弹出过程再讲
            killSessionLocked();
        }
    }
removeClient
IMMS.JAVA
    @Override
    public void removeClient(IInputMethodClient client) {
        if (!calledFromValidUser()) {
            return;
        }
        synchronized (mMethodMap) {
            ClientState cs = mClients.remove(client.asBinder());
            if (cs != null) {
                clearClientSessionLocked(cs);
                if (mCurClient == cs) {
                    if (mBoundToMethod) {
                        mBoundToMethod = false;
                        if (mCurMethod != null) {
                            executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
                                    MSG_UNBIND_INPUT, mCurMethod));
                        }
                    }
                    mCurClient = null;
                }
                if (mCurFocusedWindowClient == cs) {
                    mCurFocusedWindowClient = null;
                }
            }
        }
    }

删除存储在Map中的IInputMethodClient对象

killSessionLocked

本流程暂不涉及

窗口创建和WindowSession创建依赖图

输入法#拉起流程#窗口初始化#组件依赖图.png
  • 应用进程中,每个对象都是进程单例持有
mGlobal : WindowManagerGlobal
IMM : InputMethodManager
sWindowSession : IWindowSession
mClient : IInputMethodClient
mIInputContext : IInputContext

IInputContext:最总会被传递给InputMethod应用(如搜狗输入法);负责InputMethod进程和应用进程的编辑框的通信,如上屏、查询光标前后字符等
IInputMethodClient:IMMS使用该接口查找和IMS对应的客户端应用进程,并通知应用进程绑定/解绑输入法

  • IMMS服务中,每个ClientState代码一个应用进程,进程创建时,通过addClient创建;进程死亡时,通过removeClient删除

总结

attatch和getWindowSession主要完成PhoneWindow和Session的创建,并将IInputContext和IInputMethodClient通过addClient添加到IMMS
中;进程死亡时,通过removeClient删除应用ClientState

当窗口和Session创建完毕,应用集成将请求获取窗口焦点,下一章,我们看下窗口获取焦点过程,跟输入法相关的过程
Android 输入法(2) ,输入法窗口和应用窗口绑定

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,242评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,769评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,484评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,133评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,007评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,080评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,496评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,190评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,464评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,549评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,330评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,205评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,567评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,889评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,160评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,475评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,650评论 2 335