Skip to content

Commit 72fb68f

Browse files
Support opening supported barcode files directly
1 parent 1740b44 commit 72fb68f

File tree

2 files changed

+46
-18
lines changed

2 files changed

+46
-18
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,20 @@
4040
<category android:name="android.intent.category.LAUNCHER" />
4141
</intent-filter>
4242
<intent-filter>
43-
<action android:name="android.intent.action.SEND" />
43+
<action android:name="android.intent.action.VIEW" />
4444

4545
<category android:name="android.intent.category.DEFAULT" />
46+
47+
<data android:scheme="content"/>
48+
<data android:host="*"/>
49+
<data android:mimeType="image/*" />
50+
<data android:mimeType="application/pdf" />
51+
<data android:mimeType="application/vnd.apple.pkpass" />
52+
</intent-filter>
53+
<intent-filter>
54+
<action android:name="android.intent.action.SEND" />
55+
<category android:name="android.intent.category.DEFAULT" />
56+
4657
<data android:mimeType="text/plain" />
4758
<data android:mimeType="image/*" />
4859
<data android:mimeType="application/pdf" />

app/src/main/java/protect/card_locker/MainActivity.java

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import android.content.SharedPreferences;
88
import android.database.CursorIndexOutOfBoundsException;
99
import android.database.sqlite.SQLiteDatabase;
10+
import android.net.Uri;
1011
import android.os.Build;
1112
import android.os.Bundle;
1213
import android.util.DisplayMetrics;
@@ -445,32 +446,48 @@ private void onSharedIntent(Intent intent) {
445446
String receivedAction = intent.getAction();
446447
String receivedType = intent.getType();
447448

448-
// Check if an image or file was shared to us
449-
if (Intent.ACTION_SEND.equals(receivedAction)) {
450-
List<ParseResult> parseResultList;
449+
if (receivedAction == null || receivedType == null) {
450+
return;
451+
}
451452

452-
if (receivedType.equals("text/plain")) {
453-
LoyaltyCard loyaltyCard = new LoyaltyCard();
454-
loyaltyCard.setCardId(intent.getStringExtra(Intent.EXTRA_TEXT));
455-
parseResultList = Collections.singletonList(new ParseResult(ParseResultType.BARCODE_ONLY, loyaltyCard));
456-
} else if (receivedType.startsWith("image/")) {
457-
parseResultList = Utils.retrieveBarcodesFromImage(this, intent.getParcelableExtra(Intent.EXTRA_STREAM));
458-
} else if (receivedType.equals("application/pdf")) {
459-
parseResultList = Utils.retrieveBarcodesFromPdf(this, intent.getParcelableExtra(Intent.EXTRA_STREAM));
460-
} else if (receivedType.equals("application/vnd.apple.pkpass")) {
461-
parseResultList = Utils.retrieveBarcodesFromPkPass(this, intent.getParcelableExtra(Intent.EXTRA_STREAM));
453+
List<ParseResult> parseResultList;
454+
455+
// Check for shared text
456+
if (receivedAction.equals(Intent.ACTION_SEND) && receivedType.equals("text/plain")) {
457+
LoyaltyCard loyaltyCard = new LoyaltyCard();
458+
loyaltyCard.setCardId(intent.getStringExtra(Intent.EXTRA_TEXT));
459+
parseResultList = Collections.singletonList(new ParseResult(ParseResultType.BARCODE_ONLY, loyaltyCard));
460+
} else {
461+
// Parse whatever file was sent, regardless of opening or sharing
462+
Uri data;
463+
if (receivedAction.equals(Intent.ACTION_VIEW)) {
464+
data = intent.getData();
465+
} else if (receivedAction.equals(Intent.ACTION_SEND)) {
466+
data = intent.getParcelableExtra(Intent.EXTRA_STREAM);
462467
} else {
463-
Log.e(TAG, "Wrong mime-type");
468+
Log.e(TAG, "Wrong action type to parse intent");
464469
return;
465470
}
466471

467-
if (parseResultList.isEmpty()) {
468-
finish();
472+
if (receivedType.startsWith("image/")) {
473+
parseResultList = Utils.retrieveBarcodesFromImage(this, data);
474+
} else if (receivedType.equals("application/pdf")) {
475+
parseResultList = Utils.retrieveBarcodesFromPdf(this, data);
476+
} else if (receivedType.equals("application/vnd.apple.pkpass")) {
477+
parseResultList = Utils.retrieveBarcodesFromPkPass(this, data);
478+
} else {
479+
Log.e(TAG, "Wrong mime-type");
469480
return;
470481
}
482+
}
471483

472-
processParseResultList(parseResultList, null, true);
484+
// Give up if we should parse but there is nothing to parse
485+
if (parseResultList == null || parseResultList.isEmpty()) {
486+
finish();
487+
return;
473488
}
489+
490+
processParseResultList(parseResultList, null, true);
474491
}
475492

476493
private void extractIntentFields(Intent intent) {

0 commit comments

Comments
 (0)