寫在前面,轉載者注:
該文章主要分析了phonewindow中對expanded Menu處理的流程,介紹的較爲詳細,可供參考。代碼是基於4.1的,同樣適用於4.4。
1. PhoneWindow.onKeyDown()
1. onKeyDownPanel.
當Menu鍵按下去之後,會產生一個KeyEvent,是keyDown事件,如果Activity沒有處理這個Menu Down事件,就會由PhonwWindow默認onKeyDown處理。
在onKeyDown中只要就是調用了onKeyDownPanel把事件傳遞給對應的panel。
- protected boolean onKeyDown(int featureId, int keyCode, KeyEvent event) {
- switch (keyCode) {
- ... ...
- case KeyEvent.KEYCODE_MENU: {
- onKeyDownPanel((featureId < 0) ? FEATURE_OPTIONS_PANEL : featureId, event);
- return true;
- }
- ... ...
- }
- return false;
- }
1.1 PhoneWindow.onKeyDownPanel(int featureId, KeyEvent event)
1. getPanelState()
根據featureId獲得對應的PanelFeatureState,PanelFeatureState用來保存一個panel對象,比如option menu是對應一個PanelFeatureState 和 featureId。
2. preparePanel()
由於是第一次得到PanelFeatureState,st.isOpen返回false,所以就去preparePanel。
- public final boolean onKeyDownPanel(int featureId, KeyEvent event) {
- final int keyCode = event.getKeyCode();
- if (event.getRepeatCount() == 0) {
- // The panel key was pushed, so set the chording key
- mPanelChordingKey = keyCode;
- PanelFeatureState st = getPanelState(featureId, true);
- if (!st.isOpen) {
- return preparePanel(st, event);
- }
- }
- return false;
- }
1.1.1 PhoneWindow. getPanelState(int featureId, boolean required, PanelFeatureState convertPanelState)
這個getPanelState也很簡單,如果存在與featureId對應的PanelFeatureState就直接返回,沒有的就new一個新的。也就是如果如果我們再當前Activity中,如果是第一次按menu的話,應該是不存在FEATURE_OPTIONS_PANEL相對應的PanelFeatureState。於是就會new一個新的出來。
- private PanelFeatureState getPanelState(int featureId, boolean required,
- PanelFeatureState convertPanelState) {
- PanelFeatureState[] ar;
- if ((ar = mPanels) == null || ar.length <= featureId) {
- PanelFeatureState[] nar = new PanelFeatureState[featureId + 1];
- if (ar != null) {
- System.arraycopy(ar, 0, nar, 0, ar.length);
- }
- mPanels = ar = nar;
- }
- PanelFeatureState st = ar[featureId];
- if (st == null) {
- ar[featureId] = st = (convertPanelState != null)
- ? convertPanelState
- : new PanelFeatureState(featureId);
- }
- return st;
- }
1. 通過Callback(Activity)的onCreatePanelView去創建一個panel,用戶可以複寫這個方法, 如果用戶沒有複寫,則st.createPanelView = null。
2. initializePanelMenu(), 初始化st中的MenuBuilder,用於以後創建Menu。
3. 調用cb.onPreparePanel,就會調用Activity的onPreaparePanel方法,而在onPreparePanel中會調用onPrepareOptionsMenu去完成 option menu的prepare工作。完成之後把st中的狀態設置一下。 st.isPrepared = true; st.isHandled = false; mPreparedPanel = st;
- public final boolean preparePanel(PanelFeatureState st, KeyEvent event) {
- final Callback cb = getCallback();
- if (cb != null) {
- st.createdPanelView = cb.onCreatePanelView(st.featureId);
- }
- if (st.createdPanelView == null) {
- // Init the panel state's menu--return false if init failed
- if (st.menu == null || st.refreshMenuContent) {
- if (st.menu == null) {
- if (!initializePanelMenu(st) || (st.menu == null)) {
- return false;
- }
- }
- if (mActionBar != null) {
- if (mActionMenuPresenterCallback == null) {
- mActionMenuPresenterCallback = new ActionMenuPresenterCallback();
- }
- mActionBar.setMenu(st.menu, mActionMenuPresenterCallback);
- }
- st.menu.stopDispatchingItemsChanged();
- st.refreshMenuContent = false;
- }
- // Callback and return if the callback does not want to show the menu
- // Preparing the panel menu can involve a lot of manipulation;
- // don't dispatch change events to presenters until we're done.
- st.menu.stopDispatchingItemsChanged();
- if (!cb.onPreparePanel(st.featureId, st.createdPanelView, st.menu)) {
- if (mActionBar != null) {
- // The app didn't want to show the menu for now but it still exists.
- // Clear it out of the action bar.
- mActionBar.setMenu(null, mActionMenuPresenterCallback);
- }
- st.menu.startDispatchingItemsChanged();
- return false;
- }
- ... ...
- }
- // Set other state
- st.isPrepared = true;
- st.isHandled = false;
- mPreparedPanel = st;
- return true;
- }
2. PhoneWindow.onKeyUp()
跟KeyDown對應,按鍵起來之後會有一個up事件。而Menu的up事件也是又PhoneWindow來處理。
1. onKeyUpPanel
- protected boolean onKeyUp(int featureId, int keyCode, KeyEvent event) {
- ... ...
- case KeyEvent.KEYCODE_MENU: {
- onKeyUpPanel(featureId < 0 ? FEATURE_OPTIONS_PANEL : featureId,
- event);
- return true;
- }
- ... ...
- }
2.1 PhoneWindow.onKeyUpPanel(int featureId, KeyEvent event)
1. 如果panel在前面的down事件中已經prepare成功了,調用 openPanel(st, event);去打開option menu。並且把 playSoundEffect 設爲 true;
PS: 會打出Event log。
2.
- public final void onKeyUpPanel(int featureId, KeyEvent event) {
- // The panel key was released, so clear the chording key
- if (mPanelChordingKey != 0) {
- ... ...
- boolean playSoundEffect = false;
- final PanelFeatureState st = getPanelState(featureId, true);
- if (featureId == FEATURE_OPTIONS_PANEL && mActionBar != null &&
- mActionBar.isOverflowReserved()) {
- if (mActionBar.getVisibility() == View.VISIBLE) {
- if (!mActionBar.isOverflowMenuShowing()) {
- if (!isDestroyed() && preparePanel(st, event)) {
- playSoundEffect = mActionBar.showOverflowMenu();
- }
- } else {
- playSoundEffect = mActionBar.hideOverflowMenu();
- }
- }
- } else {
- if (st.isOpen || st.isHandled) {
- // Play the sound effect if the user closed an open menu (and not if
- // they just released a menu shortcut)
- playSoundEffect = st.isOpen;
- // Close menu
- closePanel(st, true);
- } else if (st.isPrepared) {
- boolean show = true;
- if (st.refreshMenuContent) {
- // Something may have invalidated the menu since we prepared it.
- // Re-prepare it to refresh.
- st.isPrepared = false;
- show = preparePanel(st, event);
- }
- if (show) {
- // Write 'menu opened' to event log
- EventLog.writeEvent(50001, 0);
- // Show menu
- openPanel(st, event);
- playSoundEffect = true;
- }
- }
- }
- if (playSoundEffect) {
- AudioManager audioManager = (AudioManager) getContext().getSystemService(
- Context.AUDIO_SERVICE);
- if (audioManager != null) {
- audioManager.playSoundEffect(AudioManager.FX_KEY_CLICK);
- } else {
- Log.w(TAG, "Couldn't get audio manager");
- }
- }
- }
- }
2.1.1 PhoneWindow.openPanel(PanelFeatureState st, KeyEvent event)
在打開panel之前,會對平臺的屏幕進行檢測,如果發現不是手機,就直接返回。
1. initializePanelDecor(PanelFeatureState st) //初始化對應panel的DecorView; 如果menu沒有item的話,就直接return
2. initializePanelContent(PanelFeatureState st), //初始化Panel的MenuView,並賦給st.shownPanelView;
3. 把st.shownPanelView寄到decorView中,然後通過wm.addView(st.decorView, lp);把新建出來的option menu的decorView加到WMS中。
這樣option menu就顯示出來了。
- private void openPanel(PanelFeatureState st, KeyEvent event) {
- Callback cb = getCallback();
- if ((cb != null) && (!cb.onMenuOpened(st.featureId, st.menu))) {
- // Callback doesn't want the menu to open, reset any state
- closePanel(st, true);
- return;
- }
- final WindowManager wm = getWindowManager();
- int width = WRAP_CONTENT;
- if (st.decorView == null || st.refreshDecorView) {
- if (st.decorView == null) {
- // Initialize the panel decor, this will populate st.decorView
- if (!initializePanelDecor(st) || (st.decorView == null))
- return;
- } else if (st.refreshDecorView && (st.decorView.getChildCount() > 0)) {
- // Decor needs refreshing, so remove its views
- st.decorView.removeAllViews();
- }
- // This will populate st.shownPanelView
- if (!initializePanelContent(st) || !st.hasPanelItems()) {
- return;
- }
- ViewGroup.LayoutParams lp = st.shownPanelView.getLayoutParams();
- if (lp == null) {
- lp = new ViewGroup.LayoutParams(WRAP_CONTENT, WRAP_CONTENT);
- }
- int backgroundResId;
- if (lp.width == ViewGroup.LayoutParams.MATCH_PARENT) {
- // If the contents is fill parent for the width, set the
- // corresponding background
- backgroundResId = st.fullBackground;
- width = MATCH_PARENT;
- } else {
- // Otherwise, set the normal panel background
- backgroundResId = st.background;
- }
- st.decorView.setWindowBackground(getContext().getResources().getDrawable(
- backgroundResId));
- ViewParent shownPanelParent = st.shownPanelView.getParent();
- if (shownPanelParent != null && shownPanelParent instanceof ViewGroup) {
- ((ViewGroup) shownPanelParent).removeView(st.shownPanelView);
- }
- st.decorView.addView(st.shownPanelView, lp);
- /*
- * Give focus to the view, if it or one of its children does not
- * already have it.
- */
- if (!st.shownPanelView.hasFocus()) {
- st.shownPanelView.requestFocus();
- }
- } else if (!st.isInListMode()) {
- width = MATCH_PARENT;
- } else if (st.createdPanelView != null) {
- // If we already had a panel view, carry width=MATCH_PARENT through
- // as we did above when it was created.
- ViewGroup.LayoutParams lp = st.createdPanelView.getLayoutParams();
- if (lp != null && lp.width == ViewGroup.LayoutParams.MATCH_PARENT) {
- width = MATCH_PARENT;
- }
- }
- st.isOpen = true;
- st.isHandled = false;
- WindowManager.LayoutParams lp = new WindowManager.LayoutParams(
- width, WRAP_CONTENT,
- st.x, st.y, WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG,
- WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM
- | WindowManager.LayoutParams.FLAG_SPLIT_TOUCH,
- st.decorView.mDefaultOpacity);
- if (st.isCompact) {
- lp.gravity = getOptionsPanelGravity();
- sRotationWatcher.addWindow(this);
- } else {
- lp.gravity = st.gravity;
- }
- lp.windowAnimations = st.windowAnimations;
- wm.addView(st.decorView, lp);
- // Log.v(TAG, "Adding main menu to window manager.");
- }
2.1.1.1 PhoneWindow.initializePanelDecor(PanelFeatureState st)
new出一個DecorView並且賦值給st.decorView。
- protected boolean initializePanelDecor(PanelFeatureState st) {
- st.decorView = new DecorView(getContext(), st.featureId);
- st.gravity = Gravity.CENTER | Gravity.BOTTOM;
- st.setStyle(getContext());
- return true;
- }
2.1.1.2 PhoneWindow. initializePanelContent(PanelFeatureState st)
//初始化Panel的MenuView,並賦給st.shownPanelView;
- protected boolean initializePanelContent(PanelFeatureState st) {
- if (st.createdPanelView != null) {
- st.shownPanelView = st.createdPanelView;
- return true;
- }
- if (st.menu == null) {
- return false;
- }
- if (mPanelMenuPresenterCallback == null) {
- mPanelMenuPresenterCallback = new PanelMenuPresenterCallback();
- }
- MenuView menuView = st.isInListMode()
- ? st.getListMenuView(getContext(), mPanelMenuPresenterCallback)
- : st.getIconMenuView(getContext(), mPanelMenuPresenterCallback);
- st.shownPanelView = (View) menuView;
- if (st.shownPanelView != null) {
- // Use the menu View's default animations if it has any
- final int defaultAnimations = menuView.getWindowAnimations();
- if (defaultAnimations != 0) {
- st.windowAnimations = defaultAnimations;
- }
- return true;
- } else {
- return false;
- }
- }