From 1711f7a6e132d4a0675c26aff054a41db4908e02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20L=C3=A4nge?= Date: Fri, 19 Jan 2024 15:07:28 +0100 Subject: [PATCH] Convert `Converters.java` and `HistoryItem.java` to kotlin --- .../qrscanner/database/Converters.java | 79 --------- .../qrscanner/database/Converters.kt | 77 +++++++++ .../qrscanner/database/HistoryItem.java | 158 ------------------ .../qrscanner/database/HistoryItem.kt | 87 ++++++++++ 4 files changed, 164 insertions(+), 237 deletions(-) delete mode 100644 app/src/main/java/com/secuso/privacyfriendlycodescanner/qrscanner/database/Converters.java create mode 100644 app/src/main/java/com/secuso/privacyfriendlycodescanner/qrscanner/database/Converters.kt delete mode 100644 app/src/main/java/com/secuso/privacyfriendlycodescanner/qrscanner/database/HistoryItem.java create mode 100644 app/src/main/java/com/secuso/privacyfriendlycodescanner/qrscanner/database/HistoryItem.kt diff --git a/app/src/main/java/com/secuso/privacyfriendlycodescanner/qrscanner/database/Converters.java b/app/src/main/java/com/secuso/privacyfriendlycodescanner/qrscanner/database/Converters.java deleted file mode 100644 index 994f6c8..0000000 --- a/app/src/main/java/com/secuso/privacyfriendlycodescanner/qrscanner/database/Converters.java +++ /dev/null @@ -1,79 +0,0 @@ -package com.secuso.privacyfriendlycodescanner.qrscanner.database; - -import android.content.Context; -import android.graphics.Bitmap; -import android.graphics.BitmapFactory; -import android.util.Base64; - -import androidx.annotation.Nullable; -import androidx.room.TypeConverter; - -import com.google.gson.Gson; -import com.google.zxing.BarcodeFormat; -import com.google.zxing.ResultPoint; - -import java.io.ByteArrayOutputStream; - -/** - * This class offers type converters for the room database. - * - * @author Christopher Beckmann - * @see AppDatabase - */ -public final class Converters { - - @TypeConverter - @Nullable - public static String encodeImage(Bitmap bitmap) { - if (bitmap == null) return null; - ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); - bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream); - byte[] byteArray = byteArrayOutputStream.toByteArray(); - return Base64.encodeToString(byteArray, Base64.NO_WRAP); - } - - @TypeConverter - @Nullable - public static Bitmap decodeImage(String encodedImage) { - if (encodedImage == null) return null; - byte[] decodedBytes = Base64.decode(encodedImage, Base64.NO_WRAP); - return BitmapFactory.decodeByteArray(decodedBytes, 0, decodedBytes.length); - } - - @TypeConverter - @Nullable - public static BarcodeFormat fromText(String text) { - try { - return BarcodeFormat.valueOf(text); - } catch (IllegalArgumentException | NullPointerException e) { - return null; - } - } - - @TypeConverter - public static String fromBarcodeFormat(BarcodeFormat bcf) { - return bcf.name(); - } - - @TypeConverter - @Nullable - public static String fromResultPoints(ResultPoint[] rp) { - Gson gson = new Gson(); - return gson.toJson(rp); - } - - @TypeConverter - @Nullable - public static ResultPoint[] convertResultPointFromJson(String jsonString) { - Gson gson = new Gson(); - return gson.fromJson(jsonString, ResultPoint[].class); - } - - public static float dpFromPx(final Context context, final float px) { - return px / context.getResources().getDisplayMetrics().density; - } - - public static float pxFromDp(final Context context, final float dp) { - return dp * context.getResources().getDisplayMetrics().density; - } -} diff --git a/app/src/main/java/com/secuso/privacyfriendlycodescanner/qrscanner/database/Converters.kt b/app/src/main/java/com/secuso/privacyfriendlycodescanner/qrscanner/database/Converters.kt new file mode 100644 index 0000000..663fb60 --- /dev/null +++ b/app/src/main/java/com/secuso/privacyfriendlycodescanner/qrscanner/database/Converters.kt @@ -0,0 +1,77 @@ +package com.secuso.privacyfriendlycodescanner.qrscanner.database + +import android.content.Context +import android.graphics.Bitmap +import android.graphics.BitmapFactory +import android.util.Base64 +import androidx.room.TypeConverter +import com.google.gson.Gson +import com.google.zxing.BarcodeFormat +import com.google.zxing.ResultPoint +import java.io.ByteArrayOutputStream + +/** + * This class offers type converters for the room database. + * + * @author Christopher Beckmann + * @see AppDatabase + */ +object Converters { + @JvmStatic + @TypeConverter + fun encodeImage(bitmap: Bitmap?): String? { + if (bitmap == null) return null + val byteArrayOutputStream = ByteArrayOutputStream() + bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream) + val byteArray = byteArrayOutputStream.toByteArray() + return Base64.encodeToString(byteArray, Base64.NO_WRAP) + } + + @JvmStatic + @TypeConverter + fun decodeImage(encodedImage: String?): Bitmap? { + if (encodedImage == null) return null + val decodedBytes = Base64.decode(encodedImage, Base64.NO_WRAP) + return BitmapFactory.decodeByteArray(decodedBytes, 0, decodedBytes.size) + } + + @JvmStatic + @TypeConverter + fun fromText(text: String?): BarcodeFormat? { + return try { + BarcodeFormat.valueOf(text!!) + } catch (e: IllegalArgumentException) { + null + } catch (e: NullPointerException) { + null + } + } + + @JvmStatic + @TypeConverter + fun fromBarcodeFormat(bcf: BarcodeFormat): String { + return bcf.name + } + + @JvmStatic + @TypeConverter + fun fromResultPoints(rp: Array?): String? { + val gson = Gson() + return gson.toJson(rp) + } + + @JvmStatic + @TypeConverter + fun convertResultPointFromJson(jsonString: String?): Array? { + val gson = Gson() + return gson.fromJson(jsonString, Array::class.java) + } + + fun dpFromPx(context: Context, px: Float): Float { + return px / context.resources.displayMetrics.density + } + + fun pxFromDp(context: Context, dp: Float): Float { + return dp * context.resources.displayMetrics.density + } +} \ No newline at end of file diff --git a/app/src/main/java/com/secuso/privacyfriendlycodescanner/qrscanner/database/HistoryItem.java b/app/src/main/java/com/secuso/privacyfriendlycodescanner/qrscanner/database/HistoryItem.java deleted file mode 100644 index 88677a6..0000000 --- a/app/src/main/java/com/secuso/privacyfriendlycodescanner/qrscanner/database/HistoryItem.java +++ /dev/null @@ -1,158 +0,0 @@ -package com.secuso.privacyfriendlycodescanner.qrscanner.database; - -import android.graphics.Bitmap; -import android.os.Parcel; - -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; -import androidx.room.Entity; -import androidx.room.Ignore; -import androidx.room.PrimaryKey; - -import com.google.zxing.BarcodeFormat; -import com.google.zxing.Result; -import com.google.zxing.ResultPoint; - -/** - * Data class for the Histories table. The saved {@link Result} does not include - * the ResultMetadata. The Bitmap is saved as a Base64 String. - * - * @see Result - * @see Converters - * - * @author Christopher Beckmann - */ -@Entity(tableName = "Histories") -public class HistoryItem { - - @PrimaryKey(autoGenerate = true) private int _id; - private Bitmap image; - @NonNull - private String text = ""; - @Nullable - private byte[] rawBytes; - private int numBits; - private ResultPoint[] resultPoints; - private BarcodeFormat format; - private long timestamp; - - public void setImage(Bitmap image) { - this.image = image; - } - - public int get_id() { - return _id; - } - - public void set_id(int _id) { - this._id = _id; - } - - @NonNull - public String getText() { - return text; - } - - public void setText(String text) { - this.text = text; - } - - public byte[] getRawBytes() { - return rawBytes; - } - - public void setRawBytes(byte[] rawBytes) { - this.rawBytes = rawBytes; - } - - public int getNumBits() { - return numBits; - } - - public void setNumBits(int numBits) { - this.numBits = numBits; - } - - public ResultPoint[] getResultPoints() { - return resultPoints; - } - - public void setResultPoints(ResultPoint[] resultPoints) { - this.resultPoints = resultPoints; - } - - public BarcodeFormat getFormat() { - return format; - } - - public void setFormat(BarcodeFormat format) { - this.format = format; - } - - public void setTimestamp(long timestamp) { - this.timestamp = timestamp; - } - - public HistoryItem() {} - - @Ignore - protected HistoryItem(Parcel in) { - _id = in.readInt(); - image = Converters.decodeImage(in.readString()); - text = in.readString(); - rawBytes = in.createByteArray(); - numBits = in.readInt(); - timestamp = in.readLong(); - format = BarcodeFormat.values()[in.readInt()]; - - resultPoints = new ResultPoint[in.readInt()]; - for(int i = 0; i < resultPoints.length; i++) { - resultPoints[i] = new ResultPoint(in.readFloat(), in.readFloat()); - } - } - - public void writeToParcel(Parcel dest, int flags) { - dest.writeInt(_id); - dest.writeString(Converters.encodeImage(image)); - dest.writeString(text); - dest.writeByteArray(rawBytes); - dest.writeInt(numBits); - dest.writeLong(timestamp); - dest.writeInt(format.ordinal()); - - dest.writeInt(resultPoints != null ? resultPoints.length : 0); - for(ResultPoint rp : resultPoints) { - dest.writeFloat(rp.getX()); - dest.writeFloat(rp.getY()); - } - } - - public int describeContents() { - return 0; - } - -// public static final Creator CREATOR = new Creator() { -// @Override -// public HistoryItem createFromParcel(Parcel in) { -// return new HistoryItem(in); -// } -// -// @Override -// public HistoryItem[] newArray(int size) { -// return new HistoryItem[size]; -// } -// }; - - public Result getResult() { - return new Result(text, rawBytes, numBits, resultPoints,format,timestamp); - } - public Bitmap getImage() { - return image; - } - - public long getTimestamp() { - return timestamp; - } - - -} diff --git a/app/src/main/java/com/secuso/privacyfriendlycodescanner/qrscanner/database/HistoryItem.kt b/app/src/main/java/com/secuso/privacyfriendlycodescanner/qrscanner/database/HistoryItem.kt new file mode 100644 index 0000000..a027214 --- /dev/null +++ b/app/src/main/java/com/secuso/privacyfriendlycodescanner/qrscanner/database/HistoryItem.kt @@ -0,0 +1,87 @@ +package com.secuso.privacyfriendlycodescanner.qrscanner.database + +import android.graphics.Bitmap +import android.os.Parcel +import androidx.room.Entity +import androidx.room.Ignore +import androidx.room.PrimaryKey +import com.google.zxing.BarcodeFormat +import com.google.zxing.Result +import com.google.zxing.ResultPoint +import com.secuso.privacyfriendlycodescanner.qrscanner.database.Converters.decodeImage +import com.secuso.privacyfriendlycodescanner.qrscanner.database.Converters.encodeImage + +/** + * Data class for the Histories table. The saved [Result] does not include + * the ResultMetadata. The Bitmap is saved as a Base64 String. + * + * @see Result + * + * @see Converters + * + * + * @author Christopher Beckmann + */ +@Entity(tableName = "Histories") +data class HistoryItem( + @PrimaryKey(autoGenerate = true) + private var _id: Int = 0, + var image: Bitmap? = null, + var text: String = "", + var rawBytes: ByteArray? = null, + var numBits: Int = 0, + var resultPoints: Array? = null, + var format: BarcodeFormat? = null, + var timestamp: Long = 0 +) { + fun get_id(): Int { + return _id + } + + fun set_id(_id: Int) { + this._id = _id + } + + @Ignore + constructor() : this( + _id = 0 + ) + + @Ignore + constructor(`in`: Parcel) : this() { + _id = `in`.readInt() + image = decodeImage(`in`.readString()) + text = `in`.readString()!! + rawBytes = `in`.createByteArray() + numBits = `in`.readInt() + timestamp = `in`.readLong() + format = BarcodeFormat.values()[`in`.readInt()] + resultPoints = arrayOfNulls(`in`.readInt()) + for (i in resultPoints!!.indices) { + resultPoints!![i] = ResultPoint(`in`.readFloat(), `in`.readFloat()) + } + } + + fun writeToParcel(dest: Parcel, flags: Int) { + dest.writeInt(_id) + dest.writeString(encodeImage(image)) + dest.writeString(text) + dest.writeByteArray(rawBytes) + dest.writeInt(numBits) + dest.writeLong(timestamp) + dest.writeInt(format!!.ordinal) + dest.writeInt(if (resultPoints != null) resultPoints!!.size else 0) + for (rp in resultPoints!!) { + dest.writeFloat(rp!!.x) + dest.writeFloat(rp.y) + } + } + + fun describeContents(): Int { + return 0 + } + + val result: Result + // public static final Creator CREATOR = new Creator() { + get() = Result(text, rawBytes, numBits, resultPoints, format, timestamp) +} \ No newline at end of file