Skip to content

Commit 275702c

Browse files
committed
Fix bug 5355889 - Search action showing up in the menu dropdown in
spite of search actionview being expanded Make sure that menu items with an expanded action view don't show up in list menus presenting the rest of the menu. Change-Id: I8c7b4e184a9d3ea2457543d0b8b36bc8e7068052
1 parent b410330 commit 275702c

4 files changed

Lines changed: 104 additions & 17 deletions

File tree

core/java/com/android/internal/view/menu/ActionMenuPresenter.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,19 @@ public void updateMenuView(boolean cleared) {
200200
}
201201
}
202202

203-
final boolean hasOverflow = mReserveOverflow && mMenu != null &&
204-
mMenu.getNonActionItems().size() > 0;
203+
final ArrayList<MenuItemImpl> nonActionItems = mMenu != null ?
204+
mMenu.getNonActionItems() : null;
205+
206+
boolean hasOverflow = false;
207+
if (mReserveOverflow && nonActionItems != null) {
208+
final int count = nonActionItems.size();
209+
if (count == 1) {
210+
hasOverflow = !nonActionItems.get(0).isActionViewExpanded();
211+
} else {
212+
hasOverflow = count > 0;
213+
}
214+
}
215+
205216
if (hasOverflow) {
206217
if (mOverflowButton == null) {
207218
mOverflowButton = new OverflowMenuButton(mContext);

core/java/com/android/internal/view/menu/ListMenuPresenter.java

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.android.internal.view.menu;
1818

1919
import android.content.Context;
20+
import android.database.DataSetObserver;
2021
import android.os.Bundle;
2122
import android.os.Parcelable;
2223
import android.util.SparseArray;
@@ -47,7 +48,7 @@ public class ListMenuPresenter implements MenuPresenter, AdapterView.OnItemClick
4748
int mItemLayoutRes;
4849

4950
private Callback mCallback;
50-
private MenuAdapter mAdapter;
51+
MenuAdapter mAdapter;
5152

5253
private int mId;
5354

@@ -216,14 +217,29 @@ public void onRestoreInstanceState(Parcelable state) {
216217
}
217218

218219
private class MenuAdapter extends BaseAdapter {
220+
private int mExpandedIndex = -1;
221+
222+
public MenuAdapter() {
223+
registerDataSetObserver(new ExpandedIndexObserver());
224+
findExpandedIndex();
225+
}
226+
219227
public int getCount() {
220228
ArrayList<MenuItemImpl> items = mMenu.getNonActionItems();
221-
return items.size() - mItemIndexOffset;
229+
int count = items.size() - mItemIndexOffset;
230+
if (mExpandedIndex < 0) {
231+
return count;
232+
}
233+
return count - 1;
222234
}
223235

224236
public MenuItemImpl getItem(int position) {
225237
ArrayList<MenuItemImpl> items = mMenu.getNonActionItems();
226-
return items.get(position + mItemIndexOffset);
238+
position += mItemIndexOffset;
239+
if (mExpandedIndex >= 0 && position >= mExpandedIndex) {
240+
position++;
241+
}
242+
return items.get(position);
227243
}
228244

229245
public long getItemId(int position) {
@@ -241,5 +257,28 @@ public View getView(int position, View convertView, ViewGroup parent) {
241257
itemView.initialize(getItem(position), 0);
242258
return convertView;
243259
}
260+
261+
void findExpandedIndex() {
262+
final MenuItemImpl expandedItem = mMenu.getExpandedItem();
263+
if (expandedItem != null) {
264+
final ArrayList<MenuItemImpl> items = mMenu.getNonActionItems();
265+
final int count = items.size();
266+
for (int i = 0; i < count; i++) {
267+
final MenuItemImpl item = items.get(i);
268+
if (item == expandedItem) {
269+
mExpandedIndex = i;
270+
return;
271+
}
272+
}
273+
}
274+
mExpandedIndex = -1;
275+
}
276+
}
277+
278+
private class ExpandedIndexObserver extends DataSetObserver {
279+
@Override
280+
public void onChanged() {
281+
mAdapter.findExpandedIndex();
282+
}
244283
}
245284
}

core/java/com/android/internal/view/menu/MenuBuilder.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1258,4 +1258,8 @@ public boolean collapseItemActionView(MenuItemImpl item) {
12581258
}
12591259
return collapsed;
12601260
}
1261+
1262+
public MenuItemImpl getExpandedItem() {
1263+
return mExpandedItem;
1264+
}
12611265
}

core/java/com/android/internal/view/menu/MenuPopupHelper.java

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import android.content.Context;
2020
import android.content.res.Resources;
21+
import android.database.DataSetObserver;
2122
import android.os.Parcelable;
2223
import android.view.KeyEvent;
2324
import android.view.LayoutInflater;
@@ -286,22 +287,45 @@ public boolean collapseItemActionView(MenuBuilder menu, MenuItemImpl item) {
286287
return false;
287288
}
288289

290+
@Override
291+
public int getId() {
292+
return 0;
293+
}
294+
295+
@Override
296+
public Parcelable onSaveInstanceState() {
297+
return null;
298+
}
299+
300+
@Override
301+
public void onRestoreInstanceState(Parcelable state) {
302+
}
303+
289304
private class MenuAdapter extends BaseAdapter {
290305
private MenuBuilder mAdapterMenu;
306+
private int mExpandedIndex = -1;
291307

292308
public MenuAdapter(MenuBuilder menu) {
293309
mAdapterMenu = menu;
310+
registerDataSetObserver(new ExpandedIndexObserver());
311+
findExpandedIndex();
294312
}
295313

296314
public int getCount() {
297315
ArrayList<MenuItemImpl> items = mOverflowOnly ?
298316
mAdapterMenu.getNonActionItems() : mAdapterMenu.getVisibleItems();
299-
return items.size();
317+
if (mExpandedIndex < 0) {
318+
return items.size();
319+
}
320+
return items.size() - 1;
300321
}
301322

302323
public MenuItemImpl getItem(int position) {
303324
ArrayList<MenuItemImpl> items = mOverflowOnly ?
304325
mAdapterMenu.getNonActionItems() : mAdapterMenu.getVisibleItems();
326+
if (mExpandedIndex >= 0 && position >= mExpandedIndex) {
327+
position++;
328+
}
305329
return items.get(position);
306330
}
307331

@@ -323,19 +347,28 @@ public View getView(int position, View convertView, ViewGroup parent) {
323347
itemView.initialize(getItem(position), 0);
324348
return convertView;
325349
}
326-
}
327-
328-
@Override
329-
public int getId() {
330-
return 0;
331-
}
332350

333-
@Override
334-
public Parcelable onSaveInstanceState() {
335-
return null;
351+
void findExpandedIndex() {
352+
final MenuItemImpl expandedItem = mMenu.getExpandedItem();
353+
if (expandedItem != null) {
354+
final ArrayList<MenuItemImpl> items = mMenu.getNonActionItems();
355+
final int count = items.size();
356+
for (int i = 0; i < count; i++) {
357+
final MenuItemImpl item = items.get(i);
358+
if (item == expandedItem) {
359+
mExpandedIndex = i;
360+
return;
361+
}
362+
}
363+
}
364+
mExpandedIndex = -1;
365+
}
336366
}
337367

338-
@Override
339-
public void onRestoreInstanceState(Parcelable state) {
368+
private class ExpandedIndexObserver extends DataSetObserver {
369+
@Override
370+
public void onChanged() {
371+
mAdapter.findExpandedIndex();
372+
}
340373
}
341374
}

0 commit comments

Comments
 (0)