diff --git a/app/build.gradle b/app/build.gradle
index 1ff625b18..5a14cdd49 100644
--- a/app/build.gradle
+++ b/app/build.gradle
@@ -85,6 +85,7 @@ dependencies {
implementation 'androidx.exifinterface:exifinterface:1.3.3'
implementation 'androidx.preference:preference:1.1.1'
implementation 'com.google.android.material:material:1.4.0'
+ implementation 'com.github.yalantis:ucrop:2.2.6'
coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.1.5'
// Splash Screen
diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml
index 1e81afb71..5a5217267 100644
--- a/app/src/main/AndroidManifest.xml
+++ b/app/src/main/AndroidManifest.xml
@@ -115,6 +115,11 @@
+
+
mCropperLauncher;
+ int mRequestedImage;
+ UCrop.Options mCropperOptions;
+ // FIX ME: for until everything is switched to activity launcher callbacks
+ private boolean skipOnce = false;
+
final private TaskHandler mTasks = new TaskHandler();
private static LoyaltyCard updateTempState(LoyaltyCard loyaltyCard, LoyaltyCardField fieldName, Object value) {
@@ -188,6 +199,7 @@ public class LoyaltyCardEditActivity extends CatimaAppCompatActivity {
tabs = findViewById(R.id.tabs);
savedInstanceState.putInt(STATE_TAB_INDEX, tabs.getSelectedTabPosition());
savedInstanceState.putParcelable(STATE_TEMP_CARD, tempLoyaltyCard);
+ savedInstanceState.putInt("skipOnce", skipOnce? 1: 0);
}
@Override
@@ -509,6 +521,56 @@ public class LoyaltyCardEditActivity extends CatimaAppCompatActivity {
});
tabs.selectTab(tabs.getTabAt(0));
+
+ mCropperLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(),
+ new ActivityResultCallback() {
+ @Override
+ public void onActivityResult(ActivityResult result) {
+ Intent intent = result.getData();
+ if (result.getResultCode() == Activity.RESULT_OK) {
+ Uri debugUri = UCrop.getOutput(intent);
+ String cropOutputPath = debugUri.getPath();
+ Log.d("cropper", "cropper thinks it has produced image at " + cropOutputPath + " " + debugUri.toString());
+ Bitmap bitmap = BitmapFactory.decodeFile(cropOutputPath);
+
+
+ if (bitmap != null) {
+ bitmap = Utils.resizeBitmap(bitmap);
+ try {
+ bitmap = Utils.rotateBitmap(bitmap, new ExifInterface(cropOutputPath));
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+
+ if (mRequestedImage == Utils.CARD_IMAGE_FROM_CAMERA_FRONT || mRequestedImage == Utils.CARD_IMAGE_FROM_FILE_FRONT) {
+ setCardImage(cardImageFront, bitmap);
+ } else {
+ setCardImage(cardImageBack, bitmap);
+ }
+
+ hasChanged = true;
+ } else {
+ Toast.makeText(LoyaltyCardEditActivity.this, R.string.errorReadingImage, Toast.LENGTH_LONG).show();
+ }
+ }else if(result.getResultCode() == UCrop.RESULT_ERROR){
+ Log.e("cropper error", UCrop.getError(intent).toString());
+ }
+ }
+ });
+
+ if (savedInstanceState != null) {
+ skipOnce = savedInstanceState.getInt("skipOnce") == 1 ? true : false;
+ }
+ mCropperOptions = new UCrop.Options();
+ mCropperOptions.setCompressionFormat(Bitmap.CompressFormat.JPEG);
+ setCropperTheme();
+ }
+
+ private void setCropperTheme(){
+ mCropperOptions.setToolbarColor(getResources().getColor(R.color.colorPrimary));
+ mCropperOptions.setStatusBarColor(getResources().getColor(R.color.colorPrimary));
+ mCropperOptions.setToolbarWidgetColor(Color.WHITE);
+ mCropperOptions.setActiveControlsWidgetColor(getResources().getColor(R.color.colorPrimary));
}
@Override
@@ -786,15 +848,21 @@ public class LoyaltyCardEditActivity extends CatimaAppCompatActivity {
confirmExitDialog.show();
}
+ private File createTempFile(String prefix, String suffix) throws IOException{
+ String imageFileName = prefix + new Date().getTime();
+ File file = File.createTempFile(
+ imageFileName,
+ suffix,
+ getExternalFilesDir(Environment.DIRECTORY_PICTURES)
+ );
+ return file;
+ }
+
private void takePhotoForCard(int type) throws IOException {
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
+ File image = createTempFile("CATIMA_", ".jpg");
String imageFileName = "CATIMA_" + new Date().getTime();
- File image = File.createTempFile(
- imageFileName,
- ".jpg",
- getExternalFilesDir(Environment.DIRECTORY_PICTURES)
- );
tempCameraPicturePath = image.getAbsolutePath();
@@ -1035,58 +1103,45 @@ public class LoyaltyCardEditActivity extends CatimaAppCompatActivity {
return super.onOptionsItemSelected(item);
}
+ public boolean startCropper(String sourceImagePath){
+ return startCropperUri(Uri.parse("file://" + sourceImagePath));
+ }
+ public boolean startCropperUri(Uri sourceUri){
+ Log.d("cropper", "launching cropper with image " + sourceUri.getPath());
+ File cropOutput;
+ try {
+ cropOutput = createTempFile("UCROP_", ".jpg");
+ }catch(java.io.IOException e){
+ Log.e("image cropping", "failed creating temporarily file");
+ return false;
+ }
+ Uri destUri = Uri.parse("file://" + cropOutput.getAbsolutePath());
+ Log.d("cropper", "asking cropper to output to " + destUri.toString());
+ skipOnce = true;
+ mCropperLauncher.launch(
+ UCrop.of(
+ sourceUri,
+ destUri
+ ).withOptions(mCropperOptions).getIntent(this)
+ );
+ return true;
+ }
+
@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
-
+ if(skipOnce){
+ skipOnce = false;
+ return;
+ }
if (resultCode == RESULT_OK) {
if (requestCode == Utils.CARD_IMAGE_FROM_CAMERA_FRONT || requestCode == Utils.CARD_IMAGE_FROM_CAMERA_BACK) {
- Bitmap bitmap = BitmapFactory.decodeFile(tempCameraPicturePath);
-
- if (bitmap != null) {
- bitmap = Utils.resizeBitmap(bitmap);
- try {
- bitmap = Utils.rotateBitmap(bitmap, new ExifInterface(tempCameraPicturePath));
- } catch (IOException e) {
- e.printStackTrace();
- }
-
- if (requestCode == Utils.CARD_IMAGE_FROM_CAMERA_FRONT) {
- setCardImage(cardImageFront, bitmap);
- } else {
- setCardImage(cardImageBack, bitmap);
- }
-
- hasChanged = true;
- } else {
- Toast.makeText(this, R.string.errorReadingImage, Toast.LENGTH_LONG).show();
- }
+ mRequestedImage = requestCode;
+ startCropper(tempCameraPicturePath);
} else if (requestCode == Utils.CARD_IMAGE_FROM_FILE_FRONT || requestCode == Utils.CARD_IMAGE_FROM_FILE_BACK) {
- Bitmap bitmap = null;
- try {
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
- ImageDecoder.Source image_source = ImageDecoder.createSource(getContentResolver(), intent.getData());
- bitmap = ImageDecoder.decodeBitmap(image_source, (decoder, info, source) -> decoder.setMutableRequired(true));
- } else {
- bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), intent.getData());
- }
- } catch (IOException e) {
- Log.e(TAG, "Error getting data from image file");
- e.printStackTrace();
- }
-
- if (bitmap != null) {
- bitmap = Utils.resizeBitmap(bitmap);
- if (requestCode == Utils.CARD_IMAGE_FROM_FILE_FRONT) {
- setCardImage(cardImageFront, bitmap);
- } else {
- setCardImage(cardImageBack, bitmap);
- }
-
- hasChanged = true;
- } else {
- Toast.makeText(this, R.string.errorReadingImage, Toast.LENGTH_LONG).show();
- }
+ mRequestedImage = requestCode;
+ Uri uri = intent.getData();
+ startCropperUri(uri);
} else {
BarcodeValues barcodeValues = Utils.parseSetBarcodeActivityResult(requestCode, resultCode, intent, this);