Skip to content

Commit

Permalink
Support opening supported barcode files directly
Browse files Browse the repository at this point in the history
  • Loading branch information
TheLastProject committed Oct 28, 2024
1 parent 10bfebb commit 6eaf23e
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 18 deletions.
13 changes: 12 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,20 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND" />
<action android:name="android.intent.action.VIEW" />

<category android:name="android.intent.category.DEFAULT" />

<data android:scheme="content"/>
<data android:host="*"/>
<data android:mimeType="image/*" />
<data android:mimeType="application/pdf" />
<data android:mimeType="application/vnd.apple.pkpass" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />

<data android:mimeType="text/plain" />
<data android:mimeType="image/*" />
<data android:mimeType="application/pdf" />
Expand Down
51 changes: 34 additions & 17 deletions app/src/main/java/protect/card_locker/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<ParseResult> 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<ParseResult> 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) {
Expand Down

0 comments on commit 6eaf23e

Please sign in to comment.