Merge "Fix FloatingToolbar refresh on menu changes." into oc-dev

am: 7c4998e614

Change-Id: Id7e6c2485fa3a0519b77dd64498808d255f10fbc
This commit is contained in:
Abodunrinwa Toki
2017-05-23 16:10:59 +00:00
committed by android-build-merger

View File

@@ -52,7 +52,6 @@ import android.view.animation.AnimationSet;
import android.view.animation.Transformation; import android.view.animation.Transformation;
import android.view.animation.AnimationUtils; import android.view.animation.AnimationUtils;
import android.view.animation.Interpolator; import android.view.animation.Interpolator;
import android.widget.AdapterView;
import android.widget.ArrayAdapter; import android.widget.ArrayAdapter;
import android.widget.ImageButton; import android.widget.ImageButton;
import android.widget.ImageView; import android.widget.ImageView;
@@ -67,6 +66,7 @@ import java.util.List;
import com.android.internal.R; import com.android.internal.R;
import com.android.internal.util.Preconditions; import com.android.internal.util.Preconditions;
import java.util.Objects;
/** /**
* A floating toolbar for showing contextual menu items. * A floating toolbar for showing contextual menu items.
@@ -82,12 +82,7 @@ public final class FloatingToolbar {
public static final String FLOATING_TOOLBAR_TAG = "floating_toolbar"; public static final String FLOATING_TOOLBAR_TAG = "floating_toolbar";
private static final MenuItem.OnMenuItemClickListener NO_OP_MENUITEM_CLICK_LISTENER = private static final MenuItem.OnMenuItemClickListener NO_OP_MENUITEM_CLICK_LISTENER =
new MenuItem.OnMenuItemClickListener() { item -> false;
@Override
public boolean onMenuItemClick(MenuItem item) {
return false;
}
};
private final Context mContext; private final Context mContext;
private final Window mWindow; private final Window mWindow;
@@ -97,7 +92,7 @@ public final class FloatingToolbar {
private final Rect mPreviousContentRect = new Rect(); private final Rect mPreviousContentRect = new Rect();
private Menu mMenu; private Menu mMenu;
private List<Object> mShowingMenuItems = new ArrayList<Object>(); private List<MenuItem> mShowingMenuItems = new ArrayList<>();
private MenuItem.OnMenuItemClickListener mMenuItemClickListener = NO_OP_MENUITEM_CLICK_LISTENER; private MenuItem.OnMenuItemClickListener mMenuItemClickListener = NO_OP_MENUITEM_CLICK_LISTENER;
private int mSuggestedWidth; private int mSuggestedWidth;
@@ -237,7 +232,7 @@ public final class FloatingToolbar {
if (!isCurrentlyShowing(menuItems) || mWidthChanged) { if (!isCurrentlyShowing(menuItems) || mWidthChanged) {
mPopup.dismiss(); mPopup.dismiss();
mPopup.layoutMenuItems(menuItems, mMenuItemClickListener, mSuggestedWidth); mPopup.layoutMenuItems(menuItems, mMenuItemClickListener, mSuggestedWidth);
mShowingMenuItems = getShowingMenuItemsReferences(menuItems); mShowingMenuItems = menuItems;
} }
if (!mPopup.isShowing()) { if (!mPopup.isShowing()) {
mPopup.show(mContentRect); mPopup.show(mContentRect);
@@ -252,7 +247,23 @@ public final class FloatingToolbar {
* Returns true if this floating toolbar is currently showing the specified menu items. * Returns true if this floating toolbar is currently showing the specified menu items.
*/ */
private boolean isCurrentlyShowing(List<MenuItem> menuItems) { private boolean isCurrentlyShowing(List<MenuItem> menuItems) {
return mShowingMenuItems.equals(getShowingMenuItemsReferences(menuItems)); if (mShowingMenuItems == null || menuItems.size() != mShowingMenuItems.size()) {
return false;
}
final int size = menuItems.size();
for (int i = 0; i < size; i++) {
final MenuItem menuItem = menuItems.get(i);
final MenuItem showingItem = mShowingMenuItems.get(i);
if (menuItem.getItemId() != showingItem.getItemId()
|| !TextUtils.equals(menuItem.getTitle(), showingItem.getTitle())
|| !Objects.equals(menuItem.getIcon(), showingItem.getIcon())
|| menuItem.getGroupId() != showingItem.getGroupId()) {
return false;
}
}
return true;
} }
/** /**
@@ -260,7 +271,7 @@ public final class FloatingToolbar {
* This method is recursive. * This method is recursive.
*/ */
private List<MenuItem> getVisibleAndEnabledMenuItems(Menu menu) { private List<MenuItem> getVisibleAndEnabledMenuItems(Menu menu) {
List<MenuItem> menuItems = new ArrayList<MenuItem>(); List<MenuItem> menuItems = new ArrayList<>();
for (int i = 0; (menu != null) && (i < menu.size()); i++) { for (int i = 0; (menu != null) && (i < menu.size()); i++) {
MenuItem menuItem = menu.getItem(i); MenuItem menuItem = menu.getItem(i);
if (menuItem.isVisible() && menuItem.isEnabled()) { if (menuItem.isVisible() && menuItem.isEnabled()) {
@@ -305,22 +316,6 @@ public final class FloatingToolbar {
} }
} }
private List<Object> getShowingMenuItemsReferences(List<MenuItem> menuItems) {
List<Object> references = new ArrayList<Object>();
for (MenuItem menuItem : menuItems) {
if (menuItem.getItemId() != Menu.NONE) {
references.add(menuItem.getItemId());
} else if (!TextUtils.isEmpty(menuItem.getTitle())) {
references.add(menuItem.getTitle());
} else if (menuItem.getIcon() != null){
references.add(menuItem.getIcon());
} else {
references.add(menuItem);
}
}
return references;
}
private void registerOrientationHandler() { private void registerOrientationHandler() {
unregisterOrientationHandler(); unregisterOrientationHandler();
mWindow.getDecorView().addOnLayoutChangeListener(mOrientationChangeHandler); mWindow.getDecorView().addOnLayoutChangeListener(mOrientationChangeHandler);
@@ -383,19 +378,15 @@ public final class FloatingToolbar {
private final Point mCoordsOnWindow = new Point(); // popup window coordinates. private final Point mCoordsOnWindow = new Point(); // popup window coordinates.
/* Temporary data holders. Reset values before using. */ /* Temporary data holders. Reset values before using. */
private final int[] mTmpCoords = new int[2]; private final int[] mTmpCoords = new int[2];
private final Rect mTmpRect = new Rect();
private final Region mTouchableRegion = new Region(); private final Region mTouchableRegion = new Region();
private final ViewTreeObserver.OnComputeInternalInsetsListener mInsetsComputer = private final ViewTreeObserver.OnComputeInternalInsetsListener mInsetsComputer =
new ViewTreeObserver.OnComputeInternalInsetsListener() { info -> {
public void onComputeInternalInsets( info.contentInsets.setEmpty();
ViewTreeObserver.InternalInsetsInfo info) { info.visibleInsets.setEmpty();
info.contentInsets.setEmpty(); info.touchableRegion.set(mTouchableRegion);
info.visibleInsets.setEmpty(); info.setTouchableInsets(
info.touchableRegion.set(mTouchableRegion); ViewTreeObserver.InternalInsetsInfo.TOUCHABLE_INSETS_REGION);
info.setTouchableInsets(ViewTreeObserver.InternalInsetsInfo
.TOUCHABLE_INSETS_REGION);
}
}; };
private final int mLineHeight; private final int mLineHeight;
@@ -1407,18 +1398,15 @@ public final class FloatingToolbar {
final ImageButton overflowButton = (ImageButton) LayoutInflater.from(mContext) final ImageButton overflowButton = (ImageButton) LayoutInflater.from(mContext)
.inflate(R.layout.floating_popup_overflow_button, null); .inflate(R.layout.floating_popup_overflow_button, null);
overflowButton.setImageDrawable(mOverflow); overflowButton.setImageDrawable(mOverflow);
overflowButton.setOnClickListener(new View.OnClickListener() { overflowButton.setOnClickListener(v -> {
@Override if (mIsOverflowOpen) {
public void onClick(View v) { overflowButton.setImageDrawable(mToOverflow);
if (mIsOverflowOpen) { mToOverflow.start();
overflowButton.setImageDrawable(mToOverflow); closeOverflow();
mToOverflow.start(); } else {
closeOverflow(); overflowButton.setImageDrawable(mToArrow);
} else { mToArrow.start();
overflowButton.setImageDrawable(mToArrow); openOverflow();
mToArrow.start();
openOverflow();
}
} }
}); });
return overflowButton; return overflowButton;
@@ -1441,13 +1429,10 @@ public final class FloatingToolbar {
}; };
overflowPanel.setAdapter(adapter); overflowPanel.setAdapter(adapter);
overflowPanel.setOnItemClickListener(new AdapterView.OnItemClickListener() { overflowPanel.setOnItemClickListener((parent, view, position, id) -> {
@Override MenuItem menuItem = (MenuItem) overflowPanel.getAdapter().getItem(position);
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { if (mOnMenuItemClickListener != null) {
MenuItem menuItem = (MenuItem) overflowPanel.getAdapter().getItem(position); mOnMenuItemClickListener.onMenuItemClick(menuItem);
if (mOnMenuItemClickListener != null) {
mOnMenuItemClickListener.onMenuItemClick(menuItem);
}
} }
}); });
@@ -1479,12 +1464,9 @@ public final class FloatingToolbar {
public void onAnimationEnd(Animation animation) { public void onAnimationEnd(Animation animation) {
// Posting this because it seems like this is called before the animation // Posting this because it seems like this is called before the animation
// actually ends. // actually ends.
mContentContainer.post(new Runnable() { mContentContainer.post(() -> {
@Override setPanelsStatesAtRestingPosition();
public void run() { setContentAreaAsTouchableSurface();
setPanelsStatesAtRestingPosition();
setContentAreaAsTouchableSurface();
}
}); });
} }