From f691b20952c201910349f23f0ba38d06decb5d1a Mon Sep 17 00:00:00 2001 From: soraefir Date: Sun, 17 Mar 2024 15:40:14 +0100 Subject: [PATCH] Added clearing list --- app/src/main/AndroidManifest.xml | 9 ++-- .../helcel/fidelity/activity/MainActivity.kt | 12 ++--- .../fidelity/activity/fragment/CreateEntry.kt | 14 +++--- .../fidelity/activity/fragment/Scanner.kt | 11 +++-- .../fidelity/activity/fragment/ViewEntry.kt | 41 ++++++++++------- .../fidelity/activity/view/ScannerView.kt | 45 +++++++++++++++++++ .../helcel/fidelity/tools/BacodeScanner.kt | 4 +- .../fidelity/tools/BarcodeFormatConverter.kt | 1 - .../helcel/fidelity/tools/BarcodeGenerator.kt | 9 ++-- .../helcel/fidelity/tools/KeepassWrapper.kt | 16 +++---- app/src/main/res/drawable/heart.xml | 21 --------- app/src/main/res/drawable/key.xml | 31 ------------- app/src/main/res/drawable/locked.xml | 31 ------------- app/src/main/res/layout/act_main.xml | 4 +- app/src/main/res/layout/frag_create_entry.xml | 2 - app/src/main/res/layout/frag_launcher.xml | 4 +- app/src/main/res/layout/frag_scanner.xml | 24 +++++++--- .../main/res/layout/list_item_dropdown.xml | 4 +- app/src/main/res/values/strings.xml | 4 +- app/src/main/res/values/styles.xml | 3 -- app/src/main/res/values/themes.xml | 9 ++-- gradle.properties | 2 +- 22 files changed, 137 insertions(+), 164 deletions(-) create mode 100644 app/src/main/java/net/helcel/fidelity/activity/view/ScannerView.kt delete mode 100644 app/src/main/res/drawable/heart.xml delete mode 100644 app/src/main/res/drawable/key.xml delete mode 100644 app/src/main/res/drawable/locked.xml delete mode 100644 app/src/main/res/values/styles.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 648a679..da1536d 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -1,5 +1,6 @@ @@ -21,7 +22,8 @@ + android:exported="true" + tools:ignore="ExportedReceiver"> @@ -31,12 +33,13 @@ + android:exported="true" + tools:ignore="ExportedReceiver"> - + diff --git a/app/src/main/java/net/helcel/fidelity/activity/MainActivity.kt b/app/src/main/java/net/helcel/fidelity/activity/MainActivity.kt index e06eb18..9f1cad3 100644 --- a/app/src/main/java/net/helcel/fidelity/activity/MainActivity.kt +++ b/app/src/main/java/net/helcel/fidelity/activity/MainActivity.kt @@ -1,7 +1,9 @@ package net.helcel.fidelity.activity +import android.annotation.SuppressLint import android.content.Context import android.content.SharedPreferences +import android.content.pm.ActivityInfo import android.os.Bundle import androidx.activity.addCallback import androidx.appcompat.app.AppCompatActivity @@ -10,38 +12,38 @@ import net.helcel.fidelity.activity.fragment.Launcher import net.helcel.fidelity.databinding.ActMainBinding import net.helcel.fidelity.tools.CacheManager +@SuppressLint("SourceLockedOrientationActivity") class MainActivity : AppCompatActivity() { private lateinit var binding: ActMainBinding - private lateinit var sharedPreferences: SharedPreferences - override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) sharedPreferences = this.getSharedPreferences(CacheManager.PREF_NAME, Context.MODE_PRIVATE) CacheManager.loadFidelity(sharedPreferences) - binding = ActMainBinding.inflate(layoutInflater) setContentView(binding.root) onBackPressedDispatcher.addCallback(this) { if (supportFragmentManager.backStackEntryCount > 0) { supportFragmentManager.popBackStackImmediate() + loadLauncher() + requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT } else { finish() } } + if (savedInstanceState == null) loadLauncher() - } private fun loadLauncher() { supportFragmentManager.beginTransaction() - .add(R.id.container, Launcher()) + .replace(R.id.container, Launcher()) .commit() } } diff --git a/app/src/main/java/net/helcel/fidelity/activity/fragment/CreateEntry.kt b/app/src/main/java/net/helcel/fidelity/activity/fragment/CreateEntry.kt index 068156c..e77d6b1 100644 --- a/app/src/main/java/net/helcel/fidelity/activity/fragment/CreateEntry.kt +++ b/app/src/main/java/net/helcel/fidelity/activity/fragment/CreateEntry.kt @@ -67,7 +67,7 @@ class CreateEntry : Fragment() { ErrorToaster.formIncomplete(requireActivity()) } else { - val kpentry = KeepassWrapper.entryCreate( + val kpEntry = KeepassWrapper.entryCreate( this, binding.editTextTitle.text.toString(), binding.editTextCode.text.toString(), @@ -77,8 +77,8 @@ class CreateEntry : Fragment() { try { resultLauncherAdd.launch( Kp2aControl.getAddEntryIntent( - kpentry.first, - kpentry.second + kpEntry.first, + kpEntry.second ) ) } catch (e: ActivityNotFoundException) { @@ -108,23 +108,21 @@ class CreateEntry : Fragment() { binding.editTextCode.error = e.message } catch (e: Exception) { binding.imageViewPreview.setImageBitmap(null) - println(e.javaClass) - println(e.message) e.printStackTrace() } } private fun isValid(): Boolean { var valid = true - if (binding.editTextTitle.text!!.isEmpty()) { + if (binding.editTextTitle.text.isNullOrEmpty()) { valid = false binding.editTextTitle.error = "Title cannot be empty" } - if (binding.editTextCode.text!!.isEmpty()) { + if (binding.editTextCode.text.isNullOrEmpty()) { valid = false binding.editTextCode.error = "Code cannot be empty" } - if (binding.editTextFormat.text!!.isEmpty()) { + if (binding.editTextFormat.text.isNullOrEmpty()) { valid = false binding.editTextFormat.error = "Format cannot be empty" } diff --git a/app/src/main/java/net/helcel/fidelity/activity/fragment/Scanner.kt b/app/src/main/java/net/helcel/fidelity/activity/fragment/Scanner.kt index 925ae3a..9b8fdfa 100644 --- a/app/src/main/java/net/helcel/fidelity/activity/fragment/Scanner.kt +++ b/app/src/main/java/net/helcel/fidelity/activity/fragment/Scanner.kt @@ -27,7 +27,6 @@ class Scanner : Fragment() { private var code: String = "" private var fmt: String = "" - private var valid: Boolean = false override fun onCreateView( inflater: LayoutInflater, @@ -35,13 +34,14 @@ class Scanner : Fragment() { savedInstanceState: Bundle? ): View { binding = FragScannerBinding.inflate(layoutInflater) - binding.bottomText.setOnClickListener { + binding.btnScanDone.setOnClickListener { startCreateEntry() } when (hasCameraPermission()) { true -> bindCameraUseCases() else -> requestPermission() } + binding.btnScanDone.isEnabled = false return binding.root } @@ -92,9 +92,10 @@ class Scanner : Fragment() { if (code != null && format != null) { this.code = code this.fmt = format - this.valid = true + binding.btnScanDone.isEnabled = true + } else { - this.valid = false + binding.btnScanDone.isEnabled = false } } try { @@ -111,6 +112,4 @@ class Scanner : Fragment() { } }, ContextCompat.getMainExecutor(requireContext())) } - - } \ No newline at end of file diff --git a/app/src/main/java/net/helcel/fidelity/activity/fragment/ViewEntry.kt b/app/src/main/java/net/helcel/fidelity/activity/fragment/ViewEntry.kt index 9659f4a..8d52cb6 100644 --- a/app/src/main/java/net/helcel/fidelity/activity/fragment/ViewEntry.kt +++ b/app/src/main/java/net/helcel/fidelity/activity/fragment/ViewEntry.kt @@ -1,10 +1,14 @@ package net.helcel.fidelity.activity.fragment +import android.annotation.SuppressLint +import android.content.pm.ActivityInfo import android.content.res.Configuration import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup +import android.view.WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_FULL +import android.view.WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE import androidx.fragment.app.Fragment import com.google.zxing.FormatException import net.helcel.fidelity.databinding.FragViewEntryBinding @@ -12,16 +16,14 @@ import net.helcel.fidelity.tools.BarcodeGenerator.generateBarcode import net.helcel.fidelity.tools.ErrorToaster import net.helcel.fidelity.tools.KeepassWrapper - +@SuppressLint("SourceLockedOrientationActivity") class ViewEntry : Fragment() { private lateinit var binding: FragViewEntryBinding - private var title: String? = null private var code: String? = null private var fmt: String? = null - override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, @@ -33,8 +35,15 @@ class ViewEntry : Fragment() { code = res.second fmt = res.third - adjustLayout() updatePreview() + updateLayout() + + binding.imageViewPreview.setOnClickListener { + requireActivity().requestedOrientation = + if (isLandscape()) ActivityInfo.SCREEN_ORIENTATION_PORTRAIT + else ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE + } + return binding.root } @@ -42,7 +51,7 @@ class ViewEntry : Fragment() { binding.title.text = title try { val barcodeBitmap = generateBarcode( - code!!, fmt!!, 1024 + code, fmt, 1024 ) binding.imageViewPreview.setImageBitmap(barcodeBitmap) } catch (e: FormatException) { @@ -53,23 +62,25 @@ class ViewEntry : Fragment() { ErrorToaster.invalidFormat(requireActivity()) } catch (e: Exception) { binding.imageViewPreview.setImageBitmap(null) - println(e.javaClass) - println(e.message) e.printStackTrace() } } - - override fun onConfigurationChanged(newConfig: Configuration) { - super.onConfigurationChanged(newConfig) - adjustLayout() - } - - private fun adjustLayout() { - if (resources.configuration.orientation == Configuration.ORIENTATION_LANDSCAPE) { + private fun updateLayout() { + if (isLandscape()) { binding.title.visibility = View.GONE + setScreenBrightness(BRIGHTNESS_OVERRIDE_FULL) } else { binding.title.visibility = View.VISIBLE + setScreenBrightness(BRIGHTNESS_OVERRIDE_NONE) } } + + private fun isLandscape(): Boolean { + return (resources.configuration.orientation == Configuration.ORIENTATION_LANDSCAPE) + } + + private fun setScreenBrightness(brightness: Float?) { + requireActivity().window?.attributes?.screenBrightness = brightness + } } \ No newline at end of file diff --git a/app/src/main/java/net/helcel/fidelity/activity/view/ScannerView.kt b/app/src/main/java/net/helcel/fidelity/activity/view/ScannerView.kt new file mode 100644 index 0000000..7a5399b --- /dev/null +++ b/app/src/main/java/net/helcel/fidelity/activity/view/ScannerView.kt @@ -0,0 +1,45 @@ +package net.helcel.fidelity.activity.view + +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.util.AttributeSet +import android.view.View + +class ScannerView : View { + + private val overlayPaint = Paint().apply { + color = Color.parseColor("#80000000") // Semi-transparent black + style = Paint.Style.FILL + } + + private val clearPaint = Paint().apply { + xfermode = PorterDuffXfermode(PorterDuff.Mode.CLEAR) + } + + constructor(context: Context) : super(context) + constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) + constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super( + context, + attrs, + defStyleAttr + ) + + override fun onDraw(canvas: Canvas) { + super.onDraw(canvas) + + canvas.drawRect(0f, 0f, width.toFloat(), height.toFloat(), overlayPaint) + + val centerX = width / 2f + val centerY = height / 2f + val squareSize = 0.75f * width.coerceAtMost(height) + canvas.drawRect( + centerX - squareSize / 2, centerY - squareSize / 2, + centerX + squareSize / 2, centerY + squareSize / 2, clearPaint + ) + } +} + diff --git a/app/src/main/java/net/helcel/fidelity/tools/BacodeScanner.kt b/app/src/main/java/net/helcel/fidelity/tools/BacodeScanner.kt index 28803b3..bc24ef2 100644 --- a/app/src/main/java/net/helcel/fidelity/tools/BacodeScanner.kt +++ b/app/src/main/java/net/helcel/fidelity/tools/BacodeScanner.kt @@ -15,9 +15,9 @@ import net.helcel.fidelity.tools.BarcodeFormatConverter.formatToString import java.util.concurrent.Executors -@OptIn(ExperimentalGetImage::class) object BarcodeScanner { + @OptIn(ExperimentalGetImage::class) private fun processImageProxy( barcodeScanner: BarcodeScanner, imageProxy: ImageProxy, @@ -33,8 +33,6 @@ object BarcodeScanner { barcodeScanner.process(inputImage) .addOnSuccessListener { barcodeList -> - println(barcodeList.map { e -> e.displayValue }) - println(barcodeList.map { e -> e.format }) val barcode = barcodeList.getOrNull(0) if (barcode != null) diff --git a/app/src/main/java/net/helcel/fidelity/tools/BarcodeFormatConverter.kt b/app/src/main/java/net/helcel/fidelity/tools/BarcodeFormatConverter.kt index 03df33a..0d00e08 100644 --- a/app/src/main/java/net/helcel/fidelity/tools/BarcodeFormatConverter.kt +++ b/app/src/main/java/net/helcel/fidelity/tools/BarcodeFormatConverter.kt @@ -20,7 +20,6 @@ object BarcodeFormatConverter { } } - fun formatToString(f: Int): String { return when (f) { Barcode.FORMAT_CODE_128 -> "CODE_128" diff --git a/app/src/main/java/net/helcel/fidelity/tools/BarcodeGenerator.kt b/app/src/main/java/net/helcel/fidelity/tools/BarcodeGenerator.kt index d74aa80..15e9f77 100644 --- a/app/src/main/java/net/helcel/fidelity/tools/BarcodeGenerator.kt +++ b/app/src/main/java/net/helcel/fidelity/tools/BarcodeGenerator.kt @@ -19,8 +19,8 @@ object BarcodeGenerator { android.graphics.Color.WHITE } - fun generateBarcode(content: String, f: String, width: Int): Bitmap? { - if (content.isEmpty() || f.isEmpty()) { + fun generateBarcode(content: String?, f: String?, width: Int): Bitmap? { + if (content.isNullOrEmpty() || f.isNullOrEmpty()) { return null } try { @@ -33,11 +33,8 @@ object BarcodeGenerator { for (x in 0 until width) { for (y in 0 until height) { bitmap.setPixel( - x, - y, - getPixelColor(bitMatrix, x, y) + x, y, getPixelColor(bitMatrix, x, y) ) - } } return bitmap diff --git a/app/src/main/java/net/helcel/fidelity/tools/KeepassWrapper.kt b/app/src/main/java/net/helcel/fidelity/tools/KeepassWrapper.kt index 08f7b9d..0d799e3 100644 --- a/app/src/main/java/net/helcel/fidelity/tools/KeepassWrapper.kt +++ b/app/src/main/java/net/helcel/fidelity/tools/KeepassWrapper.kt @@ -42,12 +42,11 @@ object KeepassWrapper { callback: (HashMap) -> Unit ): ActivityResultLauncher { return fragment.registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result -> + + println(result.toString()) if (result.resultCode == Activity.RESULT_OK) { - val data: Intent? = result.data - val credentials = Kp2aControl.getEntryFieldsFromIntent( - data!! - ) - println(credentials) + val credentials = Kp2aControl.getEntryFieldsFromIntent(result.data) + println(credentials.toList().toString()) callback(credentials) } } @@ -59,11 +58,8 @@ object KeepassWrapper { ): ActivityResultLauncher { return fragment.registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result -> if (result.resultCode == Activity.RESULT_OK) { - val data: Intent? = result.data - val credentials = Kp2aControl.getEntryFieldsFromIntent( - data!! - ) - println(credentials) + val credentials = Kp2aControl.getEntryFieldsFromIntent(result.data) + println(credentials.toList().toString()) callback(credentials) } } diff --git a/app/src/main/res/drawable/heart.xml b/app/src/main/res/drawable/heart.xml deleted file mode 100644 index 9a4cca7..0000000 --- a/app/src/main/res/drawable/heart.xml +++ /dev/null @@ -1,21 +0,0 @@ - - - - - - - diff --git a/app/src/main/res/drawable/key.xml b/app/src/main/res/drawable/key.xml deleted file mode 100644 index 0b51384..0000000 --- a/app/src/main/res/drawable/key.xml +++ /dev/null @@ -1,31 +0,0 @@ - - - - - - - diff --git a/app/src/main/res/drawable/locked.xml b/app/src/main/res/drawable/locked.xml deleted file mode 100644 index 21f1db9..0000000 --- a/app/src/main/res/drawable/locked.xml +++ /dev/null @@ -1,31 +0,0 @@ - - - - - - - diff --git a/app/src/main/res/layout/act_main.xml b/app/src/main/res/layout/act_main.xml index 7b75a3e..476a412 100644 --- a/app/src/main/res/layout/act_main.xml +++ b/app/src/main/res/layout/act_main.xml @@ -1,8 +1,6 @@ - + android:layout_height="match_parent" + android:layout_margin="24dp" /> - + + + android:layout_centerHorizontal="true" + android:layout_margin="24dp" + android:contentDescription="@string/manual" /> + + + \ No newline at end of file diff --git a/app/src/main/res/layout/list_item_dropdown.xml b/app/src/main/res/layout/list_item_dropdown.xml index 606dcfa..56c46c3 100644 --- a/app/src/main/res/layout/list_item_dropdown.xml +++ b/app/src/main/res/layout/list_item_dropdown.xml @@ -1,7 +1,5 @@ - Fidelity - Stores and Displays fidelity and other cards - [soraefir](soraefir) + Fidelity adds an interface to manage fidelity cards and other barcodes to Keepass2Android + Soraefir Keepass Fidelity diff --git a/app/src/main/res/values/styles.xml b/app/src/main/res/values/styles.xml deleted file mode 100644 index f11f745..0000000 --- a/app/src/main/res/values/styles.xml +++ /dev/null @@ -1,3 +0,0 @@ - - - diff --git a/app/src/main/res/values/themes.xml b/app/src/main/res/values/themes.xml index 952b68d..e35cb51 100644 --- a/app/src/main/res/values/themes.xml +++ b/app/src/main/res/values/themes.xml @@ -1,9 +1,12 @@ - \ No newline at end of file diff --git a/gradle.properties b/gradle.properties index 1a982b5..d7d6ba3 100644 --- a/gradle.properties +++ b/gradle.properties @@ -15,7 +15,7 @@ org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8 # Android operating system, and which are packaged with your app's APK # https://developer.android.com/topic/libraries/support-library/androidx-rn android.useAndroidX=true -android.enableJetifier=true +android.enableJetifier=false # Kotlin code style for this project: "official" or "obsolete": kotlin.code.style=official # Enables namespacing of each library's R class so that its R class includes only the