/* * Copyright (C) 2012 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package carbon.internal; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; import android.content.res.Configuration; import android.content.res.Resources; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.os.Parcelable; import android.support.v4.internal.view.SupportMenu; import android.support.v4.internal.view.SupportMenuItem; import android.support.v4.view.MenuItemCompat; import android.support.v7.appcompat.R; import android.support.v7.content.res.AppCompatResources; import android.support.v7.view.menu.MenuPresenter; import android.util.SparseArray; import android.view.KeyCharacterMap; import android.view.KeyEvent; import android.view.MenuItem; import android.view.SubMenu; import android.view.View; import java.util.ArrayList; import java.util.List; import carbon.CarbonContextWrapper; import carbon.widget.FloatingActionMenu; import carbon.widget.FloatingMenuItem; /** * Implementation of the {@link android.support.v4.internal.view.SupportMenu} interface for creating * a menu for {@link carbon.widget.FloatingActionButton}. */ public class FloatingMenuBuilder implements SupportMenu { private static final String TAG = "FloatingMenuBuilder"; private static final String PRESENTER_KEY = "android:menu:presenters"; private static final String ACTION_VIEW_STATES_KEY = "android:menu:actionviewstates"; private static final String EXPANDED_ACTION_VIEW_ID = "android:menu:expandedactionview"; private static final int[] sCategoryToOrder = new int[]{ 1, /* No category */ 4, /* CONTAINER */ 5, /* SYSTEM */ 3, /* SECONDARY */ 2, /* ALTERNATIVE */ 0, /* SELECTED_ALTERNATIVE */ }; private final Context mContext; private final Resources mResources; /** * Whether the shortcuts should be qwerty-accessible. Use isQwertyMode() instead of accessing * this directly. */ private boolean mQwertyMode; /** * Whether the shortcuts should be visible on menus. Use isShortcutsVisible() instead of * accessing this directly. */ private boolean mShortcutsVisible; /** * Callback that will receive the various menu-related events generated by this class. Use * getCallback to get a reference to the callback. */ private Callback mCallback; /** * Contains all of the items for this menu */ private ArrayList<FloatingMenuItem> mItems; /** * Contains only the items that are currently visible. This will be created/refreshed from * {@link #getVisibleItems()} */ private ArrayList<FloatingMenuItem> mVisibleItems; /** * Whether or not the items (or any one item's shown state) has changed since it was last * fetched from {@link #getVisibleItems()} */ private boolean mIsVisibleItemsStale; /** * Default value for how added items should show in the action list. */ private int mDefaultShowAsAction = SupportMenuItem.SHOW_AS_ACTION_NEVER; /** * Header title for menu types that have a header (context and submenus) */ CharSequence mHeaderTitle; /** * Header icon for menu types that have a header and support icons (context) */ Drawable mHeaderIcon; /** * Header custom view for menu types that have a header and support custom views (context) */ View mHeaderView; /** * Prevents onItemsChanged from doing its junk, useful for batching commands * that may individually call onItemsChanged. */ private boolean mPreventDispatchingItemsChanged = false; private boolean mItemsChangedWhileDispatchPrevented = false; private boolean mOptionalIconsVisible = false; private ArrayList<FloatingMenuItem> mTempShortcutItemList = new ArrayList<>(); private FloatingActionMenu menu; /** * Called by menu to notify of close and selection changes. * * @hide */ public interface Callback { /** * Called when a menu item is selected. * * @param menu The menu that is the parent of the item * @param item The menu item that is selected * @return whether the menu item selection was handled */ public boolean onMenuItemSelected(FloatingMenuBuilder menu, MenuItem item); /** * Called when the mode of the menu changes (for example, from icon to expanded). * * @param menu the menu that has changed modes */ public void onMenuModeChange(FloatingMenuBuilder menu); } public FloatingMenuBuilder(FloatingActionMenu menu) { this.menu = menu; mContext = new CarbonContextWrapper(menu.getContentView().getContext()); mResources = mContext.getResources(); mItems = new ArrayList<>(); mVisibleItems = new ArrayList<>(); mIsVisibleItemsStale = true; setShortcutsVisibleInner(true); } public FloatingMenuBuilder setDefaultShowAsAction(int defaultShowAsAction) { mDefaultShowAsAction = defaultShowAsAction; return this; } public void saveActionViewStates(Bundle outStates) { SparseArray<Parcelable> viewStates = null; final int itemCount = size(); for (int i = 0; i < itemCount; i++) { final MenuItem item = getItem(i); final View v = MenuItemCompat.getActionView(item); if (v != null && v.getId() != View.NO_ID) { if (viewStates == null) { viewStates = new SparseArray<Parcelable>(); } v.saveHierarchyState(viewStates); if (MenuItemCompat.isActionViewExpanded(item)) { outStates.putInt(EXPANDED_ACTION_VIEW_ID, item.getItemId()); } } } if (viewStates != null) { outStates.putSparseParcelableArray(getActionViewStatesKey(), viewStates); } } public void restoreActionViewStates(Bundle states) { if (states == null) { return; } SparseArray<Parcelable> viewStates = states.getSparseParcelableArray( getActionViewStatesKey()); final int itemCount = size(); for (int i = 0; i < itemCount; i++) { final MenuItem item = getItem(i); final View v = MenuItemCompat.getActionView(item); if (v != null && v.getId() != View.NO_ID) { v.restoreHierarchyState(viewStates); } } final int expandedId = states.getInt(EXPANDED_ACTION_VIEW_ID); if (expandedId > 0) { MenuItem itemToExpand = findItem(expandedId); if (itemToExpand != null) { MenuItemCompat.expandActionView(itemToExpand); } } } protected String getActionViewStatesKey() { return ACTION_VIEW_STATES_KEY; } public void setCallback(Callback cb) { mCallback = cb; } /** * Adds an item to the menu. The other add methods funnel to this. */ private MenuItem addInternal(int group, int id, int categoryOrder, CharSequence title) { final int ordering = getOrdering(categoryOrder); final FloatingMenuItem item = createNewMenuItem(group, id, categoryOrder, ordering, title, mDefaultShowAsAction); /* if (mCurrentMenuInfo != null) { // Pass along the current menu info item.setMenuInfo(mCurrentMenuInfo); } */ mItems.add(findInsertIndex(mItems, ordering), item); onItemsChanged(true); return item; } // Layoutlib overrides this method to return its custom implementation of FloatingMenuItem private FloatingMenuItem createNewMenuItem(int group, int id, int categoryOrder, int ordering, CharSequence title, int defaultShowAsAction) { return new FloatingMenuItem(group, id, categoryOrder, title, menu); } public MenuItem add(CharSequence title) { return addInternal(0, 0, 0, title); } @Override public MenuItem add(int titleRes) { return addInternal(0, 0, 0, mResources.getString(titleRes)); } @Override public MenuItem add(int group, int id, int categoryOrder, CharSequence title) { return addInternal(group, id, categoryOrder, title); } @Override public MenuItem add(int group, int id, int categoryOrder, int title) { return addInternal(group, id, categoryOrder, mResources.getString(title)); } @Override public SubMenu addSubMenu(CharSequence title) { return addSubMenu(0, 0, 0, title); } @Override public SubMenu addSubMenu(int titleRes) { return addSubMenu(0, 0, 0, mResources.getString(titleRes)); } @Override public SubMenu addSubMenu(int group, int id, int categoryOrder, CharSequence title) { throw new RuntimeException("not supported"); } @Override public SubMenu addSubMenu(int group, int id, int categoryOrder, int title) { return addSubMenu(group, id, categoryOrder, mResources.getString(title)); } @Override public int addIntentOptions(int group, int id, int categoryOrder, ComponentName caller, Intent[] specifics, Intent intent, int flags, MenuItem[] outSpecificItems) { PackageManager pm = mContext.getPackageManager(); final List<ResolveInfo> lri = pm.queryIntentActivityOptions(caller, specifics, intent, 0); final int N = lri != null ? lri.size() : 0; if ((flags & FLAG_APPEND_TO_GROUP) == 0) { removeGroup(group); } for (int i = 0; i < N; i++) { final ResolveInfo ri = lri.get(i); Intent rintent = new Intent( ri.specificIndex < 0 ? intent : specifics[ri.specificIndex]); rintent.setComponent(new ComponentName( ri.activityInfo.applicationInfo.packageName, ri.activityInfo.name)); final MenuItem item = add(group, id, categoryOrder, ri.loadLabel(pm)) .setIcon(ri.loadIcon(pm)) .setIntent(rintent); if (outSpecificItems != null && ri.specificIndex >= 0) { outSpecificItems[ri.specificIndex] = item; } } return N; } @Override public void removeItem(int id) { removeItemAtInt(findItemIndex(id), true); } @Override public void removeGroup(int group) { final int i = findGroupIndex(group); if (i >= 0) { final int maxRemovable = mItems.size() - i; int numRemoved = 0; while ((numRemoved++ < maxRemovable) && (mItems.get(i).getGroupId() == group)) { // Don't force update for each one, this method will do it at the end removeItemAtInt(i, false); } // Notify menu views onItemsChanged(true); } } /** * Remove the item at the given index and optionally forces menu views to * update. * * @param index The index of the item to be removed. If this index is * invalid an exception is thrown. * @param updateChildrenOnMenuViews Whether to force update on menu views. Please make sure you * eventually call this after your batch of removals. */ private void removeItemAtInt(int index, boolean updateChildrenOnMenuViews) { if ((index < 0) || (index >= mItems.size())) return; mItems.remove(index); if (updateChildrenOnMenuViews) onItemsChanged(true); } public void removeItemAt(int index) { removeItemAtInt(index, true); } public void clearAll() { mPreventDispatchingItemsChanged = true; clear(); clearHeader(); mPreventDispatchingItemsChanged = false; mItemsChangedWhileDispatchPrevented = false; onItemsChanged(true); } @Override public void clear() { mItems.clear(); onItemsChanged(true); } @Override public void setGroupCheckable(int group, boolean checkable, boolean exclusive) { throw new RuntimeException("not supported"); } @Override public void setGroupVisible(int group, boolean visible) { final int N = mItems.size(); // We handle the notification of items being changed ourselves, so we use setVisibleInt rather // than setVisible and at the end notify of items being changed boolean changedAtLeastOneItem = false; for (int i = 0; i < N; i++) { FloatingMenuItem item = mItems.get(i); if (item.getGroupId() == group) { if (item.isVisible() != visible) { item.setVisible(visible); changedAtLeastOneItem = true; } } } if (changedAtLeastOneItem) onItemsChanged(true); } @Override public void setGroupEnabled(int group, boolean enabled) { final int N = mItems.size(); for (int i = 0; i < N; i++) { FloatingMenuItem item = mItems.get(i); if (item.getGroupId() == group) { item.setEnabled(enabled); } } } @Override public boolean hasVisibleItems() { final int size = size(); for (int i = 0; i < size; i++) { FloatingMenuItem item = mItems.get(i); if (item.isVisible()) { return true; } } return false; } @Override public MenuItem findItem(int id) { final int size = size(); for (int i = 0; i < size; i++) { FloatingMenuItem item = mItems.get(i); if (item.getItemId() == id) { return item; } else if (item.hasSubMenu()) { MenuItem possibleItem = item.getSubMenu().findItem(id); if (possibleItem != null) { return possibleItem; } } } return null; } public int findItemIndex(int id) { final int size = size(); for (int i = 0; i < size; i++) { FloatingMenuItem item = mItems.get(i); if (item.getItemId() == id) { return i; } } return -1; } public int findGroupIndex(int group) { return findGroupIndex(group, 0); } public int findGroupIndex(int group, int start) { final int size = size(); if (start < 0) { start = 0; } for (int i = start; i < size; i++) { final FloatingMenuItem item = mItems.get(i); if (item.getGroupId() == group) { return i; } } return -1; } @Override public int size() { return mItems.size(); } @Override public MenuItem getItem(int index) { return mItems.get(index); } @Override public boolean isShortcutKey(int keyCode, KeyEvent event) { return findItemWithShortcutForKey(keyCode, event) != null; } @Override public void setQwertyMode(boolean isQwerty) { mQwertyMode = isQwerty; onItemsChanged(false); } /** * Returns the ordering across all items. This will grab the category from * the upper bits, find out how to order the category with respect to other * categories, and combine it with the lower bits. * * @param categoryOrder The category order for a particular item (if it has not been or/add with * a category, the default category is assumed). * @return An ordering integer that can be used to order this item across all the items (even * from other categories). */ private static int getOrdering(int categoryOrder) { final int index = (categoryOrder & CATEGORY_MASK) >> CATEGORY_SHIFT; if (index < 0 || index >= sCategoryToOrder.length) { throw new IllegalArgumentException("order does not contain a valid category."); } return (sCategoryToOrder[index] << CATEGORY_SHIFT) | (categoryOrder & USER_MASK); } /** * @return whether the menu shortcuts are in qwerty mode or not */ boolean isQwertyMode() { return mQwertyMode; } /** * Sets whether the shortcuts should be visible on menus. Devices without hardware key input * will never make shortcuts visible even if this method is passed 'true'. * * @param shortcutsVisible Whether shortcuts should be visible (if true and a menu item does not * have a shortcut defined, that item will still NOT show a shortcut) */ public void setShortcutsVisible(boolean shortcutsVisible) { if (mShortcutsVisible == shortcutsVisible) { return; } setShortcutsVisibleInner(shortcutsVisible); onItemsChanged(false); } private void setShortcutsVisibleInner(boolean shortcutsVisible) { mShortcutsVisible = shortcutsVisible && mResources.getConfiguration().keyboard != Configuration.KEYBOARD_NOKEYS && mResources.getBoolean(R.bool.abc_config_showMenuShortcutsWhenKeyboardPresent); } /** * @return Whether shortcuts should be visible on menus. */ public boolean isShortcutsVisible() { return mShortcutsVisible; } Resources getResources() { return mResources; } public Context getContext() { return mContext; } boolean dispatchMenuItemSelected(FloatingMenuBuilder menu, MenuItem item) { return mCallback != null && mCallback.onMenuItemSelected(menu, item); } /** * Dispatch a mode change event to this menu's callback. */ public void changeMenuMode() { if (mCallback != null) { mCallback.onMenuModeChange(this); } } private static int findInsertIndex(ArrayList<FloatingMenuItem> items, int ordering) { for (int i = items.size() - 1; i >= 0; i--) { FloatingMenuItem item = items.get(i); if (item.getOrder() <= ordering) { return i + 1; } } return 0; } @Override public boolean performShortcut(int keyCode, KeyEvent event, int flags) { final FloatingMenuItem item = findItemWithShortcutForKey(keyCode, event); boolean handled = false; if (item != null) { handled = performItemAction(item, flags); } return handled; } /* * This function will return all the menu and sub-menu items that can * be directly (the shortcut directly corresponds) and indirectly * (the ALT-enabled char corresponds to the shortcut) associated * with the keyCode. */ @SuppressWarnings("deprecation") void findItemsWithShortcutForKey(List<FloatingMenuItem> items, int keyCode, KeyEvent event) { final boolean qwerty = isQwertyMode(); final int metaState = event.getMetaState(); final KeyCharacterMap.KeyData possibleChars = new KeyCharacterMap.KeyData(); // Get the chars associated with the keyCode (i.e using any chording combo) final boolean isKeyCodeMapped = event.getKeyData(possibleChars); // The delete key is not mapped to '\b' so we treat it specially if (!isKeyCodeMapped && (keyCode != KeyEvent.KEYCODE_DEL)) { return; } // Look for an item whose shortcut is this key. final int N = mItems.size(); for (int i = 0; i < N; i++) { FloatingMenuItem item = mItems.get(i); if (item.hasSubMenu()) { ((FloatingMenuBuilder) item.getSubMenu()).findItemsWithShortcutForKey(items, keyCode, event); } final char shortcutChar = qwerty ? item.getAlphabeticShortcut() : item.getNumericShortcut(); if (((metaState & (KeyEvent.META_SHIFT_ON | KeyEvent.META_SYM_ON)) == 0) && (shortcutChar != 0) && (shortcutChar == possibleChars.meta[0] || shortcutChar == possibleChars.meta[2] || (qwerty && shortcutChar == '\b' && keyCode == KeyEvent.KEYCODE_DEL)) && item.isEnabled()) { items.add(item); } } } /* * We want to return the menu item associated with the key, but if there is no * ambiguity (i.e. there is only one menu item corresponding to the key) we want * to return it even if it's not an exact match; this allow the user to * _not_ use the ALT key for example, making the use of shortcuts slightly more * user-friendly. An example is on the G1, '!' and '1' are on the same key, and * in Gmail, Menu+1 will trigger Menu+! (the actual shortcut). * * On the other hand, if two (or more) shortcuts corresponds to the same key, * we have to only return the exact match. */ @SuppressWarnings("deprecation") FloatingMenuItem findItemWithShortcutForKey(int keyCode, KeyEvent event) { // Get all items that can be associated directly or indirectly with the keyCode ArrayList<FloatingMenuItem> items = mTempShortcutItemList; items.clear(); findItemsWithShortcutForKey(items, keyCode, event); if (items.isEmpty()) { return null; } final int metaState = event.getMetaState(); final KeyCharacterMap.KeyData possibleChars = new KeyCharacterMap.KeyData(); // Get the chars associated with the keyCode (i.e using any chording combo) event.getKeyData(possibleChars); // If we have only one element, we can safely returns it final int size = items.size(); if (size == 1) { return items.get(0); } final boolean qwerty = isQwertyMode(); // If we found more than one item associated with the key, // we have to return the exact match for (int i = 0; i < size; i++) { final FloatingMenuItem item = items.get(i); final char shortcutChar = qwerty ? item.getAlphabeticShortcut() : item.getNumericShortcut(); if ((shortcutChar == possibleChars.meta[0] && (metaState & KeyEvent.META_ALT_ON) == 0) || (shortcutChar == possibleChars.meta[2] && (metaState & KeyEvent.META_ALT_ON) != 0) || (qwerty && shortcutChar == '\b' && keyCode == KeyEvent.KEYCODE_DEL)) { return item; } } return null; } @Override public boolean performIdentifierAction(int id, int flags) { // Look for an item whose identifier is the id. return performItemAction(findItem(id), flags); } public boolean performItemAction(MenuItem item, int flags) { return performItemAction(item, null, flags); } public boolean performItemAction(MenuItem item, MenuPresenter preferredPresenter, int flags) { FloatingMenuItem itemImpl = (FloatingMenuItem) item; if (itemImpl == null || !itemImpl.isEnabled()) { return false; } itemImpl.performAction(); return true; } @Override public void close() { } /** * Called when an item is added or removed. * * @param structureChanged true if the menu structure changed, false if only item properties * changed. (Visibility is a structural property since it affects * layout.) */ public void onItemsChanged(boolean structureChanged) { if (!mPreventDispatchingItemsChanged) { if (structureChanged) { mIsVisibleItemsStale = true; } } else { mItemsChangedWhileDispatchPrevented = true; } } /** * Stop dispatching item changed events to presenters until * {@link #startDispatchingItemsChanged()} is called. Useful when * many menu operations are going to be performed as a batch. */ public void stopDispatchingItemsChanged() { if (!mPreventDispatchingItemsChanged) { mPreventDispatchingItemsChanged = true; mItemsChangedWhileDispatchPrevented = false; } } public void startDispatchingItemsChanged() { mPreventDispatchingItemsChanged = false; if (mItemsChangedWhileDispatchPrevented) { mItemsChangedWhileDispatchPrevented = false; onItemsChanged(true); } } /** * Called by {@link FloatingMenuItem} when its visible flag is changed. * * @param item The item that has gone through a visibility change. */ void onItemVisibleChanged(FloatingMenuItem item) { // Notify of items being changed mIsVisibleItemsStale = true; onItemsChanged(true); } /** * Called by {@link FloatingMenuItem} when its action request status is changed. * * @param item The item that has gone through a change in action request status. */ void onItemActionRequestChanged(FloatingMenuItem item) { // Notify of items being changed onItemsChanged(true); } public ArrayList<FloatingMenuItem> getVisibleItems() { if (!mIsVisibleItemsStale) return mVisibleItems; // Refresh the visible items mVisibleItems.clear(); final int itemsSize = mItems.size(); FloatingMenuItem item; for (int i = 0; i < itemsSize; i++) { item = mItems.get(i); if (item.isVisible()) mVisibleItems.add(item); } mIsVisibleItemsStale = false; return mVisibleItems; } public void clearHeader() { mHeaderIcon = null; mHeaderTitle = null; mHeaderView = null; onItemsChanged(false); } private void setHeaderInternal(final int titleRes, final CharSequence title, final int iconRes, final Drawable icon, final View view) { if (view != null) { mHeaderView = view; // If using a custom view, then the title and icon aren't used mHeaderTitle = null; mHeaderIcon = null; } else { if (titleRes > 0) { mHeaderTitle = getResources().getText(titleRes); } else if (title != null) { mHeaderTitle = title; } if (iconRes > 0) { mHeaderIcon = AppCompatResources.getDrawable(getContext(), iconRes); } else if (icon != null) { mHeaderIcon = icon; } // If using the title or icon, then a custom view isn't used mHeaderView = null; } // Notify of change onItemsChanged(false); } /** * Sets the header's title. This replaces the header view. Called by the * builder-style methods of subclasses. * * @param title The new title. * @return This FloatingMenuBuilder so additional setters can be called. */ protected FloatingMenuBuilder setHeaderTitleInt(CharSequence title) { setHeaderInternal(0, title, 0, null, null); return this; } /** * Sets the header's title. This replaces the header view. Called by the * builder-style methods of subclasses. * * @param titleRes The new title (as a resource ID). * @return This FloatingMenuBuilder so additional setters can be called. */ protected FloatingMenuBuilder setHeaderTitleInt(int titleRes) { setHeaderInternal(titleRes, null, 0, null, null); return this; } /** * Sets the header's icon. This replaces the header view. Called by the * builder-style methods of subclasses. * * @param icon The new icon. * @return This FloatingMenuBuilder so additional setters can be called. */ protected FloatingMenuBuilder setHeaderIconInt(Drawable icon) { setHeaderInternal(0, null, 0, icon, null); return this; } /** * Sets the header's icon. This replaces the header view. Called by the * builder-style methods of subclasses. * * @param iconRes The new icon (as a resource ID). * @return This FloatingMenuBuilder so additional setters can be called. */ protected FloatingMenuBuilder setHeaderIconInt(int iconRes) { setHeaderInternal(0, null, iconRes, null, null); return this; } /** * Sets the header's view. This replaces the title and icon. Called by the * builder-style methods of subclasses. * * @param view The new view. * @return This FloatingMenuBuilder so additional setters can be called. */ protected FloatingMenuBuilder setHeaderViewInt(View view) { setHeaderInternal(0, null, 0, null, view); return this; } public CharSequence getHeaderTitle() { return mHeaderTitle; } public Drawable getHeaderIcon() { return mHeaderIcon; } public View getHeaderView() { return mHeaderView; } /** * Gets the root menu (if this is a submenu, find its root menu). * * @return The root menu. */ public FloatingMenuBuilder getRootMenu() { return this; } void setOptionalIconsVisible(boolean visible) { mOptionalIconsVisible = visible; } boolean getOptionalIconsVisible() { return mOptionalIconsVisible; } }