• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

opensrp / opensrp-client-native-form / #57

12 Sep 2023 07:43AM UTC coverage: 66.18% (+0.2%) from 66.013%
#57

push

github-actions

web-flow
Merge pull request #667 from opensrp/fix_tech_debt_660

Add tests for setUpOptiBpActivityResultListener

8432 of 12741 relevant lines covered (66.18%)

0.66 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.92
android-json-form-wizard/src/main/java/com/vijay/jsonwizard/widgets/LabelFactory.java
1
package com.vijay.jsonwizard.widgets;
2

3
import android.content.Context;
4
import android.graphics.Color;
5
import android.os.Build;
6
import androidx.constraintlayout.widget.ConstraintLayout;
7
import android.text.Html;
8
import android.text.Spanned;
9
import android.text.TextUtils;
10
import android.util.Log;
11
import android.view.View;
12
import android.widget.LinearLayout;
13

14
import com.rey.material.util.ViewUtil;
15
import com.vijay.jsonwizard.R;
16
import com.vijay.jsonwizard.constants.JsonFormConstants;
17
import com.vijay.jsonwizard.fragments.JsonFormFragment;
18
import com.vijay.jsonwizard.interfaces.CommonListener;
19
import com.vijay.jsonwizard.interfaces.FormWidgetFactory;
20
import com.vijay.jsonwizard.utils.FormUtils;
21
import com.vijay.jsonwizard.views.CustomTextView;
22

23
import org.json.JSONArray;
24
import org.json.JSONException;
25
import org.json.JSONObject;
26

27
import java.util.ArrayList;
28
import java.util.HashSet;
29
import java.util.List;
30
import java.util.Set;
31

32
/**
33
 * Created by vijay on 24-05-2015.
34
 */
35
public class LabelFactory implements FormWidgetFactory {
1✔
36
    private final String TAG = this.getClass().getSimpleName();
1✔
37
    private CustomTextView numberText;
38
    private FormUtils formUtils = new FormUtils();
1✔
39

40
    @Override
41
    public List<View> getViewsFromJson(String stepName, Context context, JsonFormFragment formFragment,
42
                                       JSONObject jsonObject, CommonListener
43
                                               listener, boolean popup) throws Exception {
44
        return attachJson(stepName, context, jsonObject, listener, popup);
×
45
    }
46

47
    @Override
48
    public List<View> getViewsFromJson(String stepName, Context context, JsonFormFragment formFragment,
49
                                       JSONObject jsonObject, CommonListener listener) throws Exception {
50
        return attachJson(stepName, context, jsonObject, listener, false);
1✔
51
    }
52

53
    private List<View> attachJson(String stepName, Context context, JSONObject jsonObject, CommonListener
54
            listener, boolean popup) throws JSONException {
55
        List<View> views = new ArrayList<>(1);
1✔
56
        if (jsonObject.has(JsonFormConstants.TEXT)) {
1✔
57
            JSONArray canvasIds = new JSONArray();
1✔
58
            ConstraintLayout constraintLayout = formUtils.createLabelLinearLayout(stepName, canvasIds, jsonObject, context, listener);
1✔
59

60
            constraintLayout.setTag(R.id.address, stepName + ":" + jsonObject.getString(JsonFormConstants.KEY));
1✔
61

62
            boolean hasBg = jsonObject.optBoolean("has_bg", false);
1✔
63
            String topMargin = jsonObject.optString("top_margin", "0dp");
1✔
64
            String bottomMargin = null;
1✔
65
            if (hasBg) {
1✔
66
                bottomMargin = jsonObject.optString("bottom_margin", "0dp");
1✔
67
            }
68

69
            int topMarginInt = getValueFromSpOrDpOrPx(context, topMargin);
1✔
70
            int bottomMarginInt = (int) context.getResources().getDimension(R.dimen.default_bottom_margin);
1✔
71
            if (hasBg) {
1✔
72
                bottomMarginInt = getValueFromSpOrDpOrPx(context, bottomMargin);
1✔
73
            }
74

75
            LinearLayout.LayoutParams layoutParams = FormUtils
1✔
76
                    .getLinearLayoutParams(FormUtils.MATCH_PARENT, FormUtils.WRAP_CONTENT, 0,
1✔
77
                            topMarginInt, 0, bottomMarginInt);
78
            constraintLayout.setLayoutParams(layoutParams);
1✔
79

80
            createLabelTextView(jsonObject, context, constraintLayout);
1✔
81

82
            // Set the id for the view
83
            constraintLayout.setId(ViewUtil.generateViewId());
1✔
84
            canvasIds.put(constraintLayout.getId());
1✔
85
            constraintLayout.setTag(R.id.canvas_ids, canvasIds.toString());
1✔
86
            constraintLayout.setTag(R.id.extraPopup, popup);
1✔
87
            views.add(constraintLayout);
1✔
88
        } else {
1✔
89
            Log.e(TAG, "A label requires a text. You cannot have a label with blank text");
×
90
        }
91
        return views;
1✔
92
    }
93

94
    public int getValueFromSpOrDpOrPx(Context context, String bottomMargin) {
95
        return FormUtils.getValueFromSpOrDpOrPx(bottomMargin, context);
×
96
    }
97

98
    /**
99
     * Instantiates the label Custom TextView and adds in its attributes
100
     *
101
     * @param jsonObject
102
     * @param context
103
     * @param constraintLayout
104
     * @throws JSONException
105
     */
106
    private void createLabelTextView(JSONObject jsonObject, Context context, ConstraintLayout constraintLayout)
107
            throws JSONException {
108
        boolean hintOnText = jsonObject.optBoolean(JsonFormConstants.HINT_ON_TEXT, false);
1✔
109
        boolean hasBg = jsonObject.optBoolean(JsonFormConstants.HAS_BG, false);
1✔
110
        String labelNumber = jsonObject.optString(JsonFormConstants.LABEL_NUMBER, null);
1✔
111

112
        String bgColor = null;
1✔
113
        String topPadding = null;
1✔
114
        String bottomPadding = null;
1✔
115
        String leftPadding = null;
1✔
116
        String rightPadding = null;
1✔
117
        if (hasBg) {
1✔
118
            bgColor = jsonObject.optString(JsonFormConstants.BG_COLOR, "#F3F3F3");
1✔
119
            topPadding = jsonObject.optString(JsonFormConstants.TOP_PADDING, "5dp");
1✔
120
            bottomPadding = jsonObject.optString(JsonFormConstants.BOTTOM_PADDING, "5dp");
1✔
121
            leftPadding = jsonObject.optString(JsonFormConstants.LEFT_PADDING, "5dp");
1✔
122
            rightPadding = jsonObject.optString(JsonFormConstants.RIGHT_PADDING, "5dp");
1✔
123
        }
124

125
        int bgColorInt = 0;
1✔
126
        if (hasBg) {
1✔
127
            bgColorInt = Color.parseColor(bgColor);
1✔
128
        }
129

130
        CustomTextView labelText = constraintLayout.findViewById(R.id.label_text);
1✔
131
        if (bgColorInt != 0) {
1✔
132
            labelText.setBackgroundColor(bgColorInt);
1✔
133
            if (labelNumber != null && numberText != null) {
1✔
134
                numberText.setBackgroundColor(bgColorInt);
×
135
            }
136
        }
137

138
        if (hasBg) {
1✔
139
            labelText.setPadding(
1✔
140
                    getValueFromSpOrDpOrPx(context, leftPadding),
1✔
141
                    getValueFromSpOrDpOrPx(context, topPadding),
1✔
142
                    getValueFromSpOrDpOrPx(context, rightPadding),
1✔
143
                    getValueFromSpOrDpOrPx(context, bottomPadding)
1✔
144
            );
145
        }
146
        int labelTextSize = getValueFromSpOrDpOrPx(context, jsonObject.optString(JsonFormConstants.TEXT_SIZE, String.valueOf(context.getResources().getDimension(R
1✔
147
                .dimen.default_label_text_size))));
148
        String textStyle = jsonObject.optString(JsonFormConstants.TEXT_STYLE, JsonFormConstants.NORMAL);
1✔
149
        FormUtils.setTextStyle(textStyle, labelText);
1✔
150
        labelText.setTag(R.id.key, jsonObject.getString(JsonFormConstants.KEY));
1✔
151
        labelText.setTextSize(labelTextSize);
1✔
152
        labelText.setEnabled(!jsonObject
1✔
153
                .optBoolean(JsonFormConstants.READ_ONLY, false));//Gotcha: Should be set before createLabelText is used
1✔
154
        labelText.setHintOnText(hintOnText);//Gotcha: Should be set before createLabelText is used
1✔
155
        labelText.setText(createLabelText(jsonObject));
1✔
156

157
        createNumberLabel(constraintLayout, labelNumber, jsonObject, labelTextSize, textStyle, context);
1✔
158
    }
1✔
159

160
    private void createNumberLabel(ConstraintLayout constraintLayout, String labelNumber, JSONObject jsonObject,
161
                                   int labelTextSize,
162
                                   String textStyle, Context context) {
163
        if (!TextUtils.isEmpty(labelNumber)) {
1✔
164
            boolean hasBg = jsonObject.optBoolean(JsonFormConstants.HAS_BG, false);
1✔
165

166
            String topPadding = null;
1✔
167
            String bottomPadding = null;
1✔
168
            String leftPadding = null;
1✔
169
            String rightPadding = null;
1✔
170
            if (hasBg) {
1✔
171
                topPadding = jsonObject.optString(JsonFormConstants.TOP_PADDING, "5dp");
1✔
172
                bottomPadding = jsonObject.optString(JsonFormConstants.BOTTOM_PADDING, "5dp");
1✔
173
                leftPadding = jsonObject.optString(JsonFormConstants.LEFT_PADDING, "5dp");
1✔
174
                rightPadding = jsonObject.optString(JsonFormConstants.RIGHT_PADDING, "5dp");
1✔
175
            }
176

177

178
            numberText = constraintLayout.findViewById(R.id.label_text_number);
1✔
179
            numberText.setVisibility(View.VISIBLE);
1✔
180
            Boolean readOnly = jsonObject.optBoolean(JsonFormConstants.READ_ONLY);
1✔
181
            String labelTextColor = readOnly ? "#737373" : jsonObject.optString(JsonFormConstants.TEXT_COLOR, null);
1✔
182
            FormUtils.setTextStyle(textStyle, numberText);
1✔
183
            numberText.setTextSize(labelTextSize);
1✔
184
            numberText.setEnabled(!jsonObject
1✔
185
                    .optBoolean(JsonFormConstants.READ_ONLY, false));//Gotcha: Should be set before createLabelText is used
1✔
186
            numberText.setText(labelNumber + ". ");
1✔
187
            if (labelTextColor != null) {
1✔
188
                numberText.setTextColor(Color.parseColor(labelTextColor));
1✔
189
            }
190
            numberText.setPadding(
1✔
191
                    getValueFromSpOrDpOrPx(context, leftPadding),
1✔
192
                    getValueFromSpOrDpOrPx(context, topPadding),
1✔
193
                    getValueFromSpOrDpOrPx(context, rightPadding),
1✔
194
                    getValueFromSpOrDpOrPx(context, bottomPadding));
1✔
195
        }
196
    }
1✔
197

198
    /**
199
     * Generates the spanned text to be passed to the label Custom TextView
200
     *
201
     * @param jsonObject
202
     * @return
203
     * @throws JSONException
204
     */
205
    private Spanned createLabelText(JSONObject jsonObject) throws JSONException {
206
        String text = jsonObject.getString(JsonFormConstants.TEXT);
1✔
207
        Boolean readOnly = jsonObject.optBoolean(JsonFormConstants.READ_ONLY);
1✔
208
        String asterisks = getAsterisk(jsonObject);
1✔
209
        String labelTextColor = readOnly ? "#737373" : jsonObject.optString(JsonFormConstants.TEXT_COLOR, null);
1✔
210
        String combinedLabelText = getCombinedLabel(text, asterisks, labelTextColor);
1✔
211

212
        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.N ? Html
1✔
213
                .fromHtml(combinedLabelText, Html.FROM_HTML_MODE_LEGACY) : Html
1✔
214
                .fromHtml(combinedLabelText);
×
215
    }
216

217
    private String getAsterisk(JSONObject jsonObject) throws JSONException {
218
        JSONObject requiredObject = jsonObject.optJSONObject(JsonFormConstants.V_REQUIRED);
1✔
219
        String asterisks = "";
1✔
220
        if (requiredObject != null) {
1✔
221
            boolean requiredValue = requiredObject.getBoolean(JsonFormConstants.VALUE);
1✔
222
            if (Boolean.TRUE.equals(requiredValue)) {
1✔
223
                asterisks = "<font color=" + "#CF0800" + "> *</font>";
1✔
224
            }
225
        }
226
        return asterisks;
1✔
227
    }
228

229
    private String getCombinedLabel(String text, String asterisks, String labelTextColor) {
230
        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN ? "<font color=" + labelTextColor + ">" + Html
1✔
231
                .escapeHtml(text) + "</font>" + asterisks : "<font color=" + labelTextColor + ">" + TextUtils
1✔
232
                .htmlEncode(text) + "</font>" +
×
233
                asterisks;
234
    }
235

236
    @Override
237
    public Set<String> getCustomTranslatableWidgetFields() {
238
        return new HashSet<>();
1✔
239
    }
240
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc