Skip to content

Commit

Permalink
Merge pull request #718 from PhenoApps/print_trait_improvements
Browse files Browse the repository at this point in the history
Print trait improvements
  • Loading branch information
trife authored Sep 25, 2023
2 parents b5aa913 + 5420265 commit 8698c22
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,9 @@ public class GeneralKeys {
//Calendar Trait
public static final String CALENDAR_LAST_SAVED_DATE = "com.fieldbook.tracker.CALENDAR_LAST_SAVED_DATE";

//Zebra Label Print Trait
public static final String LABEL_PRINT_DEVICE_NAME = "LABEL_PRINT_DEVICE_NAME";

//Dialog Export
public static final String DIALOG_EXPORT_BUNDLE_CHECKED = "com.fieldbook.tracker.DIALOG_EXPORT_BUNDLE_CHECKED";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.fieldbook.tracker.R;
import com.fieldbook.tracker.activities.CollectActivity;
import com.fieldbook.tracker.preferences.GeneralKeys;
import com.fieldbook.tracker.utilities.BluetoothChooseCallback;
import com.fieldbook.tracker.utilities.BluetoothUtil;
import com.fieldbook.tracker.utilities.Constants;

Expand Down Expand Up @@ -54,6 +55,7 @@ public class LabelPrintTraitLayout extends BaseTraitLayout {
private Spinner barcodefield;
private Spinner labelcopies;

private ImageButton connectPrinter;
private ImageView label;
private ImageButton printLabel;

Expand Down Expand Up @@ -158,8 +160,9 @@ public void init(Activity act) {

mActivity = act;

connectPrinter = act.findViewById(R.id.connectPrinterButton);
printLabel = act.findViewById(R.id.printLabelButton);

LocalBroadcastManager.getInstance(getContext()).unregisterReceiver(mPrinterMessageReceiver);
LocalBroadcastManager.getInstance(getContext()).registerReceiver(mPrinterMessageReceiver,
new IntentFilter("printer_message"));

Expand Down Expand Up @@ -292,6 +295,17 @@ public void onNothingSelected(AdapterView<?> arg0) {
e.printStackTrace();

}
connectPrinter.setOnClickListener(view -> {

mBluetoothUtil.choose(getContext(), new BluetoothChooseCallback() {
@Override
public void onDeviceChosen(String newDeviceName) {
Log.d("LabelPrintTraitLayout", "Chosen printerName is " + newDeviceName);
saveDeviceNamePreference(newDeviceName);
}
});

});

/*
* This section handles print events. TODO: Create a label prototype based class. Move most of this logic to a function/class. chaneylc 8/26/2020
Expand Down Expand Up @@ -389,7 +403,11 @@ public void onNothingSelected(AdapterView<?> arg0) {
labelData = labelData.replace("size2", size2);
labelData = labelData.replace("size3", size3);
labelData = labelData.replace("size4", size4);
labelData = labelData.replace("barcode", barcode);
if (barcode.isEmpty()) { // remove barcode if it will not encode anything
labelData = labelData.replace("^BQ,,sizeb^FDMA,barcode^FS", "");
} else {
labelData = labelData.replace("barcode", barcode);
}
labelData = labelData.replace("sizeb", Integer.toString(barcode_size));

}
Expand All @@ -413,7 +431,20 @@ public void onNothingSelected(AdapterView<?> arg0) {
* This bluetooth utility class is used to connect with a paired printer and send print commands.
* A local broadcast receiver is used to communicate with the print thread within this utility class.
*/
mBluetoothUtil.print(getContext(), size, labels);
String printerName = getPrefs().getString(GeneralKeys.LABEL_PRINT_DEVICE_NAME, null);
Log.d("LabelPrintTraitLayout", "retrieved printerName is " + printerName);
if (printerName == null) {
mBluetoothUtil.choose(getContext(), new BluetoothChooseCallback() {
@Override
public void onDeviceChosen(String newDeviceName) {
Log.d("LabelPrintTraitLayout", "Chosen printerName is " + newDeviceName);
saveDeviceNamePreference(newDeviceName);
mBluetoothUtil.print(getContext(), newDeviceName, size, labels);
}
});
} else {
mBluetoothUtil.print(getContext(), printerName, size, labels);
}
}
}
} else {
Expand All @@ -424,6 +455,12 @@ public void onNothingSelected(AdapterView<?> arg0) {
});
}

private void saveDeviceNamePreference(String newDeviceName) {
SharedPreferences.Editor editor = getPrefs().edit();
editor.putString(GeneralKeys.LABEL_PRINT_DEVICE_NAME, newDeviceName);
editor.apply();
}

@Override
public void deleteTraitListener() {

Expand Down
81 changes: 39 additions & 42 deletions app/src/main/java/com/fieldbook/tracker/utilities/BluetoothUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,82 +3,79 @@ package com.fieldbook.tracker.utilities
import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothDevice
import android.content.Context
import android.content.SharedPreferences
import android.util.Log
import android.widget.RadioButton
import android.widget.RadioGroup
import androidx.appcompat.app.AlertDialog
import com.fieldbook.tracker.R
import com.fieldbook.tracker.preferences.GeneralKeys


interface BluetoothChooseCallback {
fun onDeviceChosen(deviceName: String)
}
//Bluetooth Utility class for printing ZPL code and choosing bluetooth devices to print from.
class BluetoothUtil {

private var mBtName: String = String()

private val mBluetoothAdapter: BluetoothAdapter? by lazy {
BluetoothAdapter.getDefaultAdapter()
}

//operation that uses the provided context to prompt the user for a paired bluetooth device
private fun choose(ctx: Context, f: () -> Unit) {
fun choose(ctx: Context, callback: BluetoothChooseCallback) {

if (mBtName.isBlank()) {
var deviceName: String = ""

mBluetoothAdapter?.let {
mBluetoothAdapter?.let {

val pairedDevices = it.bondedDevices
val pairedDevices = it.bondedDevices

val map = HashMap<Int, BluetoothDevice>()
val map = HashMap<Int, BluetoothDevice>()

val input = RadioGroup(ctx)
val input = RadioGroup(ctx)

pairedDevices.forEachIndexed { _, t ->
val button = RadioButton(ctx)
button.text = t.name
input.addView(button)
map[button.id] = t
}
pairedDevices.forEachIndexed { _, t ->
val button = RadioButton(ctx)
button.text = t.name
input.addView(button)
map[button.id] = t
}

val builder = AlertDialog.Builder(ctx).apply {
val builder = AlertDialog.Builder(ctx).apply {

setTitle(context.getString(R.string.bluetooth_printer_choose_device_title))
setTitle(context.getString(R.string.bluetooth_printer_choose_device_title))

setView(input)
setView(input)

setNegativeButton(android.R.string.cancel) { _, _ ->
setNegativeButton(android.R.string.cancel) { _, _ ->

}
}

setPositiveButton(android.R.string.ok) { _, _ ->
setPositiveButton(android.R.string.ok) { _, _ ->

if (input.checkedRadioButtonId == -1) return@setPositiveButton
else {
mBtName = map[input.checkedRadioButtonId]?.name ?: ""
}
f()
if (input.checkedRadioButtonId == -1) return@setPositiveButton
else {
Log.d("BluetoothUtil", "Setting mBtName")
deviceName = map[input.checkedRadioButtonId]?.name ?: ""
Log.d("BluetoothUtil", "Selected Bluetooth Device: $deviceName")
callback.onDeviceChosen(deviceName)
}
}

builder.show()
}

} else f()
builder.show()
}
}

/**
* This function will first ask the user to select a printer.
* As long as the same object is used the user only needs to ask once.
* This sends a list of label commands, and only updates the printer message when the
* This function will send a list of label commands to the printer, and only updates the printer message when the
* button is pressed.
*/
fun print(ctx: Context, size: String, labelCommand: List<String>) {

choose(ctx) {

if (labelCommand.isNotEmpty()) {

PrintThread(ctx, mBtName).print(size, labelCommand)

}
fun print(ctx: Context, printerName: String, size: String, labelCommand: List<String>) {
Log.d("BluetoothUtil", "Label Command is: $labelCommand")
if (labelCommand.isNotEmpty()) {
Log.d("BluetoothUtil", "printing to $printerName")
PrintThread(ctx, printerName).print(size, labelCommand)
}
}
}
}
1 change: 1 addition & 0 deletions app/src/main/res/drawable/connection.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!-- drawable/connection.xml --><vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="24dp" android:width="24dp" android:viewportWidth="24" android:viewportHeight="24"><path android:fillColor="#000000" android:pathData="M21.4 7.5C22.2 8.3 22.2 9.6 21.4 10.3L18.6 13.1L10.8 5.3L13.6 2.5C14.4 1.7 15.7 1.7 16.4 2.5L18.2 4.3L21.2 1.3L22.6 2.7L19.6 5.7L21.4 7.5M15.6 13.3L14.2 11.9L11.4 14.7L9.3 12.6L12.1 9.8L10.7 8.4L7.9 11.2L6.4 9.8L3.6 12.6C2.8 13.4 2.8 14.7 3.6 15.4L5.4 17.2L1.4 21.2L2.8 22.6L6.8 18.6L8.6 20.4C9.4 21.2 10.7 21.2 11.4 20.4L14.2 17.6L12.8 16.2L15.6 13.3Z" /></vector>
46 changes: 32 additions & 14 deletions app/src/main/res/layout/trait_labelprint.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,23 @@
android:gravity="center"
android:orientation="horizontal">

<ImageButton
android:id="@+id/connectPrinterButton"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:layout_margin="10dp"
android:background="@drawable/custom_round_button"
android:gravity="center"
android:padding="10dp"
android:scaleType="fitXY"
android:src="@drawable/connection"
android:contentDescription="@string/trait_labelprint_connect_button_description" />

<ImageView
android:id="@+id/labelPreview"
android:layout_width="125dp"
android:layout_height="88dp"
android:layout_width="150dp"
android:layout_height="100dp"
android:layout_gravity="center"
android:layout_margin="15dip"
android:adjustViewBounds="false"
Expand All @@ -27,8 +40,8 @@

<ImageButton
android:id="@+id/printLabelButton"
android:layout_width="65dp"
android:layout_height="65dp"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_gravity="center"
android:layout_margin="10dp"
android:background="@drawable/custom_round_button"
Expand Down Expand Up @@ -74,9 +87,10 @@

<Spinner
android:id="@+id/textfield"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="200dp" />
android:layout_width="0dp"
android:layout_weight="3"
android:maxLines="1" />
</LinearLayout>

<LinearLayout
Expand All @@ -95,9 +109,10 @@

<Spinner
android:id="@+id/textfield2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="200dp" />
android:layout_width="0dp"
android:layout_weight="3"
android:maxLines="1" />
</LinearLayout>

<LinearLayout style="@style/TextViewStyle.Bold"
Expand All @@ -116,9 +131,10 @@

<Spinner
android:id="@+id/textfield3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="200dp" />
android:layout_width="0dp"
android:layout_weight="3"
android:maxLines="1" />
</LinearLayout>

<LinearLayout
Expand All @@ -137,9 +153,10 @@

<Spinner
android:id="@+id/textfield4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="200dp" />
android:layout_width="0dp"
android:layout_weight="3"
android:maxLines="1" />
</LinearLayout>

<LinearLayout
Expand All @@ -157,9 +174,10 @@

<Spinner
android:id="@+id/barcodefield"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="200dp" />
android:layout_width="0dp"
android:layout_weight="3"
android:maxLines="1" />
</LinearLayout>

<LinearLayout
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,7 @@
<string name="printer_not_connected">Printer not connected</string>
<string name="no_device_paired">No device paired</string>

<string name="trait_labelprint_connect_button_description">Button which connects a printer.</string>
<string name="trait_labelprint_preview_description">Label preview.</string>
<string name="trait_labelprint_print_button_description">Button which prints labels configured in this dialog.</string>

Expand Down

0 comments on commit 8698c22

Please sign in to comment.