diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index e27451079..bbda0aeef 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -33,6 +33,12 @@ + + + + + + { @@ -511,9 +495,66 @@ public class MainActivity extends CatimaAppCompatActivity implements LoyaltyCard } } + private void processBarcodeValues(BarcodeValues barcodeValues, String group) { + if (barcodeValues.isEmpty()) { + return; + } + Intent newIntent = new Intent(getApplicationContext(), LoyaltyCardEditActivity.class); + Bundle newBundle = new Bundle(); + newBundle.putString(LoyaltyCardEditActivity.BUNDLE_BARCODETYPE, barcodeValues.format()); + newBundle.putString(LoyaltyCardEditActivity.BUNDLE_CARDID, barcodeValues.content()); + if (group != null) { + newBundle.putString(LoyaltyCardEditActivity.BUNDLE_ADDGROUP, group); + } + newIntent.putExtras(newBundle); + startActivity(newIntent); + } + + private void onSharedIntent(Intent intent) { + String receivedAction = intent.getAction(); + String receivedType = intent.getType(); + + // Check if an image was shared to us + if (Intent.ACTION_SEND.equals(receivedAction)) { + if (receivedType.startsWith("image/")) { + BarcodeValues barcodeValues; + try { + Bitmap bitmap; + try { + Uri data = intent.getParcelableExtra(Intent.EXTRA_STREAM); + bitmap = Utils.retrieveImageFromUri(this, data); + } catch (IOException e) { + Log.e(TAG, "Error getting data from image file"); + e.printStackTrace(); + Toast.makeText(this, R.string.errorReadingImage, Toast.LENGTH_LONG).show(); + finish(); + return; + } + + barcodeValues = Utils.getBarcodeFromBitmap(bitmap); + + if (barcodeValues.isEmpty()) { + Log.i(TAG, "No barcode found in image file"); + Toast.makeText(this, R.string.noBarcodeFound, Toast.LENGTH_LONG).show(); + finish(); + return; + } + } catch (NullPointerException e) { + Toast.makeText(this, R.string.errorReadingImage, Toast.LENGTH_LONG).show(); + finish(); + return; + } + processBarcodeValues(barcodeValues, null); + } else { + Log.e(TAG, "Wrong mime-type"); + } + } + } + private void extractIntentFields(Intent intent) { final Bundle b = intent.getExtras(); mArchiveMode = b != null && b.getBoolean(BUNDLE_ARCHIVE_MODE, false); + onSharedIntent(intent); } public void updateTabGroups(TabLayout groupsTabLayout) { diff --git a/app/src/main/java/protect/card_locker/ScanActivity.java b/app/src/main/java/protect/card_locker/ScanActivity.java index a3cc90648..fddabaedc 100644 --- a/app/src/main/java/protect/card_locker/ScanActivity.java +++ b/app/src/main/java/protect/card_locker/ScanActivity.java @@ -12,6 +12,11 @@ import android.view.MenuItem; import android.view.View; import android.widget.Toast; +import androidx.activity.result.ActivityResultLauncher; +import androidx.activity.result.contract.ActivityResultContracts; +import androidx.appcompat.app.ActionBar; +import androidx.appcompat.widget.Toolbar; + import com.google.zxing.ResultPoint; import com.google.zxing.client.android.Intents; import com.journeyapps.barcodescanner.BarcodeCallback; @@ -21,11 +26,6 @@ import com.journeyapps.barcodescanner.DecoratedBarcodeView; import java.util.List; -import androidx.activity.result.ActivityResultLauncher; -import androidx.activity.result.contract.ActivityResultContracts; -import androidx.appcompat.app.ActionBar; -import androidx.appcompat.widget.Toolbar; - import protect.card_locker.databinding.CustomBarcodeScannerBinding; import protect.card_locker.databinding.ScanActivityBinding; diff --git a/app/src/main/java/protect/card_locker/Utils.java b/app/src/main/java/protect/card_locker/Utils.java index fe98efa7c..86b4b5870 100644 --- a/app/src/main/java/protect/card_locker/Utils.java +++ b/app/src/main/java/protect/card_locker/Utils.java @@ -11,6 +11,7 @@ import android.graphics.BitmapFactory; import android.graphics.Color; import android.graphics.ImageDecoder; import android.graphics.Matrix; +import android.net.Uri; import android.os.Build; import android.os.LocaleList; import android.provider.MediaStore; @@ -19,6 +20,12 @@ import android.util.TypedValue; import android.view.MenuItem; import android.widget.Toast; +import androidx.appcompat.app.AppCompatActivity; +import androidx.appcompat.app.AppCompatDelegate; +import androidx.core.graphics.ColorUtils; +import androidx.exifinterface.media.ExifInterface; +import androidx.palette.graphics.Palette; + import com.google.android.material.color.DynamicColors; import com.google.zxing.BinaryBitmap; import com.google.zxing.LuminanceSource; @@ -44,11 +51,6 @@ import java.util.GregorianCalendar; import java.util.Locale; import java.util.Map; -import androidx.appcompat.app.AppCompatActivity; -import androidx.appcompat.app.AppCompatDelegate; -import androidx.core.graphics.ColorUtils; -import androidx.exifinterface.media.ExifInterface; -import androidx.palette.graphics.Palette; import protect.card_locker.preferences.Settings; public class Utils { @@ -118,12 +120,8 @@ public class Utils { Bitmap bitmap; try { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { - ImageDecoder.Source image_source = ImageDecoder.createSource(context.getContentResolver(), intent.getData()); - bitmap = ImageDecoder.decodeBitmap(image_source, (decoder, info, source) -> decoder.setMutableRequired(true)); - } else { - bitmap = getBitmapSdkLessThan29(intent, context); - } + Uri data = intent.getData(); + bitmap = retrieveImageFromUri(context, data); } catch (IOException e) { Log.e(TAG, "Error getting data from image file"); e.printStackTrace(); @@ -163,9 +161,18 @@ public class Utils { throw new UnsupportedOperationException("Unknown request code for parseSetBarcodeActivityResult"); } + static public Bitmap retrieveImageFromUri(Context context, Uri data) throws IOException { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + ImageDecoder.Source image_source = ImageDecoder.createSource(context.getContentResolver(), data); + return ImageDecoder.decodeBitmap(image_source, (decoder, info, source) -> decoder.setMutableRequired(true)); + } else { + return getBitmapSdkLessThan29(data, context); + } + } + @SuppressWarnings("deprecation") - private static Bitmap getBitmapSdkLessThan29(Intent intent, Context context) throws IOException { - return MediaStore.Images.Media.getBitmap(context.getContentResolver(), intent.getData()); + private static Bitmap getBitmapSdkLessThan29(Uri data, Context context) throws IOException { + return MediaStore.Images.Media.getBitmap(context.getContentResolver(), data); } static public BarcodeValues getBarcodeFromBitmap(Bitmap bitmap) {