/
NavigationMenuPresenter.java
928 lines (809 loc) · 29 KB
/
NavigationMenuPresenter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
/*
* Copyright (C) 2015 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 com.google.android.material.internal;
import com.google.android.material.R;
import static androidx.annotation.RestrictTo.Scope.LIBRARY_GROUP;
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.RippleDrawable;
import android.os.Bundle;
import android.os.Parcelable;
import androidx.appcompat.view.menu.MenuBuilder;
import androidx.appcompat.view.menu.MenuItemImpl;
import androidx.appcompat.view.menu.MenuPresenter;
import androidx.appcompat.view.menu.MenuView;
import androidx.appcompat.view.menu.SubMenuBuilder;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.RecyclerViewAccessibilityDelegate;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.SubMenu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.Dimension;
import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.Px;
import androidx.annotation.RestrictTo;
import androidx.annotation.StyleRes;
import androidx.core.view.AccessibilityDelegateCompat;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.core.view.accessibility.AccessibilityNodeInfoCompat;
import androidx.core.view.accessibility.AccessibilityNodeInfoCompat.CollectionInfoCompat;
import androidx.core.view.accessibility.AccessibilityNodeInfoCompat.CollectionItemInfoCompat;
import androidx.core.widget.TextViewCompat;
import java.util.ArrayList;
/** @hide */
@RestrictTo(LIBRARY_GROUP)
public class NavigationMenuPresenter implements MenuPresenter {
public static final int NO_TEXT_APPEARANCE_SET = 0;
private static final String STATE_HIERARCHY = "android:menu:list";
private static final String STATE_ADAPTER = "android:menu:adapter";
private static final String STATE_HEADER = "android:menu:header";
private NavigationMenuView menuView;
LinearLayout headerLayout;
private Callback callback;
MenuBuilder menu;
private int id;
NavigationMenuAdapter adapter;
LayoutInflater layoutInflater;
int subheaderTextAppearance = NO_TEXT_APPEARANCE_SET;
@Nullable ColorStateList subheaderColor;
int textAppearance = NO_TEXT_APPEARANCE_SET;
ColorStateList textColor;
ColorStateList iconTintList;
Drawable itemBackground;
RippleDrawable itemForeground;
int itemHorizontalPadding;
@Px int itemVerticalPadding;
int itemIconPadding;
int itemIconSize;
@Px int dividerInsetStart;
@Px int dividerInsetEnd;
@Px int subheaderInsetStart;
@Px int subheaderInsetEnd;
boolean hasCustomItemIconSize;
boolean isBehindStatusBar = true;
private int itemMaxLines;
/**
* Padding to be inserted at the top of the list to avoid the first menu item from being placed
* underneath the status bar.
*/
private int paddingTopDefault;
/** Padding for separators between items */
int paddingSeparator;
private int overScrollMode = -1;
@Override
public void initForMenu(@NonNull Context context, @NonNull MenuBuilder menu) {
layoutInflater = LayoutInflater.from(context);
this.menu = menu;
Resources res = context.getResources();
paddingSeparator =
res.getDimensionPixelOffset(R.dimen.design_navigation_separator_vertical_padding);
}
@Override
public MenuView getMenuView(ViewGroup root) {
if (menuView == null) {
menuView =
(NavigationMenuView) layoutInflater.inflate(R.layout.design_navigation_menu, root, false);
menuView.setAccessibilityDelegateCompat(
new NavigationMenuViewAccessibilityDelegate(menuView));
if (adapter == null) {
adapter = new NavigationMenuAdapter();
}
if (overScrollMode != -1) {
menuView.setOverScrollMode(overScrollMode);
}
headerLayout =
(LinearLayout)
layoutInflater.inflate(R.layout.design_navigation_item_header, menuView, false);
menuView.setAdapter(adapter);
}
return menuView;
}
@Override
public void updateMenuView(boolean cleared) {
if (adapter != null) {
adapter.update();
}
}
@Override
public void setCallback(Callback cb) {
callback = cb;
}
@Override
public boolean onSubMenuSelected(SubMenuBuilder subMenu) {
return false;
}
@Override
public void onCloseMenu(MenuBuilder menu, boolean allMenusAreClosing) {
if (callback != null) {
callback.onCloseMenu(menu, allMenusAreClosing);
}
}
@Override
public boolean flagActionItems() {
return false;
}
@Override
public boolean expandItemActionView(MenuBuilder menu, MenuItemImpl item) {
return false;
}
@Override
public boolean collapseItemActionView(MenuBuilder menu, MenuItemImpl item) {
return false;
}
@Override
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@NonNull
@Override
public Parcelable onSaveInstanceState() {
final Bundle state = new Bundle();
if (menuView != null) {
SparseArray<Parcelable> hierarchy = new SparseArray<>();
menuView.saveHierarchyState(hierarchy);
state.putSparseParcelableArray(STATE_HIERARCHY, hierarchy);
}
if (adapter != null) {
state.putBundle(STATE_ADAPTER, adapter.createInstanceState());
}
if (headerLayout != null) {
SparseArray<Parcelable> header = new SparseArray<>();
headerLayout.saveHierarchyState(header);
state.putSparseParcelableArray(STATE_HEADER, header);
}
return state;
}
@Override
public void onRestoreInstanceState(final Parcelable parcelable) {
if (parcelable instanceof Bundle) {
Bundle state = (Bundle) parcelable;
SparseArray<Parcelable> hierarchy = state.getSparseParcelableArray(STATE_HIERARCHY);
if (hierarchy != null) {
menuView.restoreHierarchyState(hierarchy);
}
Bundle adapterState = state.getBundle(STATE_ADAPTER);
if (adapterState != null) {
adapter.restoreInstanceState(adapterState);
}
SparseArray<Parcelable> header = state.getSparseParcelableArray(STATE_HEADER);
if (header != null) {
headerLayout.restoreHierarchyState(header);
}
}
}
public void setCheckedItem(@NonNull MenuItemImpl item) {
adapter.setCheckedItem(item);
}
@Nullable
public MenuItemImpl getCheckedItem() {
return adapter.getCheckedItem();
}
public View inflateHeaderView(@LayoutRes int res) {
View view = layoutInflater.inflate(res, headerLayout, false);
addHeaderView(view);
return view;
}
public void addHeaderView(@NonNull View view) {
headerLayout.addView(view);
// The padding on top should be cleared.
menuView.setPadding(0, 0, 0, menuView.getPaddingBottom());
}
public void removeHeaderView(@NonNull View view) {
headerLayout.removeView(view);
if (headerLayout.getChildCount() == 0) {
menuView.setPadding(0, paddingTopDefault, 0, menuView.getPaddingBottom());
}
}
public int getHeaderCount() {
return headerLayout.getChildCount();
}
public View getHeaderView(int index) {
return headerLayout.getChildAt(index);
}
public void setSubheaderColor(@Nullable ColorStateList subheaderColor) {
this.subheaderColor = subheaderColor;
updateMenuView(false);
}
public void setSubheaderTextAppearance(@StyleRes int resId) {
subheaderTextAppearance = resId;
updateMenuView(false);
}
@Nullable
public ColorStateList getItemTintList() {
return iconTintList;
}
public void setItemIconTintList(@Nullable ColorStateList tint) {
iconTintList = tint;
updateMenuView(false);
}
@Nullable
public ColorStateList getItemTextColor() {
return textColor;
}
public void setItemTextColor(@Nullable ColorStateList textColor) {
this.textColor = textColor;
updateMenuView(false);
}
public void setItemTextAppearance(@StyleRes int resId) {
textAppearance = resId;
updateMenuView(false);
}
@Nullable
public Drawable getItemBackground() {
return itemBackground;
}
public void setItemBackground(@Nullable Drawable itemBackground) {
this.itemBackground = itemBackground;
updateMenuView(false);
}
public void setItemForeground(@Nullable RippleDrawable itemForeground) {
this.itemForeground = itemForeground;
updateMenuView(false);
}
public int getItemHorizontalPadding() {
return itemHorizontalPadding;
}
public void setItemHorizontalPadding(int itemHorizontalPadding) {
this.itemHorizontalPadding = itemHorizontalPadding;
updateMenuView(false);
}
@Px
public int getItemVerticalPadding() {
return itemVerticalPadding;
}
public void setItemVerticalPadding(@Px int itemVerticalPadding) {
this.itemVerticalPadding = itemVerticalPadding;
updateMenuView(false);
}
@Px
public int getDividerInsetStart() {
return this.dividerInsetStart;
}
public void setDividerInsetStart(@Px int dividerInsetStart) {
this.dividerInsetStart = dividerInsetStart;
updateMenuView(false);
}
@Px
public int getDividerInsetEnd() {
return this.dividerInsetEnd;
}
public void setDividerInsetEnd(@Px int dividerInsetEnd) {
this.dividerInsetEnd = dividerInsetEnd;
updateMenuView(false);
}
@Px
public int getSubheaderInsetStart() {
return this.subheaderInsetStart;
}
public void setSubheaderInsetStart(@Px int subheaderInsetStart) {
this.subheaderInsetStart = subheaderInsetStart;
updateMenuView(false);
}
@Px
public int getSubheaderInsetEnd() {
return this.subheaderInsetEnd;
}
public void setSubheaderInsetEnd(@Px int subheaderInsetEnd) {
this.subheaderInsetEnd = subheaderInsetEnd;
updateMenuView(false);
}
public int getItemIconPadding() {
return itemIconPadding;
}
public void setItemIconPadding(int itemIconPadding) {
this.itemIconPadding = itemIconPadding;
updateMenuView(false);
}
public void setItemMaxLines(int itemMaxLines) {
this.itemMaxLines = itemMaxLines;
updateMenuView(false);
}
public int getItemMaxLines() {
return itemMaxLines;
}
public void setItemIconSize(@Dimension int itemIconSize) {
if (this.itemIconSize != itemIconSize) {
this.itemIconSize = itemIconSize;
hasCustomItemIconSize = true;
updateMenuView(false);
}
}
public void setUpdateSuspended(boolean updateSuspended) {
if (adapter != null) {
adapter.setUpdateSuspended(updateSuspended);
}
}
/** Updates the top padding depending on if this view is drawn behind the status bar. */
public void setBehindStatusBar(boolean behindStatusBar) {
if (isBehindStatusBar != behindStatusBar) {
isBehindStatusBar = behindStatusBar;
updateTopPadding();
}
}
/** True if the NavigationView will be drawn behind the status bar */
public boolean isBehindStatusBar() {
return isBehindStatusBar;
}
private void updateTopPadding() {
int topPadding = 0;
// Set padding if there's no header and we are drawing behind the status bar.
if (headerLayout.getChildCount() == 0 && isBehindStatusBar) {
topPadding = paddingTopDefault;
}
menuView.setPadding(0, topPadding, 0, menuView.getPaddingBottom());
}
public void dispatchApplyWindowInsets(@NonNull WindowInsetsCompat insets) {
int top = insets.getSystemWindowInsetTop();
if (paddingTopDefault != top) {
paddingTopDefault = top;
// Apply the padding to the top of the view if it has changed.
updateTopPadding();
}
// Always apply the bottom padding.
menuView.setPadding(0, menuView.getPaddingTop(), 0, insets.getSystemWindowInsetBottom());
ViewCompat.dispatchApplyWindowInsets(headerLayout, insets);
}
public void setOverScrollMode(int overScrollMode) {
this.overScrollMode = overScrollMode;
if (menuView != null) {
menuView.setOverScrollMode(overScrollMode);
}
}
private abstract static class ViewHolder extends RecyclerView.ViewHolder {
public ViewHolder(View itemView) {
super(itemView);
}
}
private static class NormalViewHolder extends ViewHolder {
public NormalViewHolder(
@NonNull LayoutInflater inflater, ViewGroup parent, View.OnClickListener listener) {
super(inflater.inflate(R.layout.design_navigation_item, parent, false));
itemView.setOnClickListener(listener);
}
}
private static class SubheaderViewHolder extends ViewHolder {
public SubheaderViewHolder(@NonNull LayoutInflater inflater, ViewGroup parent) {
super(inflater.inflate(R.layout.design_navigation_item_subheader, parent, false));
}
}
private static class SeparatorViewHolder extends ViewHolder {
public SeparatorViewHolder(@NonNull LayoutInflater inflater, ViewGroup parent) {
super(inflater.inflate(R.layout.design_navigation_item_separator, parent, false));
}
}
private static class HeaderViewHolder extends ViewHolder {
public HeaderViewHolder(View itemView) {
super(itemView);
}
}
/**
* Handles click events for the menu items. The items has to be {@link NavigationMenuItemView}.
*/
final View.OnClickListener onClickListener =
new View.OnClickListener() {
@Override
public void onClick(View view) {
NavigationMenuItemView itemView = (NavigationMenuItemView) view;
setUpdateSuspended(true);
MenuItemImpl item = itemView.getItemData();
boolean result = menu.performItemAction(item, NavigationMenuPresenter.this, 0);
boolean checkStateChanged = false;
if (item != null && item.isCheckable() && result) {
adapter.setCheckedItem(item);
checkStateChanged = true;
}
setUpdateSuspended(false);
if (checkStateChanged) {
updateMenuView(false);
}
}
};
private class NavigationMenuAdapter extends RecyclerView.Adapter<ViewHolder> {
private static final String STATE_CHECKED_ITEM = "android:menu:checked";
private static final String STATE_ACTION_VIEWS = "android:menu:action_views";
private static final int VIEW_TYPE_NORMAL = 0;
private static final int VIEW_TYPE_SUBHEADER = 1;
private static final int VIEW_TYPE_SEPARATOR = 2;
private static final int VIEW_TYPE_HEADER = 3;
private final ArrayList<NavigationMenuItem> items = new ArrayList<>();
private MenuItemImpl checkedItem;
private boolean updateSuspended;
NavigationMenuAdapter() {
prepareMenuItems();
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getItemCount() {
return items.size();
}
@Override
public int getItemViewType(int position) {
NavigationMenuItem item = items.get(position);
if (item instanceof NavigationMenuSeparatorItem) {
return VIEW_TYPE_SEPARATOR;
} else if (item instanceof NavigationMenuHeaderItem) {
return VIEW_TYPE_HEADER;
} else if (item instanceof NavigationMenuTextItem) {
NavigationMenuTextItem textItem = (NavigationMenuTextItem) item;
if (textItem.getMenuItem().hasSubMenu()) {
return VIEW_TYPE_SUBHEADER;
} else {
return VIEW_TYPE_NORMAL;
}
}
throw new RuntimeException("Unknown item type.");
}
@Nullable
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
switch (viewType) {
case VIEW_TYPE_NORMAL:
return new NormalViewHolder(layoutInflater, parent, onClickListener);
case VIEW_TYPE_SUBHEADER:
return new SubheaderViewHolder(layoutInflater, parent);
case VIEW_TYPE_SEPARATOR:
return new SeparatorViewHolder(layoutInflater, parent);
case VIEW_TYPE_HEADER:
return new HeaderViewHolder(headerLayout);
}
return null;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
switch (getItemViewType(position)) {
case VIEW_TYPE_NORMAL:
{
NavigationMenuItemView itemView = (NavigationMenuItemView) holder.itemView;
itemView.setIconTintList(iconTintList);
if (textAppearance != NO_TEXT_APPEARANCE_SET) {
itemView.setTextAppearance(textAppearance);
}
if (textColor != null) {
itemView.setTextColor(textColor);
}
ViewCompat.setBackground(
itemView,
itemBackground != null ? itemBackground.getConstantState().newDrawable() : null);
if (itemForeground != null) {
itemView.setForeground(itemForeground.getConstantState().newDrawable());
}
NavigationMenuTextItem item = (NavigationMenuTextItem) items.get(position);
itemView.setNeedsEmptyIcon(item.needsEmptyIcon);
itemView.setPadding(
itemHorizontalPadding,
itemVerticalPadding,
itemHorizontalPadding,
itemVerticalPadding);
itemView.setIconPadding(itemIconPadding);
if (hasCustomItemIconSize) {
itemView.setIconSize(itemIconSize);
}
itemView.setMaxLines(itemMaxLines);
itemView.initialize(item.getMenuItem(), 0);
setAccessibilityDelegate(itemView, position, false);
break;
}
case VIEW_TYPE_SUBHEADER:
{
TextView subHeader = (TextView) holder.itemView;
NavigationMenuTextItem item = (NavigationMenuTextItem) items.get(position);
subHeader.setText(item.getMenuItem().getTitle());
if (subheaderTextAppearance != NO_TEXT_APPEARANCE_SET) {
TextViewCompat.setTextAppearance(subHeader, subheaderTextAppearance);
}
subHeader.setPadding(
subheaderInsetStart,
subHeader.getPaddingTop(),
subheaderInsetEnd,
subHeader.getPaddingBottom()
);
if (subheaderColor != null) {
subHeader.setTextColor(subheaderColor);
}
setAccessibilityDelegate(subHeader, position, true);
break;
}
case VIEW_TYPE_SEPARATOR:
{
NavigationMenuSeparatorItem item = (NavigationMenuSeparatorItem) items.get(position);
holder.itemView.setPadding(
dividerInsetStart,
item.getPaddingTop(),
dividerInsetEnd,
item.getPaddingBottom());
break;
}
case VIEW_TYPE_HEADER:
{
setAccessibilityDelegate(holder.itemView, position, true);
break;
}
}
}
private void setAccessibilityDelegate(View view, int position, boolean isHeader) {
ViewCompat.setAccessibilityDelegate(
view,
new AccessibilityDelegateCompat() {
@Override
public void onInitializeAccessibilityNodeInfo(
@NonNull View host, @NonNull AccessibilityNodeInfoCompat info) {
super.onInitializeAccessibilityNodeInfo(host, info);
info.setCollectionItemInfo(
CollectionItemInfoCompat.obtain(
/* rowIndex= */ adjustItemPositionForA11yDelegate(position),
/* rowSpan= */ 1,
/* columnIndex =*/ 1,
/* columnSpan= */ 1,
/* heading= */ isHeader,
/* selected= */ host.isSelected()));
}
});
}
/** Adjusts position based on the presence of separators and header. */
private int adjustItemPositionForA11yDelegate(int position) {
int adjustedPosition = position;
for (int i = 0; i < position; i++) {
if (adapter.getItemViewType(i) == VIEW_TYPE_SEPARATOR) {
adjustedPosition--;
}
}
if (headerLayout.getChildCount() == 0) { // no header
adjustedPosition--;
}
return adjustedPosition;
}
@Override
public void onViewRecycled(ViewHolder holder) {
if (holder instanceof NormalViewHolder) {
((NavigationMenuItemView) holder.itemView).recycle();
}
}
public void update() {
prepareMenuItems();
notifyDataSetChanged();
}
/**
* Flattens the visible menu items of {@link #menu} into {@link #items}, while inserting
* separators between items when necessary.
*/
private void prepareMenuItems() {
if (updateSuspended) {
return;
}
updateSuspended = true;
items.clear();
items.add(new NavigationMenuHeaderItem());
int currentGroupId = -1;
int currentGroupStart = 0;
boolean currentGroupHasIcon = false;
for (int i = 0, totalSize = menu.getVisibleItems().size(); i < totalSize; i++) {
MenuItemImpl item = menu.getVisibleItems().get(i);
if (item.isChecked()) {
setCheckedItem(item);
}
if (item.isCheckable()) {
item.setExclusiveCheckable(false);
}
if (item.hasSubMenu()) {
SubMenu subMenu = item.getSubMenu();
if (subMenu.hasVisibleItems()) {
if (i != 0) {
items.add(new NavigationMenuSeparatorItem(paddingSeparator, 0));
}
items.add(new NavigationMenuTextItem(item));
boolean subMenuHasIcon = false;
int subMenuStart = items.size();
for (int j = 0, size = subMenu.size(); j < size; j++) {
MenuItemImpl subMenuItem = (MenuItemImpl) subMenu.getItem(j);
if (subMenuItem.isVisible()) {
if (!subMenuHasIcon && subMenuItem.getIcon() != null) {
subMenuHasIcon = true;
}
if (subMenuItem.isCheckable()) {
subMenuItem.setExclusiveCheckable(false);
}
if (item.isChecked()) {
setCheckedItem(item);
}
items.add(new NavigationMenuTextItem(subMenuItem));
}
}
if (subMenuHasIcon) {
appendTransparentIconIfMissing(subMenuStart, items.size());
}
}
} else {
int groupId = item.getGroupId();
if (groupId != currentGroupId) { // first item in group
currentGroupStart = items.size();
currentGroupHasIcon = item.getIcon() != null;
if (i != 0) {
currentGroupStart++;
items.add(new NavigationMenuSeparatorItem(paddingSeparator, paddingSeparator));
}
} else if (!currentGroupHasIcon && item.getIcon() != null) {
currentGroupHasIcon = true;
appendTransparentIconIfMissing(currentGroupStart, items.size());
}
NavigationMenuTextItem textItem = new NavigationMenuTextItem(item);
textItem.needsEmptyIcon = currentGroupHasIcon;
items.add(textItem);
currentGroupId = groupId;
}
}
updateSuspended = false;
}
private void appendTransparentIconIfMissing(int startIndex, int endIndex) {
for (int i = startIndex; i < endIndex; i++) {
NavigationMenuTextItem textItem = (NavigationMenuTextItem) items.get(i);
textItem.needsEmptyIcon = true;
}
}
public void setCheckedItem(@NonNull MenuItemImpl checkedItem) {
if (this.checkedItem == checkedItem || !checkedItem.isCheckable()) {
return;
}
if (this.checkedItem != null) {
this.checkedItem.setChecked(false);
}
this.checkedItem = checkedItem;
checkedItem.setChecked(true);
}
public MenuItemImpl getCheckedItem() {
return checkedItem;
}
@NonNull
public Bundle createInstanceState() {
Bundle state = new Bundle();
if (checkedItem != null) {
state.putInt(STATE_CHECKED_ITEM, checkedItem.getItemId());
}
// Store the states of the action views.
SparseArray<ParcelableSparseArray> actionViewStates = new SparseArray<>();
for (int i = 0, size = items.size(); i < size; i++) {
NavigationMenuItem navigationMenuItem = items.get(i);
if (navigationMenuItem instanceof NavigationMenuTextItem) {
MenuItemImpl item = ((NavigationMenuTextItem) navigationMenuItem).getMenuItem();
View actionView = item != null ? item.getActionView() : null;
if (actionView != null) {
ParcelableSparseArray container = new ParcelableSparseArray();
actionView.saveHierarchyState(container);
actionViewStates.put(item.getItemId(), container);
}
}
}
state.putSparseParcelableArray(STATE_ACTION_VIEWS, actionViewStates);
return state;
}
public void restoreInstanceState(@NonNull Bundle state) {
int checkedItem = state.getInt(STATE_CHECKED_ITEM, 0);
if (checkedItem != 0) {
updateSuspended = true;
for (int i = 0, size = items.size(); i < size; i++) {
NavigationMenuItem item = items.get(i);
if (item instanceof NavigationMenuTextItem) {
MenuItemImpl menuItem = ((NavigationMenuTextItem) item).getMenuItem();
if (menuItem != null && menuItem.getItemId() == checkedItem) {
setCheckedItem(menuItem);
break;
}
}
}
updateSuspended = false;
prepareMenuItems();
}
// Restore the states of the action views.
SparseArray<ParcelableSparseArray> actionViewStates =
state.getSparseParcelableArray(STATE_ACTION_VIEWS);
if (actionViewStates != null) {
for (int i = 0, size = items.size(); i < size; i++) {
NavigationMenuItem navigationMenuItem = items.get(i);
if (!(navigationMenuItem instanceof NavigationMenuTextItem)) {
continue;
}
MenuItemImpl item = ((NavigationMenuTextItem) navigationMenuItem).getMenuItem();
if (item == null) {
continue;
}
View actionView = item.getActionView();
if (actionView == null) {
continue;
}
ParcelableSparseArray container = actionViewStates.get(item.getItemId());
if (container == null) {
continue;
}
actionView.restoreHierarchyState(container);
}
}
}
public void setUpdateSuspended(boolean updateSuspended) {
this.updateSuspended = updateSuspended;
}
/** Returns the number of rows that will be used for accessibility. */
int getRowCount() {
int itemCount = headerLayout.getChildCount() == 0 ? 0 : 1;
for (int i = 0; i < adapter.getItemCount(); i++) {
int type = adapter.getItemViewType(i);
if (type == VIEW_TYPE_NORMAL || type == VIEW_TYPE_SUBHEADER) {
itemCount++;
}
}
return itemCount;
}
}
/** Unified data model for all sorts of navigation menu items. */
private interface NavigationMenuItem {}
/** Normal or subheader items. */
private static class NavigationMenuTextItem implements NavigationMenuItem {
private final MenuItemImpl menuItem;
boolean needsEmptyIcon;
NavigationMenuTextItem(MenuItemImpl item) {
menuItem = item;
}
public MenuItemImpl getMenuItem() {
return menuItem;
}
}
/** Separator items. */
private static class NavigationMenuSeparatorItem implements NavigationMenuItem {
private final int paddingTop;
private final int paddingBottom;
public NavigationMenuSeparatorItem(int paddingTop, int paddingBottom) {
this.paddingTop = paddingTop;
this.paddingBottom = paddingBottom;
}
public int getPaddingTop() {
return paddingTop;
}
public int getPaddingBottom() {
return paddingBottom;
}
}
/** Header (not subheader) items. */
private static class NavigationMenuHeaderItem implements NavigationMenuItem {
NavigationMenuHeaderItem() {}
// The actual content is hold by NavigationMenuPresenter#mHeaderLayout.
}
private class NavigationMenuViewAccessibilityDelegate extends RecyclerViewAccessibilityDelegate {
NavigationMenuViewAccessibilityDelegate(@NonNull RecyclerView recyclerView) {
super(recyclerView);
}
@Override
public void onInitializeAccessibilityNodeInfo(
View host, @NonNull AccessibilityNodeInfoCompat info) {
super.onInitializeAccessibilityNodeInfo(host, info);
info.setCollectionInfo(
CollectionInfoCompat.obtain(
adapter.getRowCount(), /* columnCount= */ 1, /* hierarchical= */ false));
}
}
}