Skip to content

Commit 9687433

Browse files
jreckAndroid (Google) Code Review
authored andcommitted
Merge "Cleanup PositionMetadata recycles"
2 parents 5cef554 + 820b236 commit 9687433

File tree

3 files changed

+33
-16
lines changed

3 files changed

+33
-16
lines changed

core/java/android/widget/ExpandableListConnector.java

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,8 @@ public boolean areAllItemsEnabled() {
372372

373373
@Override
374374
public boolean isEnabled(int flatListPos) {
375-
final ExpandableListPosition pos = getUnflattenedPos(flatListPos).position;
375+
final PositionMetadata metadata = getUnflattenedPos(flatListPos);
376+
final ExpandableListPosition pos = metadata.position;
376377

377378
boolean retValue;
378379
if (pos.type == ExpandableListPosition.CHILD) {
@@ -382,7 +383,7 @@ public boolean isEnabled(int flatListPos) {
382383
retValue = true;
383384
}
384385

385-
pos.recycle();
386+
metadata.recycle();
386387

387388
return retValue;
388389
}
@@ -461,7 +462,8 @@ public View getView(int flatListPos, View convertView, ViewGroup parent) {
461462

462463
@Override
463464
public int getItemViewType(int flatListPos) {
464-
final ExpandableListPosition pos = getUnflattenedPos(flatListPos).position;
465+
final PositionMetadata metadata = getUnflattenedPos(flatListPos);
466+
final ExpandableListPosition pos = metadata.position;
465467

466468
int retValue;
467469
if (mExpandableListAdapter instanceof HeterogeneousExpandableList) {
@@ -481,7 +483,7 @@ public int getItemViewType(int flatListPos) {
481483
}
482484
}
483485

484-
pos.recycle();
486+
metadata.recycle();
485487

486488
return retValue;
487489
}
@@ -590,8 +592,10 @@ private void refreshExpGroupMetadataList(boolean forceChildrenCountRefresh,
590592
* @param groupPos position of the group to collapse
591593
*/
592594
boolean collapseGroup(int groupPos) {
593-
PositionMetadata pm = getFlattenedPos(ExpandableListPosition.obtain(
594-
ExpandableListPosition.GROUP, groupPos, -1, -1));
595+
ExpandableListPosition elGroupPos = ExpandableListPosition.obtain(
596+
ExpandableListPosition.GROUP, groupPos, -1, -1);
597+
PositionMetadata pm = getFlattenedPos(elGroupPos);
598+
elGroupPos.recycle();
595599
if (pm == null) return false;
596600

597601
boolean retValue = collapseGroup(pm);
@@ -631,8 +635,10 @@ boolean collapseGroup(PositionMetadata posMetadata) {
631635
* @param groupPos the group to be expanded
632636
*/
633637
boolean expandGroup(int groupPos) {
634-
PositionMetadata pm = getFlattenedPos(ExpandableListPosition.obtain(
635-
ExpandableListPosition.GROUP, groupPos, -1, -1));
638+
ExpandableListPosition elGroupPos = ExpandableListPosition.obtain(
639+
ExpandableListPosition.GROUP, groupPos, -1, -1);
640+
PositionMetadata pm = getFlattenedPos(elGroupPos);
641+
elGroupPos.recycle();
636642
boolean retValue = expandGroup(pm);
637643
pm.recycle();
638644
return retValue;
@@ -971,7 +977,10 @@ static public class PositionMetadata {
971977
public int groupInsertIndex;
972978

973979
private void resetState() {
974-
position = null;
980+
if (position != null) {
981+
position.recycle();
982+
position = null;
983+
}
975984
groupMetadata = null;
976985
groupInsertIndex = 0;
977986
}
@@ -1005,6 +1014,7 @@ private static PositionMetadata getRecycledOrCreate() {
10051014
}
10061015

10071016
public void recycle() {
1017+
resetState();
10081018
synchronized (sPool) {
10091019
if (sPool.size() < MAX_POOL_SIZE) {
10101020
sPool.add(this);

core/java/android/widget/ExpandableListPosition.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,10 @@ private static ExpandableListPosition getRecycledOrCreate() {
125125
return elp;
126126
}
127127

128+
/**
129+
* Do not call this unless you obtained this via ExpandableListPosition.obtain().
130+
* PositionMetadata will handle recycling its own children.
131+
*/
128132
public void recycle() {
129133
synchronized (sPool) {
130134
if (sPool.size() < MAX_POOL_SIZE) {

core/java/android/widget/ExpandableListView.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,6 @@ protected void dispatchDraw(Canvas canvas) {
326326
indicator.draw(canvas);
327327
}
328328
}
329-
330329
pos.recycle();
331330
}
332331

@@ -613,8 +612,10 @@ public boolean expandGroup(int groupPos) {
613612
* was already expanded, this will return false)
614613
*/
615614
public boolean expandGroup(int groupPos, boolean animate) {
616-
PositionMetadata pm = mConnector.getFlattenedPos(ExpandableListPosition.obtain(
617-
ExpandableListPosition.GROUP, groupPos, -1, -1));
615+
ExpandableListPosition elGroupPos = ExpandableListPosition.obtain(
616+
ExpandableListPosition.GROUP, groupPos, -1, -1);
617+
PositionMetadata pm = mConnector.getFlattenedPos(elGroupPos);
618+
elGroupPos.recycle();
618619
boolean retValue = mConnector.expandGroup(pm);
619620

620621
if (mOnGroupExpandListener != null) {
@@ -776,8 +777,10 @@ public long getExpandableListPosition(int flatListPosition) {
776777
* @return The flat list position for the given child or group.
777778
*/
778779
public int getFlatListPosition(long packedPosition) {
779-
PositionMetadata pm = mConnector.getFlattenedPos(ExpandableListPosition
780-
.obtainPosition(packedPosition));
780+
ExpandableListPosition elPackedPos = ExpandableListPosition
781+
.obtainPosition(packedPosition);
782+
PositionMetadata pm = mConnector.getFlattenedPos(elPackedPos);
783+
elPackedPos.recycle();
781784
final int flatListPosition = pm.position.flatListPos;
782785
pm.recycle();
783786
return getAbsoluteFlatPosition(flatListPosition);
@@ -988,11 +991,11 @@ ContextMenuInfo createContextMenuInfo(View view, int flatListPosition, long id)
988991
final int adjustedPosition = getFlatPositionForConnector(flatListPosition);
989992
PositionMetadata pm = mConnector.getUnflattenedPos(adjustedPosition);
990993
ExpandableListPosition pos = pm.position;
991-
pm.recycle();
992994

993995
id = getChildOrGroupId(pos);
994996
long packedPosition = pos.getPackedPosition();
995-
pos.recycle();
997+
998+
pm.recycle();
996999

9971000
return new ExpandableListContextMenuInfo(view, packedPosition, id);
9981001
}

0 commit comments

Comments
 (0)