From 84178ebbf9d0adb29ff8f9c4100ce9fe9d2bf2a0 Mon Sep 17 00:00:00 2001 From: Daniel Micay Date: Thu, 30 Sep 2021 13:50:48 -0400 Subject: [PATCH] convert QROverlay to Kotlin --- .../app/attestation/auditor/QROverlay.java | 126 ------------------ .../java/app/attestation/auditor/QROverlay.kt | 121 +++++++++++++++++ 2 files changed, 121 insertions(+), 126 deletions(-) delete mode 100644 app/src/main/java/app/attestation/auditor/QROverlay.java create mode 100644 app/src/main/java/app/attestation/auditor/QROverlay.kt diff --git a/app/src/main/java/app/attestation/auditor/QROverlay.java b/app/src/main/java/app/attestation/auditor/QROverlay.java deleted file mode 100644 index b807d4eb3..000000000 --- a/app/src/main/java/app/attestation/auditor/QROverlay.java +++ /dev/null @@ -1,126 +0,0 @@ -package app.attestation.auditor; - -import android.content.Context; -import android.graphics.Canvas; -import android.graphics.Color; -import android.graphics.Paint; -import android.graphics.PorterDuff; -import android.graphics.PorterDuffXfermode; -import android.text.TextPaint; -import android.util.AttributeSet; -import android.view.View; - -public class QROverlay extends View { - - private static final String TAG = "QROverlay"; - - private Paint bPaint; - private TextPaint tPaint; - private Paint fPaint; - private final float T_SIZE = 24; - private int size = 256; - private int frameSideSize = 3; - private int frameSideLength = 42; - - private final float SIZE_FACTOR = 0.6f; - - public QROverlay(Context context, AttributeSet attrs) { - super(context, attrs); - initPaints(); - } - - private void initPaints() { - frameSideSize *= getResources().getDisplayMetrics().density; - frameSideLength *= getResources().getDisplayMetrics().density; - - bPaint = new Paint(); - bPaint.setColor(Color.parseColor("#A6000000")); - bPaint.setStrokeWidth(10); - bPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC)); - - tPaint = new TextPaint(); - tPaint.setColor(Color.parseColor("#ffffff")); - - fPaint = new Paint(); - fPaint.setColor(Color.parseColor("#8BC34A")); // green500 - fPaint.setStrokeWidth(frameSideSize); - } - - @Override - protected void onDraw(Canvas canvas) { - super.onDraw(canvas); - - final int width = getWidth(); - final int height = getHeight(); - - final int dim = Math.min(width, height); - - size = (int) (dim * SIZE_FACTOR); - tPaint.setTextSize(T_SIZE * SIZE_FACTOR * getResources().getDisplayMetrics().density); - - final int verticalHeight = (height - size) / 2; - final int horizontalWidth = (width - size) / 2; - - // Drawing the background - canvas.drawRect(0, 0, width, verticalHeight, bPaint); - canvas.drawRect(0, 0, horizontalWidth, height, bPaint); - canvas.drawRect(horizontalWidth + size, 0, width, height, bPaint); - canvas.drawRect(0, verticalHeight + size, width, height, bPaint); - - final String text = getContext().getString(R.string.scanner_label); - final int textX = horizontalWidth + size / 2; - final int textY = verticalHeight + size + frameSideSize * 4; - - int xPos = textX - (int)(tPaint. - measureText(text) / 2); - int yPos = (int) (textY - ((tPaint.descent() + tPaint.ascent()) / 2)) ; - - canvas.drawText(text, xPos, yPos, tPaint); - - // Drawing the frame - - final int halfFrameSideSize = frameSideSize / 2; - - int x1 = width - horizontalWidth; - int y1 = height - verticalHeight; - - // Top left - canvas.drawLine(horizontalWidth, verticalHeight + halfFrameSideSize, - horizontalWidth + frameSideLength, verticalHeight + halfFrameSideSize, - fPaint); - canvas.drawLine(horizontalWidth + halfFrameSideSize, verticalHeight, - horizontalWidth + halfFrameSideSize, verticalHeight + frameSideLength, - fPaint); - - // Top right - canvas.drawLine(x1, verticalHeight + halfFrameSideSize, - x1 - frameSideLength, verticalHeight + halfFrameSideSize, - fPaint); - - canvas.drawLine(x1 - halfFrameSideSize, verticalHeight, - x1 - halfFrameSideSize, verticalHeight + frameSideLength, - fPaint); - - // Bottom left - canvas.drawLine(horizontalWidth, y1 - halfFrameSideSize, - horizontalWidth + frameSideLength, y1 - halfFrameSideSize, - fPaint); - - canvas.drawLine(horizontalWidth + halfFrameSideSize, y1, - horizontalWidth + halfFrameSideSize, y1 - frameSideLength, - fPaint); - - // Bottom right - canvas.drawLine(x1 - halfFrameSideSize, y1, - x1 - halfFrameSideSize, y1 - frameSideLength, - fPaint); - - canvas.drawLine(x1, y1 - halfFrameSideSize, - x1 - frameSideLength, y1 - halfFrameSideSize, - fPaint); - } - - public int getSize() { - return size; - } -} diff --git a/app/src/main/java/app/attestation/auditor/QROverlay.kt b/app/src/main/java/app/attestation/auditor/QROverlay.kt new file mode 100644 index 000000000..c9cd4c264 --- /dev/null +++ b/app/src/main/java/app/attestation/auditor/QROverlay.kt @@ -0,0 +1,121 @@ +package app.attestation.auditor + +import android.content.Context +import android.graphics.Canvas; +import android.graphics.Color; +import android.graphics.Paint; +import android.graphics.PorterDuff; +import android.graphics.PorterDuffXfermode; +import android.text.TextPaint +import android.util.AttributeSet +import android.view.View +import app.attestation.auditor.R + +class QROverlay(context: Context?, attrs: AttributeSet?) : View(context, attrs) { + companion object { + private const val TAG = "QROverlay" + private const val T_SIZE = 24f + private const val SIZE_FACTOR = 0.6f + } + + private var bPaint: Paint + private var tPaint: TextPaint + private var fPaint: Paint + var size = 256 + private set + private var frameSideSize = 3 + private var frameSideLength = 42 + + init { + frameSideSize *= resources.displayMetrics.density.toInt() + frameSideLength *= resources.displayMetrics.density.toInt() + bPaint = Paint() + bPaint.color = Color.parseColor("#A6000000") + bPaint.strokeWidth = 10f + bPaint.xfermode = PorterDuffXfermode(PorterDuff.Mode.SRC) + tPaint = TextPaint() + tPaint.color = Color.parseColor("#ffffff") + fPaint = Paint() + fPaint.color = Color.parseColor("#8BC34A") // green500 + fPaint.strokeWidth = frameSideSize.toFloat() + } + + override fun onDraw(canvas: Canvas) { + super.onDraw(canvas) + val dim = Math.min(width, height) + size = (dim * SIZE_FACTOR).toInt() + tPaint.textSize = T_SIZE * SIZE_FACTOR * resources.displayMetrics.density + val verticalHeight = (height - size) / 2 + val horizontalWidth = (width - size) / 2 + + // Drawing the background + canvas.drawRect(0f, 0f, width.toFloat(), verticalHeight.toFloat(), bPaint) + canvas.drawRect(0f, 0f, horizontalWidth.toFloat(), height.toFloat(), bPaint) + canvas.drawRect((horizontalWidth + size).toFloat(), 0f, width.toFloat(), height.toFloat(), bPaint) + canvas.drawRect(0f, + (verticalHeight + size).toFloat(), + width.toFloat(), + height.toFloat(), + bPaint + ) + val text = context.getString(R.string.scanner_label) + val textX = horizontalWidth + size / 2 + val textY = verticalHeight + size + frameSideSize * 4 + val xPos = textX - (tPaint.measureText(text) / 2).toInt() + val yPos = (textY - (tPaint.descent() + tPaint.ascent()) / 2).toInt() + canvas.drawText(text, xPos.toFloat(), yPos.toFloat(), tPaint) + + // Drawing the frame + val halfFrameSideSize = frameSideSize / 2 + val x1 = width - horizontalWidth + val y1 = height - verticalHeight + + // Top left + canvas.drawLine( + horizontalWidth.toFloat(), (verticalHeight + halfFrameSideSize).toFloat(), + (horizontalWidth + frameSideLength).toFloat(), (verticalHeight + halfFrameSideSize).toFloat(), + fPaint + ) + canvas.drawLine( + (horizontalWidth + halfFrameSideSize).toFloat(), verticalHeight.toFloat(), + (horizontalWidth + halfFrameSideSize).toFloat(), (verticalHeight + frameSideLength).toFloat(), + fPaint + ) + + // Top right + canvas.drawLine( + x1.toFloat(), (verticalHeight + halfFrameSideSize).toFloat(), + (x1 - frameSideLength).toFloat(), (verticalHeight + halfFrameSideSize).toFloat(), + fPaint + ) + canvas.drawLine( + (x1 - halfFrameSideSize).toFloat(), verticalHeight.toFloat(), + (x1 - halfFrameSideSize).toFloat(), (verticalHeight + frameSideLength).toFloat(), + fPaint + ) + + // Bottom left + canvas.drawLine( + horizontalWidth.toFloat(), (y1 - halfFrameSideSize).toFloat(), + (horizontalWidth + frameSideLength).toFloat(), (y1 - halfFrameSideSize).toFloat(), + fPaint + ) + canvas.drawLine( + (horizontalWidth + halfFrameSideSize).toFloat(), y1.toFloat(), + (horizontalWidth + halfFrameSideSize).toFloat(), (y1 - frameSideLength).toFloat(), + fPaint + ) + + // Bottom right + canvas.drawLine( + (x1 - halfFrameSideSize).toFloat(), y1.toFloat(), + (x1 - halfFrameSideSize).toFloat(), (y1 - frameSideLength).toFloat(), + fPaint + ) + canvas.drawLine( + x1.toFloat(), (y1 - halfFrameSideSize).toFloat(), + (x1 - frameSideLength).toFloat(), (y1 - halfFrameSideSize).toFloat(), + fPaint + ) + } +}