From 99a8c917b9fcb0da778eb77a9453348a332bd36c Mon Sep 17 00:00:00 2001 From: StellarSand Date: Mon, 28 Jul 2025 02:47:44 +0530 Subject: [PATCH 1/5] Rename .java to .kt --- .../{CardShortcutConfigure.java => CardShortcutConfigure.kt} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename app/src/main/java/protect/card_locker/{CardShortcutConfigure.java => CardShortcutConfigure.kt} (100%) diff --git a/app/src/main/java/protect/card_locker/CardShortcutConfigure.java b/app/src/main/java/protect/card_locker/CardShortcutConfigure.kt similarity index 100% rename from app/src/main/java/protect/card_locker/CardShortcutConfigure.java rename to app/src/main/java/protect/card_locker/CardShortcutConfigure.kt From aed145239bde97af0849205262083908d4c6f90b Mon Sep 17 00:00:00 2001 From: StellarSand Date: Mon, 28 Jul 2025 02:47:44 +0530 Subject: [PATCH 2/5] convert CardShortcutConfigure to kotlin --- .../card_locker/CardShortcutConfigure.kt | 187 ++++++++---------- 1 file changed, 86 insertions(+), 101 deletions(-) diff --git a/app/src/main/java/protect/card_locker/CardShortcutConfigure.kt b/app/src/main/java/protect/card_locker/CardShortcutConfigure.kt index 2d7a643b8..ebef1778e 100644 --- a/app/src/main/java/protect/card_locker/CardShortcutConfigure.kt +++ b/app/src/main/java/protect/card_locker/CardShortcutConfigure.kt @@ -1,111 +1,96 @@ -package protect.card_locker; +package protect.card_locker -import android.database.Cursor; -import android.database.sqlite.SQLiteDatabase; -import android.os.Bundle; -import android.util.Log; -import android.view.Menu; -import android.view.MenuItem; -import android.widget.Toast; - -import androidx.appcompat.widget.Toolbar; -import androidx.core.content.pm.ShortcutInfoCompat; -import androidx.core.content.pm.ShortcutManagerCompat; -import androidx.recyclerview.widget.GridLayoutManager; - -import protect.card_locker.databinding.CardShortcutConfigureActivityBinding; -import protect.card_locker.preferences.Settings; - -/** - * The configuration screen for creating a shortcut. - */ -public class CardShortcutConfigure extends CatimaAppCompatActivity implements LoyaltyCardCursorAdapter.CardAdapterListener { - private CardShortcutConfigureActivityBinding binding; - static final String TAG = "Catima"; - private SQLiteDatabase mDatabase; - private LoyaltyCardCursorAdapter mAdapter; - - @Override - public void onCreate(Bundle bundle) { - super.onCreate(bundle); - binding = CardShortcutConfigureActivityBinding.inflate(getLayoutInflater()); - mDatabase = new DBHelper(this).getReadableDatabase(); - - // Set the result to CANCELED. This will cause nothing to happen if the - // aback button is pressed. - setResult(RESULT_CANCELED); - - setContentView(binding.getRoot()); - Utils.applyWindowInsets(binding.getRoot()); - Toolbar toolbar = binding.toolbar; - toolbar.setTitle(R.string.shortcutSelectCard); - setSupportActionBar(toolbar); +import android.database.sqlite.SQLiteDatabase +import android.os.Bundle +import android.util.Log +import android.view.Menu +import android.view.MenuInflater +import android.view.MenuItem +import android.widget.Toast +import androidx.core.content.pm.ShortcutManagerCompat +import androidx.core.view.MenuProvider +import androidx.recyclerview.widget.GridLayoutManager +import protect.card_locker.LoyaltyCardCursorAdapter.CardAdapterListener +import protect.card_locker.databinding.CardShortcutConfigureActivityBinding +import protect.card_locker.preferences.Settings +class CardShortcutConfigure : CatimaAppCompatActivity(), CardAdapterListener, MenuProvider { + + private lateinit var binding: CardShortcutConfigureActivityBinding + private lateinit var mDatabase: SQLiteDatabase + private lateinit var mAdapter: LoyaltyCardCursorAdapter + + companion object { + const val TAG: String = "Catima" + } + + public override fun onCreate(savedInstanceBundle: Bundle?) { + super.onCreate(savedInstanceBundle) + addMenuProvider(this) + binding = CardShortcutConfigureActivityBinding.inflate(layoutInflater) + mDatabase = DBHelper(this).readableDatabase + + // Set the result to CANCELED. + // This will cause nothing to happen if the back button is pressed. + setResult(RESULT_CANCELED) + + setContentView(binding.getRoot()) + Utils.applyWindowInsets(binding.getRoot()) + + binding.toolbar.apply { + setTitle(R.string.shortcutSelectCard) + setSupportActionBar(this) + } + // If there are no cards, bail - int cardCount = DBHelper.getLoyaltyCardCount(mDatabase); - if (cardCount == 0) { - Toast.makeText(this, R.string.noCardsMessage, Toast.LENGTH_LONG).show(); - finish(); + if (DBHelper.getLoyaltyCardCount(mDatabase) == 0) { + Toast.makeText(this, R.string.noCardsMessage, Toast.LENGTH_LONG).show() + finish() } - - Cursor cardCursor = DBHelper.getLoyaltyCardCursor(mDatabase, DBHelper.LoyaltyCardArchiveFilter.All); - mAdapter = new LoyaltyCardCursorAdapter(this, cardCursor, this, null); - binding.list.setAdapter(mAdapter); + + val cardCursor = DBHelper.getLoyaltyCardCursor(mDatabase, DBHelper.LoyaltyCardArchiveFilter.All) + mAdapter = LoyaltyCardCursorAdapter(this, cardCursor, this, null) + binding.list.setAdapter(mAdapter) } - - @Override - protected void onResume() { - super.onResume(); - - var layoutManager = (GridLayoutManager) binding.list.getLayoutManager(); - if (layoutManager != null) { - var settings = new Settings(this); - layoutManager.setSpanCount(settings.getPreferredColumnCount()); + + override fun onResume() { + super.onResume() + + val layoutManager = binding.list.layoutManager as GridLayoutManager? + layoutManager?.setSpanCount(Settings(this).getPreferredColumnCount()) + } + + private fun onClickAction(position: Int) { + val selected = DBHelper.getLoyaltyCardCursor(mDatabase, DBHelper.LoyaltyCardArchiveFilter.All) + selected.moveToPosition(position) + val loyaltyCard = LoyaltyCard.fromCursor(this@CardShortcutConfigure, selected) + + Log.d(TAG, "Creating shortcut for card " + loyaltyCard.store + "," + loyaltyCard.id) + + val shortcut = ShortcutHelper.createShortcutBuilder(this@CardShortcutConfigure, loyaltyCard).build() + + setResult(RESULT_OK, + ShortcutManagerCompat.createShortcutResultIntent(this@CardShortcutConfigure, shortcut)) + + finish() + } + + override fun onCreateMenu(inputMenu: Menu, menuInflater: MenuInflater) { + menuInflater.inflate(R.menu.card_details_menu, inputMenu) + } + + override fun onMenuItemSelected(menuItem: MenuItem): Boolean { + if (menuItem.itemId == R.id.action_display_options) { + mAdapter.showDisplayOptionsDialog() } + return true } - - private void onClickAction(int position) { - Cursor selected = DBHelper.getLoyaltyCardCursor(mDatabase, DBHelper.LoyaltyCardArchiveFilter.All); - selected.moveToPosition(position); - LoyaltyCard loyaltyCard = LoyaltyCard.fromCursor(CardShortcutConfigure.this, selected); - - Log.d(TAG, "Creating shortcut for card " + loyaltyCard.store + "," + loyaltyCard.id); - - ShortcutInfoCompat shortcut = ShortcutHelper.createShortcutBuilder(CardShortcutConfigure.this, loyaltyCard).build(); - - setResult(RESULT_OK, ShortcutManagerCompat.createShortcutResultIntent(CardShortcutConfigure.this, shortcut)); - - finish(); + + override fun onRowClicked(inputPosition: Int) { + onClickAction(inputPosition) } - - @Override - public boolean onCreateOptionsMenu(Menu inputMenu) { - getMenuInflater().inflate(R.menu.card_details_menu, inputMenu); - - return super.onCreateOptionsMenu(inputMenu); - } - - @Override - public boolean onOptionsItemSelected(MenuItem inputItem) { - int id = inputItem.getItemId(); - - if (id == R.id.action_display_options) { - mAdapter.showDisplayOptionsDialog(); - invalidateOptionsMenu(); - - return true; - } - - return super.onOptionsItemSelected(inputItem); - } - - @Override - public void onRowClicked(int inputPosition) { - onClickAction(inputPosition); - } - - @Override - public void onRowLongClicked(int inputPosition) { + + override fun onRowLongClicked(inputPosition: Int) { // do nothing } -} +} \ No newline at end of file From 6f549813338fdd0b3c8f72c2b0c8d6f6bf1b34a5 Mon Sep 17 00:00:00 2001 From: StellarSand Date: Mon, 28 Jul 2025 02:47:58 +0530 Subject: [PATCH 3/5] Rename .java to .kt --- .../{BarcodeSelectorActivity.java => BarcodeSelectorActivity.kt} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename app/src/main/java/protect/card_locker/{BarcodeSelectorActivity.java => BarcodeSelectorActivity.kt} (100%) diff --git a/app/src/main/java/protect/card_locker/BarcodeSelectorActivity.java b/app/src/main/java/protect/card_locker/BarcodeSelectorActivity.kt similarity index 100% rename from app/src/main/java/protect/card_locker/BarcodeSelectorActivity.java rename to app/src/main/java/protect/card_locker/BarcodeSelectorActivity.kt From bf63390f655aa12af44b13682566d7be0656bed5 Mon Sep 17 00:00:00 2001 From: StellarSand Date: Mon, 28 Jul 2025 02:47:58 +0530 Subject: [PATCH 4/5] convert BarcodeSelectorActivity to kotlin --- .../card_locker/BarcodeSelectorActivity.kt | 210 +++++++++--------- 1 file changed, 101 insertions(+), 109 deletions(-) diff --git a/app/src/main/java/protect/card_locker/BarcodeSelectorActivity.kt b/app/src/main/java/protect/card_locker/BarcodeSelectorActivity.kt index 7023046cb..cb2fcf697 100644 --- a/app/src/main/java/protect/card_locker/BarcodeSelectorActivity.kt +++ b/app/src/main/java/protect/card_locker/BarcodeSelectorActivity.kt @@ -1,24 +1,21 @@ -package protect.card_locker; +package protect.card_locker -import android.app.Activity; -import android.content.Intent; -import android.os.Bundle; -import android.os.Handler; -import android.os.Looper; -import android.util.Log; -import android.view.MenuItem; -import android.view.View; -import android.widget.EditText; -import android.widget.ListView; -import android.widget.Toast; - -import androidx.appcompat.widget.Toolbar; - -import com.google.zxing.BarcodeFormat; - -import java.util.ArrayList; - -import protect.card_locker.databinding.BarcodeSelectorActivityBinding; +import android.content.Intent +import android.os.Bundle +import android.util.Log +import android.view.Menu +import android.view.MenuInflater +import android.view.MenuItem +import android.view.View +import android.widget.Toast +import androidx.core.view.MenuProvider +import androidx.core.widget.doOnTextChanged +import androidx.lifecycle.lifecycleScope +import kotlinx.coroutines.Job +import kotlinx.coroutines.delay +import kotlinx.coroutines.launch +import protect.card_locker.BarcodeSelectorAdapter.BarcodeSelectorListener +import protect.card_locker.databinding.BarcodeSelectorActivityBinding /** * This activity is callable and will allow a user to enter @@ -26,101 +23,96 @@ import protect.card_locker.databinding.BarcodeSelectorActivityBinding; * the data. The user may then select any barcode, where its * data and type will be returned to the caller. */ -public class BarcodeSelectorActivity extends CatimaAppCompatActivity implements BarcodeSelectorAdapter.BarcodeSelectorListener { - private BarcodeSelectorActivityBinding binding; - private static final String TAG = "Catima"; - - // Result this activity will return - public static final String BARCODE_CONTENTS = "contents"; - public static final String BARCODE_FORMAT = "format"; - - private final Handler typingDelayHandler = new Handler(Looper.getMainLooper()); - public static final Integer INPUT_DELAY = 250; - - private BarcodeSelectorAdapter mAdapter; - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - binding = BarcodeSelectorActivityBinding.inflate(getLayoutInflater()); - setTitle(R.string.selectBarcodeTitle); - setContentView(binding.getRoot()); - Utils.applyWindowInsets(binding.getRoot()); - Toolbar toolbar = binding.toolbar; - setSupportActionBar(toolbar); - enableToolbarBackButton(); - - EditText cardId = binding.cardId; - ListView mBarcodeList = binding.barcodes; - mAdapter = new BarcodeSelectorAdapter(this, new ArrayList<>(), this); - mBarcodeList.setAdapter(mAdapter); - - cardId.addTextChangedListener(new SimpleTextWatcher() { - @Override - public void onTextChanged(CharSequence s, int start, int before, int count) { - // Delay the input processing so we avoid overload - typingDelayHandler.removeCallbacksAndMessages(null); - - typingDelayHandler.postDelayed(() -> { - Log.d(TAG, "Entered text: " + s); - - runOnUiThread(() -> { - generateBarcodes(s.toString()); - }); - }, INPUT_DELAY); - } - }); - - final Bundle b = getIntent().getExtras(); - final String initialCardId = b != null ? b.getString(LoyaltyCard.BUNDLE_LOYALTY_CARD_CARD_ID) : null; - - if (initialCardId != null) { - cardId.setText(initialCardId); - } else { - generateBarcodes(""); - } +class BarcodeSelectorActivity : CatimaAppCompatActivity(), BarcodeSelectorListener, MenuProvider { + + private lateinit var binding: BarcodeSelectorActivityBinding + private lateinit var mAdapter: BarcodeSelectorAdapter + + companion object { + private const val TAG = "Catima" + + // Result this activity will return + const val BARCODE_CONTENTS = "contents" + const val BARCODE_FORMAT = "format" + + const val INPUT_DELAY = 250L } - - private void generateBarcodes(String value) { + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + addMenuProvider(this) + binding = BarcodeSelectorActivityBinding.inflate(layoutInflater) + setTitle(R.string.selectBarcodeTitle) + setContentView(binding.getRoot()) + Utils.applyWindowInsets(binding.getRoot()) + setSupportActionBar(binding.toolbar) + enableToolbarBackButton() + + var typingDelayJob: Job? = null + val cardId = binding.cardId + val mBarcodeList = binding.barcodes + mAdapter = BarcodeSelectorAdapter(this, ArrayList(), this) + mBarcodeList.adapter = mAdapter + + cardId.doOnTextChanged { s, _, _, _ -> + typingDelayJob?.cancel() + typingDelayJob = + lifecycleScope.launch { + delay(INPUT_DELAY) // Delay the input processing so we avoid overload + Log.d(TAG, "Entered text: $s") + generateBarcodes(s.toString()) + } + } + + val initialCardId = intent.extras?.getString(LoyaltyCard.BUNDLE_LOYALTY_CARD_CARD_ID) + + initialCardId?.let { + cardId.setText(initialCardId) + } ?: generateBarcodes("") + + } + + private fun generateBarcodes(value: String?) { // Update barcodes - ArrayList barcodes = new ArrayList<>(); - for (BarcodeFormat barcodeFormat : CatimaBarcode.barcodeFormats) { - CatimaBarcode catimaBarcode = CatimaBarcode.fromBarcode(barcodeFormat); - barcodes.add(new CatimaBarcodeWithValue(catimaBarcode, value)); + val barcodes = ArrayList() + CatimaBarcode.barcodeFormats.forEach { + val catimaBarcode = CatimaBarcode.fromBarcode(it) + barcodes.add(CatimaBarcodeWithValue(catimaBarcode, value)) } - mAdapter.setBarcodes(barcodes); + mAdapter.setBarcodes(barcodes) } - - @Override - public boolean onOptionsItemSelected(MenuItem item) { - if (item.getItemId() == android.R.id.home) { - setResult(Activity.RESULT_CANCELED); - finish(); - return true; + + override fun onCreateMenu(menu: Menu, menuInflater: MenuInflater) {} + + override fun onMenuItemSelected(menuItem: MenuItem): Boolean { + if (menuItem.itemId == android.R.id.home) { + setResult(RESULT_CANCELED) + finish() } - - return super.onOptionsItemSelected(item); + + return true } - - @Override - public void onRowClicked(int inputPosition, View view) { - CatimaBarcodeWithValue barcodeWithValue = mAdapter.getItem(inputPosition); - CatimaBarcode catimaBarcode = barcodeWithValue.catimaBarcode(); - + + override fun onRowClicked(inputPosition: Int, view: View) { + val barcodeWithValue = mAdapter.getItem(inputPosition) + val catimaBarcode = barcodeWithValue!!.catimaBarcode() + if (!mAdapter.isValid(view)) { - Toast.makeText(this, getString(R.string.wrongValueForBarcodeType), Toast.LENGTH_LONG).show(); - return; + Toast.makeText(this, getString(R.string.wrongValueForBarcodeType), Toast.LENGTH_LONG).show() + return } - - String barcodeFormat = catimaBarcode.format().name(); - String value = barcodeWithValue.value(); - - Log.d(TAG, "Selected barcode type " + barcodeFormat); - - Intent result = new Intent(); - result.putExtra(BARCODE_FORMAT, barcodeFormat); - result.putExtra(BARCODE_CONTENTS, value); - BarcodeSelectorActivity.this.setResult(RESULT_OK, result); - finish(); + + val barcodeFormat = catimaBarcode.format().name + val value = barcodeWithValue.value() + + Log.d(TAG, "Selected barcode type $barcodeFormat") + + Intent().apply { + putExtra(BARCODE_FORMAT, barcodeFormat) + putExtra(BARCODE_CONTENTS, value) + setResult(RESULT_OK, this) + } + finish() } -} + +} \ No newline at end of file From 96359e59425122d38868dfd293325024d3350e41 Mon Sep 17 00:00:00 2001 From: StellarSand Date: Mon, 28 Jul 2025 04:03:06 +0530 Subject: [PATCH 5/5] minor changes --- .../protect/card_locker/CardShortcutConfigure.kt | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/protect/card_locker/CardShortcutConfigure.kt b/app/src/main/java/protect/card_locker/CardShortcutConfigure.kt index ebef1778e..150a65d7a 100644 --- a/app/src/main/java/protect/card_locker/CardShortcutConfigure.kt +++ b/app/src/main/java/protect/card_locker/CardShortcutConfigure.kt @@ -20,8 +20,8 @@ class CardShortcutConfigure : CatimaAppCompatActivity(), CardAdapterListener, Me private lateinit var mDatabase: SQLiteDatabase private lateinit var mAdapter: LoyaltyCardCursorAdapter - companion object { - const val TAG: String = "Catima" + private companion object { + private const val TAG: String = "Catima" } public override fun onCreate(savedInstanceBundle: Bundle?) { @@ -63,14 +63,14 @@ class CardShortcutConfigure : CatimaAppCompatActivity(), CardAdapterListener, Me private fun onClickAction(position: Int) { val selected = DBHelper.getLoyaltyCardCursor(mDatabase, DBHelper.LoyaltyCardArchiveFilter.All) selected.moveToPosition(position) - val loyaltyCard = LoyaltyCard.fromCursor(this@CardShortcutConfigure, selected) + val loyaltyCard = LoyaltyCard.fromCursor(this, selected) - Log.d(TAG, "Creating shortcut for card " + loyaltyCard.store + "," + loyaltyCard.id) + Log.d(TAG, "Creating shortcut for card ${loyaltyCard.store}, ${loyaltyCard.id}") - val shortcut = ShortcutHelper.createShortcutBuilder(this@CardShortcutConfigure, loyaltyCard).build() + val shortcut = ShortcutHelper.createShortcutBuilder(this, loyaltyCard).build() setResult(RESULT_OK, - ShortcutManagerCompat.createShortcutResultIntent(this@CardShortcutConfigure, shortcut)) + ShortcutManagerCompat.createShortcutResultIntent(this, shortcut)) finish() }