Skip to content

Commit

Permalink
Code improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
grzesiek2010 committed May 17, 2019
1 parent 739d558 commit f791e0c
Show file tree
Hide file tree
Showing 15 changed files with 136 additions and 125 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import com.journeyapps.barcodescanner.DecoratedBarcodeView;

import org.odk.collect.android.R;
import org.odk.collect.android.utilities.WidgetAppearances;
import org.odk.collect.android.utilities.WidgetAppearanceUtils;

/**
* Custom Scannner Activity extending from Activity to display a custom layout form scanner view.
Expand Down Expand Up @@ -103,7 +103,7 @@ private boolean hasFlash() {

private boolean frontCameraUsed() {
Bundle bundle = getIntent().getExtras();
return bundle != null && bundle.getBoolean(WidgetAppearances.FRONT);
return bundle != null && bundle.getBoolean(WidgetAppearanceUtils.FRONT);
}

public void switchFlashlight(View view) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
import org.odk.collect.android.utilities.FileUtils;
import org.odk.collect.android.utilities.FormEntryPromptUtils;
import org.odk.collect.android.utilities.ImageConverter;
import org.odk.collect.android.utilities.WidgetAppearances;
import org.odk.collect.android.utilities.WidgetAppearanceUtils;
import org.odk.collect.android.views.MediaLayout;
import org.odk.collect.android.views.ODKView;
import org.odk.collect.android.widgets.SelectWidget;
Expand Down Expand Up @@ -73,7 +73,7 @@ public abstract class AbstractSelectListAdapter extends RecyclerView.Adapter<Abs
this.widget = widget;
filteredItems = items;
this.numColumns = numColumns;
noButtonsMode = WidgetAppearances.getAppearance(widget.getFormEntryPrompt()).contains(WidgetAppearances.NO_BUTTONS);
noButtonsMode = WidgetAppearanceUtils.getAppearance(widget.getFormEntryPrompt()).contains(WidgetAppearanceUtils.NO_BUTTONS);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,28 +166,28 @@ public static DatePickerDetails getDatePickerDetails(String appearance) {
DatePickerDetails.DatePickerMode datePickerMode = DatePickerDetails.DatePickerMode.CALENDAR;
if (appearance != null) {
appearance = appearance.toLowerCase(Locale.US);
if (appearance.contains(WidgetAppearances.ETHIOPIAN)) {
if (appearance.contains(WidgetAppearanceUtils.ETHIOPIAN)) {
datePickerType = DatePickerDetails.DatePickerType.ETHIOPIAN;
datePickerMode = DatePickerDetails.DatePickerMode.SPINNERS;
} else if (appearance.contains(WidgetAppearances.COPTIC)) {
} else if (appearance.contains(WidgetAppearanceUtils.COPTIC)) {
datePickerType = DatePickerDetails.DatePickerType.COPTIC;
datePickerMode = DatePickerDetails.DatePickerMode.SPINNERS;
} else if (appearance.contains(WidgetAppearances.ISLAMIC)) {
} else if (appearance.contains(WidgetAppearanceUtils.ISLAMIC)) {
datePickerType = DatePickerDetails.DatePickerType.ISLAMIC;
datePickerMode = DatePickerDetails.DatePickerMode.SPINNERS;
} else if (appearance.contains(WidgetAppearances.BIKRAM_SAMBAT)) {
} else if (appearance.contains(WidgetAppearanceUtils.BIKRAM_SAMBAT)) {
datePickerType = DatePickerDetails.DatePickerType.BIKRAM_SAMBAT;
datePickerMode = DatePickerDetails.DatePickerMode.SPINNERS;
} else if (appearance.contains("myanmar")) {
datePickerType = DatePickerDetails.DatePickerType.MYANMAR;
datePickerMode = DatePickerDetails.DatePickerMode.SPINNERS;
} else if (appearance.contains(WidgetAppearances.NO_CALENDAR)) {
} else if (appearance.contains(WidgetAppearanceUtils.NO_CALENDAR)) {
datePickerMode = DatePickerDetails.DatePickerMode.SPINNERS;
}

if (appearance.contains(WidgetAppearances.MONTH_YEAR)) {
if (appearance.contains(WidgetAppearanceUtils.MONTH_YEAR)) {
datePickerMode = DatePickerDetails.DatePickerMode.MONTH_YEAR;
} else if (appearance.contains(WidgetAppearances.YEAR)) {
} else if (appearance.contains(WidgetAppearanceUtils.YEAR)) {
datePickerMode = DatePickerDetails.DatePickerMode.YEAR;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public static String getAnswerText(FormEntryPrompt fep, Context context, FormCon
DateTimeUtils.getDatePickerDetails(appearance), false, context);
}

if (data != null && appearance != null && appearance.contains(WidgetAppearances.THOUSANDS_SEP)) {
if (data != null && appearance != null && appearance.contains(WidgetAppearanceUtils.THOUSANDS_SEP)) {
try {
final BigDecimal answerAsDecimal = new BigDecimal(fep.getAnswerText());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,56 +27,67 @@
import androidx.annotation.NonNull;
import timber.log.Timber;

public class WidgetAppearances {
public static final String NO_APPEARANCE = "";
public class WidgetAppearanceUtils {
private static final String EXCEPTION_PARSING_COLUMNS = "Exception parsing columns";

// Date appearances
public static final String ETHIOPIAN = "ethiopian";
public static final String COPTIC = "coptic";
public static final String ISLAMIC = "islamic";
public static final String BIKRAM_SAMBAT = "bikram-sambat";
public static final String NO_CALENDAR = "no-calendar";
public static final String MONTH_YEAR = "month-year";
public static final String YEAR = "year";
public static final String BEARING = "bearing";
public static final String EX = "ex:";
public static final String THOUSANDS_SEP = "thousands-sep";
public static final String PRINTER = "printer";
public static final String NUMBERS = "numbers";
public static final String QUICK = "quick";
public static final String URL = "url";
public static final String SIGNATURE = "signature";
public static final String ANNOTATE = "annotate";
public static final String DRAW = "draw";

// Select one/multiple appearances
public static final String COMPACT = "compact";
public static final String COMPACT_N = "compact-";
public static final String QUICKCOMPACT = "quickcompact";
public static final String MINIMAL = "minimal";
public static final String COLUMNS = "columns";
public static final String COLUMNS_N = "columns-";
public static final String COLUMNS_FLEX = "columns-flex";
public static final String MINIMAL = "minimal";
public static final String QUICKCOMPACT = "quickcompact";
public static final String SEARCH = "search";
public static final String AUTOCOMPLETE = "autocomplete";
public static final String LIST_NO_LABEL = "list-nolabel";
public static final String LIST = "list";
public static final String LABEL = "label";
public static final String IMAGE_MAP = "image-map";
public static final String RATING = "rating";
public static final String NO_BUTTONS = "no-buttons";
public static final String QUICK = "quick";

// Media appearances
public static final String SIGNATURE = "signature";
public static final String ANNOTATE = "annotate";
public static final String DRAW = "draw";
public static final String SELFIE = "selfie";
public static final String NEW_FRONT = "new-front";
public static final String NEW = "new";
public static final String FRONT = "front";

// Maps appearances
public static final String PLACEMENT_MAP = "placement-map";
public static final String MAPS = "maps";

private WidgetAppearances() {
// Other appearances
public static final String NO_APPEARANCE = "";
public static final String BEARING = "bearing";
public static final String EX = "ex:";
public static final String THOUSANDS_SEP = "thousands-sep";
public static final String PRINTER = "printer";
public static final String NUMBERS = "numbers";
public static final String URL = "url";
public static final String RATING = "rating";

private WidgetAppearanceUtils() {
}

// Get appearance hint and clean it up so it is lower case, without the search function and never null.
@NonNull
public static String getAppearance(FormEntryPrompt fep) {
String appearance = fep.getAppearanceHint();
if (appearance == null) {
appearance = WidgetAppearances.NO_APPEARANCE;
appearance = NO_APPEARANCE;
} else {
// For now, all appearance tags are in English.
appearance = appearance.toLowerCase(Locale.ENGLISH);
Expand All @@ -92,10 +103,10 @@ public static String getAppearance(FormEntryPrompt fep) {

public static int getNumberOfColumns(FormEntryPrompt formEntryPrompt, Context context) {
int numColumns = 1;
String appearance = WidgetAppearances.getAppearance(formEntryPrompt);
if (!appearance.startsWith(WidgetAppearances.COMPACT_N) && (appearance.startsWith(WidgetAppearances.COMPACT)
|| appearance.startsWith(WidgetAppearances.QUICKCOMPACT)
|| appearance.startsWith(WidgetAppearances.COLUMNS_FLEX))) {
String appearance = getAppearance(formEntryPrompt);
if (!appearance.startsWith(COMPACT_N) && (appearance.startsWith(COMPACT)
|| appearance.startsWith(QUICKCOMPACT)
|| appearance.startsWith(COLUMNS_FLEX))) {
numColumns = -1;
try {
String firstWord = appearance.split("\\s+")[0];
Expand All @@ -105,11 +116,11 @@ public static int getNumberOfColumns(FormEntryPrompt formEntryPrompt, Context co
}
} catch (Exception e) {
// Do nothing, leave numColumns as -1
Timber.e("Exception parsing columns");
Timber.e(EXCEPTION_PARSING_COLUMNS);
}
} else if (appearance.contains(WidgetAppearances.COLUMNS_N) || appearance.contains(WidgetAppearances.COMPACT_N)) {
} else if (appearance.contains(COLUMNS_N) || appearance.contains(COMPACT_N)) {
try {
String columnsAppearance = appearance.contains(WidgetAppearances.COLUMNS_N) ? WidgetAppearances.COLUMNS_N : WidgetAppearances.COMPACT_N;
String columnsAppearance = appearance.contains(COLUMNS_N) ? COLUMNS_N : COMPACT_N;
if (appearance.contains(columnsAppearance)) {
try {
appearance =
Expand All @@ -122,13 +133,13 @@ public static int getNumberOfColumns(FormEntryPrompt formEntryPrompt, Context co
: substringFromNumColumns.length()));
}
} catch (Exception e) {
Timber.e("Exception parsing columns");
Timber.e(EXCEPTION_PARSING_COLUMNS);
}
}
} catch (Exception e) {
Timber.e("Exception parsing columns");
Timber.e(EXCEPTION_PARSING_COLUMNS);
}
} else if (appearance.contains(WidgetAppearances.COLUMNS)) {
} else if (appearance.contains(COLUMNS)) {
switch (context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) {
case Configuration.SCREENLAYOUT_SIZE_SMALL:
numColumns = 2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import org.odk.collect.android.application.Collect;
import org.odk.collect.android.listeners.PermissionListener;
import org.odk.collect.android.utilities.FileUtils;
import org.odk.collect.android.utilities.WidgetAppearances;
import org.odk.collect.android.utilities.WidgetAppearanceUtils;

import java.io.File;
import java.util.Locale;
Expand Down Expand Up @@ -144,7 +144,7 @@ public void denied() {

private void hideButtonsIfNeeded() {
if (getFormEntryPrompt().getAppearanceHint() != null
&& getFormEntryPrompt().getAppearanceHint().toLowerCase(Locale.ENGLISH).contains(WidgetAppearances.NEW)) {
&& getFormEntryPrompt().getAppearanceHint().toLowerCase(Locale.ENGLISH).contains(WidgetAppearanceUtils.NEW)) {
chooseButton.setVisibility(View.GONE);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
import org.odk.collect.android.utilities.FileUtil;
import org.odk.collect.android.utilities.MediaManager;
import org.odk.collect.android.utilities.MediaUtil;
import org.odk.collect.android.utilities.WidgetAppearances;
import org.odk.collect.android.utilities.WidgetAppearanceUtils;
import org.odk.collect.android.widgets.interfaces.FileWidget;

import java.io.File;
Expand Down Expand Up @@ -194,7 +194,7 @@ public void setBinaryData(Object object) {

private void hideButtonsIfNeeded() {
if (getFormEntryPrompt().getAppearanceHint() != null
&& getFormEntryPrompt().getAppearanceHint().toLowerCase(Locale.ENGLISH).contains(WidgetAppearances.NEW)) {
&& getFormEntryPrompt().getAppearanceHint().toLowerCase(Locale.ENGLISH).contains(WidgetAppearanceUtils.NEW)) {
chooseButton.setVisibility(View.GONE);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import org.odk.collect.android.listeners.PermissionListener;
import org.odk.collect.android.utilities.CameraUtils;
import org.odk.collect.android.utilities.ToastUtils;
import org.odk.collect.android.utilities.WidgetAppearances;
import org.odk.collect.android.utilities.WidgetAppearanceUtils;
import org.odk.collect.android.widgets.interfaces.BinaryWidget;

/**
Expand Down Expand Up @@ -134,10 +134,10 @@ public void denied() {

private void setCameraIdIfNeeded(IntentIntegrator intent) {
String appearance = getFormEntryPrompt().getAppearanceHint();
if (appearance != null && appearance.equalsIgnoreCase(WidgetAppearances.FRONT)) {
if (appearance != null && appearance.equalsIgnoreCase(WidgetAppearanceUtils.FRONT)) {
if (CameraUtils.isFrontCameraAvailable()) {
intent.setCameraId(CameraUtils.getFrontCameraId());
intent.addExtra(WidgetAppearances.FRONT, true);
intent.addExtra(WidgetAppearanceUtils.FRONT, true);
} else {
ToastUtils.showLongToast(R.string.error_front_camera_unavailable);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import org.javarosa.form.api.FormEntryPrompt;
import org.joda.time.LocalDateTime;
import org.odk.collect.android.listeners.WidgetValueChangedListener;
import org.odk.collect.android.utilities.WidgetAppearances;
import org.odk.collect.android.utilities.WidgetAppearanceUtils;
import org.odk.collect.android.widgets.interfaces.BinaryWidget;

/**
Expand All @@ -47,13 +47,13 @@ public DateTimeWidget(Context context, FormEntryPrompt prompt) {
setGravity(Gravity.START);

String appearance = prompt.getQuestion().getAppearanceAttr();
if (appearance != null && appearance.contains(WidgetAppearances.ETHIOPIAN)) {
if (appearance != null && appearance.contains(WidgetAppearanceUtils.ETHIOPIAN)) {
dateWidget = new EthiopianDateWidget(context, prompt);
} else if (appearance != null && appearance.contains(WidgetAppearances.COPTIC)) {
} else if (appearance != null && appearance.contains(WidgetAppearanceUtils.COPTIC)) {
dateWidget = new CopticDateWidget(context, prompt);
} else if (appearance != null && appearance.contains(WidgetAppearances.ISLAMIC)) {
} else if (appearance != null && appearance.contains(WidgetAppearanceUtils.ISLAMIC)) {
dateWidget = new IslamicDateWidget(context, prompt);
} else if (appearance != null && appearance.contains(WidgetAppearances.BIKRAM_SAMBAT)) {
} else if (appearance != null && appearance.contains(WidgetAppearanceUtils.BIKRAM_SAMBAT)) {
dateWidget = new BikramSambatDateWidget(context, prompt);
} else if (appearance != null && appearance.contains("myanmar")) {
dateWidget = new MyanmarDateWidget(context, prompt);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
import org.odk.collect.android.listeners.PermissionListener;
import org.odk.collect.android.preferences.GeneralKeys;
import org.odk.collect.android.utilities.PlayServicesUtil;
import org.odk.collect.android.utilities.WidgetAppearances;
import org.odk.collect.android.utilities.WidgetAppearanceUtils;
import org.odk.collect.android.widgets.interfaces.BinaryWidget;

import java.text.DecimalFormat;
Expand Down Expand Up @@ -88,10 +88,10 @@ public GeoPointWidget(Context context, FormEntryPrompt prompt) {

// use mapsV2 if it is available and was requested;
useMapsV2 = useMapsV2(context);
if (appearance != null && appearance.toLowerCase(Locale.US).contains(WidgetAppearances.PLACEMENT_MAP) && useMapsV2) {
if (appearance != null && appearance.toLowerCase(Locale.US).contains(WidgetAppearanceUtils.PLACEMENT_MAP) && useMapsV2) {
draggable = true;
useMaps = true;
} else if (appearance != null && appearance.toLowerCase(Locale.US).contains(WidgetAppearances.MAPS) && useMapsV2) {
} else if (appearance != null && appearance.toLowerCase(Locale.US).contains(WidgetAppearanceUtils.MAPS) && useMapsV2) {
draggable = false;
useMaps = true;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import org.odk.collect.android.listeners.PermissionListener;
import org.odk.collect.android.utilities.CameraUtils;
import org.odk.collect.android.utilities.FileUtils;
import org.odk.collect.android.utilities.WidgetAppearances;
import org.odk.collect.android.utilities.WidgetAppearanceUtils;

import java.io.File;
import java.util.Locale;
Expand Down Expand Up @@ -68,8 +68,8 @@ protected void setUpLayout() {
super.setUpLayout();

String appearance = getFormEntryPrompt().getAppearanceHint();
selfie = appearance != null && (appearance.equalsIgnoreCase(WidgetAppearances.SELFIE)
|| appearance.equalsIgnoreCase(WidgetAppearances.NEW_FRONT));
selfie = appearance != null && (appearance.equalsIgnoreCase(WidgetAppearanceUtils.SELFIE)
|| appearance.equalsIgnoreCase(WidgetAppearanceUtils.NEW_FRONT));

captureButton = getSimpleButton(getContext().getString(R.string.capture_image), R.id.capture_image);

Expand Down Expand Up @@ -140,7 +140,7 @@ public void denied() {

private void hideButtonsIfNeeded(String appearance) {
if (selfie || ((appearance != null
&& appearance.toLowerCase(Locale.ENGLISH).contains(WidgetAppearances.NEW)))) {
&& appearance.toLowerCase(Locale.ENGLISH).contains(WidgetAppearanceUtils.NEW)))) {
chooseButton.setVisibility(View.GONE);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import org.odk.collect.android.activities.FormEntryActivity;
import org.odk.collect.android.adapters.AbstractSelectListAdapter;
import org.odk.collect.android.external.ExternalSelectChoice;
import org.odk.collect.android.utilities.WidgetAppearances;
import org.odk.collect.android.utilities.WidgetAppearanceUtils;
import org.odk.collect.android.views.MediaLayout;

import java.util.ArrayList;
Expand Down Expand Up @@ -147,7 +147,7 @@ public void addMediaFromChoice(MediaLayout mediaLayout, int index, TextView text
}

protected RecyclerView setUpRecyclerView() {
numColumns = WidgetAppearances.getNumberOfColumns(getFormEntryPrompt(), getContext());
numColumns = WidgetAppearanceUtils.getNumberOfColumns(getFormEntryPrompt(), getContext());

RecyclerView recyclerView = (RecyclerView) LayoutInflater.from(getContext()).inflate(R.layout.recycler_view, null); // keep in an xml file to enable the vertical scrollbar
if (numColumns == 1) {
Expand Down
Loading

0 comments on commit f791e0c

Please sign in to comment.