Clicking on a chat image will show it in the Gallery + don't display images by default, instead add a Show button

This commit is contained in:
Sylvain Berfini 2013-08-20 12:44:49 +02:00
parent 5ed2e63ca0
commit 3ff74418e9
4 changed files with 74 additions and 37 deletions

View file

@ -373,6 +373,7 @@
<string name="pref_background_mode">Actif en arrière plan</string> <string name="pref_background_mode">Actif en arrière plan</string>
<string name="show_image">Afficher</string>
<string name="download_image">Télécharger</string> <string name="download_image">Télécharger</string>
<string name="download_image_failed">Téléchargement échoué. Vérifiez votre connexion internet ou reéssayez plus tard.</string> <string name="download_image_failed">Téléchargement échoué. Vérifiez votre connexion internet ou reéssayez plus tard.</string>

View file

@ -421,6 +421,7 @@
<string name="pref_background_mode">Background mode</string> <string name="pref_background_mode">Background mode</string>
<string name="show_image">Show</string>
<string name="download_image">Download</string> <string name="download_image">Download</string>
<string name="download_image_failed">Download failed. Please check your internet access or try again later.</string> <string name="download_image_failed">Download failed. Please check your internet access or try again later.</string>

View file

@ -288,11 +288,11 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC
LinphoneChatMessage[] history = chatRoom.getHistory(); LinphoneChatMessage[] history = chatRoom.getHistory();
for (int i = 0; i < history.length; i++) { for (int i = 0; i < history.length; i++) {
LinphoneChatMessage msg = history[i]; LinphoneChatMessage msg = history[i];
boolean show = i >= history.length - messagesFilterLimit;
if (msg.getExternalBodyUrl() != null) { if (msg.getExternalBodyUrl() != null) {
Bitmap bm = BitmapFactory.decodeFile(msg.getExternalBodyUrl()); displayImageMessage(msg.getStorageId(), null, msg.getTime(), !msg.isOutgoing(), msg.getStatus(), messagesLayout, msg.getExternalBodyUrl(), show);
displayImageMessage(msg.getStorageId(), bm, msg.getTime(), !msg.isOutgoing(), msg.getStatus(), messagesLayout, msg.getExternalBodyUrl(), i >= history.length - messagesFilterLimit);
} else { } else {
displayMessage(msg.getStorageId(), msg.getText(), msg.getTime(), !msg.isOutgoing(), msg.getStatus(), messagesLayout, i >= history.length - messagesFilterLimit); displayMessage(msg.getStorageId(), msg.getText(), msg.getTime(), !msg.isOutgoing(), msg.getStatus(), messagesLayout, show);
} }
} }
messagesArePresentButHidden = true; messagesArePresentButHidden = true;
@ -375,7 +375,7 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC
private void displayMessage(int id, String message, long time, boolean isIncoming, LinphoneChatMessage.State status, RelativeLayout layout, boolean show) { private void displayMessage(int id, String message, long time, boolean isIncoming, LinphoneChatMessage.State status, RelativeLayout layout, boolean show) {
id = checkId(id); id = checkId(id);
BubbleChat bubble = new BubbleChat(layout.getContext(), id, message, null, time, isIncoming, status, previousMessageID); BubbleChat bubble = new BubbleChat(layout.getContext(), id, message, null, time, isIncoming, status, null, previousMessageID);
if (!isIncoming) { if (!isIncoming) {
lastSentMessageBubble = bubble; lastSentMessageBubble = bubble;
} }
@ -395,46 +395,63 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC
private void displayImageMessage(int id, Bitmap image, long time, boolean isIncoming, LinphoneChatMessage.State status, RelativeLayout layout, final String url, boolean show) { private void displayImageMessage(int id, Bitmap image, long time, boolean isIncoming, LinphoneChatMessage.State status, RelativeLayout layout, final String url, boolean show) {
id = checkId(id); id = checkId(id);
BubbleChat bubble = new BubbleChat(layout.getContext(), id, null, image, time, isIncoming, status, previousMessageID); BubbleChat bubble = new BubbleChat(layout.getContext(), id, null, image, time, isIncoming, status, url, previousMessageID);
if (!isIncoming) { if (!isIncoming) {
lastSentMessageBubble = bubble; lastSentMessageBubble = bubble;
} }
final View v = bubble.getView(); final View v = bubble.getView();
final int finalId = id; final int finalId = id;
bubble.setDownloadImageButtonListener(new OnClickListener() {
@Override if (url.startsWith("http")) { // Download
public void onClick(View view) { bubble.setShowOrDownloadText(getString(R.string.download_image));
new Thread(new Runnable() { bubble.setShowOrDownloadImageButtonListener(new OnClickListener() {
@Override @Override
public void run() { public void onClick(View view) {
final Bitmap bm = ChatFragment.downloadImage(url); new Thread(new Runnable() {
if (bm != null) { @Override
if (useLinphoneMessageStorage) { public void run() {
saveImage(bm, finalId, null); final Bitmap bm = ChatFragment.downloadImage(url);
if (bm != null) {
if (useLinphoneMessageStorage) {
saveImage(bm, finalId, null);
} else {
LinphoneActivity.instance().getChatStorage().saveImage(finalId, bm);
}
mHandler.post(new Runnable() {
@Override
public void run() {
((ImageView)v.findViewById(R.id.image)).setImageBitmap(bm);
v.findViewById(R.id.image).setVisibility(View.VISIBLE);
v.findViewById(R.id.download).setVisibility(View.GONE);
}
});
} else { } else {
LinphoneActivity.instance().getChatStorage().saveImage(finalId, bm); mHandler.post(new Runnable() {
@Override
public void run() {
LinphoneActivity.instance().displayCustomToast(getString(R.string.download_image_failed), Toast.LENGTH_LONG);
}
});
} }
mHandler.post(new Runnable() {
@Override
public void run() {
((ImageView)v.findViewById(R.id.image)).setImageBitmap(bm);
v.findViewById(R.id.image).setVisibility(View.VISIBLE);
v.findViewById(R.id.download).setVisibility(View.GONE);
}
});
} else {
mHandler.post(new Runnable() {
@Override
public void run() {
LinphoneActivity.instance().displayCustomToast(getString(R.string.download_image_failed), Toast.LENGTH_LONG);
}
});
} }
}).start();
}
});
} else { // Show
bubble.setShowOrDownloadText(getString(R.string.show_image));
bubble.setShowOrDownloadImageButtonListener(new OnClickListener() {
@Override
public void onClick(View view) {
Bitmap bm = BitmapFactory.decodeFile(url);
if (bm != null) {
((ImageView)v.findViewById(R.id.image)).setImageBitmap(bm);
v.findViewById(R.id.image).setVisibility(View.VISIBLE);
v.findViewById(R.id.download).setVisibility(View.GONE);
} }
}).start(); }
} });
}); }
if (!show) if (!show)
v.setVisibility(View.GONE); v.setVisibility(View.GONE);
@ -657,7 +674,7 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC
mHandler.post(new Runnable() { mHandler.post(new Runnable() {
@Override @Override
public void run() { public void run() {
displayImageMessage(id, fbm, message.getTime(), true, null, messagesLayout, fbm == null ? "" : message.getExternalBodyUrl()); displayImageMessage(id, fbm, message.getTime(), true, null, messagesLayout, message.getExternalBodyUrl());
} }
}); });
} }

View file

@ -28,8 +28,10 @@ import org.linphone.core.LinphoneChatMessage;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.content.Context; import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.Color; import android.graphics.Color;
import android.net.Uri;
import android.text.Html; import android.text.Html;
import android.text.Spannable; import android.text.Spannable;
import android.text.SpannableStringBuilder; import android.text.SpannableStringBuilder;
@ -87,7 +89,7 @@ public class BubbleChat {
private ImageView statusView; private ImageView statusView;
private Button download; private Button download;
public BubbleChat(Context context, int id, String message, Bitmap image, long time, boolean isIncoming, LinphoneChatMessage.State status, int previousID) { public BubbleChat(final Context context, int id, String message, Bitmap image, long time, boolean isIncoming, LinphoneChatMessage.State status, final String url, int previousID) {
view = new RelativeLayout(context); view = new RelativeLayout(context);
LayoutParams layoutParams = new LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); LayoutParams layoutParams = new LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
@ -148,6 +150,16 @@ public class BubbleChat {
} else if (imageView != null) { } else if (imageView != null) {
imageView.setVisibility(View.GONE); imageView.setVisibility(View.GONE);
} }
if (imageView != null) {
imageView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("file://" + url), "image/*");
context.startActivity(intent);
}
});
}
download = (Button) layout.findViewById(R.id.download); download = (Button) layout.findViewById(R.id.download);
if (download != null && image == null && message == null) { if (download != null && image == null && message == null) {
@ -272,9 +284,15 @@ public class BubbleChat {
return Html.fromHtml(text); return Html.fromHtml(text);
} }
public void setDownloadImageButtonListener(OnClickListener onClickListener) { public void setShowOrDownloadImageButtonListener(OnClickListener onClickListener) {
if (download != null) { if (download != null) {
download.setOnClickListener(onClickListener); download.setOnClickListener(onClickListener);
} }
} }
public void setShowOrDownloadText(String buttonName) {
if (download != null) {
download.setText(buttonName);
}
}
} }