get ripple drawables by id (#28600)
Summary:
While working on recent PRs regarding ripple radius in TouchableNativeFeedbaack and ripple support in Pressable I noticed `ReactDrawableHelper` uses a [discouraged](https://developer.android.com/reference/android/content/res/Resources#getIdentifier(java.lang.String,%20java.lang.String,%20java.lang.String)) way to obtain resources.
The attribute names (strings) `'selectableItemBackground'` and `'selectableItemBackgroundBorderless'` are used here
4a48b021d6/Libraries/Components/Touchable/TouchableNativeFeedback.js (L105)
And passed to `context.getResources().getIdentifier()` in `ReactDrawableHelper`. Since we know the attribute names beforehand I figured we can obtain the resources by id (fast) instead of by name (slow). I made it so that the slow code path is taken in case the attribute name does not match what is expected, as a fallback.
Note that I did not do any measurement of the effect of this, I'm just offering this as a PR. You'll notice that this PR relies on the fact that the string in JS is the same as the string in Java (it is duplicated). While I could export the strings from Java and use them in JS, I wasn't sure where to export them. But note that even before, the JS code depended on the `'selectableItemBackground'` and `'selectableItemBackgroundBorderless'` strings to exist on the native side, in the android SDK, I just made the dependency explicit.
## Changelog
<!-- Help reviewers and the release process by writing your own changelog entry. For an example, see:
https://github.com/facebook/react-native/wiki/Changelog
-->
[Android] [Changed] - get ripple drawables by id
Pull Request resolved: https://github.com/facebook/react-native/pull/28600
Test Plan: tested manually in RNTester
Differential Revision: D21241773
Pulled By: shergin
fbshipit-source-id: 1b8314f99616095cb6ed557c62095cf3200f53b6
This commit is contained in:
Родитель
04b8c9c925
Коммит
c8ed2dbbb2
|
@ -17,6 +17,7 @@ import android.graphics.drawable.RippleDrawable;
|
|||
import android.os.Build;
|
||||
import android.util.TypedValue;
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import com.facebook.react.bridge.JSApplicationIllegalArgumentException;
|
||||
import com.facebook.react.bridge.ReadableMap;
|
||||
import com.facebook.react.bridge.SoftAssertions;
|
||||
|
@ -37,15 +38,10 @@ public class ReactDrawableHelper {
|
|||
String type = drawableDescriptionDict.getString("type");
|
||||
if ("ThemeAttrAndroid".equals(type)) {
|
||||
String attr = drawableDescriptionDict.getString("attribute");
|
||||
SoftAssertions.assertNotNull(attr);
|
||||
int attrID = context.getResources().getIdentifier(attr, "attr", "android");
|
||||
if (attrID == 0) {
|
||||
int attrId = getAttrId(context, attr);
|
||||
if (!context.getTheme().resolveAttribute(attrId, sResolveOutValue, true)) {
|
||||
throw new JSApplicationIllegalArgumentException(
|
||||
"Attribute " + attr + " couldn't be found in the resource list");
|
||||
}
|
||||
if (!context.getTheme().resolveAttribute(attrID, sResolveOutValue, true)) {
|
||||
throw new JSApplicationIllegalArgumentException(
|
||||
"Attribute " + attr + " couldn't be resolved into a drawable");
|
||||
"Attribute " + attr + " with id " + attrId + " couldn't be resolved into a drawable");
|
||||
}
|
||||
Drawable drawable = getDefaultThemeDrawable(context);
|
||||
return setRadius(drawableDescriptionDict, drawable);
|
||||
|
@ -57,6 +53,18 @@ public class ReactDrawableHelper {
|
|||
}
|
||||
}
|
||||
|
||||
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
|
||||
private static int getAttrId(Context context, String attr) {
|
||||
SoftAssertions.assertNotNull(attr);
|
||||
if ("selectableItemBackground".equals(attr)) {
|
||||
return android.R.attr.selectableItemBackground;
|
||||
} else if ("selectableItemBackgroundBorderless".equals(attr)) {
|
||||
return android.R.attr.selectableItemBackgroundBorderless;
|
||||
} else {
|
||||
return context.getResources().getIdentifier(attr, "attr", "android");
|
||||
}
|
||||
}
|
||||
|
||||
private static Drawable getDefaultThemeDrawable(Context context) {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
||||
return context.getResources().getDrawable(sResolveOutValue.resourceId, context.getTheme());
|
||||
|
|
Загрузка…
Ссылка в новой задаче