Skip to content
This repository has been archived by the owner on Apr 3, 2020. It is now read-only.

Commit

Permalink
Fix OEM menu issue
Browse files Browse the repository at this point in the history
- Some OEMs don't let us change the background, so the padding is incorrect.
- Also including sizing the menu too large causes clipping.

BUG=398837

Review URL: https://codereview.chromium.org/469673006

Cr-Commit-Position: refs/heads/master@{#291193}
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@291193 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
dtrainor@chromium.org committed Aug 21, 2014
1 parent cffd7f9 commit 2b66232
Showing 1 changed file with 24 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
Expand Down Expand Up @@ -106,6 +107,11 @@ public void onDismiss() {
}
});

// Some OEMs don't actually let us change the background... but they still return the
// padding of the new background, which breaks the menu height. If we still have a
// drawable here even though our style says @null we should use this padding instead...
Drawable originalBgDrawable = mPopup.getBackground();

// Need to explicitly set the background here. Relying on it being set in the style caused
// an incorrectly drawn background.
if (isByHardwareButton) {
Expand Down Expand Up @@ -140,6 +146,14 @@ public void onDismiss() {
}
}

Rect sizingPadding = new Rect(bgPadding);
if (isByHardwareButton && originalBgDrawable != null) {
Rect originalPadding = new Rect();
originalBgDrawable.getPadding(originalPadding);
sizingPadding.top = originalPadding.top;
sizingPadding.bottom = originalPadding.bottom;
}

boolean showMenuButton = !mIsByHardwareButton;
if (!SHOW_SW_MENU_BUTTON) showMenuButton = false;
// A List adapter for visible items in the Menu. The first row is added as a header to the
Expand All @@ -148,8 +162,8 @@ public void onDismiss() {
this, menuItems, LayoutInflater.from(context), showMenuButton);
mPopup.setAdapter(mAdapter);

setMenuHeight(menuItems.size(), visibleDisplayFrame, screenHeight);
setPopupOffset(mPopup, mCurrentScreenRotation, visibleDisplayFrame);
setMenuHeight(menuItems.size(), visibleDisplayFrame, screenHeight, sizingPadding);
setPopupOffset(mPopup, mCurrentScreenRotation, visibleDisplayFrame, sizingPadding);
mPopup.setOnItemClickListener(this);
mPopup.show();
mPopup.getListView().setItemsCanFocus(true);
Expand All @@ -175,9 +189,8 @@ public void onLayoutChange(View v, int left, int top, int right, int bottom,
}
}

private void setPopupOffset(ListPopupWindow popup, int screenRotation, Rect appRect) {
Rect paddingRect = new Rect();
popup.getBackground().getPadding(paddingRect);
private void setPopupOffset(
ListPopupWindow popup, int screenRotation, Rect appRect, Rect padding) {
int[] anchorLocation = new int[2];
popup.getAnchorView().getLocationInWindow(anchorLocation);
int anchorHeight = popup.getAnchorView().getHeight();
Expand All @@ -201,9 +214,9 @@ private void setPopupOffset(ListPopupWindow popup, int screenRotation, Rect appR
break;
}
popup.setHorizontalOffset(horizontalOffset);
// The menu is displayed above the anchored view, so shift the menu up by the top
// The menu is displayed above the anchored view, so shift the menu up by the bottom
// padding of the background.
popup.setVerticalOffset(-paddingRect.bottom);
popup.setVerticalOffset(-padding.bottom);
} else {
// The menu is displayed over and below the anchored view, so shift the menu up by the
// height of the anchor view.
Expand Down Expand Up @@ -274,7 +287,8 @@ ListPopupWindow getPopup() {
return mPopup;
}

private void setMenuHeight(int numMenuItems, Rect appDimensions, int screenHeight) {
private void setMenuHeight(
int numMenuItems, Rect appDimensions, int screenHeight, Rect padding) {
assert mPopup.getAnchorView() != null;
View anchorView = mPopup.getAnchorView();
int[] anchorViewLocation = new int[2];
Expand All @@ -289,9 +303,8 @@ private void setMenuHeight(int numMenuItems, Rect appDimensions, int screenHeigh
int availableScreenSpace = Math.max(anchorViewLocation[1],
appDimensions.height() - anchorViewLocation[1] - anchorViewImpactHeight);

Rect padding = new Rect();
mPopup.getBackground().getPadding(padding);
availableScreenSpace -= mIsByHardwareButton ? padding.top : padding.bottom;
availableScreenSpace -= padding.bottom;
if (mIsByHardwareButton) availableScreenSpace -= padding.top;

int numCanFit = availableScreenSpace / (mItemRowHeight + mItemDividerHeight);

Expand Down

0 comments on commit 2b66232

Please sign in to comment.