Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 773b1b97 authored by Adam Powell's avatar Adam Powell
Browse files

Make ActionBar menus handle configuration changes gracefully.

Change-Id: I2c9ca846143b313ef9d2a01dff81385e0693518d
parent 2b342f0a
Loading
Loading
Loading
Loading
+23 −7
Original line number Diff line number Diff line
@@ -57,17 +57,33 @@ public class ActionMenuView extends LinearLayout implements MenuBuilder.ItemInvo
        mItemMargin = mItemPadding / 2;
        a.recycle();

        // Measure for initial configuration
        mMaxItems = measureMaxActionButtons();

        // TODO There has to be a better way to indicate that we don't have a hard menu key.
        final int screen = getResources().getConfiguration().screenLayout;
        mReserveOverflow = (screen & Configuration.SCREENLAYOUT_SIZE_MASK) ==
                Configuration.SCREENLAYOUT_SIZE_XLARGE;
    }

    public void onConfigurationChanged(Configuration newConfig) {
        final int screen = newConfig.screenLayout;
        mReserveOverflow = (screen & Configuration.SCREENLAYOUT_SIZE_MASK) ==
                Configuration.SCREENLAYOUT_SIZE_XLARGE;
        mMaxItems = measureMaxActionButtons();
        if (mMenu != null) {
            mMenu.setMaxActionItems(mMaxItems);
            updateChildren(false);
        }
    }

    private int measureMaxActionButtons() {
        final Resources res = getResources();
        final int size = res.getDimensionPixelSize(com.android.internal.R.dimen.action_icon_size);
        final int spaceAvailable = res.getDisplayMetrics().widthPixels / 2;
        final int itemSpace = size + mItemPadding;
        
        mMaxItems = spaceAvailable / (itemSpace > 0 ? itemSpace : 1);

        // TODO There has to be a better way to indicate that we don't have a hard menu key.
        final int screen = res.getConfiguration().screenLayout;
        mReserveOverflow = (screen & Configuration.SCREENLAYOUT_SIZE_MASK) ==
                Configuration.SCREENLAYOUT_SIZE_XLARGE;
        return spaceAvailable / (itemSpace > 0 ? itemSpace : 1);
    }

    public boolean isOverflowReserved() {