Android Insets相关知识总结

作者:wayne啦 时间:2023-04-24 09:08:23 

目录
  • 什么是Insets?

  • Insets相关类

    • InsetsState

    • InsetsStateController

    • InsetsSource

    • InsetsSourceConsumer(ImeInsetsSourceConsumer)

    • ImeInsetsSourceConsumer

    • InsetsController

  • InsetsChanged、InsetsControlChanged方法

    • onStateChanged

    • onControlsChanged

  • 总结

    最近工作中总会涉及到Insets相关的一些内容,网上对于Insets的分析以及介绍还是较少的,这里对Insets涉及到一些概念和方法做一个总结。

    什么是Insets?

    WindowInsets 源码解释为 window content的一系列插值集合,(个人理解为 一个Activity相对于手机屏幕需要空出的地方以腾纳给statusbar、Ime、Navigationbar等系统窗口,具体表现为该区域需要的上下左右的宽高,比如输入法窗口的区域就是一个Inset)

    Android Insets相关知识总结

    WindowInsets包括三类:SystemWindowInsets、StableInsets、WIndowDecorInsets

    • SystemWindowInsets:全窗口下,被navigationbar、statusbar、ime或其他系统窗口覆盖的区域

    • StableInsets:全窗口下,被系统UI覆盖的区域

    • WIndowDecorInsets:系统预留属性

    Insets相关类

    InsetsState

    保存系统中所有的Insets的状态,他是状态描述者,持有系统中可以产生Window Insets的window状态 private InsetsSource[] mSources = new InsetsSource[SIZE]; // mSources变量维护所有产生Insets的window(也就是InsetsSource)的状态

    它主要持有以下几种类型的Insets


    ITYPE_STATUS_BAR,
    ITYPE_NAVIGATION_BAR,
    ITYPE_CAPTION_BAR,
    ITYPE_TOP_GESTURES,
    ITYPE_BOTTOM_GESTURES,
    ITYPE_LEFT_GESTURES,
    ITYPE_RIGHT_GESTURES,
    ITYPE_TOP_TAPPABLE_ELEMENT,
    ITYPE_BOTTOM_TAPPABLE_ELEMENT,
    ITYPE_LEFT_DISPLAY_CUTOUT,
    ITYPE_TOP_DISPLAY_CUTOUT,
    ITYPE_RIGHT_DISPLAY_CUTOUT,
    ITYPE_BOTTOM_DISPLAY_CUTOUT,
    ITYPE_IME,
    ITYPE_CLIMATE_BAR,
    ITYPE_EXTRA_NAVIGATION_BAR

    如果InsetsState发生改变后,会通过MSG_INSETS_CHANGED消息发送到InsetsController,进行修改并保存到变量mState中


    public boolean onStateChanged(InsetsState state) {
     boolean stateChanged = !mState.equals(state, true /* excludingCaptionInsets */,false /* excludeInvisibleIme */) || !captionInsetsUnchanged();
     if (!stateChanged && mLastDispatchedState.equals(state)) {
       return false;
     }
     updateState(state);

    boolean localStateChanged = !mState.equals(mLastDispatchedState,
         true /* excludingCaptionInsets */, true /* excludeInvisibleIme */);
     mLastDispatchedState.set(state, true /* copySources */);

    applyLocalVisibilityOverride();
     if (localStateChanged) {
       if (DEBUG) Log.d(TAG, "onStateChanged, notifyInsetsChanged, send state to WM: " + mState);
       mHost.notifyInsetsChanged();
       updateRequestedState();
     }
     return true;
    }

    InsetsState的关键方法:


    WindowInsets calculateInsets(...):基于当前source设置计算新的windowInsets
    void processSource(InsetsSource source,...): 根据计算值更新source值

    InsetsStateController

    管理所有窗口的Insets的state


    private final InsetsState mLastState = new InsetsState(); //旧的InsetsState
    private final InsetsState mState = new InsetsState(); //新的InsetsState

    几个重要的方法:


    private boolean isAboveIme(WindowContainer target)// 判断当前窗口是否处在输入法窗口层级上
    void onImeControlTargetChanged(@Nullable InsetsControlTarget imeTarget) //当输入法target 窗口发生变化触发
    InsetsState getInsetsForDispatch(@NonNull WindowState target) //分发Insets 对Insets进一步更新(更新frame 或者visible)

    InsetsSource

    是Insets产生者的描述,记录每一个产生Insets的window的状态,主要记录产生的Insets区域


    private final @InternalInsetsType int mType;  //Insets类型 nav或者status或者...
    private final Rect mFrame;  //代表Insets区域
    private boolean mVisible;   //Insets可见性

    /*几个重要的方法/


    public void setFrame(Rect frame)  //设置Insets大小
    public void setVisible(boolean visible) //设置Insets可见性
    private Insets calculateInsets(Rect relativeFrame, Rect frame, boolean ignoreVisibility)  //根据frame以及ignoreVisibility 计算Insets

    InsetsSourceConsumer(ImeInsetsSourceConsumer)

    对单一InsetsSource的消费者,其内部持有InsetsSourceControl,可以控制其leash的可见性和动画,输入法有专门的ImeInsetsSourceConsumer来消费输入法的Insets


    protected boolean mRequestedVisible;  //单一Insets的可见性
    private @Nullable InsetsSourceControl mSourceControl; // 持有InsetsSourceControl变量可以实现对单一InsetsSource的控制
    protected final InsetsController mController; //所属的InsetController
    protected final InsetsState mState;  //本地state

    /几个重要的方法/


    public void updateSource(InsetsSource newSource, @AnimationType int animationType)  //更新mstate中的source 主要更新frame
    public void show(boolean fromIme) //显示Insets
    protected void setRequestedVisible(boolean requestedVisible) //设置Insets的可见性
    public void setControl(@Nullable InsetsSourceControl control,
       @InsetsType int[] showTypes, @InsetsType int[] hideTypes) //后面讲
    public void hide() //隐藏Insets
    boolean applyLocalVisibilityOverride() //主要更新state可见性
    protected boolean isRequestedVisibleAwaitingControl() //判断当前Insets是否会在获得control时更新可见性,即判断是否存在pending show(如果是bars 该方法等同于isRequestedVisible)

    ImeInsetsSourceConsumer


    private boolean mIsRequestedVisibleAwaitingControl;  //判断是否存在一个请求要让输入法显示出来(但是由于当前尚未获得control因此暂时无法实现这个操作)
    void notifyHidden()  //控制IMM隐藏输入法
    public @ShowResult int requestShow(boolean fromIme) //控制IMM显示输入法
    public void removeSurface() //移除输入法的surface
    - InsetsSourceControl
    对InsetsSource的控制者,用来控制Insets的产生者,内部持有控制输入法动画的Leash
    private final @InternalInsetsType int mType;  //InsetsSource类型
    private final @Nullable SurfaceControl mLeash;  //播放动画需要的Leash ,app可以控制对其设置position实现位移动画
    private final Point mSurfacePosition;  //当前leash(Surface)在屏幕中的position
    - InsetsSourceProvider
    他是特定InsetsSource在server端的控制者,他被称作provider是因为他提供InsetsSource给客户端(客户端通过InsetsSourceConsumer使用InsetsSource)

    这里重点关注ImeInsetsSourceProvider


    private InsetsControlTarget mImeTargetFromIme;  //输入法Insets的control(Insets需要有一个control,否则他就会失控 不可控制)
    private Runnable mShowImeRunner;  //显示输入法线程
    private boolean mIsImeLayoutDrawn; //输入法是否已经绘制完成

    InsetsController

    它是WindowInsets在client端的实现 用来控制insets ,InsetsController只在ViewRootImpl里面创建的,每个Window会对应一个ViewRootImpl,同样每个ViewRootImpl会对应每个InsetsController


    /*关键成员变量*/
    InsetsState mState = new InsetsState();  //记录本地State (Client端的Insetsstate)
    InsetsState mLastDispatchedState = new InsetsState(); //从system端传来的InsetsState
    InsetsState mRequestedState = new InsetsState(); //发送给系统端的InsetsState
    SparseArray<InsetsSourceConsumer> mSourceConsumers = new SparseArray<>(); //持有sourceConsumers

    /*关键方法*/
    public void applyImeVisibility(boolean setVisible) //更新输入法可见性
    public void notifyFinished(InsetsAnimationControlRunner runner, boolean shown) //动画结束时回调方法
    public void onControlsChanged(InsetsSourceControl[] activeControls) //当系统端分发新的Insets Controls时被调用
    public boolean onStateChanged(InsetsState state) //Insets或者InsetsControl发生改变会调用
    public void setSystemBarsBehavior(@Behavior int behavior)
    public void setSystemBarsAppearance(@Appearance int appearance, @Appearance int mask)  //更改Systembar的表现行为
    public void show(@InsetsType int types, boolean fromIme) //显示Insets
    void hide(@InsetsType int types, boolean fromIme)  //隐藏Insets
    private void updateState(InsetsState newState) //更新state
    private void updateRequestedState() //如果Insets在client端发生改变再重新发送到server端
    public void applyAnimation(@InsetsType final int types, boolean show, boolean fromIme)  //更新Insets动画

    InsetsChanged、InsetsControlChanged方法

    Insets的变化一般是通过消息机制来进行更改的,主要是两方面的更改包括InsetsChanged和InsetsControlChanged,他们是由System_server经过WindowState调用到App进程的。


    WindowState.java //属于Server端
    void notifyInsetsChanged() {
     ProtoLog.d(WM_DEBUG_IME, "notifyInsetsChanged for %s ", this);
     try {
       mClient.insetsChanged(getInsetsState());
     } catch (RemoteException e) {
       Slog.w(TAG, "Failed to deliver inset state change w=" + this, e);
     }
    }

    ViewRootImpl#W
    @Override
    public void insetsChanged(InsetsState insetsState) {
     final ViewRootImpl viewAncestor = mViewAncestor.get();
     if (viewAncestor != null) {
       viewAncestor.dispatchInsetsChanged(insetsState);
     }
    }

    @Override
    public void insetsControlChanged(InsetsState insetsState,
       InsetsSourceControl[] activeControls) {
     final ViewRootImpl viewAncestor = mViewAncestor.get();
     if (viewAncestor != null) {
       viewAncestor.dispatchInsetsControlChanged(insetsState, activeControls);
     }
    }

    异步发送消息:MSG_INSETS_CHANGED、MSG_INSETS_CONTROL_CHANGED


    case MSG_INSETS_CHANGED:
     mInsetsController.onStateChanged((InsetsState) msg.obj);
     break;
    case MSG_INSETS_CONTROL_CHANGED: {
     mInsetsController.onStateChanged((InsetsState) args.arg1);
     mInsetsController.onControlsChanged((InsetsSourceControl[]) args.arg2);
     break;  //首先都会调用InsetsController的onStateChanged方法
    }

    onStateChanged


    public boolean onStateChanged(InsetsState state) {
     boolean stateChanged = !mState.equals(state, true /* excludingCaptionInsets */,false /* excludeInvisibleIme */) //判断client端state和传来的state是否一致
         || !captionInsetsUnchanged();
     //同时判断上次server端传来的state是否同当前传传来的state一致
     if (!stateChanged && mLastDispatchedState.equals(state)) {
       return false;
     }
     if (DEBUG) Log.d(TAG, "onStateChanged: " + state);
     updateState(state);
     //判断client端本地state是否已经发生改变
     boolean localStateChanged = !mState.equals(mLastDispatchedState,
         true /* excludingCaptionInsets */, true /* excludeInvisibleIme */);
     //更新mLastDispatchedState 即更新server端传来的state
     mLastDispatchedState.set(state, true /* copySources */);
     //将更新apply到本地
     applyLocalVisibilityOverride();
     if (localStateChanged) {
       if (DEBUG) Log.d(TAG, "onStateChanged, notifyInsetsChanged, send state to WM: " + mState);
       //如果本地Insets发生改变了,通知server端Insets更改了
       mHost.notifyInsetsChanged();
       //更新传递给server端的InsetsState
       updateRequestedState();
     }
     return true;
    }

    onControlsChanged

    该方法在窗口获取焦点或者失去焦点的时候也会调用到


    public void onControlsChanged(InsetsSourceControl[] activeControls) {
     if (activeControls != null) {
       for (InsetsSourceControl activeControl : activeControls) {
         if (activeControl != null) {
           // TODO(b/122982984): Figure out why it can be null.
           mTmpControlArray.put(activeControl.getType(), activeControl);
         }
       }
     }

    boolean requestedStateStale = false;
     final int[] showTypes = new int[1]; //系统Insets会根据showTypes数组内的值去更新可见性
     final int[] hideTypes = new int[1];

    //遍历所有的SourceConsumer 更新system_server传来的InsetsSourceControl
     for (int i = mSourceConsumers.size() - 1; i >= 0; i--) {
       final InsetsSourceConsumer consumer = mSourceConsumers.valueAt(i);
       final InsetsSourceControl control = mTmpControlArray.get(consumer.getType());
       consumer.setControl(control, showTypes, hideTypes);
     }

    // Ensure to create source consumers if not available yet.
     //便利system_server传递来的InsetsSourceControl
     for (int i = mTmpControlArray.size() - 1; i >= 0; i--) {
       final InsetsSourceControl control = mTmpControlArray.valueAt(i);
       final @InternalInsetsType int type = control.getType();
       final InsetsSourceConsumer consumer = getSourceConsumer(type);
    //如果consumer不存在会创建
       consumer.setControl(control, showTypes, hideTypes); //可以看到如果存在対赢得consumer 会调用setControl方法两次

    ...

    }
     mTmpControlArray.clear();

    //showTypes、hideTypes值会在setControl方法内进行修改
     int animatingTypes = invokeControllableInsetsChangedListeners();
     showTypes[0] &= ~animatingTypes;
     hideTypes[0] &= ~animatingTypes;

    //假设showTypes[0]=8 代表要显示输入法
     if (showTypes[0] != 0) {
       applyAnimation(showTypes[0], true /* show */, false /* fromIme */);
     }
     //假设hideTypes[0]=8 代表要隐藏输入法
     if (hideTypes[0] != 0) {
       applyAnimation(hideTypes[0], false /* show */, false /* fromIme */);
     }
     if (requestedStateStale) {
       updateRequestedState();
     }
    }

    来源:https://juejin.cn/post/6940988120661753863

    标签:Android,Insets
    0
    投稿

    猜你喜欢

  • Java并发编程之JUC并发核心AQS同步队列原理剖析

    2023-01-15 15:14:37
  • Spring框架的环境搭建和测试实现

    2023-10-27 17:57:57
  • Spring Shell应用程序开发流程解析

    2021-06-28 23:20:50
  • Spring Security中用JWT退出登录时遇到的坑

    2022-05-19 10:30:28
  • Android高级组件AutoCompleteTextView自动完成文本框使用详解

    2022-04-19 07:49:28
  • 通过实例解析Spring Ioc项目实现过程

    2023-11-24 10:12:33
  • java 内部类(匿名类,匿名对象,静态内部类)详解及实例

    2022-09-25 11:20:16
  • Java实现循环体的过滤器的方法

    2023-11-22 09:35:33
  • Android开发笔记之:一分钟学会使用Logcat调试程序的详解

    2022-07-19 05:17:22
  • Android用动画显示或隐藏视图

    2023-08-05 20:07:25
  • c#基于opencv,开发摄像头播放程序

    2023-06-20 11:54:29
  • java8 stream中Collectors.toMap空指针问题及解决

    2023-01-16 13:05:28
  • Android简单实现圆盘抽奖界面

    2022-07-25 08:35:27
  • C#使用iTextSharp操作PDF

    2023-07-29 02:03:11
  • springMVC的生命周期详解

    2022-10-29 22:27:40
  • C#中正则表达式(Regex)过滤内容的基本使用方法

    2023-11-26 12:51:00
  • C#实现IDisposable接口释放非托管资源

    2022-12-01 06:59:53
  • Java如何执行cmd命令

    2022-05-02 09:54:45
  • Java实现滑动验证码的示例代码

    2022-08-27 15:10:12
  • Mybatis使用JSONObject接收数据库查询的方法

    2023-01-17 05:10:43
  • asp之家 软件编程 m.aspxhome.com