|
| 1 | +/** |
| 2 | + * Copyright (c) Facebook, Inc. and its affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +package com.facebook.react.views.progressbar; |
| 9 | + |
| 10 | +import javax.annotation.Nullable; |
| 11 | + |
| 12 | +import android.content.Context; |
| 13 | +import android.widget.ProgressBar; |
| 14 | + |
| 15 | +import com.facebook.react.bridge.JSApplicationIllegalArgumentException; |
| 16 | +import com.facebook.react.module.annotations.ReactModule; |
| 17 | +import com.facebook.react.uimanager.BaseViewManager; |
| 18 | +import com.facebook.react.uimanager.annotations.ReactProp; |
| 19 | +import com.facebook.react.uimanager.ThemedReactContext; |
| 20 | +import com.facebook.react.uimanager.ViewProps; |
| 21 | + |
| 22 | +/** |
| 23 | + * Manages instances of ProgressBar. ProgressBar is wrapped in a ProgressBarContainerView because |
| 24 | + * the style of the ProgressBar can only be set in the constructor; whenever the style of a |
| 25 | + * ProgressBar changes, we have to drop the existing ProgressBar (if there is one) and create a new |
| 26 | + * one with the style given. |
| 27 | + */ |
| 28 | +@ReactModule(name = ReactProgressBarViewManager.REACT_CLASS) |
| 29 | +public class ReactProgressBarViewManager extends |
| 30 | + BaseViewManager<ProgressBarContainerView, ProgressBarShadowNode> { |
| 31 | + |
| 32 | + public static final String REACT_CLASS = "AndroidProgressBar"; |
| 33 | + |
| 34 | + /* package */ static final String PROP_STYLE = "styleAttr"; |
| 35 | + /* package */ static final String PROP_INDETERMINATE = "indeterminate"; |
| 36 | + /* package */ static final String PROP_PROGRESS = "progress"; |
| 37 | + /* package */ static final String PROP_ANIMATING = "animating"; |
| 38 | + |
| 39 | + /* package */ static final String DEFAULT_STYLE = "Normal"; |
| 40 | + |
| 41 | + private static Object sProgressBarCtorLock = new Object(); |
| 42 | + |
| 43 | + /** |
| 44 | + * We create ProgressBars on both the UI and shadow threads. There is a race condition in the |
| 45 | + * ProgressBar constructor that may cause crashes when two ProgressBars are constructed at the |
| 46 | + * same time on two different threads. This static ctor wrapper protects against that. |
| 47 | + */ |
| 48 | + public static ProgressBar createProgressBar(Context context, int style) { |
| 49 | + synchronized (sProgressBarCtorLock) { |
| 50 | + return new ProgressBar(context, null, style); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public String getName() { |
| 56 | + return REACT_CLASS; |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + protected ProgressBarContainerView createViewInstance(ThemedReactContext context) { |
| 61 | + return new ProgressBarContainerView(context); |
| 62 | + } |
| 63 | + |
| 64 | + @ReactProp(name = PROP_STYLE) |
| 65 | + public void setStyle(ProgressBarContainerView view, @Nullable String styleName) { |
| 66 | + view.setStyle(styleName); |
| 67 | + } |
| 68 | + |
| 69 | + @ReactProp(name = ViewProps.COLOR, customType = "Color") |
| 70 | + public void setColor(ProgressBarContainerView view, @Nullable Integer color) { |
| 71 | + view.setColor(color); |
| 72 | + } |
| 73 | + |
| 74 | + @ReactProp(name = PROP_INDETERMINATE) |
| 75 | + public void setIndeterminate(ProgressBarContainerView view, boolean indeterminate) { |
| 76 | + view.setIndeterminate(indeterminate); |
| 77 | + } |
| 78 | + |
| 79 | + @ReactProp(name = PROP_PROGRESS) |
| 80 | + public void setProgress(ProgressBarContainerView view, double progress) { |
| 81 | + view.setProgress(progress); |
| 82 | + } |
| 83 | + |
| 84 | + @ReactProp(name = PROP_ANIMATING) |
| 85 | + public void setAnimating(ProgressBarContainerView view, boolean animating) { |
| 86 | + view.setAnimating(animating); |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public ProgressBarShadowNode createShadowNodeInstance() { |
| 91 | + return new ProgressBarShadowNode(); |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public Class<ProgressBarShadowNode> getShadowNodeClass() { |
| 96 | + return ProgressBarShadowNode.class; |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public void updateExtraData(ProgressBarContainerView root, Object extraData) { |
| 101 | + // do nothing |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + protected void onAfterUpdateTransaction(ProgressBarContainerView view) { |
| 106 | + view.apply(); |
| 107 | + } |
| 108 | + |
| 109 | + /* package */ static int getStyleFromString(@Nullable String styleStr) { |
| 110 | + if (styleStr == null) { |
| 111 | + throw new JSApplicationIllegalArgumentException( |
| 112 | + "ProgressBar needs to have a style, null received"); |
| 113 | + } else if (styleStr.equals("Horizontal")) { |
| 114 | + return android.R.attr.progressBarStyleHorizontal; |
| 115 | + } else if (styleStr.equals("Small")) { |
| 116 | + return android.R.attr.progressBarStyleSmall; |
| 117 | + } else if (styleStr.equals("Large")) { |
| 118 | + return android.R.attr.progressBarStyleLarge; |
| 119 | + } else if (styleStr.equals("Inverse")) { |
| 120 | + return android.R.attr.progressBarStyleInverse; |
| 121 | + } else if (styleStr.equals("SmallInverse")) { |
| 122 | + return android.R.attr.progressBarStyleSmallInverse; |
| 123 | + } else if (styleStr.equals("LargeInverse")) { |
| 124 | + return android.R.attr.progressBarStyleLargeInverse; |
| 125 | + } else if (styleStr.equals("Normal")) { |
| 126 | + return android.R.attr.progressBarStyle; |
| 127 | + } else { |
| 128 | + throw new JSApplicationIllegalArgumentException("Unknown ProgressBar style: " + styleStr); |
| 129 | + } |
| 130 | + } |
| 131 | +} |
0 commit comments