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

Improve media displayed in widgets [BarcodeWidget] #6534

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
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

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package org.odk.collect.android.widgets

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.view.View
import com.google.zxing.integration.android.IntentIntegrator
import org.javarosa.core.model.data.IAnswerData
import org.javarosa.core.model.data.StringData
import org.javarosa.form.api.FormEntryPrompt
import org.odk.collect.android.activities.ScannerWithFlashlightActivity
import org.odk.collect.android.databinding.BarcodeWidgetBinding
import org.odk.collect.android.formentry.questions.QuestionDetails
import org.odk.collect.android.utilities.Appearances
import org.odk.collect.android.utilities.Appearances.hasAppearance
import org.odk.collect.android.utilities.Appearances.isFrontCameraAppearance
import org.odk.collect.android.widgets.interfaces.WidgetDataReceiver
import org.odk.collect.android.widgets.utilities.WaitingForDataRegistry
import org.odk.collect.androidshared.system.CameraUtils
import org.odk.collect.androidshared.ui.ToastUtils.showLongToast
import org.odk.collect.permissions.PermissionListener
import org.odk.collect.strings.R

@SuppressLint("ViewConstructor")
class BarcodeWidget(
context: Context,
questionDetails: QuestionDetails,
private val waitingForDataRegistry: WaitingForDataRegistry,
private val cameraUtils: CameraUtils
) : QuestionWidget(context, questionDetails), WidgetDataReceiver {
lateinit var binding: BarcodeWidgetBinding

init {
render()
}

override fun onCreateAnswerView(context: Context, prompt: FormEntryPrompt, answerFontSize: Int): View {
binding = BarcodeWidgetBinding.inflate((context as Activity).layoutInflater)

if (prompt.isReadOnly) {
binding.barcodeButton.visibility = GONE
} else {
binding.barcodeButton.setOnClickListener { onButtonClick() }
}
binding.answerView.setHidden(hasAppearance(prompt, Appearances.HIDDEN_ANSWER))
binding.answerView.setTextSize(answerFontSize.toFloat())

val answer = prompt.answerText
if (!answer.isNullOrEmpty()) {
binding.barcodeButton.text = getContext().getString(R.string.replace_barcode)
}
binding.answerView.setAnswer(prompt.answerText)

return binding.root
}

override fun clearAnswer() {
binding.answerView.setAnswer(null)
binding.barcodeButton.text = context.getString(R.string.get_barcode)
widgetValueChanged()
}

override fun getAnswer(): IAnswerData? {
val answer = binding.answerView.getAnswer()
return if (answer.isEmpty()) null else StringData(answer)
}

override fun setData(answer: Any) {
binding.answerView.setAnswer(answer as String)
binding.barcodeButton.text = context.getString(R.string.replace_barcode)
widgetValueChanged()
}

override fun setOnLongClickListener(l: OnLongClickListener?) {
binding.barcodeButton.setOnLongClickListener(l)
binding.answerView.setOnLongClickListener(l)
}

override fun cancelLongPress() {
super.cancelLongPress()
binding.barcodeButton.cancelLongPress()
binding.answerView.cancelLongPress()
}

private fun onButtonClick() {
getPermissionsProvider().requestCameraPermission(
(context as Activity),
object : PermissionListener {
override fun granted() {
waitingForDataRegistry.waitForData(formEntryPrompt.index)
val intent = IntentIntegrator(context as Activity)
.setCaptureActivity(ScannerWithFlashlightActivity::class.java)
setCameraIdIfNeeded(formEntryPrompt, intent)
intent.initiateScan()
}
}
)
}

private fun setCameraIdIfNeeded(prompt: FormEntryPrompt, intent: IntentIntegrator) {
if (isFrontCameraAppearance(prompt)) {
if (cameraUtils.isFrontCameraAvailable(context)) {
intent.addExtra(Appearances.FRONT, true)
} else {
showLongToast(context, R.string.error_front_camera_unavailable)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.odk.collect.android.widgets

import android.content.Context
import android.util.AttributeSet
import android.util.TypedValue
import android.view.LayoutInflater
import android.widget.FrameLayout
import org.odk.collect.android.databinding.BarcodeWidgetAnswerBinding

class BarcodeWidgetAnswer @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyle: Int = 0
) : FrameLayout(context, attrs, defStyle) {
private val binding = BarcodeWidgetAnswerBinding.inflate(LayoutInflater.from(context), this, true)
private var hidden = false

fun setAnswer(answer: String?) {
binding.answer.text = stripInvalidCharacters(answer)
binding.root.visibility = if (hidden || binding.answer.text.isNullOrBlank()) GONE else VISIBLE
}

fun setTextSize(textSize: Float) {
binding.answer.setTextSize(TypedValue.COMPLEX_UNIT_DIP, textSize)
}

fun setHidden(hidden: Boolean) {
this.hidden = hidden
}

fun getAnswer(): String {
return binding.answer.text.toString()
}

// Remove control characters, invisible characters and unused code points.
private fun stripInvalidCharacters(data: String?): String? {
return data?.replace("\\p{C}".toRegex(), "")
}
}
25 changes: 25 additions & 0 deletions collect_app/src/main/res/layout/barcode_widget.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent">

<org.odk.collect.androidshared.ui.multiclicksafe.MultiClickSafeMaterialButton
android:id="@+id/barcode_button"
style="?widgetButtonIconQuestionWidgetStyle"
android:text="@string/get_barcode"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:icon="@drawable/ic_baseline_barcode_scanner_white_24" />

<org.odk.collect.android.widgets.BarcodeWidgetAnswer
android:id="@+id/answer_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@id/barcode_button"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>
32 changes: 20 additions & 12 deletions collect_app/src/main/res/layout/barcode_widget_answer.xml
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/barcode_widget_answer"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<org.odk.collect.androidshared.ui.multiclicksafe.MultiClickSafeMaterialButton
android:id="@+id/barcode_button"
style="?widgetButtonIconQuestionWidgetStyle"
android:text="@string/get_barcode"
android:layout_width="match_parent"
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:icon="@drawable/ic_baseline_barcode_scanner_white_24" />
app:tint="?colorOnSurface"
app:srcCompat="@drawable/ic_baseline_barcode_scanner_white_24"
app:layout_constraintTop_toTopOf="@id/answer"
app:layout_constraintBottom_toBottomOf="@id/answer"
app:layout_constraintStart_toStartOf="parent" />

<com.google.android.material.textview.MaterialTextView
android:id="@+id/barcode_answer_text"
style="@style/Widget.Collect.TextView.WidgetAnswer" />
android:id="@+id/answer"
style="@style/Widget.Collect.TextView.WidgetAnswer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toEndOf="@id/icon"
tools:text="123456789" />

</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Loading