Android Insets相关知识总结

 更新时间:2021年03月22日 10:21:00   作者:wayne啦  
这篇文章主要介绍了Android Insets相关知识总结,帮助大家更好的理解和学习使用Android,感兴趣的朋友可以了解下

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

什么是Insets?

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

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();
  }
}

总结

  1. 每个ViewRootImpl对应一个InsetsController实例,他是一个App进程中控制Insets的核心类,用于保存传递系统中产生Insets的window的状态和动画需要的leash以及控制播放动画
  2. InsetsSource是对产生Insets的窗口的状态描述,包括可见性以及Insets的大小
  3. 每个InsetsController会持有一个成员变量mState(InsetsState),它保存了系统中所有产生Insets的Window(InsetsSource)的状态列表,状态主要是指可见性以及产生Insets的window的区域大小
  4. InsetsSourceConsumer 是用来消费特定InsetsSource,消费主要是指对产生Insets 的window即InsetsSource进行可见性控制以及播放动画,通过持有的window的Leash来实现,也就是mSourceControl(InsetsSourceControl)
  5. 每个InsetsController会持有多个InsetsSourceConsumer,他持有一个InsetsSourceConsumers列表,SparseArray mSourceConsumers

到这里Insets已经总结完毕,后续将进一步通过源码分析Insets的原理以及和App之间的关系,由于水平有限,难免有错误,若在阅读时发现不妥或者错误的地方留言指正,共同进步,谢谢!

Have a nice day!

以上就是Android Insets相关知识总结的详细内容,更多关于Android Insets的资料请关注脚本之家其它相关文章!

相关文章

最新评论