From 015b83c9ca1e2c7e3e12d2fd67890b20a7e800a8 Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Tue, 13 Aug 2013 17:00:50 +0200 Subject: [PATCH] Started to handle pictures in liblinphone message storage --- src/org/linphone/ChatFragment.java | 61 +++++++++++++++++++----------- src/org/linphone/ChatStorage.java | 4 +- submodules/linphone | 2 +- 3 files changed, 42 insertions(+), 25 deletions(-) diff --git a/src/org/linphone/ChatFragment.java b/src/org/linphone/ChatFragment.java index f4aaf7337..5e194e88c 100644 --- a/src/org/linphone/ChatFragment.java +++ b/src/org/linphone/ChatFragment.java @@ -117,6 +117,7 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC private HashMap latestImageMessages; private int messagesFilterLimit = 0; private List messagesList; + private boolean useLinphoneMessageStorage; private ProgressBar progressBar; private int bytesSent; @@ -136,6 +137,8 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC view = inflater.inflate(R.layout.chat, container, false); + useLinphoneMessageStorage = getResources().getBoolean(R.bool.use_linphone_chat_storage); + contactName = (TextView) view.findViewById(R.id.contactName); contactPicture = (AvatarWithShadow) view.findViewById(R.id.contactPicture); @@ -406,7 +409,11 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC public void run() { final Bitmap bm = ChatFragment.downloadImage(url); if (bm != null) { - LinphoneActivity.instance().getChatStorage().saveImage(finalId, bm); + if (useLinphoneMessageStorage) { + saveImage(bm, finalId); + } else { + LinphoneActivity.instance().getChatStorage().saveImage(finalId, bm); + } mHandler.post(new Runnable() { @Override public void run() { @@ -468,7 +475,9 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC menu.add(v.getId(), MENU_DELETE_MESSAGE, 0, getString(R.string.delete)); ImageView iv = (ImageView) v.findViewById(R.id.image); if (iv != null && iv.getVisibility() == View.VISIBLE) { - menu.add(v.getId(), MENU_SAVE_PICTURE, 0, getString(R.string.save_picture)); + if (!useLinphoneMessageStorage) { + menu.add(v.getId(), MENU_SAVE_PICTURE, 0, getString(R.string.save_picture)); + } } else { menu.add(v.getId(), MENU_COPY_TEXT, 0, getString(R.string.copy_text)); } @@ -623,23 +632,22 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC } }); } else if (message.getExternalBodyUrl() != null) { - byte[] rawImage = LinphoneActivity.instance().getChatStorage().getRawImageFromMessage(id); - if (rawImage == null) { - mHandler.post(new Runnable() { - @Override - public void run() { - displayImageMessage(id, null, String.valueOf(message.getTime()), true, null, messagesLayout, message.getExternalBodyUrl()); - } - }); + Bitmap bm = null; + if (useLinphoneMessageStorage) { + //TODO: Try to read image from file } else { - final Bitmap bm = BitmapFactory.decodeByteArray(rawImage, 0, rawImage.length); - mHandler.post(new Runnable() { - @Override - public void run() { - displayImageMessage(id, bm, String.valueOf(message.getTime()), true, null, messagesLayout, ""); - } - }); + byte[] rawImage = LinphoneActivity.instance().getChatStorage().getRawImageFromMessage(id); + if (rawImage != null) { + bm = BitmapFactory.decodeByteArray(rawImage, 0, rawImage.length); + } } + final Bitmap fbm = bm; + mHandler.post(new Runnable() { + @Override + public void run() { + displayImageMessage(id, fbm, String.valueOf(message.getTime()), true, null, messagesLayout, fbm == null ? "" : message.getExternalBodyUrl()); + } + }); } scrollToEnd(); } @@ -727,10 +735,17 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC } private void saveImage(int id) { + byte[] rawImage = LinphoneActivity.instance().getChatStorage().getRawImageFromMessage(id); + Bitmap bm = BitmapFactory.decodeByteArray(rawImage, 0, rawImage.length); + if (saveImage(bm, id)) { + Toast.makeText(getActivity(), getString(R.string.image_saved), Toast.LENGTH_SHORT).show(); + } else { + Toast.makeText(getActivity(), getString(R.string.image_not_saved), Toast.LENGTH_LONG).show(); + } + } + + private boolean saveImage(Bitmap bm, int id) { try { - byte[] rawImage = LinphoneActivity.instance().getChatStorage().getRawImageFromMessage(id); - Bitmap bm = BitmapFactory.decodeByteArray(rawImage, 0, rawImage.length); - String path = Environment.getExternalStorageDirectory().toString(); OutputStream fOut = null; File file = new File(path, getString(R.string.picture_name_format).replace("%s", String.valueOf(id))); @@ -739,13 +754,15 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC bm.compress(Bitmap.CompressFormat.JPEG, 100, fOut); fOut.flush(); fOut.close(); + + //TODO: Update url path in liblinphone database - Toast.makeText(getActivity(), getString(R.string.image_saved), Toast.LENGTH_SHORT).show(); MediaStore.Images.Media.insertImage(getActivity().getContentResolver(),file.getAbsolutePath(),file.getName(),file.getName()); + return true; } catch (Exception e) { - Toast.makeText(getActivity(), getString(R.string.image_not_saved), Toast.LENGTH_LONG).show(); e.printStackTrace(); } + return false; } private long hashBitmap(Bitmap bmp){ diff --git a/src/org/linphone/ChatStorage.java b/src/org/linphone/ChatStorage.java index 15660bbba..674fe7d4e 100644 --- a/src/org/linphone/ChatStorage.java +++ b/src/org/linphone/ChatStorage.java @@ -162,7 +162,7 @@ public class ChatStorage { public void saveImage(int id, Bitmap image) { if (useNativeAPI) { - //TODO + //Handled before this point return; } @@ -409,7 +409,7 @@ public class ChatStorage { public byte[] getRawImageFromMessage(int id) { if (useNativeAPI) { - //TODO + //Handled before this point return null; } diff --git a/submodules/linphone b/submodules/linphone index 88a0da21c..d56d927ca 160000 --- a/submodules/linphone +++ b/submodules/linphone @@ -1 +1 @@ -Subproject commit 88a0da21c15c3ac031e55b40e219aebee85e82b0 +Subproject commit d56d927caf4c22554e333ff3227628da2d370a65