From d2fc19f4aa94888b7c3d3f4a5fb621bf96a1aff9 Mon Sep 17 00:00:00 2001 From: Dulmandakh Date: Sat, 9 Feb 2019 22:21:40 -0800 Subject: [PATCH] fix lint error/warnings (#23333) Summary: Fix lint errors and warning, which might be a cause of various crashes on older Android OS, using Android Support Library. ```bash ./gradlew :ReactAndroid:lint ``` [Android] [Changed] - fix lint error/warnings Pull Request resolved: https://github.com/facebook/react-native/pull/23333 Differential Revision: D14019322 Pulled By: mdvacca fbshipit-source-id: 74c98da269c318cf3b114c8d9c876186369f2b8c --- .../java/com/facebook/react/HeadlessJsTaskService.java | 2 ++ .../react/modules/systeminfo/AndroidInfoModule.java | 2 ++ .../com/facebook/react/uimanager/BaseViewManager.java | 9 +++++---- .../views/scroll/ReactHorizontalScrollContainerView.java | 3 ++- .../react/views/scroll/ReactHorizontalScrollView.java | 6 +++--- .../react/views/text/ReactBaseTextShadowNode.java | 2 ++ .../facebook/react/views/text/ReactTextShadowNode.java | 2 ++ .../react/views/textinput/ReactTextInputShadowNode.java | 9 ++++++--- .../react/views/toolbar/ReactToolbarManager.java | 3 ++- .../facebook/react/views/view/ReactDrawableHelper.java | 2 ++ .../react/views/webview/ReactWebViewManager.java | 1 + 11 files changed, 29 insertions(+), 12 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/HeadlessJsTaskService.java b/ReactAndroid/src/main/java/com/facebook/react/HeadlessJsTaskService.java index 8f84722bbb3849..9c4668f2e0c3c4 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/HeadlessJsTaskService.java +++ b/ReactAndroid/src/main/java/com/facebook/react/HeadlessJsTaskService.java @@ -12,6 +12,7 @@ import java.util.Set; import java.util.concurrent.CopyOnWriteArraySet; +import android.annotation.SuppressLint; import android.app.Service; import android.content.BroadcastReceiver; import android.content.Context; @@ -67,6 +68,7 @@ public int onStartCommand(Intent intent, int flags, int startId) { /** * Acquire a wake lock to ensure the device doesn't go to sleep while processing background tasks. */ + @SuppressLint("WakelockTimeout") public static void acquireWakeLockNow(Context context) { if (sWakeLock == null || !sWakeLock.isHeld()) { PowerManager powerManager = diff --git a/ReactAndroid/src/main/java/com/facebook/react/modules/systeminfo/AndroidInfoModule.java b/ReactAndroid/src/main/java/com/facebook/react/modules/systeminfo/AndroidInfoModule.java index 3860cb9671f4f9..88193944009202 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/modules/systeminfo/AndroidInfoModule.java +++ b/ReactAndroid/src/main/java/com/facebook/react/modules/systeminfo/AndroidInfoModule.java @@ -7,6 +7,7 @@ package com.facebook.react.modules.systeminfo; +import android.annotation.SuppressLint; import android.app.UiModeManager; import android.content.res.Configuration; import android.os.Build; @@ -29,6 +30,7 @@ * Module that exposes Android Constants to JS. */ @ReactModule(name = AndroidInfoModule.NAME) +@SuppressLint("HardwareIds") public class AndroidInfoModule extends ReactContextBaseJavaModule { public static final String NAME = "PlatformConstants"; private static final String IS_TESTING = "IS_TESTING"; diff --git a/ReactAndroid/src/main/java/com/facebook/react/uimanager/BaseViewManager.java b/ReactAndroid/src/main/java/com/facebook/react/uimanager/BaseViewManager.java index 5f1d91c820f9ca..e7d003854bec89 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/uimanager/BaseViewManager.java +++ b/ReactAndroid/src/main/java/com/facebook/react/uimanager/BaseViewManager.java @@ -7,6 +7,7 @@ import android.graphics.Color; import android.os.Build; +import android.support.v4.view.ViewCompat; import android.view.View; import android.view.ViewParent; import com.facebook.react.R; @@ -156,13 +157,13 @@ public void setViewStates(T view, ReadableArray accessibilityStates) { @ReactProp(name = PROP_IMPORTANT_FOR_ACCESSIBILITY) public void setImportantForAccessibility(T view, String importantForAccessibility) { if (importantForAccessibility == null || importantForAccessibility.equals("auto")) { - view.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_AUTO); + ViewCompat.setImportantForAccessibility(view, ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_AUTO); } else if (importantForAccessibility.equals("yes")) { - view.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_YES); + ViewCompat.setImportantForAccessibility(view, ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_YES); } else if (importantForAccessibility.equals("no")) { - view.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_NO); + ViewCompat.setImportantForAccessibility(view, ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_NO); } else if (importantForAccessibility.equals("no-hide-descendants")) { - view.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS); + ViewCompat.setImportantForAccessibility(view, ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS); } } diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactHorizontalScrollContainerView.java b/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactHorizontalScrollContainerView.java index f387fba4c1795e..0459b0688fdbfb 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactHorizontalScrollContainerView.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactHorizontalScrollContainerView.java @@ -6,6 +6,7 @@ package com.facebook.react.views.scroll; import android.content.Context; +import android.support.v4.view.ViewCompat; import android.view.ViewGroup; import android.widget.HorizontalScrollView; import com.facebook.react.modules.i18nmanager.I18nUtil; @@ -19,7 +20,7 @@ public class ReactHorizontalScrollContainerView extends ViewGroup { public ReactHorizontalScrollContainerView(Context context) { super(context); mLayoutDirection = - I18nUtil.getInstance().isRTL(context) ? LAYOUT_DIRECTION_RTL : LAYOUT_DIRECTION_LTR; + I18nUtil.getInstance().isRTL(context) ? ViewCompat.LAYOUT_DIRECTION_RTL : ViewCompat.LAYOUT_DIRECTION_LTR; mCurrentWidth = 0; } diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactHorizontalScrollView.java b/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactHorizontalScrollView.java index 8a2e1da4b24ea5..566b6018b02e1f 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactHorizontalScrollView.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactHorizontalScrollView.java @@ -305,7 +305,7 @@ public void fling(int velocityX) { // as there is content. See #onOverScrolled() to see the second part of this change which properly // aborts the scroller animation when we get to the bottom of the ScrollView content. - int scrollWindowWidth = getWidth() - getPaddingStart() - getPaddingEnd(); + int scrollWindowWidth = getWidth() - ViewCompat.getPaddingStart(this) - ViewCompat.getPaddingEnd(this); mScroller.fling( getScrollX(), // startX @@ -501,7 +501,7 @@ private int predictFinalScrollPosition(int velocityX) { // predict where a fling would end up so we can scroll to the nearest snap offset int maximumOffset = Math.max(0, computeHorizontalScrollRange() - getWidth()); - int width = getWidth() - getPaddingStart() - getPaddingEnd(); + int width = getWidth() - ViewCompat.getPaddingStart(this) - ViewCompat.getPaddingEnd(this); scroller.fling( getScrollX(), // startX getScrollY(), // startY @@ -583,7 +583,7 @@ private void flingAndSnap(int velocityX) { int largerOffset = maximumOffset; int firstOffset = 0; int lastOffset = maximumOffset; - int width = getWidth() - getPaddingStart() - getPaddingEnd(); + int width = getWidth() - ViewCompat.getPaddingStart(this) - ViewCompat.getPaddingEnd(this); // offsets are from the right edge in RTL layouts boolean isRTL = TextUtilsCompat.getLayoutDirectionFromLocale(Locale.getDefault()) == ViewCompat.LAYOUT_DIRECTION_RTL; diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactBaseTextShadowNode.java b/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactBaseTextShadowNode.java index 502f311ac22b23..ec00cd5b30662e 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactBaseTextShadowNode.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactBaseTextShadowNode.java @@ -6,6 +6,7 @@ */ package com.facebook.react.views.text; +import android.annotation.TargetApi; import android.graphics.Color; import android.graphics.Typeface; import android.os.Build; @@ -37,6 +38,7 @@ *

This also node calculates {@link Spannable} object based on subnodes of the same type, which * can be used in concrete classes to feed native views and compute layout. */ +@TargetApi(Build.VERSION_CODES.M) public abstract class ReactBaseTextShadowNode extends LayoutShadowNode { private static final String INLINE_IMAGE_PLACEHOLDER = "I"; diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextShadowNode.java b/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextShadowNode.java index b38c8424ef3273..eaef5690304b33 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextShadowNode.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextShadowNode.java @@ -6,6 +6,7 @@ */ package com.facebook.react.views.text; +import android.annotation.TargetApi; import android.os.Build; import android.text.BoringLayout; import android.text.Layout; @@ -37,6 +38,7 @@ *

The class measures text in {@code } view and feeds native {@link TextView} using {@code * Spannable} object constructed in superclass. */ +@TargetApi(Build.VERSION_CODES.M) public class ReactTextShadowNode extends ReactBaseTextShadowNode { // It's important to pass the ANTI_ALIAS_FLAG flag to the constructor rather than setting it diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactTextInputShadowNode.java b/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactTextInputShadowNode.java index 0ce7819bcfac8d..53efe02e06e46d 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactTextInputShadowNode.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactTextInputShadowNode.java @@ -7,7 +7,9 @@ package com.facebook.react.views.textinput; +import android.annotation.TargetApi; import android.os.Build; +import android.support.v4.view.ViewCompat; import android.text.Layout; import android.util.TypedValue; import android.view.ViewGroup; @@ -33,6 +35,7 @@ import javax.annotation.Nullable; @VisibleForTesting +@TargetApi(Build.VERSION_CODES.M) public class ReactTextInputShadowNode extends ReactBaseTextShadowNode implements YogaMeasureFunction { @@ -49,7 +52,7 @@ public class ReactTextInputShadowNode extends ReactBaseTextShadowNode public ReactTextInputShadowNode() { mTextBreakStrategy = (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) ? - 0 : Layout.BREAK_STRATEGY_SIMPLE; + Layout.BREAK_STRATEGY_SIMPLE : Layout.BREAK_STRATEGY_HIGH_QUALITY; initMeasureFunction(); } @@ -71,9 +74,9 @@ public void setThemedContext(ThemedReactContext themedContext) { // So, we have to enforce it as a default padding. // TODO #7120264: Cache this stuff better. EditText editText = new EditText(getThemedContext()); - setDefaultPadding(Spacing.START, editText.getPaddingStart()); + setDefaultPadding(Spacing.START, ViewCompat.getPaddingStart(editText)); setDefaultPadding(Spacing.TOP, editText.getPaddingTop()); - setDefaultPadding(Spacing.END, editText.getPaddingEnd()); + setDefaultPadding(Spacing.END, ViewCompat.getPaddingEnd(editText)); setDefaultPadding(Spacing.BOTTOM, editText.getPaddingBottom()); mDummyEditText = editText; diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/toolbar/ReactToolbarManager.java b/ReactAndroid/src/main/java/com/facebook/react/views/toolbar/ReactToolbarManager.java index d15d9ba96a4cd7..8467c957237022 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/toolbar/ReactToolbarManager.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/toolbar/ReactToolbarManager.java @@ -11,6 +11,7 @@ import android.content.res.Resources; import android.content.res.TypedArray; import android.graphics.Color; +import android.support.v4.view.ViewCompat; import android.util.LayoutDirection; import android.view.MenuItem; import android.view.View; @@ -62,7 +63,7 @@ public void setOverflowIcon(ReactToolbar view, @Nullable ReadableMap overflowIco @ReactProp(name = "rtl") public void setRtl(ReactToolbar view, boolean rtl) { - view.setLayoutDirection(rtl ? LayoutDirection.RTL : LayoutDirection.LTR); + ViewCompat.setLayoutDirection(view, rtl ? ViewCompat.LAYOUT_DIRECTION_RTL : ViewCompat.LAYOUT_DIRECTION_LTR); } @ReactProp(name = "subtitle") diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/view/ReactDrawableHelper.java b/ReactAndroid/src/main/java/com/facebook/react/views/view/ReactDrawableHelper.java index 96021ea8f0545f..51b24ceeaa2935 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/view/ReactDrawableHelper.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/view/ReactDrawableHelper.java @@ -7,6 +7,7 @@ package com.facebook.react.views.view; +import android.annotation.TargetApi; import android.content.Context; import android.content.res.ColorStateList; import android.graphics.Color; @@ -29,6 +30,7 @@ public class ReactDrawableHelper { private static final TypedValue sResolveOutValue = new TypedValue(); + @TargetApi(Build.VERSION_CODES.LOLLIPOP) public static Drawable createDrawableFromJSDescription( Context context, ReadableMap drawableDescriptionDict) { diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/webview/ReactWebViewManager.java b/ReactAndroid/src/main/java/com/facebook/react/views/webview/ReactWebViewManager.java index 4ac247b59000a7..a5f54d0ca6f205 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/webview/ReactWebViewManager.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/webview/ReactWebViewManager.java @@ -504,6 +504,7 @@ public void setUserAgent(WebView view, @Nullable String userAgent) { } @ReactProp(name = "mediaPlaybackRequiresUserAction") + @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1) public void setMediaPlaybackRequiresUserAction(WebView view, boolean requires) { view.getSettings().setMediaPlaybackRequiresUserGesture(requires); }