More list menu fixes

Fix Menu not having it's callback set. Caused by a timing issue
in ToolbarActionBar's new list menu. If Toolbar.getMenu() is
called before Toolbar.setMenuCallbacks() then the menu
callback is never used. Fixed by making sure we set
the callbacks before getMenu() is called.

Also fixes the scenario where Toolbar does not return a panel
view (no action-items to display), resulting in the standard
'window' panels being checked. This previously meant that the
Toolbar's panel view is never used. We now never check the
'window' panels if there is a Toolbar list menu presenter
available.

BUG: 17049242

Change-Id: I355db4c782b38cd09f7def29224af849b74da0b7
diff --git a/v7/appcompat/src/android/support/v7/app/ActionBarActivityDelegateBase.java b/v7/appcompat/src/android/support/v7/app/ActionBarActivityDelegateBase.java
index 1c34c88..bc1f5dd 100644
--- a/v7/appcompat/src/android/support/v7/app/ActionBarActivityDelegateBase.java
+++ b/v7/appcompat/src/android/support/v7/app/ActionBarActivityDelegateBase.java
@@ -478,8 +478,11 @@
                 panelView = callback.onCreatePanelView(featureId);
             }
 
-            if (panelView == null) {
-                // If the callback didn't return a view, check our panels
+            if (panelView == null && mToolbarListMenuPresenter == null) {
+                // Only check our panels if the callback didn't return a view and we do not have
+                // a ListMenuPresenter for Toolbars. We check for the ListMenuPresenter because
+                // once created, Toolbar needs to control the panel view regardless of whether it
+                // has any non-action items to display.
                 PanelFeatureState st = getPanelState(featureId, true);
                 openPanel(st, null);
                 if (st.isOpen) {
diff --git a/v7/appcompat/src/android/support/v7/internal/app/ToolbarActionBar.java b/v7/appcompat/src/android/support/v7/internal/app/ToolbarActionBar.java
index 0b5e200..08f8552 100644
--- a/v7/appcompat/src/android/support/v7/internal/app/ToolbarActionBar.java
+++ b/v7/appcompat/src/android/support/v7/internal/app/ToolbarActionBar.java
@@ -449,11 +449,7 @@
     }
 
     void populateOptionsMenu() {
-        if (!mMenuCallbackSet) {
-            mToolbar.setMenuCallbacks(new ActionMenuPresenterCallback(), new MenuBuilderCallback());
-            mMenuCallbackSet = true;
-        }
-        final Menu menu = mToolbar.getMenu();
+        final Menu menu = getMenu();
         final MenuBuilder mb = menu instanceof MenuBuilder ? (MenuBuilder) menu : null;
         if (mb != null) {
             mb.stopDispatchingItemsChanged();
@@ -537,7 +533,7 @@
 
                     if (mToolbarMenuPrepared && mWindowCallback != null) {
                         // If we are prepared, check to see if the callback wants a menu opened
-                        final Menu menu = mToolbar.getMenu();
+                        final Menu menu = getMenu();
 
                         if (mWindowCallback.onPreparePanel(featureId, null, menu) &&
                                 mWindowCallback.onMenuOpened(featureId, menu)) {
@@ -550,8 +546,16 @@
         }
     }
 
+    private Menu getMenu() {
+        if (!mMenuCallbackSet) {
+            mToolbar.setMenuCallbacks(new ActionMenuPresenterCallback(), new MenuBuilderCallback());
+            mMenuCallbackSet = true;
+        }
+        return mToolbar.getMenu();
+    }
+
     public void setListMenuPresenter(ListMenuPresenter listMenuPresenter) {
-        Menu menu = mToolbar.getMenu();
+        final Menu menu = getMenu();
 
         if (menu instanceof MenuBuilder) {
             MenuBuilder mb = (MenuBuilder) menu;