Skip to content

Commit 611c755

Browse files
authored
Fix compiler warnings (#540)
Fix compiler warnings
1 parent 5b9f531 commit 611c755

File tree

3 files changed

+71
-36
lines changed

3 files changed

+71
-36
lines changed

flexbox/src/main/java/com/google/android/flexbox/FlexboxHelper.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -976,7 +976,7 @@ void determineMainSize(int widthMeasureSpec, int heightMeasureSpec, int fromInde
976976
if (widthMode == View.MeasureSpec.EXACTLY) {
977977
mainSize = widthSize;
978978
} else {
979-
mainSize = largestMainSize > widthSize ? widthSize : largestMainSize;
979+
mainSize = Math.min(largestMainSize, widthSize);
980980
}
981981
paddingAlongMainAxis = mFlexContainer.getPaddingLeft()
982982
+ mFlexContainer.getPaddingRight();
@@ -1016,10 +1016,10 @@ void determineMainSize(int widthMeasureSpec, int heightMeasureSpec, int fromInde
10161016

10171017
private void ensureChildrenFrozen(int size) {
10181018
if (mChildrenFrozen == null) {
1019-
mChildrenFrozen = new boolean[size < INITIAL_CAPACITY ? INITIAL_CAPACITY : size];
1019+
mChildrenFrozen = new boolean[Math.max(size, INITIAL_CAPACITY)];
10201020
} else if (mChildrenFrozen.length < size) {
10211021
int newCapacity = mChildrenFrozen.length * 2;
1022-
mChildrenFrozen = new boolean[newCapacity >= size ? newCapacity : size];
1022+
mChildrenFrozen = new boolean[Math.max(newCapacity, size)];
10231023
} else {
10241024
Arrays.fill(mChildrenFrozen, false);
10251025
}
@@ -1908,20 +1908,20 @@ void layoutSingleChildVertical(View view, FlexLine flexLine, boolean isRtl,
19081908

19091909
void ensureMeasuredSizeCache(int size) {
19101910
if (mMeasuredSizeCache == null) {
1911-
mMeasuredSizeCache = new long[size < INITIAL_CAPACITY ? INITIAL_CAPACITY : size];
1911+
mMeasuredSizeCache = new long[Math.max(size, INITIAL_CAPACITY)];
19121912
} else if (mMeasuredSizeCache.length < size) {
19131913
int newCapacity = mMeasuredSizeCache.length * 2;
1914-
newCapacity = newCapacity >= size ? newCapacity : size;
1914+
newCapacity = Math.max(newCapacity, size);
19151915
mMeasuredSizeCache = Arrays.copyOf(mMeasuredSizeCache, newCapacity);
19161916
}
19171917
}
19181918

19191919
void ensureMeasureSpecCache(int size) {
19201920
if (mMeasureSpecCache == null) {
1921-
mMeasureSpecCache = new long[size < INITIAL_CAPACITY ? INITIAL_CAPACITY : size];
1921+
mMeasureSpecCache = new long[Math.max(size, INITIAL_CAPACITY)];
19221922
} else if (mMeasureSpecCache.length < size) {
19231923
int newCapacity = mMeasureSpecCache.length * 2;
1924-
newCapacity = newCapacity >= size ? newCapacity : size;
1924+
newCapacity = Math.max(newCapacity, size);
19251925
mMeasureSpecCache = Arrays.copyOf(mMeasureSpecCache, newCapacity);
19261926
}
19271927
}
@@ -1977,10 +1977,10 @@ private void updateMeasureCache(int index, int widthMeasureSpec, int heightMeasu
19771977

19781978
void ensureIndexToFlexLine(int size) {
19791979
if (mIndexToFlexLine == null) {
1980-
mIndexToFlexLine = new int[size < INITIAL_CAPACITY ? INITIAL_CAPACITY : size];
1980+
mIndexToFlexLine = new int[Math.max(size, INITIAL_CAPACITY)];
19811981
} else if (mIndexToFlexLine.length < size) {
19821982
int newCapacity = mIndexToFlexLine.length * 2;
1983-
newCapacity = newCapacity >= size ? newCapacity : size;
1983+
newCapacity = Math.max(newCapacity, size);
19841984
mIndexToFlexLine = Arrays.copyOf(mIndexToFlexLine, newCapacity);
19851985
}
19861986
}
@@ -2002,8 +2002,8 @@ void clearFlexLines(List<FlexLine> flexLines, int fromFlexItem) {
20022002

20032003
// Deleting from the last to avoid unneeded copy it happens when deleting the middle of the
20042004
// item in the ArrayList
2005-
for (int i = flexLines.size() - 1; i >= fromFlexLine; i--) {
2006-
flexLines.remove(i);
2005+
if (flexLines.size() > fromFlexLine) {
2006+
flexLines.subList(fromFlexLine, flexLines.size()).clear();
20072007
}
20082008

20092009
int fillTo = mIndexToFlexLine.length - 1;

flexbox/src/main/java/com/google/android/flexbox/FlexboxItemDecoration.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
import java.util.List;
2929

30+
import androidx.annotation.NonNull;
3031
import androidx.recyclerview.widget.RecyclerView;
3132

3233
/**
@@ -87,14 +88,20 @@ public void setOrientation(int orientation) {
8788
}
8889

8990
@Override
90-
public void onDraw(Canvas canvas, RecyclerView parent, RecyclerView.State state) {
91+
public void onDraw(
92+
@NonNull Canvas canvas,
93+
@NonNull RecyclerView parent,
94+
@NonNull RecyclerView.State state) {
9195
drawHorizontalDecorations(canvas, parent);
9296
drawVerticalDecorations(canvas, parent);
9397
}
9498

9599
@Override
96-
public void getItemOffsets(Rect outRect, View view, RecyclerView parent,
97-
RecyclerView.State state) {
100+
public void getItemOffsets(
101+
@NonNull Rect outRect,
102+
@NonNull View view,
103+
RecyclerView parent,
104+
@NonNull RecyclerView.State state) {
98105
int position = parent.getChildAdapterPosition(view);
99106
if (position == 0) {
100107
return;

flexbox/src/main/java/com/google/android/flexbox/FlexboxLayoutManager.java

Lines changed: 50 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,6 @@ public FlexboxLayoutManager(Context context, @FlexDirection int flexDirection,
223223
setFlexDirection(flexDirection);
224224
setFlexWrap(flexWrap);
225225
setAlignItems(AlignItems.STRETCH);
226-
setAutoMeasureEnabled(true);
227226
mContext = context;
228227
}
229228

@@ -262,10 +261,14 @@ public FlexboxLayoutManager(Context context, AttributeSet attrs, int defStyleAtt
262261
}
263262
setFlexWrap(FlexWrap.WRAP);
264263
setAlignItems(AlignItems.STRETCH);
265-
setAutoMeasureEnabled(true);
266264
mContext = context;
267265
}
268266

267+
@Override
268+
public boolean isAutoMeasureEnabled() {
269+
return true;
270+
}
271+
269272
// From here, methods from FlexContainer
270273
@FlexDirection
271274
@Override
@@ -371,6 +374,7 @@ public void setMaxLine(int maxLine) {
371374
}
372375

373376
@Override
377+
@NonNull
374378
public List<FlexLine> getFlexLines() {
375379
List<FlexLine> result = new ArrayList<>(mFlexLines.size());
376380
for (int i = 0, size = mFlexLines.size(); i < size; i++) {
@@ -537,7 +541,11 @@ public PointF computeScrollVectorForPosition(int targetPosition) {
537541
if (getChildCount() == 0) {
538542
return null;
539543
}
540-
int firstChildPos = getPosition(getChildAt(0));
544+
View view = getChildAt(0);
545+
if (view == null) {
546+
return null;
547+
}
548+
int firstChildPos = getPosition(view);
541549
int direction = targetPosition < firstChildPos ? -1 : 1;
542550
if (isMainAxisDirectionHorizontal()) {
543551
return new PointF(0, direction);
@@ -738,8 +746,8 @@ public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State
738746

739747
int startOffset;
740748
int endOffset;
749+
int filledToEnd = fill(recycler, state, mLayoutState);
741750
if (mAnchorInfo.mLayoutFromEnd) {
742-
int filledToEnd = fill(recycler, state, mLayoutState);
743751
if (DEBUG) {
744752
Log.d(TAG, String.format("filled: %d toward start", filledToEnd));
745753
}
@@ -751,7 +759,6 @@ public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State
751759
}
752760
endOffset = mLayoutState.mOffset;
753761
} else {
754-
int filledToEnd = fill(recycler, state, mLayoutState);
755762
if (DEBUG) {
756763
Log.d(TAG, String.format("filled: %d toward end", filledToEnd));
757764
}
@@ -1112,8 +1119,11 @@ private boolean updateAnchorFromPendingState(RecyclerView.State state, AnchorInf
11121119
: mOrientationHelper.getDecoratedStart(anchorView);
11131120
} else {
11141121
if (getChildCount() > 0) {
1115-
int position = getPosition(getChildAt(0));
1116-
anchorInfo.mLayoutFromEnd = mPendingScrollPosition < position;
1122+
View view = getChildAt(0);
1123+
if (view != null) {
1124+
int position = getPosition(view);
1125+
anchorInfo.mLayoutFromEnd = mPendingScrollPosition < position;
1126+
}
11171127
}
11181128
anchorInfo.assignCoordinateFromPadding();
11191129
}
@@ -1229,6 +1239,9 @@ private View findReferenceChild(int start, int end, int itemCount) {
12291239
int diff = end > start ? 1 : -1;
12301240
for (int i = start; i != end; i += diff) {
12311241
View view = getChildAt(i);
1242+
if (view == null) {
1243+
continue;
1244+
}
12321245
int position = getPosition(view);
12331246
if (position >= 0 && position < itemCount) {
12341247
if (((RecyclerView.LayoutParams) view.getLayoutParams()).isItemRemoved()) {
@@ -1327,7 +1340,9 @@ private void recycleFlexLinesFromStart(RecyclerView.Recycler recycler,
13271340
return;
13281341
}
13291342
View firstView = getChildAt(0);
1330-
1343+
if (firstView == null) {
1344+
return;
1345+
}
13311346
int currentLineIndex = mFlexboxHelper.mIndexToFlexLine[getPosition(firstView)];
13321347
if (currentLineIndex == NO_POSITION) {
13331348
return;
@@ -1336,6 +1351,9 @@ private void recycleFlexLinesFromStart(RecyclerView.Recycler recycler,
13361351
int recycleTo = -1;
13371352
for (int i = 0; i < childCount; i++) {
13381353
View view = getChildAt(i);
1354+
if (view == null) {
1355+
continue;
1356+
}
13391357
if (canViewBeRecycledFromStart(view, layoutState.mScrollingOffset)) {
13401358
if (flexLine.mLastIndex == getPosition(view)) {
13411359
// Recycle the views in a flex line if all views end positions are lower than
@@ -1371,13 +1389,15 @@ private void recycleFlexLinesFromEnd(RecyclerView.Recycler recycler, LayoutState
13711389
return;
13721390
}
13731391
assert mFlexboxHelper.mIndexToFlexLine != null;
1374-
int limit = mOrientationHelper.getEnd() - layoutState.mScrollingOffset;
13751392
int childCount = getChildCount();
13761393
if (childCount == 0) {
13771394
return;
13781395
}
13791396

13801397
View lastView = getChildAt(childCount - 1);
1398+
if (lastView == null) {
1399+
return;
1400+
}
13811401
int currentLineIndex = mFlexboxHelper.mIndexToFlexLine[getPosition(lastView)];
13821402
if (currentLineIndex == NO_POSITION) {
13831403
return;
@@ -1387,6 +1407,9 @@ private void recycleFlexLinesFromEnd(RecyclerView.Recycler recycler, LayoutState
13871407
FlexLine flexLine = mFlexLines.get(currentLineIndex);
13881408
for (int i = childCount - 1; i >= 0; i--) {
13891409
View view = getChildAt(i);
1410+
if (view == null) {
1411+
continue;
1412+
}
13901413
if (canViewBeRecycledFromEnd(view, layoutState.mScrollingOffset)) {
13911414
if (flexLine.mFirstIndex == getPosition(view)) {
13921415
// Recycle the views in a flex line if all views start positions are beyond the
@@ -1914,8 +1937,7 @@ public boolean canScrollVertically() {
19141937
@Override
19151938
public int scrollHorizontallyBy(int dx, RecyclerView.Recycler recycler,
19161939
RecyclerView.State state) {
1917-
if (!isMainAxisDirectionHorizontal() ||
1918-
(mFlexWrap == FlexWrap.NOWRAP && isMainAxisDirectionHorizontal())) {
1940+
if (!isMainAxisDirectionHorizontal() || (mFlexWrap == FlexWrap.NOWRAP)) {
19191941
int scrolled = handleScrollingMainOrientation(dx, recycler, state);
19201942
mViewCache.clear();
19211943
return scrolled;
@@ -2046,6 +2068,9 @@ private void updateLayoutState(int layoutDirection, int absDelta) {
20462068
boolean columnAndRtl = !mainAxisHorizontal && mIsRtl;
20472069
if (layoutDirection == LayoutState.LAYOUT_END) {
20482070
View lastVisible = getChildAt(getChildCount() - 1);
2071+
if (lastVisible == null) {
2072+
return;
2073+
}
20492074
mLayoutState.mOffset = mOrientationHelper.getDecoratedEnd(lastVisible);
20502075
int lastVisiblePosition = getPosition(lastVisible);
20512076
int lastVisibleLinePosition = mFlexboxHelper.mIndexToFlexLine[lastVisiblePosition];
@@ -2067,8 +2092,7 @@ private void updateLayoutState(int layoutDirection, int absDelta) {
20672092
mLayoutState.mOffset = mOrientationHelper.getDecoratedStart(referenceView);
20682093
mLayoutState.mScrollingOffset = -mOrientationHelper.getDecoratedStart(referenceView)
20692094
+ mOrientationHelper.getStartAfterPadding();
2070-
mLayoutState.mScrollingOffset = mLayoutState.mScrollingOffset >= 0 ?
2071-
mLayoutState.mScrollingOffset : 0;
2095+
mLayoutState.mScrollingOffset = Math.max(mLayoutState.mScrollingOffset, 0);
20722096
} else {
20732097
mLayoutState.mOffset = mOrientationHelper.getDecoratedEnd(referenceView);
20742098
mLayoutState.mScrollingOffset = mOrientationHelper.getDecoratedEnd(referenceView)
@@ -2100,7 +2124,9 @@ private void updateLayoutState(int layoutDirection, int absDelta) {
21002124
}
21012125
} else {
21022126
View firstVisible = getChildAt(0);
2103-
2127+
if (firstVisible == null) {
2128+
return;
2129+
}
21042130
mLayoutState.mOffset = mOrientationHelper.getDecoratedStart(firstVisible);
21052131
int firstVisiblePosition = getPosition(firstVisible);
21062132
int firstVisibleLinePosition = mFlexboxHelper.mIndexToFlexLine[firstVisiblePosition];
@@ -2129,8 +2155,7 @@ private void updateLayoutState(int layoutDirection, int absDelta) {
21292155
mLayoutState.mOffset = mOrientationHelper.getDecoratedEnd(referenceView);
21302156
mLayoutState.mScrollingOffset = mOrientationHelper.getDecoratedEnd(referenceView)
21312157
- mOrientationHelper.getEndAfterPadding();
2132-
mLayoutState.mScrollingOffset = mLayoutState.mScrollingOffset >= 0 ?
2133-
mLayoutState.mScrollingOffset : 0;
2158+
mLayoutState.mScrollingOffset = Math.max(mLayoutState.mScrollingOffset, 0);
21342159
} else {
21352160
mLayoutState.mOffset = mOrientationHelper.getDecoratedStart(referenceView);
21362161
mLayoutState.mScrollingOffset = -mOrientationHelper.getDecoratedStart(referenceView)
@@ -2207,7 +2232,7 @@ private View findLastReferenceViewInLine(View lastView, FlexLine lastVisibleLine
22072232
}
22082233

22092234
@Override
2210-
public int computeHorizontalScrollExtent(RecyclerView.State state) {
2235+
public int computeHorizontalScrollExtent(@NonNull RecyclerView.State state) {
22112236
int scrollExtent = computeScrollExtent(state);
22122237
if (DEBUG) {
22132238
Log.d(TAG, "computeHorizontalScrollExtent: " + scrollExtent);
@@ -2216,7 +2241,7 @@ public int computeHorizontalScrollExtent(RecyclerView.State state) {
22162241
}
22172242

22182243
@Override
2219-
public int computeVerticalScrollExtent(RecyclerView.State state) {
2244+
public int computeVerticalScrollExtent(@NonNull RecyclerView.State state) {
22202245
int scrollExtent = computeScrollExtent(state);
22212246
if (DEBUG) {
22222247
Log.d(TAG, "computeVerticalScrollExtent: " + scrollExtent);
@@ -2242,7 +2267,7 @@ private int computeScrollExtent(RecyclerView.State state) {
22422267
}
22432268

22442269
@Override
2245-
public int computeHorizontalScrollOffset(RecyclerView.State state) {
2270+
public int computeHorizontalScrollOffset(@NonNull RecyclerView.State state) {
22462271
int scrollOffset = computeScrollOffset(state);
22472272
if (DEBUG) {
22482273
Log.d(TAG, "computeHorizontalScrollOffset: " + scrollOffset);
@@ -2251,7 +2276,7 @@ public int computeHorizontalScrollOffset(RecyclerView.State state) {
22512276
}
22522277

22532278
@Override
2254-
public int computeVerticalScrollOffset(RecyclerView.State state) {
2279+
public int computeVerticalScrollOffset(@NonNull RecyclerView.State state) {
22552280
int scrollOffset = computeScrollOffset(state);
22562281
if (DEBUG) {
22572282
Log.d(TAG, "computeVerticalScrollOffset: " + scrollOffset);
@@ -2288,7 +2313,7 @@ private int computeScrollOffset(RecyclerView.State state) {
22882313
}
22892314

22902315
@Override
2291-
public int computeHorizontalScrollRange(RecyclerView.State state) {
2316+
public int computeHorizontalScrollRange(@NonNull RecyclerView.State state) {
22922317
int scrollRange = computeScrollRange(state);
22932318
if (DEBUG) {
22942319
Log.d(TAG, "computeHorizontalScrollRange: " + scrollRange);
@@ -2297,7 +2322,7 @@ public int computeHorizontalScrollRange(RecyclerView.State state) {
22972322
}
22982323

22992324
@Override
2300-
public int computeVerticalScrollRange(RecyclerView.State state) {
2325+
public int computeVerticalScrollRange(@NonNull RecyclerView.State state) {
23012326
int scrollRange = computeScrollRange(state);
23022327
if (DEBUG) {
23032328
Log.d(TAG, "computeVerticalScrollRange: " + scrollRange);
@@ -2929,6 +2954,7 @@ private void assignFromView(View anchor) {
29292954
}
29302955

29312956
@Override
2957+
@NonNull
29322958
public String toString() {
29332959
return "AnchorInfo{" +
29342960
"mPosition=" + mPosition +
@@ -3001,6 +3027,7 @@ private boolean hasMore(RecyclerView.State state, List<FlexLine> flexLines) {
30013027
}
30023028

30033029
@Override
3030+
@NonNull
30043031
public String toString() {
30053032
return "LayoutState{" +
30063033
"mAvailable=" + mAvailable +
@@ -3074,6 +3101,7 @@ public SavedState[] newArray(int size) {
30743101
};
30753102

30763103
@Override
3104+
@NonNull
30773105
public String toString() {
30783106
return "SavedState{" +
30793107
"mAnchorPosition=" + mAnchorPosition +

0 commit comments

Comments
 (0)