diff --git a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/customviews/GenericPopupDialog.java b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/customviews/GenericPopupDialog.java index 750046a3d..f09dd21a3 100644 --- a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/customviews/GenericPopupDialog.java +++ b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/customviews/GenericPopupDialog.java @@ -1,12 +1,13 @@ package com.vijay.jsonwizard.customviews; +import static android.view.inputmethod.InputMethodManager.HIDE_NOT_ALWAYS; + import android.app.Activity; import android.app.DialogFragment; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.os.Bundle; -import androidx.annotation.Nullable; import android.text.TextUtils; import android.view.LayoutInflater; import android.view.View; @@ -16,6 +17,8 @@ import android.widget.Button; import android.widget.LinearLayout; +import androidx.annotation.Nullable; + import com.vijay.jsonwizard.NativeFormLibrary; import com.vijay.jsonwizard.R; import com.vijay.jsonwizard.constants.JsonFormConstants; @@ -43,8 +46,6 @@ import timber.log.Timber; -import static android.view.inputmethod.InputMethodManager.HIDE_NOT_ALWAYS; - public class GenericPopupDialog extends DialogFragment implements GenericDialogInterface { private ViewGroup dialogView; private JsonFormInteractor jsonFormInteractor = JsonFormInteractor.getInstance(); @@ -89,51 +90,50 @@ public void onActivityCreated(Bundle savedInstanceState) { "The Context is not set. Did you forget to set context with Generic Dialog setContext method?"); } - getJsonApi().getAppExecutors().diskIO().execute(new Runnable() { - @Override - public void run() { + getJsonApi().getAppExecutors().diskIO().execute(() -> { + getJsonApi().getAppExecutors().mainThread().execute(() -> { + }); - if (isDetached()) { - return; - } - // support translation of sub-forms - // support translation of sub-forms - Intent activityIntent = getActivity().getIntent(); - translateSubForm = activityIntent != null && activityIntent.hasExtra(JsonFormConstants.PERFORM_FORM_TRANSLATION) ? - activityIntent.getBooleanExtra(JsonFormConstants.PERFORM_FORM_TRANSLATION, false) : - NativeFormLibrary.getInstance().isPerformFormTranslation(); + if (isDetached()) { + return; + } + // support translation of sub-forms + // support translation of sub-forms + Intent activityIntent = getActivity().getIntent(); + translateSubForm = activityIntent != null && activityIntent.hasExtra(JsonFormConstants.PERFORM_FORM_TRANSLATION) ? + activityIntent.getBooleanExtra(JsonFormConstants.PERFORM_FORM_TRANSLATION, false) : + NativeFormLibrary.getInstance().isPerformFormTranslation(); - try { - setMainFormFields(formUtils.getFormFields(getStepName(), context)); - loadPartialSecondaryValues(); - createSecondaryValuesMap(); + try { + setMainFormFields(formUtils.getFormFields(getStepName(), context)); + loadPartialSecondaryValues(); + createSecondaryValuesMap(); - loadSubForms(); + loadSubForms(); - getJsonApi().updateGenericPopupSecondaryValues(specifyContent, stepName); - } catch (JSONException e) { - Timber.e(e, " --> onCreate"); - } + getJsonApi().updateGenericPopupSecondaryValues(specifyContent, stepName); + } catch (JSONException e) { + Timber.e(e, " --> onCreate"); + } - final List views = initiateViews(); - if (isDetached()) { - return; - } - getJsonApi().initializeDependencyMaps(); - getJsonApi().getAppExecutors().mainThread().execute(new Runnable() { - @Override - public void run() { - if (isDetached()) { - return; - } - setViewList(views); - getJsonApi().invokeRefreshLogic(null, true, null, null, getStepName(), false); - if (getDialogView() != null) { - addWidgetViews(getDialogView()); - } - } - }); + final List views = initiateViews(); + if (isDetached()) { + return; } + getJsonApi().initializeDependencyMaps(); + getJsonApi().getAppExecutors().mainThread().execute(new Runnable() { + @Override + public void run() { + if (isDetached()) { + return; + } + setViewList(views); + getJsonApi().invokeRefreshLogic(null, true, null, null, getStepName(), false); + if (getDialogView() != null) { + addWidgetViews(getDialogView()); + } + } + }); }); } diff --git a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/fragments/JsonWizardFormFragment.java b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/fragments/JsonWizardFormFragment.java index eedc12321..474b7c72e 100644 --- a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/fragments/JsonWizardFormFragment.java +++ b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/fragments/JsonWizardFormFragment.java @@ -3,8 +3,6 @@ import android.content.res.Resources; import android.graphics.drawable.ColorDrawable; import android.os.Bundle; -import androidx.annotation.Nullable; -import androidx.appcompat.widget.Toolbar; import android.text.TextUtils; import android.view.LayoutInflater; import android.view.Menu; @@ -17,6 +15,9 @@ import android.widget.TextView; import android.widget.Toast; +import androidx.annotation.Nullable; +import androidx.appcompat.widget.Toolbar; + import com.vijay.jsonwizard.R; import com.vijay.jsonwizard.activities.JsonFormActivity; import com.vijay.jsonwizard.constants.JsonFormConstants; @@ -286,11 +287,11 @@ public void onClick(View view) { getJsonApi().setPreviousPressed(false); Object nextStateTag = view.getTag(R.id.NEXT_STATE); if (nextStateTag == null) { - new NextProgressDialogTask(getJsonFormFragment()).execute(); + new NextProgressDialogTask(getJsonFormFragment()).init(); } else { boolean next = (boolean) nextStateTag; if (next) { - new NextProgressDialogTask(getJsonFormFragment()).execute(); + new NextProgressDialogTask(getJsonFormFragment()).init(); } else { save(); } diff --git a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/listeners/ExpansionPanelRecordButtonClickListener.java b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/listeners/ExpansionPanelRecordButtonClickListener.java index 6cab6cff7..7ebbf51af 100644 --- a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/listeners/ExpansionPanelRecordButtonClickListener.java +++ b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/listeners/ExpansionPanelRecordButtonClickListener.java @@ -43,7 +43,7 @@ public void onClick(View view) { } protected void initiateTask(View view) { - new ExpansionPanelGenericPopupDialogTask(view).execute(); + new ExpansionPanelGenericPopupDialogTask(view).init(); } /** diff --git a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/presenters/JsonFormFragmentPresenter.java b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/presenters/JsonFormFragmentPresenter.java index 30be9c1bc..2d2d569e0 100644 --- a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/presenters/JsonFormFragmentPresenter.java +++ b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/presenters/JsonFormFragmentPresenter.java @@ -848,7 +848,7 @@ protected void nativeRadioButtonClickActions(View view) { .equals(JsonFormConstants.DATE_PICKER)) { NativeRadioButtonFactory.showDateDialog(view); } else if (JsonFormConstants.CONTENT_INFO.equals(type) && !specifyWidget.equals(JsonFormConstants.DATE_PICKER)) { - new ExpansionPanelGenericPopupDialogTask(view).execute(); + new ExpansionPanelGenericPopupDialogTask(view).init(); } else if (view.getId() == R.id.label_edit_button) { setRadioViewsEditable(view); } else { diff --git a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/task/AttachRepeatingGroupTask.java b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/task/AttachRepeatingGroupTask.java index 3b8abeb47..9b1210d64 100644 --- a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/task/AttachRepeatingGroupTask.java +++ b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/task/AttachRepeatingGroupTask.java @@ -1,8 +1,19 @@ package com.vijay.jsonwizard.task; +import static com.vijay.jsonwizard.constants.JsonFormConstants.CALCULATION; +import static com.vijay.jsonwizard.constants.JsonFormConstants.FIELDS; +import static com.vijay.jsonwizard.constants.JsonFormConstants.KEY; +import static com.vijay.jsonwizard.constants.JsonFormConstants.RELEVANCE; +import static com.vijay.jsonwizard.constants.JsonFormConstants.STEPNAME; +import static com.vijay.jsonwizard.constants.JsonFormConstants.STEP_TITLE; +import static com.vijay.jsonwizard.constants.JsonFormConstants.TYPE; +import static com.vijay.jsonwizard.constants.JsonFormConstants.VALUE; +import static com.vijay.jsonwizard.constants.JsonFormConstants.V_RELATIVE_MAX; +import static com.vijay.jsonwizard.utils.Utils.hideProgressDialog; +import static com.vijay.jsonwizard.utils.Utils.showProgressDialog; + import android.content.Context; import android.graphics.Typeface; -import android.os.AsyncTask; import android.text.SpannableString; import android.text.style.StyleSpan; import android.view.View; @@ -20,6 +31,7 @@ import com.vijay.jsonwizard.fragments.JsonFormFragment; import com.vijay.jsonwizard.interfaces.FormWidgetFactory; import com.vijay.jsonwizard.interfaces.JsonApi; +import com.vijay.jsonwizard.utils.AppExecutors; import com.vijay.jsonwizard.utils.Utils; import com.vijay.jsonwizard.utils.ValidationStatus; @@ -38,19 +50,7 @@ import timber.log.Timber; -import static com.vijay.jsonwizard.constants.JsonFormConstants.CALCULATION; -import static com.vijay.jsonwizard.constants.JsonFormConstants.FIELDS; -import static com.vijay.jsonwizard.constants.JsonFormConstants.KEY; -import static com.vijay.jsonwizard.constants.JsonFormConstants.RELEVANCE; -import static com.vijay.jsonwizard.constants.JsonFormConstants.STEPNAME; -import static com.vijay.jsonwizard.constants.JsonFormConstants.STEP_TITLE; -import static com.vijay.jsonwizard.constants.JsonFormConstants.TYPE; -import static com.vijay.jsonwizard.constants.JsonFormConstants.VALUE; -import static com.vijay.jsonwizard.constants.JsonFormConstants.V_RELATIVE_MAX; -import static com.vijay.jsonwizard.utils.Utils.hideProgressDialog; -import static com.vijay.jsonwizard.utils.Utils.showProgressDialog; - -public class AttachRepeatingGroupTask extends AsyncTask> { +public class AttachRepeatingGroupTask { protected final int REPEATING_GROUP_LABEL_TEXT_COLOR = R.color.black; private final ViewParent parent; @@ -64,6 +64,7 @@ public class AttachRepeatingGroupTask extends AsyncTask> private Map>> rulesFileMap = new HashMap<>(); private Map repeatingGroupLayouts; private int currNumRepeatingGroups; + private final AppExecutors appExecutors; public AttachRepeatingGroupTask(final ViewParent parent, int numRepeatingGroups, Map repeatingGroupLayouts, WidgetArgs widgetArgs, ImageButton doneButton) { this.rootLayout = (LinearLayout) parent; @@ -73,28 +74,34 @@ public AttachRepeatingGroupTask(final ViewParent parent, int numRepeatingGroups, this.doneButton = doneButton; this.repeatingGroupLayouts = repeatingGroupLayouts; currNumRepeatingGroups = ((ViewGroup) parent).getChildCount() - 1; + appExecutors = new AppExecutors(); + } + + public void init() { + appExecutors.mainThread().execute(this::onPreExecute); + appExecutors.diskIO().execute(() -> { + this.updateRepeatingGrpInBackground(); + appExecutors.mainThread().execute(this::onPostExecute); + }); + onCancelled(); + } - @Override + protected void onPreExecute() { showProgressDialog(R.string.please_wait_title, R.string.creating_repeating_group_message, widgetArgs.getFormFragment().getContext()); } - @Override - protected List doInBackground(Void... voids) { + protected List updateRepeatingGrpInBackground() { diff = numRepeatingGroups - currNumRepeatingGroups; for (int i = 0; i < diff; i++) { try { - repeatingGroups.add(buildRepeatingGroupLayout(parent)); - } catch (Exception e) { Timber.e(e); } } - updateRepeatingGrpCountObject(); - return repeatingGroups; } @@ -109,11 +116,9 @@ private void updateRepeatingGrpCountObject() { } } - @Override - protected void onPostExecute(List result) { + protected void onPostExecute() { if (diff < 0) { try { - JSONObject step = ((JsonApi) widgetArgs.getContext()).getmJSONObject().getJSONObject(widgetArgs.getStepName()); JSONArray fields = step.getJSONArray(FIELDS); int currNumRepeatingGroups = rootLayout.getChildCount() - 1; @@ -165,18 +170,15 @@ protected void onPostExecute(List result) { hideProgressDialog(); doneButton.setImageResource(R.drawable.ic_done_green); - //for validation validationCleanUp(); } private void validationCleanUp() { doneButton.setTag(R.id.is_repeating_group_generated, true); - LinearLayout referenceLayout = (LinearLayout) ((LinearLayout) parent).getChildAt(0); MaterialEditText materialEditText = (MaterialEditText) referenceLayout.getChildAt(0); materialEditText.setError(null); - Map validationStatusMap = widgetArgs.getFormFragment().getJsonApi().getInvalidFields(); if (validationStatusMap != null) { String fieldKey = Utils.getFieldKeyPrefix(widgetArgs.getStepName(), widgetArgs.getFormFragment().getPresenter().getStepTitle()) + JsonFormConstants.REFERENCE_EDIT_TEXT; @@ -186,20 +188,16 @@ private void validationCleanUp() { private LinearLayout buildRepeatingGroupLayout(final ViewParent parent) throws Exception { Context context = widgetArgs.getContext(); - LinearLayout repeatingGroup = new LinearLayout(context); repeatingGroup.setLayoutParams(WIDTH_MATCH_PARENT_HEIGHT_WRAP_CONTENT); repeatingGroup.setOrientation(LinearLayout.VERTICAL); - LinearLayout rootLayout = (LinearLayout) ((LinearLayout) parent).getChildAt(0); EditText referenceEditText = (EditText) rootLayout.getChildAt(0); TextView repeatingGroupLabel = new TextView(context); if (widgetArgs.getJsonObject().optBoolean(JsonFormConstants.RepeatingGroupFactory.SHOW_GROUP_LABEL, true)) { formatRepeatingGroupLabelText(referenceEditText, repeatingGroupLabel, context); } - repeatingGroup.addView(repeatingGroupLabel); - JSONArray repeatingGroupJson = new JSONArray(repeatingGroupLayouts.get(((LinearLayout) parent).getId())); String groupUniqueId = UUID.randomUUID().toString().replace("-", ""); for (int i = 0; i < repeatingGroupJson.length(); i++) { @@ -255,7 +253,6 @@ protected void addUniqueIdentifiers(JSONObject element, String uniqueId) throws } } - @Override protected void onCancelled() { hideProgressDialog(); } diff --git a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/task/ExpansionPanelGenericPopupDialogTask.java b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/task/ExpansionPanelGenericPopupDialogTask.java index 9c833afe3..ecca0b92b 100644 --- a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/task/ExpansionPanelGenericPopupDialogTask.java +++ b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/task/ExpansionPanelGenericPopupDialogTask.java @@ -13,6 +13,7 @@ import com.vijay.jsonwizard.customviews.ExpansionPanelGenericPopupDialog; import com.vijay.jsonwizard.fragments.JsonFormFragment; import com.vijay.jsonwizard.interfaces.CommonListener; +import com.vijay.jsonwizard.utils.AppExecutors; import com.vijay.jsonwizard.utils.FormUtils; import com.vijay.jsonwizard.utils.Utils; import com.vijay.jsonwizard.views.CustomTextView; @@ -24,7 +25,7 @@ /** * The {@link AsyncTask} to start and set the required variables on the {@link ExpansionPanelGenericPopupDialog} */ -public class ExpansionPanelGenericPopupDialogTask extends AsyncTask { +public class ExpansionPanelGenericPopupDialogTask { private FormUtils formUtils = new FormUtils(); private Utils utils = new Utils(); private View view; @@ -32,17 +33,20 @@ public class ExpansionPanelGenericPopupDialogTask extends AsyncTask { + progressBar = new ProgressDialog(multiSelectListFactory.getContext()); + progressBar.setMessage(multiSelectListFactory.getContext().getString(R.string.loading_multi_select_list)); + progressBar.show(); }); init(); } private void init() { - appExecutors.diskIO().execute(new Runnable() { - @Override - public void run() { - String source = jsonObject.optString(JsonFormConstants.MultiSelectUtils.SOURCE); - final List multiSelectItems = multiSelectListFactory.loadListItems(source); - if (multiSelectItems == null) { - return; - } - String strGroupingsArray = jsonObject.optString(JsonFormConstants.MultiSelectUtils.GROUPINGS); - boolean sort = jsonObject.optBoolean(JsonFormConstants.MultiSelectUtils.SORT); + appExecutors.diskIO().execute(() -> { + String source = jsonObject.optString(JsonFormConstants.MultiSelectUtils.SOURCE); + final List multiSelectItems = multiSelectListFactory.loadListItems(source); + if (multiSelectItems == null) { + return; + } + String strGroupingsArray = jsonObject.optString(JsonFormConstants.MultiSelectUtils.GROUPINGS); + boolean sort = jsonObject.optBoolean(JsonFormConstants.MultiSelectUtils.SORT); - if (!StringUtils.isBlank(strGroupingsArray) && sort) {//no grouping without sorting - JSONArray jsonArray = null; - try { - jsonArray = new JSONArray(strGroupingsArray); - } catch (JSONException e) { - Timber.e(e); - } - MultiSelectListUtils.addGroupings(multiSelectItems, jsonArray); + if (!StringUtils.isBlank(strGroupingsArray) && sort) {//no grouping without sorting + JSONArray jsonArray = null; + try { + jsonArray = new JSONArray(strGroupingsArray); + } catch (JSONException e) { + Timber.e(e); } - if (sort) { - String sortClass = jsonObject.optString(JsonFormConstants.MultiSelectUtils.SORTING_CLASS); - if (StringUtils.isBlank(sortClass)) { - sortClass = JsonFormConstants.MultiSelectUtils.ALPHABET_SORTING; - } - try { - Class aClass = Class.forName(sortClass); - Collections.sort(multiSelectItems, (Comparator) aClass.newInstance()); - } catch (IllegalAccessException e) { - Timber.e(e); - } catch (InstantiationException e) { - Timber.e(e); - } catch (ClassNotFoundException e) { - Timber.e(e); - } + MultiSelectListUtils.addGroupings(multiSelectItems, jsonArray); + } + if (sort) { + String sortClass = jsonObject.optString(JsonFormConstants.MultiSelectUtils.SORTING_CLASS); + if (StringUtils.isBlank(sortClass)) { + sortClass = JsonFormConstants.MultiSelectUtils.ALPHABET_SORTING; + } + try { + Class aClass = Class.forName(sortClass); + Collections.sort(multiSelectItems, (Comparator) aClass.newInstance()); + } catch (IllegalAccessException e) { + Timber.e(e); + } catch (InstantiationException e) { + Timber.e(e); + } catch (ClassNotFoundException e) { + Timber.e(e); } - - appExecutors.mainThread().execute(new Runnable() { - @Override - public void run() { - onPostExecute(multiSelectItems); - } - }); } + + appExecutors.mainThread().execute(() -> onPostExecute(multiSelectItems)); }); } diff --git a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/task/NextProgressDialogTask.java b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/task/NextProgressDialogTask.java index 76b4e18e7..477c5c212 100644 --- a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/task/NextProgressDialogTask.java +++ b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/task/NextProgressDialogTask.java @@ -2,14 +2,15 @@ import android.app.ProgressDialog; import android.content.Context; -import android.os.AsyncTask; import com.vijay.jsonwizard.fragments.JsonWizardFormFragment; +import com.vijay.jsonwizard.utils.AppExecutors; -public class NextProgressDialogTask extends AsyncTask { +public class NextProgressDialogTask { private JsonWizardFormFragment formFragment; private Context context; private ProgressDialog progressDialog; + private AppExecutors appExecutors; private void showDialog() { setProgressDialog(new ProgressDialog(getContext())); @@ -32,22 +33,13 @@ public NextProgressDialogTask(JsonWizardFormFragment jsonFormFragment) { } } - @Override - protected Void doInBackground(Void... voids) { - getFormFragment().next(); - return null; - } - - @Override - protected void onPreExecute() { - super.onPreExecute(); - showDialog(); - } - - @Override - protected void onPostExecute(Void aVoid) { - super.onPostExecute(aVoid); - hideDialog(); + public void init() { + appExecutors = getFormFragment().getJsonApi().getAppExecutors(); + appExecutors.mainThread().execute(this::showDialog); + appExecutors.diskIO().execute(() -> { + getFormFragment().next(); + appExecutors.mainThread().execute(this::hideDialog); + }); } public JsonWizardFormFragment getFormFragment() { diff --git a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/widgets/BarcodeFactory.java b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/widgets/BarcodeFactory.java index d297cc096..eeab9a1b4 100644 --- a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/widgets/BarcodeFactory.java +++ b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/widgets/BarcodeFactory.java @@ -72,17 +72,14 @@ public List getViewsFromJson(final String stepName, final Context context, List views = new ArrayList<>(1); final RelativeLayout rootLayout = getRootLayout(context); final int canvasId = ViewUtil.generateViewId(); - formFragment.getJsonApi().getAppExecutors().mainThread().execute(new Runnable() { - @Override - public void run() { - try { - rootLayout.setId(canvasId); - final MaterialEditText editText = createEditText(rootLayout, jsonObject, canvasId, stepName, popup); - attachJson(rootLayout, stepName, context, formFragment, jsonObject, editText); - ((JsonApi) context).addFormDataView(editText); - } catch (JSONException e) { - Timber.e(e); - } + formFragment.getJsonApi().getAppExecutors().mainThread().execute(() -> { + try { + rootLayout.setId(canvasId); + final MaterialEditText editText = createEditText(rootLayout, jsonObject, canvasId, stepName, popup); + attachJson(rootLayout, stepName, context, formFragment, jsonObject, editText); + ((JsonApi) context).addFormDataView(editText); + } catch (JSONException e) { + Timber.e(e); } }); views.add(rootLayout); diff --git a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/widgets/BasicRDTCaptureFactory.java b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/widgets/BasicRDTCaptureFactory.java index 7bf0a7c65..51865a422 100644 --- a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/widgets/BasicRDTCaptureFactory.java +++ b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/widgets/BasicRDTCaptureFactory.java @@ -1,37 +1,43 @@ package com.vijay.jsonwizard.widgets; +import static android.app.Activity.RESULT_CANCELED; +import static android.app.Activity.RESULT_OK; +import static com.vijay.jsonwizard.constants.JsonFormConstants.VALUE; +import static com.vijay.jsonwizard.utils.Utils.hideProgressDialog; +import static com.vijay.jsonwizard.utils.Utils.showProgressDialog; +import static edu.washington.cs.ubicomplab.rdt_reader.core.Constants.SAVED_IMAGE_FILE_PATH; + import android.Manifest; import android.app.Activity; import android.content.Intent; import android.content.pm.PackageManager; -import android.os.AsyncTask; -import androidx.core.content.ContextCompat; import android.util.Log; +import androidx.core.content.ContextCompat; + import com.vijay.jsonwizard.R; import com.vijay.jsonwizard.constants.JsonFormConstants; import com.vijay.jsonwizard.interfaces.JsonApi; +import com.vijay.jsonwizard.utils.AppExecutors; import org.json.JSONException; import edu.washington.cs.ubicomplab.rdt_reader.activity.RDTCaptureActivity; -import static android.app.Activity.RESULT_CANCELED; -import static android.app.Activity.RESULT_OK; -import static com.vijay.jsonwizard.constants.JsonFormConstants.VALUE; -import static com.vijay.jsonwizard.utils.Utils.hideProgressDialog; -import static com.vijay.jsonwizard.utils.Utils.showProgressDialog; -import static edu.washington.cs.ubicomplab.rdt_reader.core.Constants.SAVED_IMAGE_FILE_PATH; - /** * Created by Vincent Karuri on 17/06/2020 */ public class BasicRDTCaptureFactory extends RDTCaptureFactory { private static final String TAG = BasicRDTCaptureFactory.class.getName(); + private AppExecutors appExecutors; - private class LaunchRDTCameraTask extends AsyncTask { + protected void launchRDTCaptureActivity() { + if (isPermissionGiven()) { + new LaunchRDTCameraTask().init(); + } + } - @Override + private class LaunchRDTCameraTask { protected Void doInBackground(Void... voids) { Activity activity = (Activity) widgetArgs.getContext(); Intent intent = new Intent(activity, RDTCaptureActivity.class); @@ -39,15 +45,14 @@ protected Void doInBackground(Void... voids) { return null; } - @Override protected void onPreExecute() { showProgressDialog(R.string.please_wait_title, R.string.launching_rdt_capture_message, widgetArgs.getContext()); } - } - protected void launchRDTCaptureActivity() { - if (isPermissionGiven()) { - new LaunchRDTCameraTask().execute(); + public void init() { + appExecutors =new AppExecutors(); + appExecutors.mainThread().execute(this::onPreExecute); + appExecutors.diskIO().execute(this::doInBackground); } } diff --git a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/widgets/RepeatingGroupFactory.java b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/widgets/RepeatingGroupFactory.java index 98526f013..efc2bb693 100644 --- a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/widgets/RepeatingGroupFactory.java +++ b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/widgets/RepeatingGroupFactory.java @@ -383,7 +383,7 @@ private void attachRepeatingGroup(final ViewParent parent, final int numRepeatin } new AttachRepeatingGroupTask(parent, numRepeatingGroups, repeatingGroupLayouts, - widgetArgs, doneButton).execute(); + widgetArgs, doneButton).init(); } protected int getLayout() { diff --git a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/views/CustomTextViewTest.java b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/views/CustomTextViewTest.java index f6bc9bfe7..ae09ce534 100644 --- a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/views/CustomTextViewTest.java +++ b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/views/CustomTextViewTest.java @@ -13,30 +13,36 @@ import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.MockitoAnnotations; +import org.powermock.reflect.Whitebox; import org.robolectric.RuntimeEnvironment; public class CustomTextViewTest extends BaseTest { @Mock private Context context; + private CustomTextView customTextView; + @Mock + private CustomTextView customTextViewMock; @Before public void setUp() throws JSONException { + customTextView = new CustomTextView(RuntimeEnvironment.application.getApplicationContext()); Application application = Mockito.spy(Application.class); MockitoAnnotations.initMocks(this); Mockito.doReturn(context).when(application).getApplicationContext(); + Whitebox.setInternalState(customTextView, "hintOnText", true); + Mockito.doReturn(true).when(customTextViewMock).isEnabled(); } @Test public void testSetText() { - CustomTextView customTextView = new CustomTextView(RuntimeEnvironment.application.getApplicationContext()); - String text = ""; + String text = "testtest"; customTextView.setText(text); - Assert.assertEquals("", customTextView.getText()); + Assert.assertEquals("testtest", customTextView.getText().toString()); } @Test public void testSetTextColor() { - CustomTextView customTextView = new CustomTextView(RuntimeEnvironment.application.getApplicationContext()); + customTextView = new CustomTextView(RuntimeEnvironment.application.getApplicationContext()); int expectedTextColor = Color.parseColor("#00ff00"); customTextView.setTextColor(expectedTextColor); Assert.assertEquals(expectedTextColor, customTextView.getTextColors().getDefaultColor()); diff --git a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/widgets/BasicRDTCaptureFactoryTest.java b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/widgets/BasicRDTCaptureFactoryTest.java index 3dcf13126..ab6b4154c 100644 --- a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/widgets/BasicRDTCaptureFactoryTest.java +++ b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/widgets/BasicRDTCaptureFactoryTest.java @@ -1,5 +1,16 @@ package com.vijay.jsonwizard.widgets; +import static android.app.Activity.RESULT_CANCELED; +import static android.app.Activity.RESULT_OK; +import static com.vijay.jsonwizard.constants.JsonFormConstants.RDT_CAPTURE_CODE; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.verify; +import static org.robolectric.Shadows.shadowOf; +import static edu.washington.cs.ubicomplab.rdt_reader.core.Constants.SAVED_IMAGE_FILE_PATH; + import android.content.Intent; import android.content.pm.PackageManager; import android.view.View; @@ -27,17 +38,6 @@ import edu.washington.cs.ubicomplab.rdt_reader.activity.RDTCaptureActivity; -import static android.app.Activity.RESULT_CANCELED; -import static android.app.Activity.RESULT_OK; -import static com.vijay.jsonwizard.constants.JsonFormConstants.RDT_CAPTURE_CODE; -import static edu.washington.cs.ubicomplab.rdt_reader.core.Constants.SAVED_IMAGE_FILE_PATH; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.Mockito.verify; -import static org.robolectric.Shadows.shadowOf; - @Config(shadows = {ShadowContextCompat.class}) public class BasicRDTCaptureFactoryTest extends FactoryTest { @@ -66,6 +66,8 @@ public void testRDTCaptureFactoryShouldCorrectlyInitializeViews() throws Excepti List viewList = basicRDTCaptureFactory.getViewsFromJson("step1", jsonFormActivity, formFragment, rdtCapture, listener, false); + WidgetArgs widgetArgs = ReflectionHelpers.getField(basicRDTCaptureFactory, "widgetArgs"); + Thread.sleep(1000); ShadowContextCompat.setPermissionStatus(1); @@ -74,7 +76,6 @@ public void testRDTCaptureFactoryShouldCorrectlyInitializeViews() throws Excepti assertEquals(1, viewList.size()); // verify widget args were populated - WidgetArgs widgetArgs = ReflectionHelpers.getField(basicRDTCaptureFactory, "widgetArgs"); assertEquals(formFragment, widgetArgs.getFormFragment()); assertEquals(listener, widgetArgs.getListener()); assertFalse(widgetArgs.isPopup()); diff --git a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/widgets/RepeatingGroupFactoryTest.java b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/widgets/RepeatingGroupFactoryTest.java index 36018e05c..ae060373f 100644 --- a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/widgets/RepeatingGroupFactoryTest.java +++ b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/widgets/RepeatingGroupFactoryTest.java @@ -203,7 +203,7 @@ public void testUniqueChildElementKeyGenerationShouldContainChildKeyAsComponent( childElement.put(JsonFormConstants.KEY, childElementKey); AttachRepeatingGroupTask attachRepeatingGroupTask = new AttachRepeatingGroupTask(mock(LinearLayout.class), - 0, new HashMap(), widgetArgs, mock(ImageButton.class)); + 0, new HashMap<>(), widgetArgs, mock(ImageButton.class)); ReflectionHelpers.callInstanceMethod(attachRepeatingGroupTask, "addUniqueIdentifiers", diff --git a/gradle.properties b/gradle.properties index 6963929ec..5abb92c86 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,4 +1,4 @@ -VERSION_NAME=3.1.1-PREVIEW-SNAPSHOT +VERSION_NAME=3.1.1-SNAPSHOT VERSION_CODE=1 GROUP=org.smartregister POM_SETTING_DESCRIPTION=OpenSRP Client Native Form Json Wizard