Android Insets相關知識總結

最近工作中總會涉及到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的資料請關註WalkonNet其它相關文章!

推薦閱讀: