From 562d9d1fdc8d73f74f6fff00a7865d3be636dc88 Mon Sep 17 00:00:00 2001 From: Sylvia van Os Date: Wed, 16 Oct 2024 20:15:37 +0200 Subject: [PATCH] Support opening supported barcode files directly --- app/src/main/AndroidManifest.xml | 13 ++++- .../protect/card_locker/MainActivity.java | 51 ++++++++++++------- 2 files changed, 46 insertions(+), 18 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index c5482044e..760f148b1 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -40,9 +40,20 @@ - + + + + + + + + + + + + diff --git a/app/src/main/java/protect/card_locker/MainActivity.java b/app/src/main/java/protect/card_locker/MainActivity.java index 4020ef495..b8c96d9a8 100644 --- a/app/src/main/java/protect/card_locker/MainActivity.java +++ b/app/src/main/java/protect/card_locker/MainActivity.java @@ -7,6 +7,7 @@ import android.content.SharedPreferences; import android.database.CursorIndexOutOfBoundsException; import android.database.sqlite.SQLiteDatabase; +import android.net.Uri; import android.os.Build; import android.os.Bundle; import android.util.DisplayMetrics; @@ -445,32 +446,48 @@ private void onSharedIntent(Intent intent) { String receivedAction = intent.getAction(); String receivedType = intent.getType(); - // Check if an image or file was shared to us - if (Intent.ACTION_SEND.equals(receivedAction)) { - List parseResultList; + if (receivedAction == null || receivedType == null) { + return; + } - if (receivedType.equals("text/plain")) { - LoyaltyCard loyaltyCard = new LoyaltyCard(); - loyaltyCard.setCardId(intent.getStringExtra(Intent.EXTRA_TEXT)); - parseResultList = Collections.singletonList(new ParseResult(ParseResultType.BARCODE_ONLY, loyaltyCard)); - } else if (receivedType.startsWith("image/")) { - parseResultList = Utils.retrieveBarcodesFromImage(this, intent.getParcelableExtra(Intent.EXTRA_STREAM)); - } else if (receivedType.equals("application/pdf")) { - parseResultList = Utils.retrieveBarcodesFromPdf(this, intent.getParcelableExtra(Intent.EXTRA_STREAM)); - } else if (receivedType.equals("application/vnd.apple.pkpass")) { - parseResultList = Utils.retrieveBarcodesFromPkPass(this, intent.getParcelableExtra(Intent.EXTRA_STREAM)); + List parseResultList; + + // Check for shared text + if (receivedAction.equals(Intent.ACTION_SEND) && receivedType.equals("text/plain")) { + LoyaltyCard loyaltyCard = new LoyaltyCard(); + loyaltyCard.setCardId(intent.getStringExtra(Intent.EXTRA_TEXT)); + parseResultList = Collections.singletonList(new ParseResult(ParseResultType.BARCODE_ONLY, loyaltyCard)); + } else { + // Parse whatever file was sent, regardless of opening or sharing + Uri data; + if (receivedAction.equals(Intent.ACTION_VIEW)) { + data = intent.getData(); + } else if (receivedAction.equals(Intent.ACTION_SEND)) { + data = intent.getParcelableExtra(Intent.EXTRA_STREAM); } else { - Log.e(TAG, "Wrong mime-type"); + Log.e(TAG, "Wrong action type to parse intent"); return; } - if (parseResultList.isEmpty()) { - finish(); + if (receivedType.startsWith("image/")) { + parseResultList = Utils.retrieveBarcodesFromImage(this, data); + } else if (receivedType.equals("application/pdf")) { + parseResultList = Utils.retrieveBarcodesFromPdf(this, data); + } else if (receivedType.equals("application/vnd.apple.pkpass")) { + parseResultList = Utils.retrieveBarcodesFromPkPass(this, data); + } else { + Log.e(TAG, "Wrong mime-type"); return; } + } - processParseResultList(parseResultList, null, true); + // Give up if we should parse but there is nothing to parse + if (parseResultList == null || parseResultList.isEmpty()) { + finish(); + return; } + + processParseResultList(parseResultList, null, true); } private void extractIntentFields(Intent intent) {