diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index fc2262b..63d77b2 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -17,6 +17,8 @@ composeUiTooling = "1.5.6" wearToolingPreview = "1.0.0" activityCompose = "1.13.0" coreSplashscreen = "1.2.0" +zxing = "3.5.3" +zxingAndroid = "4.3.0" [libraries] androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" } @@ -41,6 +43,8 @@ compose-ui-tooling = { group = "androidx.wear.compose", name = "compose-ui-tooli androidx-wear-tooling-preview = { group = "androidx.wear", name = "wear-tooling-preview", version.ref = "wearToolingPreview" } androidx-activity-compose = { group = "androidx.activity", name = "activity-compose", version.ref = "activityCompose" } androidx-core-splashscreen = { group = "androidx.core", name = "core-splashscreen", version.ref = "coreSplashscreen" } +zxing-core = { group = "com.google.zxing", name = "core", version.ref = "zxing" } +zxing-android = { group = "com.journeyapps", name = "zxing-android-embedded", version.ref = "zxingAndroid" } [plugins] android-application = { id = "com.android.application", version.ref = "agp" } diff --git a/mobile/build.gradle.kts b/mobile/build.gradle.kts index 5597019..09f864a 100644 --- a/mobile/build.gradle.kts +++ b/mobile/build.gradle.kts @@ -45,4 +45,6 @@ dependencies { testImplementation(libs.junit) androidTestImplementation(libs.androidx.espresso.core) androidTestImplementation(libs.androidx.junit) + implementation(libs.zxing.core) + implementation(libs.zxing.android) } \ No newline at end of file diff --git a/mobile/src/main/AndroidManifest.xml b/mobile/src/main/AndroidManifest.xml index 86cb1dc..7cf540b 100644 --- a/mobile/src/main/AndroidManifest.xml +++ b/mobile/src/main/AndroidManifest.xml @@ -11,6 +11,9 @@ android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/Base.Theme.OmniCards"> + diff --git a/mobile/src/main/java/ru/omni_devel/cards/AddCardActivity.kt b/mobile/src/main/java/ru/omni_devel/cards/AddCardActivity.kt index 288c3e8..59a3639 100644 --- a/mobile/src/main/java/ru/omni_devel/cards/AddCardActivity.kt +++ b/mobile/src/main/java/ru/omni_devel/cards/AddCardActivity.kt @@ -9,6 +9,7 @@ import androidx.activity.enableEdgeToEdge import androidx.appcompat.app.AppCompatActivity import androidx.core.view.ViewCompat import androidx.core.view.WindowInsetsCompat +import com.google.zxing.BarcodeFormat class AddCardActivity : AppCompatActivity() { private lateinit var db: DbHelper @@ -30,7 +31,7 @@ class AddCardActivity : AppCompatActivity() { val cardCodeTypeSelector: Spinner = findViewById(R.id.cardCodeTypeSelector) val saveCardButton: Button = findViewById(R.id.saveCardButton) - val options = CodeType.entries.map { it.name } + val options = BarcodeFormat.entries.map { it.name } val adapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, options) adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item) cardCodeTypeSelector.adapter = adapter @@ -38,7 +39,7 @@ class AddCardActivity : AppCompatActivity() { saveCardButton.setOnClickListener { val cardName = cardNameInput.text.toString() val cardValue = cardValueInput.text.toString() - val cardCodeType = CodeType.valueOf(cardCodeTypeSelector.selectedItem.toString()) + val cardCodeType = BarcodeFormat.valueOf(cardCodeTypeSelector.selectedItem.toString()) db.addCard(cardName, cardValue, cardCodeType) diff --git a/mobile/src/main/java/ru/omni_devel/cards/CardInfo.kt b/mobile/src/main/java/ru/omni_devel/cards/CardInfo.kt index 1541ed4..a8d9d64 100644 --- a/mobile/src/main/java/ru/omni_devel/cards/CardInfo.kt +++ b/mobile/src/main/java/ru/omni_devel/cards/CardInfo.kt @@ -1,13 +1,10 @@ package ru.omni_devel.cards -enum class CodeType { - QR, - BAR, -} +import com.google.zxing.BarcodeFormat class CardInfo ( val id: Int, val name: String, + val codeType: BarcodeFormat, val codeValue: String, - val codeType: CodeType, ) \ No newline at end of file diff --git a/mobile/src/main/java/ru/omni_devel/cards/DbHelper.kt b/mobile/src/main/java/ru/omni_devel/cards/DbHelper.kt index afb3159..57295ac 100644 --- a/mobile/src/main/java/ru/omni_devel/cards/DbHelper.kt +++ b/mobile/src/main/java/ru/omni_devel/cards/DbHelper.kt @@ -4,10 +4,11 @@ import android.content.ContentValues import android.content.Context import android.database.sqlite.SQLiteDatabase import android.database.sqlite.SQLiteOpenHelper +import com.google.zxing.BarcodeFormat class DbHelper(val context: Context, val factory: SQLiteDatabase.CursorFactory?) : SQLiteOpenHelper(context, "omni_cards", factory, 1) { override fun onCreate(db: SQLiteDatabase?) { - db!!.execSQL("CREATE TABLE IF NOT EXISTS cards (id SERIAL PRIMARY KEY, name TEXT, codeType TEXT, codeValue TEXT)") + db!!.execSQL("CREATE TABLE IF NOT EXISTS cards (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, codeType TEXT, codeValue TEXT)") } override fun onUpgrade( @@ -18,7 +19,7 @@ class DbHelper(val context: Context, val factory: SQLiteDatabase.CursorFactory?) db!!.execSQL("DROP TABLE IF EXISTS cards") } - fun addCard(name: String, codeValue: String, codeType: CodeType) { + fun addCard(name: String, codeValue: String, codeType: BarcodeFormat) { val values = ContentValues() values.put("name", name) @@ -42,7 +43,7 @@ class DbHelper(val context: Context, val factory: SQLiteDatabase.CursorFactory?) cards.add(CardInfo( id = cursor.getInt(cursor.getColumnIndexOrThrow("id")), name = cursor.getString(cursor.getColumnIndexOrThrow("name")), - codeType = CodeType.valueOf(cursor.getString(cursor.getColumnIndexOrThrow("codeType"))), + codeType = BarcodeFormat.valueOf(cursor.getString(cursor.getColumnIndexOrThrow("codeType"))), codeValue = cursor.getString(cursor.getColumnIndexOrThrow("codeValue")) )) } @@ -52,4 +53,29 @@ class DbHelper(val context: Context, val factory: SQLiteDatabase.CursorFactory?) return cards } + + fun getCard(id: Int): CardInfo? { + val db = this.readableDatabase + + val cursor = db.rawQuery("SELECT * FROM cards WHERE id = ?", arrayOf(id.toString())) + + if (!cursor.moveToFirst()) { + cursor.close() + db.close() + + return null + } + + val cardInfo = CardInfo( + id = cursor.getInt(cursor.getColumnIndexOrThrow("id")), + name = cursor.getString(cursor.getColumnIndexOrThrow("name")), + codeType = BarcodeFormat.valueOf(cursor.getString(cursor.getColumnIndexOrThrow("codeType"))), + codeValue = cursor.getString(cursor.getColumnIndexOrThrow("codeValue")) + ) + + cursor.close() + db.close() + + return cardInfo + } } \ No newline at end of file diff --git a/mobile/src/main/java/ru/omni_devel/cards/MainActivity.kt b/mobile/src/main/java/ru/omni_devel/cards/MainActivity.kt index dbcc0f7..a7c1286 100644 --- a/mobile/src/main/java/ru/omni_devel/cards/MainActivity.kt +++ b/mobile/src/main/java/ru/omni_devel/cards/MainActivity.kt @@ -2,6 +2,7 @@ package ru.omni_devel.cards import android.content.Intent import android.os.Bundle +import android.view.View import android.widget.Button import android.widget.LinearLayout import android.widget.TextView @@ -55,6 +56,7 @@ class MainActivity : AppCompatActivity() { text.text = "Add a card to get started" text.layoutParams = params + text.textAlignment = View.TEXT_ALIGNMENT_CENTER cardsList.addView(text) } else { @@ -71,6 +73,14 @@ class MainActivity : AppCompatActivity() { button.layoutParams = params button.cornerRadius = (12 * resources.displayMetrics.density).toInt() + button.setOnClickListener { + val intent = Intent(this, ShowCardActivity::class.java) + + intent.putExtra("cardId", card.id) + + startActivity(intent) + } + cardsList.addView(button) } } diff --git a/mobile/src/main/java/ru/omni_devel/cards/ShowCardActivity.kt b/mobile/src/main/java/ru/omni_devel/cards/ShowCardActivity.kt new file mode 100644 index 0000000..ada5701 --- /dev/null +++ b/mobile/src/main/java/ru/omni_devel/cards/ShowCardActivity.kt @@ -0,0 +1,66 @@ +package ru.omni_devel.cards + +import android.os.Bundle +import android.view.WindowManager +import android.widget.ImageView +import android.widget.Toast +import androidx.activity.enableEdgeToEdge +import androidx.appcompat.app.AppCompatActivity +import androidx.core.view.ViewCompat +import androidx.core.view.WindowInsetsCompat + +class ShowCardActivity : AppCompatActivity() { + private lateinit var db: DbHelper + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + enableEdgeToEdge() + setContentView(R.layout.activity_show_card) + ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets -> + val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars()) + v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom) + insets + } + + db = DbHelper(this, null) + } + + override fun onResume() { + super.onResume() + + setBrightness(1.0f) + renderCard() + } + + override fun onPause() { + super.onPause() + + setBrightness(WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE) + } + + private fun renderCard() { + val cardId = intent.getIntExtra("cardId", 0) + + val cardInfo = db.getCard(cardId) + + if (cardInfo == null) { + Toast.makeText(this, "cardInfo is null", Toast.LENGTH_LONG).show() + + return + } + + val cardCodeView: ImageView = findViewById(R.id.cardCodeView) + + val code = generateCode(cardInfo.codeValue, cardInfo.codeType) + + cardCodeView.setImageBitmap(code) + } + + private fun setBrightness(level: Float) { + val params = window.attributes + + params.screenBrightness = level + + window.attributes = params + } +} \ No newline at end of file diff --git a/mobile/src/main/java/ru/omni_devel/cards/Utils.kt b/mobile/src/main/java/ru/omni_devel/cards/Utils.kt new file mode 100644 index 0000000..52664af --- /dev/null +++ b/mobile/src/main/java/ru/omni_devel/cards/Utils.kt @@ -0,0 +1,13 @@ +package ru.omni_devel.cards + +import android.graphics.Bitmap +import com.google.zxing.BarcodeFormat +import com.google.zxing.MultiFormatWriter +import com.journeyapps.barcodescanner.BarcodeEncoder + +fun generateCode(value: String, format: BarcodeFormat): Bitmap { + val writer = MultiFormatWriter() + val matrix = writer.encode(value, format, 600, 300) + + return BarcodeEncoder().createBitmap(matrix) +} diff --git a/mobile/src/main/res/layout/activity_show_card.xml b/mobile/src/main/res/layout/activity_show_card.xml new file mode 100644 index 0000000..89223a8 --- /dev/null +++ b/mobile/src/main/res/layout/activity_show_card.xml @@ -0,0 +1,52 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/wear/build.gradle.kts b/wear/build.gradle.kts index 5b18fe1..c4527b6 100644 --- a/wear/build.gradle.kts +++ b/wear/build.gradle.kts @@ -55,4 +55,6 @@ dependencies { androidTestImplementation(libs.androidx.compose.ui.test.junit4) debugImplementation(libs.androidx.compose.ui.test.manifest) debugImplementation(libs.androidx.compose.ui.tooling) + implementation(libs.zxing.core) + implementation(libs.zxing.android) } \ No newline at end of file