package org.commcare.views.widgets;

import android.content.Context;
import android.content.Intent;
import android.text.Spannable;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TableLayout;

import androidx.appcompat.app.AppCompatActivity;

import com.google.zxing.client.android.Intents;
import com.google.zxing.integration.android.IntentIntegrator;

import org.commcare.dalvik.R;
import org.commcare.preferences.MainConfigurablePreferences;

public class WidgetUtils {
    private static final TableLayout.LayoutParams params;

    static {
        params = new TableLayout.LayoutParams();
        params.setMargins(7, 5, 7, 5);
    }

    public static void setupButton(Button btn, Spannable text, int fontSize, boolean enabled) {
        btn.setText(text);
        int verticalPadding = (int)btn.getResources().getDimension(R.dimen.widget_button_padding);
        btn.setTextSize(TypedValue.COMPLEX_UNIT_DIP, fontSize);
        btn.setPadding(20, verticalPadding, 20, verticalPadding);
        btn.setEnabled(enabled);
        btn.setLayoutParams(WidgetUtils.params);
    }

    public static Button setupClearButton(Context context, ViewGroup root, String text, int visibility) {
        Button clearButton = (Button) LayoutInflater.from(context).inflate(R.layout.blue_outlined_button, root, false);
        clearButton.setText(text);
        clearButton.setVisibility(visibility);
        return clearButton;
    }

    /**
     * Works just like {@link #createScanIntent(Context, String...)} except the format is null.
     */
    public static Intent createScanIntent(Context context) {
        return createScanIntent(context, null);
    }

    /**
     * A utility to create intent for barcode scan.
     *
     * If usage of 3rd party apps for scanning is enabled by the user, then this method will
     * create a generic intent for scanning barcodes and will fallback to zxing library if no
     * app exists to handle such intent.
     *
     * @param formats names of {@link com.google.zxing.BarcodeFormat}s to scan for. eg. {@link com.google.zxing.BarcodeFormat#QR_CODE}
     */
    public static Intent createScanIntent(Context context, String... formats) {
        if (MainConfigurablePreferences.useIntentCalloutForScanner()) {
            Intent scanIntent = new Intent(Intents.Scan.ACTION);
            if (scanIntent.resolveActivity(context.getPackageManager()) != null) {
                if (formats != null) {
                    StringBuilder joinedByComma = new StringBuilder();
                    for (String format : formats) {
                        if (joinedByComma.length() > 0) {
                            joinedByComma.append(',');
                        }
                        joinedByComma.append(format);
                    }
                    scanIntent.putExtra(Intents.Scan.FORMATS, joinedByComma.toString());
                }
                return scanIntent;
            }
        }
        return new IntentIntegrator((AppCompatActivity) context)
                .setDesiredBarcodeFormats(formats)
                .createScanIntent();
    }
}

@@ -124,16 +124,47 @@
 nnable;%0A
+import android.text.TextUtils;%0A
 import a
@@ -2567,346 +2567,8 @@
 ) %7B%0A
-                    StringBuilder joinedByComma = new StringBuilder();%0A                    for (String format : formats) %7B%0A                        if (joinedByComma.length() %3E 0) %7B%0A                            joinedByComma.append(',');%0A                        %7D%0A                        joinedByComma.append(format);%0A                    %7D%0A
     
@@ -2629,31 +2629,35 @@
 TS, 
-joinedByComma.toString(
+TextUtils.join(%22,%22, formats
 ));%0A

Use TextUtils to join string