Skip to content

Commit 0ad0101

Browse files
cketchamafohrman
authored andcommitted
Add @nonnull and @nullable annotations
PiperOrigin-RevId: 266817244
1 parent 3beaee4 commit 0ad0101

File tree

6 files changed

+78
-51
lines changed

6 files changed

+78
-51
lines changed

lib/java/com/google/android/material/floatingactionbutton/BaseMotionStrategy.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@
3434
abstract class BaseMotionStrategy implements MotionStrategy {
3535

3636
private final Context context;
37-
private final ExtendedFloatingActionButton fab;
37+
@NonNull private final ExtendedFloatingActionButton fab;
3838
private final ArrayList<AnimatorListener> listeners = new ArrayList<>();
3939
private final AnimatorTracker tracker;
4040

4141
@Nullable private MotionSpec defaultMotionSpec;
4242
@Nullable private MotionSpec motionSpec;
4343

44-
BaseMotionStrategy(ExtendedFloatingActionButton fab, AnimatorTracker tracker) {
44+
BaseMotionStrategy(@NonNull ExtendedFloatingActionButton fab, AnimatorTracker tracker) {
4545
this.fab = fab;
4646
this.context = fab.getContext();
4747
this.tracker = tracker;
@@ -78,6 +78,7 @@ public final void removeAnimationListener(@NonNull AnimatorListener listener) {
7878
listeners.remove(listener);
7979
}
8080

81+
@NonNull
8182
@Override
8283
public final List<AnimatorListener> getListeners() {
8384
return listeners;
@@ -112,6 +113,7 @@ public AnimatorSet createAnimator() {
112113
return createAnimator(getCurrentMotionSpec());
113114
}
114115

116+
@NonNull
115117
AnimatorSet createAnimator(@NonNull MotionSpec spec) {
116118
List<Animator> animators = new ArrayList<>();
117119

lib/java/com/google/android/material/floatingactionbutton/BorderDrawable.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class BorderDrawable extends Drawable {
5959

6060
private final ShapeAppearancePathProvider pathProvider = new ShapeAppearancePathProvider();
6161

62-
private final Paint paint;
62+
@NonNull private final Paint paint;
6363
private final Path shapePath = new Path();
6464
private final Rect rect = new Rect();
6565
private final RectF rectF = new RectF();
@@ -119,7 +119,7 @@ void setGradientColors(
119119
}
120120

121121
@Override
122-
public void draw(Canvas canvas) {
122+
public void draw(@NonNull Canvas canvas) {
123123
if (invalidateShader) {
124124
paint.setShader(createGradientShader());
125125
invalidateShader = false;
@@ -157,7 +157,7 @@ public void getOutline(@NonNull Outline outline) {
157157
}
158158

159159
@Override
160-
public boolean getPadding(Rect padding) {
160+
public boolean getPadding(@NonNull Rect padding) {
161161
if (shapeAppearanceModel.isRoundRect()) {
162162
final int borderWidth = Math.round(this.borderWidth);
163163
padding.set(borderWidth, borderWidth, borderWidth, borderWidth);
@@ -210,6 +210,7 @@ protected boolean onStateChange(int[] state) {
210210
return invalidateShader;
211211
}
212212

213+
@NonNull
213214
private Shader createGradientShader() {
214215
final Rect rect = this.rect;
215216
copyBounds(rect);

lib/java/com/google/android/material/floatingactionbutton/ExtendedFloatingActionButton.java

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,12 @@ public class ExtendedFloatingActionButton extends MaterialButton implements Atta
7979
private int animState = ANIM_STATE_NONE;
8080

8181
private final AnimatorTracker changeVisibilityTracker = new AnimatorTracker();
82-
private final MotionStrategy shrinkStrategy;
83-
private final MotionStrategy extendStrategy;
82+
@NonNull private final MotionStrategy shrinkStrategy;
83+
@NonNull private final MotionStrategy extendStrategy;
8484
private final MotionStrategy showStrategy = new ShowStrategy(changeVisibilityTracker);
8585
private final MotionStrategy hideStrategy = new HideStrategy(changeVisibilityTracker);
8686

87-
private final Behavior<ExtendedFloatingActionButton> behavior;
87+
@NonNull private final Behavior<ExtendedFloatingActionButton> behavior;
8888

8989
private boolean isExtended = true;
9090
private boolean isUsingPillCorner = true;
@@ -137,17 +137,17 @@ public void onExtended(ExtendedFloatingActionButton extendedFab) {}
137137
public void onShrunken(ExtendedFloatingActionButton extendedFab) {}
138138
}
139139

140-
public ExtendedFloatingActionButton(Context context) {
140+
public ExtendedFloatingActionButton(@NonNull Context context) {
141141
this(context, null);
142142
}
143143

144-
public ExtendedFloatingActionButton(Context context, @Nullable AttributeSet attrs) {
144+
public ExtendedFloatingActionButton(@NonNull Context context, @Nullable AttributeSet attrs) {
145145
this(context, attrs, R.attr.extendedFloatingActionButtonStyle);
146146
}
147147

148148
@SuppressWarnings("initialization")
149149
public ExtendedFloatingActionButton(
150-
Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
150+
@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
151151
super(context, attrs, defStyleAttr);
152152
behavior = new ExtendedFloatingActionButtonBehavior<>(context, attrs);
153153
TypedArray a =
@@ -565,7 +565,7 @@ boolean isUsingPillCorner() {
565565
}
566566

567567
private void performMotion(
568-
final MotionStrategy strategy, @Nullable final OnChangedCallback callback) {
568+
@NonNull final MotionStrategy strategy, @Nullable final OnChangedCallback callback) {
569569
if (strategy.shouldCancel()) {
570570
return;
571571
}
@@ -639,16 +639,17 @@ private boolean shouldAnimateVisibilityChange() {
639639
* A Property wrapper around the <code>width</code> functionality handled by the {@link
640640
* LayoutParams#width} value.
641641
*/
642-
static final Property<View, Float> WIDTH =
642+
static final Property<View, Float> WIDTH =
643643
new Property<View, Float>(Float.class, "width") {
644644
@Override
645-
public void set(View object, Float value) {
645+
public void set(@NonNull View object, @NonNull Float value) {
646646
object.getLayoutParams().width = value.intValue();
647647
object.requestLayout();
648648
}
649649

650+
@NonNull
650651
@Override
651-
public Float get(View object) {
652+
public Float get(@NonNull View object) {
652653
return (float) object.getLayoutParams().width;
653654
}
654655
};
@@ -657,16 +658,17 @@ public Float get(View object) {
657658
* A Property wrapper around the <code>height</code> functionality handled by the {@link
658659
* LayoutParams#height} value.
659660
*/
660-
static final Property<View, Float> HEIGHT =
661+
static final Property<View, Float> HEIGHT =
661662
new Property<View, Float>(Float.class, "height") {
662663
@Override
663-
public void set(View object, Float value) {
664+
public void set(@NonNull View object, @NonNull Float value) {
664665
object.getLayoutParams().height = value.intValue();
665666
object.requestLayout();
666667
}
667668

669+
@NonNull
668670
@Override
669-
public Float get(View object) {
671+
public Float get(@NonNull View object) {
670672
return (float) object.getLayoutParams().height;
671673
}
672674
};
@@ -679,14 +681,14 @@ public Float get(View object) {
679681
static final Property<View, Float> CORNER_RADIUS =
680682
new Property<View, Float>(Float.class, "cornerRadius") {
681683
@Override
682-
public void set(View object, Float value) {
684+
public void set(@NonNull View object, @NonNull Float value) {
683685
ExtendedFloatingActionButton efab = ((ExtendedFloatingActionButton) object);
684686
efab.setShapeAppearanceModel(
685687
efab.getShapeAppearanceModel().withCornerRadius(value.intValue()));
686688
}
687689

688690
@Override
689-
public Float get(View object) {
691+
public Float get(@NonNull View object) {
690692
return ((ExtendedFloatingActionButton) object)
691693
.getShapeAppearanceModel()
692694
.getTopRightCorner()
@@ -738,7 +740,8 @@ public ExtendedFloatingActionButtonBehavior() {
738740

739741
// Behavior attrs should be nullable in the framework
740742
@SuppressWarnings("argument.type.incompatible")
741-
public ExtendedFloatingActionButtonBehavior(Context context, @Nullable AttributeSet attrs) {
743+
public ExtendedFloatingActionButtonBehavior(
744+
@NonNull Context context, @Nullable AttributeSet attrs) {
742745
super(context, attrs);
743746
TypedArray a =
744747
context.obtainStyledAttributes(
@@ -817,7 +820,7 @@ public void onAttachedToLayoutParams(@NonNull CoordinatorLayout.LayoutParams lp)
817820

818821
@Override
819822
public boolean onDependentViewChanged(
820-
CoordinatorLayout parent, ExtendedFloatingActionButton child, View dependency) {
823+
CoordinatorLayout parent, @NonNull ExtendedFloatingActionButton child, View dependency) {
821824
if (dependency instanceof AppBarLayout) {
822825
// If we're depending on an AppBarLayout we will show/hide it automatically
823826
// if the FAB is anchored to the AppBarLayout
@@ -846,7 +849,8 @@ void setInternalAutoShrinkCallback(@Nullable OnChangedCallback callback) {
846849
internalAutoShrinkCallback = callback;
847850
}
848851

849-
private boolean shouldUpdateVisibility(View dependency, ExtendedFloatingActionButton child) {
852+
private boolean shouldUpdateVisibility(
853+
@NonNull View dependency, @NonNull ExtendedFloatingActionButton child) {
850854
final CoordinatorLayout.LayoutParams lp =
851855
(CoordinatorLayout.LayoutParams) child.getLayoutParams();
852856
if (!autoHideEnabled && !autoShrinkEnabled) {
@@ -863,7 +867,9 @@ private boolean shouldUpdateVisibility(View dependency, ExtendedFloatingActionBu
863867
}
864868

865869
private boolean updateFabVisibilityForAppBarLayout(
866-
CoordinatorLayout parent, AppBarLayout appBarLayout, ExtendedFloatingActionButton child) {
870+
CoordinatorLayout parent,
871+
@NonNull AppBarLayout appBarLayout,
872+
@NonNull ExtendedFloatingActionButton child) {
867873
if (!shouldUpdateVisibility(appBarLayout, child)) {
868874
return false;
869875
}
@@ -887,7 +893,7 @@ private boolean updateFabVisibilityForAppBarLayout(
887893
}
888894

889895
private boolean updateFabVisibilityForBottomSheet(
890-
View bottomSheet, ExtendedFloatingActionButton child) {
896+
@NonNull View bottomSheet, @NonNull ExtendedFloatingActionButton child) {
891897
if (!shouldUpdateVisibility(bottomSheet, child)) {
892898
return false;
893899
}
@@ -948,7 +954,9 @@ protected void extendOrShow(@NonNull ExtendedFloatingActionButton fab) {
948954

949955
@Override
950956
public boolean onLayoutChild(
951-
CoordinatorLayout parent, ExtendedFloatingActionButton child, int layoutDirection) {
957+
@NonNull CoordinatorLayout parent,
958+
@NonNull ExtendedFloatingActionButton child,
959+
int layoutDirection) {
952960
// First, let's make sure that the visibility of the FAB is consistent
953961
final List<View> dependencies = parent.getDependencies(child);
954962
for (int i = 0, count = dependencies.size(); i < count; i++) {
@@ -991,7 +999,8 @@ public boolean getInsetDodgeRect(
991999
* offsets our layout position so that we're positioned correctly if we're on one of our
9921000
* parent's edges.
9931001
*/
994-
private void offsetIfNeeded(CoordinatorLayout parent, ExtendedFloatingActionButton fab) {
1002+
private void offsetIfNeeded(
1003+
@NonNull CoordinatorLayout parent, @NonNull ExtendedFloatingActionButton fab) {
9951004
final Rect padding = fab.shadowPadding;
9961005

9971006
if (padding != null && padding.centerX() > 0 && padding.centerY() > 0) {
@@ -1077,6 +1086,7 @@ public int getDefaultMotionSpecResource() {
10771086
return R.animator.mtrl_extended_fab_change_size_motion_spec;
10781087
}
10791088

1089+
@NonNull
10801090
@Override
10811091
public AnimatorSet createAnimator() {
10821092
MotionSpec spec = getCurrentMotionSpec();

lib/java/com/google/android/material/floatingactionbutton/FloatingActionButton.java

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -184,21 +184,22 @@ public void onHidden(FloatingActionButton fab) {}
184184
final Rect shadowPadding = new Rect();
185185
private final Rect touchArea = new Rect();
186186

187-
private final AppCompatImageHelper imageHelper;
188-
private final ExpandableWidgetHelper expandableWidgetHelper;
187+
@NonNull private final AppCompatImageHelper imageHelper;
188+
@NonNull private final ExpandableWidgetHelper expandableWidgetHelper;
189189

190190
private FloatingActionButtonImpl impl;
191191

192-
public FloatingActionButton(Context context) {
192+
public FloatingActionButton(@NonNull Context context) {
193193
this(context, null);
194194
}
195195

196-
public FloatingActionButton(Context context, @Nullable AttributeSet attrs) {
196+
public FloatingActionButton(@NonNull Context context, @Nullable AttributeSet attrs) {
197197
this(context, attrs, R.attr.floatingActionButtonStyle);
198198
}
199199

200200
@SuppressWarnings("initialization")
201-
public FloatingActionButton(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
201+
public FloatingActionButton(
202+
@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
202203
super(createThemedContext(context, attrs, defStyleAttr, DEF_STYLE_RES), attrs, defStyleAttr);
203204
// Ensure we are using the correctly themed context rather than the context that was passed in.
204205
context = getContext();
@@ -512,7 +513,7 @@ public void setImageDrawable(@Nullable Drawable drawable) {
512513
}
513514

514515
/** Sets the {@link ShapeAppearanceModel} for this {@link FloatingActionButton}. */
515-
public void setShapeAppearance(ShapeAppearanceModel shapeAppearance) {
516+
public void setShapeAppearance(@NonNull ShapeAppearanceModel shapeAppearance) {
516517
getImpl().setShapeAppearance(shapeAppearance, isUsingDefaultCorner(shapeAppearance));
517518
}
518519

@@ -914,7 +915,7 @@ private static int resolveAdjustedSize(int desiredSize, int measureSpec) {
914915
}
915916

916917
@Override
917-
public boolean onTouchEvent(MotionEvent ev) {
918+
public boolean onTouchEvent(@NonNull MotionEvent ev) {
918919
if (ev.getAction() == MotionEvent.ACTION_DOWN) {
919920
// Skipping the gesture if it doesn't start in the FAB 'content' area
920921
if (getContentRect(touchArea) && !touchArea.contains((int) ev.getX(), (int) ev.getY())) {
@@ -1006,7 +1007,7 @@ public void onAttachedToLayoutParams(@NonNull CoordinatorLayout.LayoutParams lp)
10061007

10071008
@Override
10081009
public boolean onDependentViewChanged(
1009-
CoordinatorLayout parent, FloatingActionButton child, View dependency) {
1010+
CoordinatorLayout parent, @NonNull FloatingActionButton child, View dependency) {
10101011
if (dependency instanceof AppBarLayout) {
10111012
// If we're depending on an AppBarLayout we will show/hide it automatically
10121013
// if the FAB is anchored to the AppBarLayout
@@ -1030,7 +1031,8 @@ public void setInternalAutoHideListener(OnVisibilityChangedListener listener) {
10301031
internalAutoHideListener = listener;
10311032
}
10321033

1033-
private boolean shouldUpdateVisibility(View dependency, FloatingActionButton child) {
1034+
private boolean shouldUpdateVisibility(
1035+
@NonNull View dependency, @NonNull FloatingActionButton child) {
10341036
final CoordinatorLayout.LayoutParams lp =
10351037
(CoordinatorLayout.LayoutParams) child.getLayoutParams();
10361038
if (!autoHideEnabled) {
@@ -1053,7 +1055,9 @@ private boolean shouldUpdateVisibility(View dependency, FloatingActionButton chi
10531055
}
10541056

10551057
private boolean updateFabVisibilityForAppBarLayout(
1056-
CoordinatorLayout parent, AppBarLayout appBarLayout, FloatingActionButton child) {
1058+
CoordinatorLayout parent,
1059+
@NonNull AppBarLayout appBarLayout,
1060+
@NonNull FloatingActionButton child) {
10571061
if (!shouldUpdateVisibility(appBarLayout, child)) {
10581062
return false;
10591063
}
@@ -1077,7 +1081,7 @@ private boolean updateFabVisibilityForAppBarLayout(
10771081
}
10781082

10791083
private boolean updateFabVisibilityForBottomSheet(
1080-
View bottomSheet, FloatingActionButton child) {
1084+
@NonNull View bottomSheet, @NonNull FloatingActionButton child) {
10811085
if (!shouldUpdateVisibility(bottomSheet, child)) {
10821086
return false;
10831087
}
@@ -1092,7 +1096,9 @@ private boolean updateFabVisibilityForBottomSheet(
10921096

10931097
@Override
10941098
public boolean onLayoutChild(
1095-
CoordinatorLayout parent, FloatingActionButton child, int layoutDirection) {
1099+
@NonNull CoordinatorLayout parent,
1100+
@NonNull FloatingActionButton child,
1101+
int layoutDirection) {
10961102
// First, let's make sure that the visibility of the FAB is consistent
10971103
final List<View> dependencies = parent.getDependencies(child);
10981104
for (int i = 0, count = dependencies.size(); i < count; i++) {
@@ -1135,7 +1141,8 @@ public boolean getInsetDodgeRect(
11351141
* offsets our layout position so that we're positioned correctly if we're on one of our
11361142
* parent's edges.
11371143
*/
1138-
private void offsetIfNeeded(CoordinatorLayout parent, FloatingActionButton fab) {
1144+
private void offsetIfNeeded(
1145+
@NonNull CoordinatorLayout parent, @NonNull FloatingActionButton fab) {
11391146
final Rect padding = fab.shadowPadding;
11401147

11411148
if (padding != null && padding.centerX() > 0 && padding.centerY() > 0) {
@@ -1345,7 +1352,7 @@ public void removeTransformationCallback(
13451352
getImpl().removeTransformationCallback(new TransformationCallbackWrapper(listener));
13461353
}
13471354

1348-
private boolean isUsingDefaultCorner(ShapeAppearanceModel shapeAppearance) {
1355+
private boolean isUsingDefaultCorner(@NonNull ShapeAppearanceModel shapeAppearance) {
13491356
return shapeAppearance.getTopRightCorner().getCornerSize() == -1;
13501357
}
13511358

@@ -1428,6 +1435,7 @@ private FloatingActionButtonImpl getImpl() {
14281435
return impl;
14291436
}
14301437

1438+
@NonNull
14311439
private FloatingActionButtonImpl createImpl() {
14321440
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
14331441
return new FloatingActionButtonImplLollipop(this, new ShadowDelegateImpl());

0 commit comments

Comments
 (0)