Skip to content

Commit

Permalink
Pull request #34: Jdzubak LCZ-333 empty attachment files
Browse files Browse the repository at this point in the history
Merge in MML/infobip-mobile-messaging-huawei from jdzubak-LCZ-333_empty_attachment_files to release_3.0.0

Squashed commit of the following:

commit 257ff419c64332e00f500c3edade6fd09f6506aa
Author: Jakub Dzubak <[email protected]>
Date:   Tue Sep 6 08:45:07 2022 +0200

    LCZ-333 MM SDK version increased

commit ccbf920bc86b11d3cd1c7ba0edbc4ec073e150a4
Author: Jakub Dzubak <[email protected]>
Date:   Tue Sep 6 07:32:42 2022 +0200

    LCZ-333 Change order of repos

commit 76c9b1b6903ff585a3007edb010c9562a262f7b2
Author: Jakub Dzubak <[email protected]>
Date:   Mon Sep 5 13:56:14 2022 +0200

    LCZ-333 Remove only empty attachment files
  • Loading branch information
jakubdzubak1 committed Sep 6, 2022
1 parent 6072bb1 commit 65a90cf
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 81 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ ext {
mm_compileSdkVersion = 31
mm_targetSdkVersion = 31
mm_buildToolsVersion = "31.0.0"
mm_androidSdkVersion = "6.3.3"
mm_androidSdkVersion = "6.3.4"
}

allprojects {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.infobip.mobile.messaging.chat.attachments;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
Expand All @@ -9,6 +10,7 @@
import android.os.Environment;
import android.os.ParcelFileDescriptor;
import android.provider.MediaStore;
import android.provider.OpenableColumns;

import org.infobip.mobile.messaging.logging.MobileMessagingLogger;
import org.infobip.mobile.messaging.mobileapi.InternalSdkError;
Expand All @@ -34,13 +36,10 @@ public static void makeAttachment(final FragmentActivity context, final Intent d
AsyncTask.execute(new Runnable() {
@Override
public void run() {
ParcelFileDescriptor fileDescriptor = null;
try {
//From media store Uri we need to get real Uri of the file
Uri capturedMediaRealUri = getUriFromMediaStoreURI(capturedMediaStoreUri, context);

fileDescriptor = openFileDescriptorFromMediaStoreURI(capturedMediaStoreUri, context);
final InAppChatMobileAttachment attachment = InAppChatMobileAttachment.makeAttachment(context, data, capturedMediaStoreUri, capturedMediaRealUri, fileDescriptor);
final InAppChatMobileAttachment attachment = InAppChatMobileAttachment.makeAttachment(context, data, capturedMediaStoreUri, capturedMediaRealUri);
context.runOnUiThread(new Runnable() {
@Override
public void run() {
Expand All @@ -54,8 +53,6 @@ public void run() {
listener.onError(context, e);
}
});
} finally {
closeFileDescriptor(context, capturedMediaStoreUri, fileDescriptor);
}
}
});
Expand Down Expand Up @@ -125,6 +122,28 @@ public static Uri getOutputVideoUrl(FragmentActivity fragmentActivity) {
return fragmentActivity.getContentResolver().insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, contentValues);
}

public static void deleteEmptyFileByUri(Context context, Uri uri) {
if (context == null || uri == null)
return;
try {
long fileSize = -1;
ContentResolver contentResolver = context.getApplicationContext().getContentResolver();
Cursor cursor = contentResolver.query(uri, new String[]{OpenableColumns.SIZE}, null, null, null);
if (cursor != null && cursor.moveToFirst()) {
int columnIndex = cursor.getColumnIndex(OpenableColumns.SIZE);
if (columnIndex == 0) {
fileSize = cursor.getLong(columnIndex);
}
cursor.close();
}
if (fileSize == 0) {
contentResolver.delete(uri, null, null);
}
} catch (Exception e) {
MobileMessagingLogger.e("[InAppChat] Can't delete empty file", e);
}
}

@Nullable
private static Uri getUriFromMediaStoreURI(Uri mediaStoreUri, FragmentActivity activity) {
if (activity == null || mediaStoreUri == null) {
Expand Down Expand Up @@ -163,19 +182,6 @@ private static ParcelFileDescriptor openFileDescriptorFromMediaStoreURI(Uri medi
return fileDescriptor;
}

private static void closeFileDescriptor(FragmentActivity activity, Uri mediaStoreUri, ParcelFileDescriptor fileDescriptor) {
try {
if (activity != null && mediaStoreUri != null) {
activity.getApplicationContext().getContentResolver().delete(mediaStoreUri, null, null);
}
if (fileDescriptor != null) {
fileDescriptor.close();
}
} catch (Exception e) {
MobileMessagingLogger.e("[InAppChat] Can't close file descriptor", e);
}
}

public interface InAppChatAttachmentHelperListener {
void onAttachmentCreated(InAppChatMobileAttachment attachment);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ public InAppChatMobileAttachment(String mimeType, String base64, String filename
this.fileName = filename;
}

public static InAppChatMobileAttachment makeAttachment(Context context, Intent data, Uri capturedMediaStoreUri, Uri capturedMediaRealUri, ParcelFileDescriptor fileDescriptor) throws InternalSdkError.InternalSdkException {
public static InAppChatMobileAttachment makeAttachment(Context context, Intent data, Uri capturedMediaStoreUri, Uri capturedMediaRealUri) throws InternalSdkError.InternalSdkException {
String mimeType = getMimeType(context, data, capturedMediaRealUri);
byte[] bytesArray = getBytes(context, data, capturedMediaStoreUri, capturedMediaRealUri, fileDescriptor, mimeType);
byte[] bytesArray = getBytes(context, data, capturedMediaStoreUri, capturedMediaRealUri, mimeType);

if (bytesArray == null) {
return null;
Expand Down Expand Up @@ -91,7 +91,7 @@ public static String getMimeType(Context context, Intent data, Uri capturedMedia
return mimeType;
}

private static byte[] getBytes(Context context, Intent data, Uri capturedMediaStoreUri, Uri capturedMediaRealUri, ParcelFileDescriptor fileDescriptor, String mimeType) {
private static byte[] getBytes(Context context, Intent data, Uri capturedMediaStoreUri, Uri capturedMediaRealUri, String mimeType) {
Uri uriFromIntent = null;

//data.getData() will be null for images captured by camera
Expand All @@ -100,62 +100,55 @@ private static byte[] getBytes(Context context, Intent data, Uri capturedMediaSt
}
//Ony images captured by camera are scaled for now
if (mimeType.equals("image/jpeg") && uriFromIntent == null) {
return getBytesWithBitmapScaling(context, capturedMediaStoreUri, capturedMediaRealUri, fileDescriptor);
return getBytesWithBitmapScaling(context, capturedMediaStoreUri, capturedMediaRealUri);
} else if (uriFromIntent != null) {
return getBytes(context, uriFromIntent);
} else {
return getBytes(context, capturedMediaRealUri);
}
}

public static byte[] getBytesWithBitmapScaling(Context context, Uri mediaStoreUri, Uri imageUri, ParcelFileDescriptor fileDescriptor) {
if (imageUri == null) {
private static byte[] getBytesWithBitmapScaling(Context context, Uri mediaStoreUri, Uri imageUri) {
if (context == null || mediaStoreUri == null || imageUri == null) {
return null;
}

long fileSize = fileDescriptor.getStatSize();
String filePath;
try {
filePath = imageUri.getPath();
ParcelFileDescriptor fileDescriptor = context.getContentResolver().openFileDescriptor(mediaStoreUri, "r");

long fileSize = fileDescriptor.getStatSize();
String filePath = imageUri.getPath();

boolean shouldScaleImage = fileSize > getAttachmentMaxSize(context);
int imageQuality = fileSize > DEFAULT_MAX_UPLOAD_CONTENT_SIZE / 2 ? 80 : 100;

BitmapFactory.Options options = new BitmapFactory.Options();
// setting inSampleSize value allows to load a scaled down version of the original image
options.inSampleSize = shouldScaleImage ? 2 : 1;
// set to false to load the actual bitmap
options.inJustDecodeBounds = false;
// this options allow android to claim the bitmap memory if it runs low on memory
options.inPurgeable = true;
options.inInputShareable = true;
options.inTempStorage = new byte[16 * 1024];

Bitmap bitmap = BitmapFactory.decodeFileDescriptor(fileDescriptor.getFileDescriptor(), new Rect(-1, -1, -1, -1), options);

// check the rotation of the image and display it properly
int orientationDegree = getExifOrientationDegree(context, filePath, mediaStoreUri);
Matrix rotationMatrix = new Matrix();
rotationMatrix.postRotate(orientationDegree);
bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), rotationMatrix, true);

ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, imageQuality, outputStream);
bitmap.recycle();
fileDescriptor.close();
return outputStream.toByteArray();
} catch (Exception exception) {
MobileMessagingLogger.e("[InAppChat] can't load image to send attachment", exception);
return null;
}

boolean shouldScaleImage = fileSize > getAttachmentMaxSize(context);
int imageQuality = fileSize > DEFAULT_MAX_UPLOAD_CONTENT_SIZE / 2 ? 80 : 100;

BitmapFactory.Options options = new BitmapFactory.Options();
// setting inSampleSize value allows to load a scaled down version of the original image
options.inSampleSize = shouldScaleImage ? 2 : 1;
// set to false to load the actual bitmap
options.inJustDecodeBounds = false;
// this options allow android to claim the bitmap memory if it runs low on memory
options.inPurgeable = true;
options.inInputShareable = true;
options.inTempStorage = new byte[16 * 1024];

Bitmap bitmap;
try {
// bitmap = BitmapFactory.decodeFile(filePath, options);
// if (bitmap == null) {
bitmap = BitmapFactory.decodeFileDescriptor(fileDescriptor.getFileDescriptor(), new Rect(-1, -1, -1, -1), options);
// }
} catch (Exception exception) {
MobileMessagingLogger.e("[InAppChat] can't load image to send attachment", exception);
return null;
}

// check the rotation of the image and display it properly
int orientationDegree = getExifOrientationDegree(context, filePath, mediaStoreUri);
Matrix rotationMatrix = new Matrix();
rotationMatrix.postRotate(orientationDegree);
bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), rotationMatrix, true);

ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, imageQuality, outputStream);
bitmap.recycle();
return outputStream.toByteArray();
}

private static int getExifOrientationDegree(Context context, String filepath, Uri mediaStoreUri) {
Expand Down Expand Up @@ -219,12 +212,13 @@ private static byte[] getBytes(Context context, Uri uri) {
}
byte[] bytes = getBytes(stream);
stream.close();
if (fileDescriptor != null) {
fileDescriptor.close();
}
return bytes;
} catch (Exception e) {
MobileMessagingLogger.e("[InAppChat] Can't get base64 from Uri", e);
return null;
} finally {
closeFileDescriptor(context, uri, fileDescriptor);
}
}

Expand All @@ -243,17 +237,4 @@ private static byte[] getBytes(InputStream inputStream) throws IOException {
private static Long getAttachmentMaxSize(Context context) {
return PreferenceHelper.findLong(context, MobileMessagingChatProperty.IN_APP_CHAT_WIDGET_MAX_UPLOAD_CONTENT_SIZE.getKey(), DEFAULT_MAX_UPLOAD_CONTENT_SIZE);
}

private static void closeFileDescriptor(Context context, Uri uri, ParcelFileDescriptor fileDescriptor) {
try {
if (context != null && uri != null) {
context.getApplicationContext().getContentResolver().delete(uri, null, null);
}
if (fileDescriptor != null) {
fileDescriptor.close();
}
} catch (Exception e) {
MobileMessagingLogger.e("[InAppChat] Can't close file descriptor", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,7 @@ public void onAttachmentCreated(final InAppChatMobileAttachment attachment) {
MobileMessagingLogger.e("[InAppChat] Can't create attachment");
Toast.makeText(getFragmentActivity(), R.string.ib_chat_cant_create_attachment, Toast.LENGTH_SHORT).show();
}
deleteEmptyMediaFiles();
}

@Override
Expand All @@ -768,13 +769,21 @@ public void onError(final Context context, InternalSdkError.InternalSdkException
MobileMessagingLogger.e("[InAppChat] Attachment content is not valid.");
Toast.makeText(context, R.string.ib_chat_cant_create_attachment, Toast.LENGTH_SHORT).show();
}
deleteEmptyMediaFiles();
}
});
} else {
deleteEmptyMediaFiles();
}
}
}
);

private void deleteEmptyMediaFiles(){
InAppChatAttachmentHelper.deleteEmptyFileByUri(getContext(), capturedImageUri);
InAppChatAttachmentHelper.deleteEmptyFileByUri(getContext(), capturedVideoUri);
}

private void chooseFile() {
fragmentCouldBePaused = false;
if (!isRequiredPermissionsGranted()) {
Expand Down
2 changes: 1 addition & 1 deletion infobip-mobile-messaging-huawei-demo/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ buildscript {
google()
mavenCentral()
// huawei maven
maven { url 'https://developer.huawei.com/repo/' }
maven {url 'https://plugins.gradle.org/m2/'}
maven { url 'https://developer.huawei.com/repo/' }
}
dependencies {
classpath 'com.huawei.agconnect:agcp:1.6.0.300'
Expand Down
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ pluginManagement {
google()
mavenCentral()
// huawei maven
maven { url 'https://developer.huawei.com/repo/' }
maven {url 'https://plugins.gradle.org/m2/'}
maven { url 'https://developer.huawei.com/repo/' }
}
}
include ':infobip-mobile-messaging-huawei-sdk', ':infobip-mobile-messaging-huawei-demo', ':infobip-mobile-messaging-huawei-test', ':infobip-mobile-messaging-huawei-chat-sdk', ':infobip-mobile-messaging-huawei-cryptor-migration'

0 comments on commit 65a90cf

Please sign in to comment.