• 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

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

3
import android.content.Context;
4
import android.content.Intent;
5
import android.content.res.Resources;
6
import android.graphics.Bitmap;
7
import android.os.Build;
8
import androidx.annotation.VisibleForTesting;
9
import android.text.TextUtils;
10
import android.util.TypedValue;
11
import android.view.View;
12
import android.view.ViewGroup;
13
import android.widget.Button;
14
import android.widget.ImageView;
15
import android.widget.LinearLayout;
16

17
import com.rey.material.util.ViewUtil;
18
import com.vijay.jsonwizard.R;
19
import com.vijay.jsonwizard.constants.JsonFormConstants;
20
import com.vijay.jsonwizard.fragments.JsonFormFragment;
21
import com.vijay.jsonwizard.interfaces.CommonListener;
22
import com.vijay.jsonwizard.interfaces.FormWidgetFactory;
23
import com.vijay.jsonwizard.interfaces.JsonApi;
24
import com.vijay.jsonwizard.interfaces.OnActivityResultListener;
25
import com.vijay.jsonwizard.utils.FormUtils;
26
import com.vijay.jsonwizard.utils.ImageUtils;
27
import com.vijay.jsonwizard.utils.ValidationStatus;
28
import com.vijay.jsonwizard.views.JsonFormFragmentView;
29

30
import org.json.JSONArray;
31
import org.json.JSONException;
32
import org.json.JSONObject;
33
import org.smartregister.simprint.SimPrintsConstantHelper;
34
import org.smartregister.simprint.SimPrintsRegistration;
35

36
import java.util.ArrayList;
37
import java.util.HashSet;
38
import java.util.List;
39
import java.util.Set;
40

41
import timber.log.Timber;
42

43
import static android.app.Activity.RESULT_OK;
44

45
/**
46
 * Created by vijay on 24-05-2015.
47
 */
48
public class FingerPrintFactory implements FormWidgetFactory {
1✔
49

50
    public static int dp2px(Context context, float dp) {
51
        Resources r = context.getResources();
1✔
52
        float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, r.getDisplayMetrics());
1✔
53
        return Math.round(px);
1✔
54
    }
55

56
    public static ValidationStatus validate(JsonFormFragmentView formFragmentView,
57
                                            ImageView imageView) {
58
        if (!(imageView.getTag(R.id.v_required) instanceof String) || !(imageView.getTag(R.id.error) instanceof String)) {
×
59
            return new ValidationStatus(true, null, formFragmentView, imageView);
×
60
        }
61
        Boolean isRequired = Boolean.valueOf((String) imageView.getTag(R.id.v_required));
×
62
        if (!isRequired || !imageView.isEnabled()) {
×
63
            return new ValidationStatus(true, null, formFragmentView, imageView);
×
64
        }
65
        Object path = imageView.getTag(R.id.imagePath);
×
66
        if (path instanceof String && !TextUtils.isEmpty((String) path)) {
×
67
            return new ValidationStatus(true, null, formFragmentView, imageView);
×
68
        }
69
        return new ValidationStatus(false, (String) imageView.getTag(R.id.error), formFragmentView, imageView);
×
70
    }
71

72
    @Override
73
    public List<View> getViewsFromJson(String stepName, Context context, JsonFormFragment formFragment, JSONObject jsonObject, CommonListener listener, boolean popup) throws Exception {
74
        return attachJson(stepName, context, jsonObject, listener, popup);
1✔
75
    }
76

77
    @Override
78
    public List<View> getViewsFromJson(String stepName, Context context, JsonFormFragment formFragment, JSONObject jsonObject, CommonListener listener) throws Exception {
79
        return attachJson(stepName, context, jsonObject, listener, false);
×
80
    }
81

82
    private void addFingerprintResultsListener(final Context context, final ImageView imageView) {
83
        if (context instanceof JsonApi) {
1✔
84
            JsonApi jsonApi = (JsonApi) context;
1✔
85
            jsonApi.addOnActivityResultListener(JsonFormConstants.ACTIVITY_REQUEST_CODE.REQUEST_CODE_REGISTER,
1✔
86
                    (requestCode, resultCode, data) -> {
87
                        if (requestCode == JsonFormConstants.ACTIVITY_REQUEST_CODE.REQUEST_CODE_REGISTER && resultCode == RESULT_OK) {
×
88
                            if (data != null) {
×
89

90
                                SimPrintsRegistration registration = (SimPrintsRegistration) data.getSerializableExtra(SimPrintsConstantHelper.INTENT_DATA);
×
91
                                imageView.setTag(R.id.simprints_guid, registration.getGuid());
×
92
                                setFingerprintDrawable(context, imageView, registration.getGuid(), true);
×
93
                                Timber.d("Scanned Fingerprint GUID %s ", registration.getGuid());
×
94
                            } else {
×
95
                                Timber.i("NO RESULT FOR FINGERPRINT");
×
96
                                setFingerprintDrawable(context, imageView, "", true);
×
97
                            }
98
                        }
99
                    });
×
100
        }
101
    }
1✔
102

103
    private void setFingerprintDrawable(final Context context, final ImageView imageView,
104
                                        String fingerprintValue, boolean isFromScan) {
105

106

107
        if (isFromScan && TextUtils.isEmpty(fingerprintValue)) {
1✔
108
            //From scanning fingerprint and no result has not been received
109
            imageView.setImageDrawable(context.getResources().getDrawable(R.drawable.finger_print_failed));
×
110
        } else if (isFromScan && !TextUtils.isEmpty(fingerprintValue)
1✔
111
                || (!isFromScan && !TextUtils.isEmpty(fingerprintValue))) {
1✔
112
            imageView.setImageDrawable(context.getResources().getDrawable(R.drawable.finger_print_done));
×
113
        } else {
114
            imageView.setImageDrawable(context.getResources().getDrawable(R.drawable.finger_print));
1✔
115
        }
116

117
    }
1✔
118

119
    private List<View> attachJson(String stepName, Context context, JSONObject jsonObject, CommonListener listener, boolean popup) throws JSONException {
120
        String relevance = jsonObject.optString(JsonFormConstants.RELEVANCE);
1✔
121
        String constraints = jsonObject.optString(JsonFormConstants.CONSTRAINTS);
1✔
122
        String calculation = jsonObject.optString(JsonFormConstants.CALCULATION);
1✔
123
        JSONArray canvasIds = new JSONArray();
1✔
124

125
        List<View> views = new ArrayList<>(1);
1✔
126
        createImageView(context, canvasIds, jsonObject, popup, stepName, listener, views);
1✔
127
        Button uploadButton = new Button(context);
1✔
128
        uploadButton.setText(jsonObject.getString(JsonFormConstants.UPLOAD_BUTTON_TEXT));
1✔
129
        uploadButton.setBackgroundColor(context.getResources().getColor(R.color.primary));
1✔
130
        uploadButton.setMinHeight(0);
1✔
131
        uploadButton.setMinimumHeight(0);
1✔
132
        uploadButton.setTextColor(context.getResources().getColor(android.R.color.white));
1✔
133
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
1✔
134
            uploadButton.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
1✔
135
        }
136
        uploadButton.setTextSize(TypedValue.COMPLEX_UNIT_PX,
1✔
137
                context.getResources().getDimension(R.dimen.button_text_size));
1✔
138
        uploadButton.setPadding(
1✔
139
                context.getResources().getDimensionPixelSize(R.dimen.button_padding),
1✔
140
                context.getResources().getDimensionPixelSize(R.dimen.button_padding),
1✔
141
                context.getResources().getDimensionPixelSize(R.dimen.button_padding),
1✔
142
                context.getResources().getDimensionPixelSize(R.dimen.button_padding));
1✔
143
        uploadButton.setLayoutParams(FormUtils.getRelativeLayoutParams(FormUtils.MATCH_PARENT, FormUtils.WRAP_CONTENT, 0, 0, 0, (int) context
1✔
144
                .getResources().getDimension(R.dimen.default_bottom_margin)));
1✔
145
        uploadButton.setOnClickListener(listener);
1✔
146
        setViewTags(jsonObject, stepName, uploadButton, popup);
1✔
147
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
1✔
148
                ViewGroup.LayoutParams.MATCH_PARENT,
149
                ViewGroup.LayoutParams.WRAP_CONTENT);
150
        params.setMargins(0, 0, 0, dp2px(context, 20));
1✔
151
        uploadButton.setLayoutParams(params);
1✔
152

153
        uploadButton.setId(ViewUtil.generateViewId());
1✔
154
        canvasIds.put(uploadButton.getId());
1✔
155
        uploadButton.setTag(R.id.canvas_ids, canvasIds.toString());
1✔
156

157
        views.add(uploadButton);
1✔
158

159
        if (jsonObject.has(JsonFormConstants.READ_ONLY)) {
1✔
160
            boolean readOnly = jsonObject.getBoolean(JsonFormConstants.READ_ONLY);
×
161
            uploadButton.setEnabled(!readOnly);
×
162
            uploadButton.setFocusable(!readOnly);
×
163
        }
164

165
        setRefreshLOgic(context, relevance, constraints, calculation, uploadButton);
1✔
166

167
        return views;
1✔
168
    }
169

170
    @VisibleForTesting
171
    protected void setViewTags(JSONObject jsonObject, String stepName, View view, boolean popup) throws JSONException {
172
        String openMrsEntityParent = jsonObject.getString(JsonFormConstants.OPENMRS_ENTITY_PARENT);
1✔
173
        String openMrsEntity = jsonObject.getString(JsonFormConstants.OPENMRS_ENTITY);
1✔
174
        String openMrsEntityId = jsonObject.getString(JsonFormConstants.OPENMRS_ENTITY_ID);
1✔
175
        view.setTag(R.id.key, jsonObject.getString(JsonFormConstants.KEY));
1✔
176
        view.setTag(R.id.openmrs_entity_parent, openMrsEntityParent);
1✔
177
        view.setTag(R.id.openmrs_entity, openMrsEntity);
1✔
178
        view.setTag(R.id.openmrs_entity_id, openMrsEntityId);
1✔
179
        view.setTag(R.id.extraPopup, popup);
1✔
180
        view.setTag(R.id.type, jsonObject.getString(JsonFormConstants.TYPE));
1✔
181
        view.setTag(R.id.project_id, jsonObject.optString(JsonFormConstants.SIMPRINTS_PROJECT_ID));
1✔
182
        view.setTag(R.id.user_id, jsonObject.optString(JsonFormConstants.SIMPRINTS_USER_ID));
1✔
183
        view.setTag(R.id.module_id, jsonObject.optString(JsonFormConstants.SIMPRINTS_MODULE_ID));
1✔
184
        view.setTag(R.id.guid, jsonObject.optString(JsonFormConstants.SIMPRINTS_MODULE_ID));
1✔
185
        view.setTag(R.id.finger_print_option, jsonObject.optString(JsonFormConstants.SIMPRINTS_OPTION));
1✔
186
        view.setTag(com.vijay.jsonwizard.R.id.address, stepName + ":" + jsonObject.getString(JsonFormConstants.KEY));
1✔
187
    }
1✔
188

189
    private void createImageView(Context context, JSONArray canvasIds, JSONObject jsonObject, boolean popup, String stepName, CommonListener listener, List<View> views) throws JSONException {
190
        String relevance = jsonObject.optString(JsonFormConstants.RELEVANCE);
1✔
191
        String constraints = jsonObject.optString(JsonFormConstants.CONSTRAINTS);
1✔
192
        String calculation = jsonObject.optString(JsonFormConstants.CALCULATION);
1✔
193
        ImageView imageView = new ImageView(context);
1✔
194
        imageView.setId(ViewUtil.generateViewId());
1✔
195
        canvasIds.put(imageView.getId());
1✔
196
        imageView.setImageDrawable(context.getResources().getDrawable(R.drawable.finger_print));
1✔
197
        setViewTags(jsonObject, stepName, imageView, popup);
1✔
198

199
        imageView.setTag(R.id.address, stepName + ":" + jsonObject.getString(JsonFormConstants.KEY));
1✔
200
        setRefreshLOgic(context, relevance, constraints, calculation, imageView);
1✔
201

202
        JSONObject requiredObject = jsonObject.optJSONObject(JsonFormConstants.V_REQUIRED);
1✔
203
        if (requiredObject != null) {
1✔
204
            String requiredValue = requiredObject.getString(JsonFormConstants.VALUE);
×
205
            if (!TextUtils.isEmpty(requiredValue)) {
×
206
                imageView.setTag(R.id.v_required, requiredValue);
×
207
                imageView.setTag(R.id.error, requiredObject.optString(JsonFormConstants.ERR));
×
208
            }
209
        }
210

211
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
1✔
212
        int imageHeight = FormUtils.dpToPixels(context, context.getResources().getBoolean(R.bool.isTablet) ? 50 : 50);
1✔
213
        imageView.setLayoutParams(FormUtils.getLinearLayoutParams(FormUtils.MATCH_PARENT, imageHeight, 0, 0, 0, (int) context
1✔
214
                .getResources().getDimension(R.dimen.default_bottom_margin)));
1✔
215

216
        String imagePath = jsonObject.optString(JsonFormConstants.VALUE);
1✔
217
        if (!TextUtils.isEmpty(imagePath)) {
1✔
218
            imageView.setTag(R.id.imagePath, imagePath);
×
219
            Bitmap bitmap = ImageUtils.loadBitmapFromFile(context, imagePath, ImageUtils.getDeviceWidth(context), FormUtils.dpToPixels(context, 200));
×
220
            if (bitmap != null)
×
221
                imageView.setImageBitmap(bitmap);
×
222
        }
223
        setFingerprintDrawable(context, imageView, imagePath, false);
1✔
224

225
        ((JsonApi) context).addFormDataView(imageView);
1✔
226
        imageView.setOnClickListener(listener);
1✔
227
        addFingerprintResultsListener(context, imageView);
1✔
228
        views.add(imageView);
1✔
229
    }
1✔
230

231
    private void setRefreshLOgic(Context context, String relevance, String constraints, String calculation, View view) {
232
        if (!TextUtils.isEmpty(relevance) && context instanceof JsonApi) {
1✔
233
            view.setTag(R.id.relevance, relevance);
1✔
234
            ((JsonApi) context).addSkipLogicView(view);
1✔
235
        }
236

237
        if (!TextUtils.isEmpty(constraints) && context instanceof JsonApi) {
1✔
238
            view.setTag(com.vijay.jsonwizard.R.id.constraints, constraints);
×
239
            ((JsonApi) context).addConstrainedView(view);
×
240
        }
241

242
        if (!TextUtils.isEmpty(calculation) && context instanceof JsonApi) {
1✔
243
            view.setTag(com.vijay.jsonwizard.R.id.calculation, calculation);
×
244
            ((JsonApi) context).addCalculationLogicView(view);
×
245
        }
246
    }
1✔
247

248
    @Override
249
    public Set<String> getCustomTranslatableWidgetFields() {
250
        return new HashSet<>();
1✔
251
    }
252
}
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