Skip to content

Add copy barcode on long press barcode & description #2445

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions app/src/main/java/protect/card_locker/LoyaltyCardViewActivity.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package protect.card_locker;

import android.content.ActivityNotFoundException;
import android.content.ClipboardManager;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.ColorStateList;
Expand Down Expand Up @@ -146,6 +147,21 @@ public void onMainImageTap() {
openImageInGallery(imageType);
}

private boolean copyBarcodeToClipBoard(){
if (imageTypes.get(mainImageIndex) == ImageType.BARCODE) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to crash if the card has "Barcode Type" set to "No barcode" and has no photo too.

I was about to suggest using the mainImageIndex != 0 check from binding.mainImageDescription.setOnClickListener but I just realized that that check is wrong too (it fails if there is no barcode but an image).

Maybe, given that field does not currently do anything, and copying the text "Front image" or "Back image" is useless... maybe just remove this check altogether and always copy the barcode on long-press?

String barcodeString = barcodeIdString != null ? barcodeIdString : cardIdString;
ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
if (clipboard != null) {
android.content.ClipData clip = android.content.ClipData.newPlainText("Barcode", barcodeString);
Comment on lines +152 to +155
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think most users want to copy the cardId, not the barcodeId.

CardID = the value written on the physical card (mostly the customer number)
BarcodeID = whatever is in the barcode itself (which may contain random gibberish)

The cardId is also what's written in the dialog window.

Suggested change
String barcodeString = barcodeIdString != null ? barcodeIdString : cardIdString;
ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
if (clipboard != null) {
android.content.ClipData clip = android.content.ClipData.newPlainText("Barcode", barcodeString);
ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
if (clipboard != null) {
android.content.ClipData clip = android.content.ClipData.newPlainText("Card ID", loyaltyCard.cardId);

clipboard.setPrimaryClip(clip);
Toast.makeText(this, R.string.copy_success, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, R.string.copy_failed, Toast.LENGTH_SHORT).show();
}
}
return true;
}

private void openImageInGallery(ImageType imageType) {
File file = null;

Expand Down Expand Up @@ -350,12 +366,16 @@ public void onStopTrackingTouch(SeekBar seekBar) {
});

binding.mainImage.setOnClickListener(view -> onMainImageTap());

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe I'm weird, but I quite like the setOnClickListener and setOnLongClickListener of the same object being directly below each other :)

Suggested change

// This long-press was originally only intended for when Talkback was used but sadly limiting
// this doesn't seem to work well
binding.mainImage.setOnLongClickListener(view -> {
setMainImage(true, true);
return true;
});

binding.mainImageDescription.setOnLongClickListener(view -> copyBarcodeToClipBoard());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this would be better to move directly below binding.mainImageDescription.setOnClickListener so everything of binding.mainImageDescription is in one place.


binding.fullscreenImage.setOnClickListener(view -> onMainImageTap());

getOnBackPressedDispatcher().addCallback(this, new OnBackPressedCallback(true) {
Expand Down Expand Up @@ -698,6 +718,7 @@ protected void onResume() {
builder.setTitle(R.string.cardId);
builder.setView(cardIdView);
builder.setPositiveButton(R.string.ok, (dialogInterface, i) -> dialogInterface.dismiss());
builder.setNeutralButton(R.string.copy, (dialogInterface, i) -> copyBarcodeToClipBoard());
AlertDialog dialog = builder.create();
dialog.show();
});
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -361,4 +361,7 @@
<string name="addFromPkpass">Select a Passbook file (.pkpass)</string>
<string name="unsupportedFile">This file is not supported</string>
<string name="generic_error_please_retry">Sorry, something went wrong, please try again...</string>
<string name="copy">Copy</string>
<string name="copy_success">Copied to clipboard</string>
<string name="copy_failed">Failed to copy to clipboard</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
Expand Down Expand Up @@ -1389,4 +1391,51 @@ public void importCardOldFormat() {
checkAllFields(activity, ViewMode.ADD_CARD, "Example Store", "", context.getString(R.string.anyDate), context.getString(R.string.never), "0", context.getString(R.string.points), "123456", context.getString(R.string.sameAsCardId), "Aztec", null, null);
assertEquals(-416706, ((ColorDrawable) activity.findViewById(R.id.thumbnail).getBackground()).getColor());
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change


@Test
public void longPressOnBarcodeShouldCopyTheBarcodeValue() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test name isn't completely accurate as it tests both the long and short click flows :)

Suggested change
public void longPressOnBarcodeShouldCopyTheBarcodeValue() {
public void testCopyBarcodeValue() {

final Context context = ApplicationProvider.getApplicationContext();
SQLiteDatabase database = TestHelpers.getEmptyDb(context).getWritableDatabase();

long cardId = DBHelper.insertLoyaltyCard(database, "store", "note", null, null, new BigDecimal("0"), null, BARCODE_DATA, null, BARCODE_TYPE, Color.BLACK, 0, null, 0);

ActivityController activityController = createActivityWithLoyaltyCard(false, (int) cardId);
Activity activity = (Activity) activityController.get();

activityController.start();
activityController.visible();
activityController.resume();

// Short press on description to open the modal
TextView barcodeTextView = activity.findViewById(R.id.main_image_description);
barcodeTextView.performClick();
shadowOf(getMainLooper()).idle();

// click on the copy neutral button
AlertDialog barcodeDialog = (AlertDialog) (ShadowDialog.getLatestDialog());
assertNotNull(barcodeDialog);
barcodeDialog.getButton(AlertDialog.BUTTON_NEUTRAL).performClick();
shadowOf(getMainLooper()).idle();

// Check if the barcode value is copied to the clipboard
ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clipData = clipboard.getPrimaryClip();
assertNotNull(clipData);
assertEquals(BARCODE_DATA, clipData.getItemAt(0).getText().toString());

//clear the clipboard
clipboard.setPrimaryClip(ClipData.newPlainText("", ""));

//quit the dialog
barcodeDialog.dismiss();

// Long press on the barcode description should copy the barcode value
barcodeTextView.performLongClick();
shadowOf(getMainLooper()).idle();
// Check if the barcode value is copied to the clipboard
clipData = clipboard.getPrimaryClip();
assertNotNull(clipData);
assertEquals(BARCODE_DATA, clipData.getItemAt(0).getText().toString());
}
}