Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change USB device identifier #63

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,12 @@ open class USBConnection : ConnectionType {

manager.deviceList.forEach {
try {
if ("${Integer.toHexString(it.value.vendorId)}:${Integer.toHexString(it.value.productId)}" == serial) {
if (it.value.serialNumber == serial) {
devices[it.key] = it.value
} else if ("${String.format("%04x", it.value.vendorId)}:${String.format("%04x", it.value.productId)}" == serial) {
devices[it.key] = it.value
} else if ("${Integer.toHexString(it.value.vendorId)}:${Integer.toHexString(it.value.productId)}" == serial) {
// Missing leading zeros - No longer used, but keep for backwards compatibility
devices[it.key] = it.value
} else if (it.value.deviceName == serial) {
// No longer used, but keep for backwards compatibility
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.widget.AutoCompleteTextView
import android.widget.Button
import android.widget.CheckBox
import androidx.preference.PreferenceManager
import com.google.android.material.switchmaterial.SwitchMaterial
import com.google.android.material.textfield.TextInputEditText
Expand Down Expand Up @@ -47,7 +48,7 @@ class GraphicESCPOSSettingsFragment : SetupFragment() {
val currentCompat = ((activity as PrinterSetupActivity).settingsStagingArea.get(
"hardware_${useCase}printer_graphicescposcompat"
)?.toBoolean() ) ?: prefs.getString("hardware_${useCase}printer_graphicescposcompat", "false")!!.toBoolean()
view.findViewById<SwitchMaterial>(R.id.swCompat).isChecked = currentCompat
view.findViewById<CheckBox>(R.id.swCompat).isChecked = currentCompat

val rotationAdapter = ArrayAdapter(requireContext(), R.layout.list_item, Rotation.values().map {
it.toString()
Expand All @@ -69,7 +70,7 @@ class GraphicESCPOSSettingsFragment : SetupFragment() {
val wap = view.findViewById<TextInputEditText>(R.id.teWaitAfterPage).text.toString()
val mw = view.findViewById<TextInputEditText>(R.id.teMaxWidth).text.toString()
val rotation = view.findViewById<TextInputLayout>(R.id.tilRotation).editText?.text.toString()
val compat = view.findViewById<SwitchMaterial>(R.id.swCompat).isChecked
val compat = view.findViewById<CheckBox>(R.id.swCompat).isChecked
if (TextUtils.isEmpty(mw)) {
view.findViewById<TextInputEditText>(R.id.teMaxWidth).error = getString(R.string.err_field_required)
} else if (!TextUtils.isDigitsOnly(mw)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,54 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.CheckBox
import androidx.annotation.RequiresApi
import androidx.preference.PreferenceManager
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.google.android.material.switchmaterial.SwitchMaterial
import com.google.android.material.textfield.TextInputEditText
import com.google.android.material.textfield.TextInputLayout
import eu.pretix.pretixprint.R
import splitties.toast.toast

class USBSettingsFragment : SetupFragment() {
private val ACTION_USB_PERMISSION = "eu.pretix.pretixprint.settings.USB_PERMISSION"
companion object {
private const val ACTION_USB_PERMISSION = "eu.pretix.pretixprint.settings.USB_PERMISSION"
}

private val usbReceiver = object : BroadcastReceiver() {
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
override fun onReceive(context: Context, intent: Intent) {
if (ACTION_USB_PERMISSION == intent.action) {
synchronized(this) {
val device: UsbDevice? = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE)
if (ACTION_USB_PERMISSION != intent.action) {
return
}

synchronized(this) {
if (!intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
toast(R.string.err_usb_permission_denied)
return
}

if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
if (device!!.serialNumber != null && device!!.serialNumber != "null") {
view?.findViewById<TextInputEditText>(R.id.teSerial)?.setText(device!!.serialNumber)
} else {
view?.findViewById<TextInputEditText>(R.id.teSerial)?.setText("${Integer.toHexString(device!!.vendorId)}:${Integer.toHexString(device!!.productId)}")
val device: UsbDevice = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE) ?: return
val deviceId = "${String.format("%04x", device.vendorId)}:${String.format("%04x", device.productId)}"
if (device.serialNumber != null && device.serialNumber != "null") {
view?.findViewById<TextInputLayout>(R.id.tilSerial)?.apply {
endIconMode = TextInputLayout.END_ICON_CUSTOM
isEndIconVisible = true
robbi5 marked this conversation as resolved.
Show resolved Hide resolved
helperText = getString(R.string.field_helper_serial)
setEndIconOnClickListener {
view?.findViewById<TextInputEditText>(R.id.teSerial)
?.setText(device.serialNumber)
isEndIconVisible = false
helperText = ""
}
} else {
toast(R.string.err_usb_permission_denied)
}
} else {
view?.findViewById<TextInputLayout>(R.id.tilSerial)?.apply {
helperText = ""
isEndIconVisible = false
}
}
view?.findViewById<TextInputEditText>(R.id.teSerial)?.setText(deviceId)
}
}
}
Expand All @@ -55,34 +75,35 @@ class USBSettingsFragment : SetupFragment() {
val prefs = PreferenceManager.getDefaultSharedPreferences(requireContext())
val view = inflater.inflate(R.layout.fragment_usb_settings, container, false)

val currentSerial = ((activity as PrinterSetupActivity).settingsStagingArea.get(
val currentSerial = (activity as PrinterSetupActivity).settingsStagingArea.get(
"hardware_${useCase}printer_ip"
) as String?) ?: prefs.getString("hardware_${useCase}printer_ip", "")
) ?: prefs.getString("hardware_${useCase}printer_ip", "")
view.findViewById<TextInputEditText>(R.id.teSerial).setText(currentSerial)

val currentCompat = ((activity as PrinterSetupActivity).settingsStagingArea.get(
"hardware_${useCase}printer_usbcompat"
)?.toBoolean() ) ?: prefs.getString("hardware_${useCase}printer_usbcompat", "false")!!.toBoolean()
view.findViewById<SwitchMaterial>(R.id.swCompat).isChecked = currentCompat
view.findViewById<CheckBox>(R.id.swCompat).isChecked = currentCompat

view.findViewById<Button>(R.id.btnPrev).setOnClickListener {
back()
}
view.findViewById<Button>(R.id.btnNext).setOnClickListener {
val serial = view.findViewById<TextInputEditText>(R.id.teSerial).text.toString()
val compat = view.findViewById<SwitchMaterial>(R.id.swCompat).isChecked
val compat = view.findViewById<CheckBox>(R.id.swCompat).isChecked
if (TextUtils.isEmpty(serial)) {
view.findViewById<TextInputEditText>(R.id.teSerial).error = getString(R.string.err_field_required)
} else {
view.findViewById<TextInputEditText>(R.id.teSerial).error = null
(activity as PrinterSetupActivity).settingsStagingArea.put("hardware_${useCase}printer_usbcompat", compat.toString())
(activity as PrinterSetupActivity).settingsStagingArea.put("hardware_${useCase}printer_ip",
serial)
(activity as PrinterSetupActivity).startProtocolChoice()
(activity as PrinterSetupActivity).apply {
settingsStagingArea.put("hardware_${useCase}printer_usbcompat", compat.toString())
settingsStagingArea.put("hardware_${useCase}printer_ip", serial)
startProtocolChoice()
}
}
}
view.findViewById<Button>(R.id.btnAuto).setOnClickListener {
val manager = activity!!.getSystemService(Context.USB_SERVICE) as UsbManager
val manager = requireActivity().getSystemService(Context.USB_SERVICE) as UsbManager
val deviceList = manager.deviceList.values.toList()
val deviceNames = deviceList.map { "${it.manufacturerName} ${it.productName} (${String.format("%04x", it.vendorId)}:${String.format("%04x", it.productId)})" }
MaterialAlertDialogBuilder(requireContext())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<vector android:height="24dp" android:tint="#000000"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white"
android:fillType="evenOdd" android:pathData="M16,9V4l1,0c0.55,0 1,-0.45 1,-1v0c0,-0.55 -0.45,-1 -1,-1H7C6.45,2 6,2.45 6,3v0c0,0.55 0.45,1 1,1l1,0v5c0,1.66 -1.34,3 -3,3h0v2h5.97v7l1,1l1,-1v-7H19v-2h0C17.34,12 16,10.66 16,9z"/>
</vector>
107 changes: 107 additions & 0 deletions pretixprint/app/src/main/res/layout-large/fragment_usb_settings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<ScrollView
android:id="@+id/scroll"
android:layout_width="match_parent"
android:layout_height="0dp"
android:fadeScrollbars="false"
app:layout_constraintBottom_toTopOf="@id/btnNext"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp">

<TextView
android:id="@+id/textView5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/setup_bluetooth_settings"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:orientation="horizontal"
app:layout_constraintTop_toBottomOf="@+id/textView5">

<Button
android:id="@+id/btnAuto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:text="@string/setup_bluetooth_select" />

<com.google.android.material.textfield.TextInputLayout
android:id="@+id/tilSerial"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:endIconCheckable="true"
app:endIconDrawable="@drawable/ic_push_pin_black_24dp"
app:endIconMode="none"
tools:endIconMode="custom"
tools:helperText="@string/field_helper_serial">

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/teSerial"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/field_label_identifier"
android:inputType="text" />
</com.google.android.material.textfield.TextInputLayout>
</LinearLayout>

<CheckBox
android:id="@+id/swCompat"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="@string/field_label_usbcompat"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/linearLayout" />

</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>

<Button
android:id="@+id/btnNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:text="@string/btn_next"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/scroll"
app:layout_constraintVertical_bias="1.0" />

<Button
android:id="@+id/btnPrev"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginBottom="16dp"
android:text="@string/btn_prev"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/btnNext"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/scroll"
app:layout_constraintVertical_bias="1.0" />
</androidx.constraintlayout.widget.ConstraintLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@
android:inputType="number" />
</com.google.android.material.textfield.TextInputLayout>

<com.google.android.material.switchmaterial.SwitchMaterial
<CheckBox
android:id="@+id/swCompat"
android:layout_width="0dp"
android:layout_height="wrap_content"
Expand Down
67 changes: 34 additions & 33 deletions pretixprint/app/src/main/res/layout/fragment_usb_settings.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
Expand All @@ -18,63 +19,63 @@

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
android:layout_height="wrap_content"
android:layout_margin="16dp">

<TextView
android:id="@+id/textView5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:text="@string/setup_bluetooth_settings"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<com.google.android.material.textfield.TextInputLayout
android:id="@+id/tilSerial"
android:layout_width="0dp"
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btnAuto">
android:orientation="vertical"
app:layout_constraintTop_toBottomOf="@+id/textView5">

<Button
android:id="@+id/btnAuto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:text="@string/setup_bluetooth_select" />

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/teSerial"
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/tilSerial"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/field_label_serial"
android:inputType="text" />
</com.google.android.material.textfield.TextInputLayout>
app:endIconCheckable="true"
app:endIconDrawable="@drawable/ic_push_pin_black_24dp"
app:endIconMode="none"
tools:endIconMode="custom"
tools:helperText="@string/field_helper_serial">

<com.google.android.material.switchmaterial.SwitchMaterial
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/teSerial"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/field_label_identifier"
android:inputType="text" />
</com.google.android.material.textfield.TextInputLayout>
</LinearLayout>

<CheckBox
android:id="@+id/swCompat"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:text="@string/field_label_usbcompat"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tilSerial"
android:text="@string/field_label_usbcompat"/>
app:layout_constraintTop_toBottomOf="@+id/linearLayout" />

<Button
android:id="@+id/btnAuto"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:text="@string/setup_bluetooth_select"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView5" />
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>

Expand Down
Loading