From 159e9ccfe57f087ef7c3664a2c9a46e93928dbeb Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Tue, 17 Mar 2015 16:48:13 +0100 Subject: [PATCH] Fix for black screen when trying to upload a large file --- res/values/strings.xml | 3 +- src/org/linphone/ChatFragment.java | 76 +++++++++++++++++++----------- 2 files changed, 51 insertions(+), 28 deletions(-) diff --git a/res/values/strings.xml b/res/values/strings.xml index 3ab91b8b7..79551c89c 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -405,4 +405,5 @@ Retry Failed to download or apply remote provisioning profile... Remote provisioning - Do you want to change the provisioning URI ? + Do you want to change the provisioning URI ? + Processing image, can take up to a few seconds depending on the size of the file diff --git a/src/org/linphone/ChatFragment.java b/src/org/linphone/ChatFragment.java index facd49cf8..2820c6705 100644 --- a/src/org/linphone/ChatFragment.java +++ b/src/org/linphone/ChatFragment.java @@ -41,6 +41,7 @@ import org.linphone.ui.BubbleChat; import android.annotation.SuppressLint; import android.app.Activity; +import android.app.ProgressDialog; import android.content.Context; import android.content.Intent; import android.content.res.Configuration; @@ -49,8 +50,8 @@ import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Rect; import android.net.Uri; +import android.os.AsyncTask; import android.os.Bundle; -import android.os.Handler; import android.os.Parcelable; import android.provider.MediaStore; import android.support.v4.app.Fragment; @@ -101,7 +102,6 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC private TextWatcher textWatcher; private ViewTreeObserver.OnGlobalLayoutListener keyboardListener; private ChatMessageAdapter adapter; - private Handler mHandler = new Handler(); private LinphoneCoreListenerBase mListener; private ByteArrayOutputStream mDownloadedImageStream; @@ -497,24 +497,8 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC if (chatRoom != null && path != null && path.length() > 0 && isNetworkReachable) { Bitmap bm = BitmapFactory.decodeFile(path); if (bm != null) { - ByteArrayOutputStream stream = new ByteArrayOutputStream(); - bm.compress(Bitmap.CompressFormat.PNG, 100, stream); - byte[] byteArray = stream.toByteArray(); - mUploadingImageStream = new ByteArrayInputStream(byteArray); - - LinphoneContent content = LinphoneCoreFactory.instance().createLinphoneContent("image", "jpeg", byteArray, null); - String fileName = path.substring(path.lastIndexOf("/") + 1); - content.setName(fileName); - - LinphoneChatMessage message = chatRoom.createFileTransferMessage(content); - message.setListener(this); - message.setAppData(path); - - uploadLayout.setVisibility(View.VISIBLE); - textLayout.setVisibility(View.GONE); - - chatRoom.sendChatMessage(message); - currentMessageInFileTransferUploadState = message; + FileUploadPrepareTask task = new FileUploadPrepareTask(getActivity(), path); + task.execute(bm); } else { Log.e("Error, bitmap factory can't read " + path); } @@ -522,6 +506,50 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC LinphoneActivity.instance().displayCustomToast(getString(R.string.error_network_unreachable), Toast.LENGTH_LONG); } } + + class FileUploadPrepareTask extends AsyncTask { + private String path; + private ProgressDialog progressDialog; + + public FileUploadPrepareTask(Context context, String fileToUploadPath) { + path = fileToUploadPath; + + uploadLayout.setVisibility(View.VISIBLE); + textLayout.setVisibility(View.GONE); + + progressDialog = new ProgressDialog(context); + progressDialog.setIndeterminate(true); + progressDialog.setMessage(getString(R.string.processing_image)); + progressDialog.show(); + } + + @Override + protected byte[] doInBackground(Bitmap... params) { + Bitmap bm = params[0]; + ByteArrayOutputStream stream = new ByteArrayOutputStream(); + bm.compress(Bitmap.CompressFormat.PNG, 100, stream); + byte[] byteArray = stream.toByteArray(); + return byteArray; + } + + @Override + protected void onPostExecute(byte[] result) { + progressDialog.dismiss(); + + mUploadingImageStream = new ByteArrayInputStream(result); + + LinphoneContent content = LinphoneCoreFactory.instance().createLinphoneContent("image", "jpeg", result, null); + String fileName = path.substring(path.lastIndexOf("/") + 1); + content.setName(fileName); + + LinphoneChatMessage message = chatRoom.createFileTransferMessage(content); + message.setListener(ChatFragment.this); + message.setAppData(path); + + chatRoom.sendChatMessage(message); + currentMessageInFileTransferUploadState = message; + } + } private LinphoneChatMessage getMessageForId(int id) { for (LinphoneChatMessage message : chatRoom.getHistory()) { @@ -610,13 +638,7 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC } if (fileToUploadPath != null) { - final String filePath = fileToUploadPath; - mHandler.post(new Runnable() { - @Override - public void run() { - sendImageMessage(filePath); - } - }); + sendImageMessage(fileToUploadPath); } } else { super.onActivityResult(requestCode, resultCode, data);