Text message status log should not be treated as an error log + formatting

This commit is contained in:
Gautier Pelloux-Prayer 2014-07-29 14:17:28 +02:00
parent 3ab3ed3632
commit 7dc23b0cc1
2 changed files with 139 additions and 139 deletions

View file

@ -102,7 +102,7 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC
private static final int SIZE_MEDIUM = 1000; private static final int SIZE_MEDIUM = 1000;
private static final int SIZE_LARGE = 1500; private static final int SIZE_LARGE = 1500;
private static final int MESSAGES_STEP = 20; private static final int MESSAGES_STEP = 20;
private LinphoneChatRoom chatRoom; private LinphoneChatRoom chatRoom;
private View view; private View view;
private String sipUri; private String sipUri;
@ -119,7 +119,7 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC
private int messagesFilterLimit = 0; private int messagesFilterLimit = 0;
private boolean useLinphoneMessageStorage; private boolean useLinphoneMessageStorage;
private boolean messagesArePresentButHidden = false; private boolean messagesArePresentButHidden = false;
private ProgressBar progressBar; private ProgressBar progressBar;
private int bytesSent; private int bytesSent;
private String uploadServerUri; private String uploadServerUri;
@ -129,39 +129,39 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC
private Thread uploadThread; private Thread uploadThread;
private TextWatcher textWatcher; private TextWatcher textWatcher;
private OnGlobalLayoutListener keyboardListener; private OnGlobalLayoutListener keyboardListener;
@Override @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
sipUri = getArguments().getString("SipUri"); sipUri = getArguments().getString("SipUri");
String displayName = getArguments().getString("DisplayName"); String displayName = getArguments().getString("DisplayName");
String pictureUri = getArguments().getString("PictureUri"); String pictureUri = getArguments().getString("PictureUri");
view = inflater.inflate(R.layout.chat, container, false); view = inflater.inflate(R.layout.chat, container, false);
useLinphoneMessageStorage = getResources().getBoolean(R.bool.use_linphone_chat_storage); useLinphoneMessageStorage = getResources().getBoolean(R.bool.use_linphone_chat_storage);
contactName = (TextView) view.findViewById(R.id.contactName); contactName = (TextView) view.findViewById(R.id.contactName);
contactPicture = (AvatarWithShadow) view.findViewById(R.id.contactPicture); contactPicture = (AvatarWithShadow) view.findViewById(R.id.contactPicture);
sendMessage = (TextView) view.findViewById(R.id.sendMessage); sendMessage = (TextView) view.findViewById(R.id.sendMessage);
sendMessage.setOnClickListener(this); sendMessage.setOnClickListener(this);
remoteComposing = (TextView) view.findViewById(R.id.remoteComposing); remoteComposing = (TextView) view.findViewById(R.id.remoteComposing);
remoteComposing.setVisibility(View.GONE); remoteComposing.setVisibility(View.GONE);
message = (EditText) view.findViewById(R.id.message); message = (EditText) view.findViewById(R.id.message);
if (!getActivity().getResources().getBoolean(R.bool.allow_chat_multiline)) { if (!getActivity().getResources().getBoolean(R.bool.allow_chat_multiline)) {
message.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_SHORT_MESSAGE); message.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_SHORT_MESSAGE);
message.setMaxLines(1); message.setMaxLines(1);
} }
uploadLayout = (RelativeLayout) view.findViewById(R.id.uploadLayout); uploadLayout = (RelativeLayout) view.findViewById(R.id.uploadLayout);
textLayout = (RelativeLayout) view.findViewById(R.id.messageLayout); textLayout = (RelativeLayout) view.findViewById(R.id.messageLayout);
messagesLayout = (RelativeLayout) view.findViewById(R.id.messages); messagesLayout = (RelativeLayout) view.findViewById(R.id.messages);
messagesScrollView = (LinphoneScrollView) view.findViewById(R.id.chatScrollView); messagesScrollView = (LinphoneScrollView) view.findViewById(R.id.chatScrollView);
progressBar = (ProgressBar) view.findViewById(R.id.progressbar); progressBar = (ProgressBar) view.findViewById(R.id.progressbar);
sendImage = (TextView) view.findViewById(R.id.sendPicture); sendImage = (TextView) view.findViewById(R.id.sendPicture);
if (!getResources().getBoolean(R.bool.disable_chat_send_file)) { if (!getResources().getBoolean(R.bool.disable_chat_send_file)) {
registerForContextMenu(sendImage); registerForContextMenu(sendImage);
@ -174,7 +174,7 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC
} else { } else {
sendImage.setEnabled(false); sendImage.setEnabled(false);
} }
cancelUpload = (ImageView) view.findViewById(R.id.cancelUpload); cancelUpload = (ImageView) view.findViewById(R.id.cancelUpload);
cancelUpload.setOnClickListener(new OnClickListener() { cancelUpload.setOnClickListener(new OnClickListener() {
@Override @Override
@ -192,24 +192,24 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC
//Only works if using liblinphone storage //Only works if using liblinphone storage
chatRoom.markAsRead(); chatRoom.markAsRead();
} }
if (savedInstanceState != null) { if (savedInstanceState != null) {
messagesFilterLimit = savedInstanceState.getInt("messagesFilterLimit"); messagesFilterLimit = savedInstanceState.getInt("messagesFilterLimit");
} }
displayChatHeader(displayName, pictureUri); displayChatHeader(displayName, pictureUri);
uploadServerUri = LinphonePreferences.instance().getSharingPictureServerUrl(); uploadServerUri = LinphonePreferences.instance().getSharingPictureServerUrl();
textWatcher = new TextWatcher() { textWatcher = new TextWatcher() {
public void afterTextChanged(Editable arg0) { public void afterTextChanged(Editable arg0) {
} }
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
} }
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3)
{ {
if (message.getText().toString().equals("")) { if (message.getText().toString().equals("")) {
sendMessage.setEnabled(false); sendMessage.setEnabled(false);
@ -220,7 +220,7 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC
} }
} }
}; };
// Force hide keyboard // Force hide keyboard
if (LinphoneActivity.isInstanciated()) { if (LinphoneActivity.isInstanciated()) {
InputMethodManager imm = (InputMethodManager) LinphoneActivity.instance().getSystemService(Context.INPUT_METHOD_SERVICE); InputMethodManager imm = (InputMethodManager) LinphoneActivity.instance().getSystemService(Context.INPUT_METHOD_SERVICE);
@ -240,10 +240,10 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC
} }
}); });
} }
return view; return view;
} }
@Override @Override
public void onSaveInstanceState(Bundle outState) { public void onSaveInstanceState(Bundle outState) {
outState.putString("fileToUploadPath", fileToUploadPath); outState.putString("fileToUploadPath", fileToUploadPath);
@ -251,14 +251,14 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC
outState.putInt("messagesFilterLimit", messagesFilterLimit); outState.putInt("messagesFilterLimit", messagesFilterLimit);
super.onSaveInstanceState(outState); super.onSaveInstanceState(outState);
} }
private void addVirtualKeyboardVisiblityListener() { private void addVirtualKeyboardVisiblityListener() {
keyboardListener = new OnGlobalLayoutListener() { keyboardListener = new OnGlobalLayoutListener() {
@Override @Override
public void onGlobalLayout() { public void onGlobalLayout() {
Rect visibleArea = new Rect(); Rect visibleArea = new Rect();
view.getWindowVisibleDisplayFrame(visibleArea); view.getWindowVisibleDisplayFrame(visibleArea);
int heightDiff = view.getRootView().getHeight() - (visibleArea.bottom - visibleArea.top); int heightDiff = view.getRootView().getHeight() - (visibleArea.bottom - visibleArea.top);
if (heightDiff > 200) { if (heightDiff > 200) {
showKeyboardVisibleMode(); showKeyboardVisibleMode();
@ -267,28 +267,28 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC
} }
} }
}; };
view.getViewTreeObserver().addOnGlobalLayoutListener(keyboardListener); view.getViewTreeObserver().addOnGlobalLayoutListener(keyboardListener);
} }
private void removeVirtualKeyboardVisiblityListener() { private void removeVirtualKeyboardVisiblityListener() {
Compatibility.removeGlobalLayoutListener(view.getViewTreeObserver(), keyboardListener); Compatibility.removeGlobalLayoutListener(view.getViewTreeObserver(), keyboardListener);
} }
public void showKeyboardVisibleMode() { public void showKeyboardVisibleMode() {
LinphoneActivity.instance().hideMenu(true); LinphoneActivity.instance().hideMenu(true);
contactPicture.setVisibility(View.GONE); contactPicture.setVisibility(View.GONE);
scrollToEnd(); scrollToEnd();
} }
public void hideKeyboardVisibleMode() { public void hideKeyboardVisibleMode() {
LinphoneActivity.instance().hideMenu(false); LinphoneActivity.instance().hideMenu(false);
contactPicture.setVisibility(View.VISIBLE); contactPicture.setVisibility(View.VISIBLE);
scrollToEnd(); scrollToEnd();
} }
private void invalidate() { private void invalidate() {
messagesFilterLimit += MESSAGES_STEP; messagesFilterLimit += MESSAGES_STEP;
if (!messagesArePresentButHidden) { if (!messagesArePresentButHidden) {
LinphoneChatMessage[] history = chatRoom.getHistory(); LinphoneChatMessage[] history = chatRoom.getHistory();
for (int i = 0; i < history.length; i++) { for (int i = 0; i < history.length; i++) {
@ -302,25 +302,25 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC
} }
messagesArePresentButHidden = true; messagesArePresentButHidden = true;
} }
invalidate(messagesFilterLimit); invalidate(messagesFilterLimit);
} }
private void invalidate(final int limit) { private void invalidate(final int limit) {
mHandler.post(new Runnable() { mHandler.post(new Runnable() {
@Override @Override
public void run() { public void run() {
int start = messagesLayout.getChildCount() - messagesFilterLimit; int start = messagesLayout.getChildCount() - messagesFilterLimit;
if (start < 0) if (start < 0)
start = 0; start = 0;
for (int i = start; i < messagesLayout.getChildCount(); i++) { for (int i = start; i < messagesLayout.getChildCount(); i++) {
messagesLayout.getChildAt(i).setVisibility(View.VISIBLE); messagesLayout.getChildAt(i).setVisibility(View.VISIBLE);
} }
LinphoneActivity.instance().updateMissedChatCount(); LinphoneActivity.instance().updateMissedChatCount();
if (start != 0) { if (start != 0) {
messagesScrollView.setScrollViewListener(new ScrollViewListener() { messagesScrollView.setScrollViewListener(new ScrollViewListener() {
@Override @Override
@ -342,7 +342,7 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC
} }
}); });
} }
private void displayChatHeader(String displayName, String pictureUri) { private void displayChatHeader(String displayName, String pictureUri) {
if (displayName == null && getResources().getBoolean(R.bool.only_display_username_if_unknown) && LinphoneUtils.isSipAddress(sipUri)) { if (displayName == null && getResources().getBoolean(R.bool.only_display_username_if_unknown) && LinphoneUtils.isSipAddress(sipUri)) {
contactName.setText(LinphoneUtils.getUsernameFromAddress(sipUri)); contactName.setText(LinphoneUtils.getUsernameFromAddress(sipUri));
@ -351,25 +351,25 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC
} else { } else {
contactName.setText(displayName); contactName.setText(displayName);
} }
if (pictureUri != null) { if (pictureUri != null) {
LinphoneUtils.setImagePictureFromUri(view.getContext(), contactPicture.getView(), Uri.parse(pictureUri), R.drawable.unknown_small); LinphoneUtils.setImagePictureFromUri(view.getContext(), contactPicture.getView(), Uri.parse(pictureUri), R.drawable.unknown_small);
} else { } else {
contactPicture.setImageResource(R.drawable.unknown_small); contactPicture.setImageResource(R.drawable.unknown_small);
} }
} }
private int checkId(int id) { private int checkId(int id) {
if (id <= 0) { if (id <= 0) {
return previousMessageID + 1; return previousMessageID + 1;
} }
return id; return id;
} }
private BubbleChat displayMessage(int id, String message, long time, boolean isIncoming, LinphoneChatMessage.State status, RelativeLayout layout) { private BubbleChat displayMessage(int id, String message, long time, boolean isIncoming, LinphoneChatMessage.State status, RelativeLayout layout) {
return displayMessage(id, message, time, isIncoming, status, layout, true); return displayMessage(id, message, time, isIncoming, status, layout, true);
} }
private BubbleChat displayMessage(int id, String message, long time, boolean isIncoming, LinphoneChatMessage.State status, RelativeLayout layout, boolean show) { private BubbleChat 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, null, previousMessageID); BubbleChat bubble = new BubbleChat(layout.getContext(), id, message, null, time, isIncoming, status, null, previousMessageID);
@ -378,23 +378,23 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC
lastSentMessagesBubbles = new ArrayList<BubbleChat>(); lastSentMessagesBubbles = new ArrayList<BubbleChat>();
lastSentMessagesBubbles.add(bubble); lastSentMessagesBubbles.add(bubble);
} }
View v = bubble.getView(); View v = bubble.getView();
if (!show) if (!show)
v.setVisibility(View.GONE); v.setVisibility(View.GONE);
previousMessageID = id; previousMessageID = id;
layout.addView(v); layout.addView(v);
registerForContextMenu(v); registerForContextMenu(v);
return bubble; return bubble;
} }
private BubbleChat displayImageMessage(int id, Bitmap image, long time, boolean isIncoming, LinphoneChatMessage.State status, RelativeLayout layout, final String url) { private BubbleChat displayImageMessage(int id, Bitmap image, long time, boolean isIncoming, LinphoneChatMessage.State status, RelativeLayout layout, final String url) {
return displayImageMessage(id, image, time, isIncoming, status, layout, url, true); return displayImageMessage(id, image, time, isIncoming, status, layout, url, true);
} }
private BubbleChat displayImageMessage(int id, Bitmap image, long time, boolean isIncoming, LinphoneChatMessage.State status, RelativeLayout layout, final String url, boolean show) { private BubbleChat 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);
final BubbleChat bubble = new BubbleChat(layout.getContext(), id, null, image, time, isIncoming, status, url, previousMessageID); final BubbleChat bubble = new BubbleChat(layout.getContext(), id, null, image, time, isIncoming, status, url, previousMessageID);
@ -406,7 +406,7 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC
final View v = bubble.getView(); final View v = bubble.getView();
final int finalId = id; final int finalId = id;
if (url.startsWith("http")) { // Download if (url.startsWith("http")) { // Download
bubble.setShowOrDownloadText(getString(R.string.download_image)); bubble.setShowOrDownloadText(getString(R.string.download_image));
bubble.setShowOrDownloadImageButtonListener(new OnClickListener() { bubble.setShowOrDownloadImageButtonListener(new OnClickListener() {
@ -414,7 +414,7 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC
public void onClick(View view) { public void onClick(View view) {
v.findViewById(R.id.spinner).setVisibility(View.VISIBLE); v.findViewById(R.id.spinner).setVisibility(View.VISIBLE);
v.findViewById(R.id.download).setVisibility(View.GONE); v.findViewById(R.id.download).setVisibility(View.GONE);
new Thread(new Runnable() { new Thread(new Runnable() {
@Override @Override
public void run() { public void run() {
@ -462,14 +462,14 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC
} }
}); });
} }
if (!show) if (!show)
v.setVisibility(View.GONE); v.setVisibility(View.GONE);
previousMessageID = id; previousMessageID = id;
layout.addView(v); layout.addView(v);
registerForContextMenu(v); registerForContextMenu(v);
return bubble; return bubble;
} }
@ -484,7 +484,7 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC
} else if (LinphoneActivity.isInstanciated()) { } else if (LinphoneActivity.isInstanciated()) {
LinphoneActivity.instance().getChatStorage().deleteDraft(sipUri); LinphoneActivity.instance().getChatStorage().deleteDraft(sipUri);
} }
messagesFilterLimit = 0; messagesFilterLimit = 0;
sipUri = newSipUri; sipUri = newSipUri;
if (LinphoneActivity.isInstanciated()) { if (LinphoneActivity.isInstanciated()) {
@ -493,11 +493,11 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC
draft = ""; draft = "";
message.setText(draft); message.setText(draft);
} }
displayChatHeader(displayName, pictureUri); displayChatHeader(displayName, pictureUri);
displayMessages(); displayMessages();
} }
@Override @Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
if (v.getId() == R.id.sendPicture) { if (v.getId() == R.id.sendPicture) {
@ -516,14 +516,14 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC
} else { } else {
menu.add(v.getId(), MENU_COPY_TEXT, 0, getString(R.string.copy_text)); menu.add(v.getId(), MENU_COPY_TEXT, 0, getString(R.string.copy_text));
} }
LinphoneChatMessage msg = getMessageForId(v.getId()); LinphoneChatMessage msg = getMessageForId(v.getId());
if (msg != null && msg.getStatus() == LinphoneChatMessage.State.NotDelivered) { if (msg != null && msg.getStatus() == LinphoneChatMessage.State.NotDelivered) {
menu.add(v.getId(), MENU_RESEND_MESSAGE, 0, getString(R.string.retry)); menu.add(v.getId(), MENU_RESEND_MESSAGE, 0, getString(R.string.retry));
} }
} }
} }
@Override @Override
public boolean onContextItemSelected(MenuItem item) { public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) { switch (item.getItemId()) {
@ -555,7 +555,7 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC
} }
return true; return true;
} }
@Override @Override
public void onPause() { public void onPause() {
latestImageMessages = null; latestImageMessages = null;
@ -563,12 +563,12 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC
removeVirtualKeyboardVisiblityListener(); removeVirtualKeyboardVisiblityListener();
LinphoneService.instance().removeMessageNotification(); LinphoneService.instance().removeMessageNotification();
if (LinphoneManager.isInstanciated()) if (LinphoneManager.isInstanciated())
LinphoneManager.getInstance().setOnComposingReceivedListener(null); LinphoneManager.getInstance().setOnComposingReceivedListener(null);
super.onPause(); super.onPause();
if (!message.getText().toString().equals("") && LinphoneActivity.isInstanciated()) { if (!message.getText().toString().equals("") && LinphoneActivity.isInstanciated()) {
ChatStorage chatStorage = LinphoneActivity.instance().getChatStorage(); ChatStorage chatStorage = LinphoneActivity.instance().getChatStorage();
if (chatStorage.getDraft(sipUri) == null) { if (chatStorage.getDraft(sipUri) == null) {
@ -580,35 +580,35 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC
LinphoneActivity.instance().getChatStorage().deleteDraft(sipUri); LinphoneActivity.instance().getChatStorage().deleteDraft(sipUri);
} }
} }
@SuppressLint("UseSparseArrays") @SuppressLint("UseSparseArrays")
@Override @Override
public void onResume() { public void onResume() {
latestImageMessages = new HashMap<Integer, String>(); latestImageMessages = new HashMap<Integer, String>();
message.addTextChangedListener(textWatcher); message.addTextChangedListener(textWatcher);
addVirtualKeyboardVisiblityListener(); addVirtualKeyboardVisiblityListener();
if (LinphoneManager.isInstanciated()) if (LinphoneManager.isInstanciated())
LinphoneManager.getInstance().setOnComposingReceivedListener(this); LinphoneManager.getInstance().setOnComposingReceivedListener(this);
super.onResume(); super.onResume();
if (LinphoneActivity.isInstanciated()) { if (LinphoneActivity.isInstanciated()) {
LinphoneActivity.instance().selectMenu(FragmentsAvailable.CHAT); LinphoneActivity.instance().selectMenu(FragmentsAvailable.CHAT);
LinphoneActivity.instance().updateChatFragment(this); LinphoneActivity.instance().updateChatFragment(this);
if (getResources().getBoolean(R.bool.show_statusbar_only_on_dialer)) { if (getResources().getBoolean(R.bool.show_statusbar_only_on_dialer)) {
LinphoneActivity.instance().hideStatusBar(); LinphoneActivity.instance().hideStatusBar();
} }
} }
if (LinphoneActivity.isInstanciated()) { if (LinphoneActivity.isInstanciated()) {
String draft = LinphoneActivity.instance().getChatStorage().getDraft(sipUri); String draft = LinphoneActivity.instance().getChatStorage().getDraft(sipUri);
message.setText(draft); message.setText(draft);
} }
remoteComposing.setVisibility(chatRoom.isRemoteComposing() ? View.VISIBLE : View.GONE); remoteComposing.setVisibility(chatRoom.isRemoteComposing() ? View.VISIBLE : View.GONE);
displayMessages(); displayMessages();
} }
@ -616,7 +616,7 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC
public void onClick(View v) { public void onClick(View v) {
sendTextMessage(); sendTextMessage();
} }
private void displayMessages() { private void displayMessages() {
messagesLayout.removeAllViews(); messagesLayout.removeAllViews();
messagesArePresentButHidden = false; messagesArePresentButHidden = false;
@ -625,53 +625,53 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC
invalidate(); invalidate();
scrollToEnd(); scrollToEnd();
} }
private void sendTextMessage() { private void sendTextMessage() {
sendTextMessage(message.getText().toString()); sendTextMessage(message.getText().toString());
message.setText(""); message.setText("");
} }
private void sendTextMessage(String messageToSend) { private void sendTextMessage(String messageToSend) {
LinphoneCore lc = LinphoneManager.getLcIfManagerNotDestroyedOrNull(); LinphoneCore lc = LinphoneManager.getLcIfManagerNotDestroyedOrNull();
boolean isNetworkReachable = lc == null ? false : lc.isNetworkReachable(); boolean isNetworkReachable = lc == null ? false : lc.isNetworkReachable();
if (chatRoom != null && messageToSend != null && messageToSend.length() > 0 && isNetworkReachable) { if (chatRoom != null && messageToSend != null && messageToSend.length() > 0 && isNetworkReachable) {
LinphoneChatMessage chatMessage = chatRoom.createLinphoneChatMessage(messageToSend); LinphoneChatMessage chatMessage = chatRoom.createLinphoneChatMessage(messageToSend);
chatRoom.sendMessage(chatMessage, this); chatRoom.sendMessage(chatMessage, this);
int newId = -1; int newId = -1;
if (LinphoneActivity.isInstanciated()) { if (LinphoneActivity.isInstanciated()) {
newId = LinphoneActivity.instance().onMessageSent(sipUri, messageToSend); newId = LinphoneActivity.instance().onMessageSent(sipUri, messageToSend);
} }
BubbleChat bubble = displayMessage(newId, messageToSend, System.currentTimeMillis(), false, State.InProgress, messagesLayout); BubbleChat bubble = displayMessage(newId, messageToSend, System.currentTimeMillis(), false, State.InProgress, messagesLayout);
bubble.setNativeMessageObject(chatMessage); bubble.setNativeMessageObject(chatMessage);
Log.e("Sent message current status: " + chatMessage.getStatus()); Log.i("Sent message current status: " + chatMessage.getStatus());
scrollToEnd(); scrollToEnd();
} else if (!isNetworkReachable && LinphoneActivity.isInstanciated()) { } else if (!isNetworkReachable && LinphoneActivity.isInstanciated()) {
LinphoneActivity.instance().displayCustomToast(getString(R.string.error_network_unreachable), Toast.LENGTH_LONG); LinphoneActivity.instance().displayCustomToast(getString(R.string.error_network_unreachable), Toast.LENGTH_LONG);
} }
} }
private void sendImageMessage(String url, Bitmap bitmap) { private void sendImageMessage(String url, Bitmap bitmap) {
LinphoneCore lc = LinphoneManager.getLcIfManagerNotDestroyedOrNull(); LinphoneCore lc = LinphoneManager.getLcIfManagerNotDestroyedOrNull();
boolean isNetworkReachable = lc == null ? false : lc.isNetworkReachable(); boolean isNetworkReachable = lc == null ? false : lc.isNetworkReachable();
if (chatRoom != null && url != null && url.length() > 0 && isNetworkReachable) { if (chatRoom != null && url != null && url.length() > 0 && isNetworkReachable) {
LinphoneChatMessage chatMessage = chatRoom.createLinphoneChatMessage(""); LinphoneChatMessage chatMessage = chatRoom.createLinphoneChatMessage("");
chatMessage.setExternalBodyUrl(url); chatMessage.setExternalBodyUrl(url);
chatRoom.sendMessage(chatMessage, this); chatRoom.sendMessage(chatMessage, this);
int newId = -1; int newId = -1;
if (LinphoneActivity.isInstanciated()) { if (LinphoneActivity.isInstanciated()) {
newId = LinphoneActivity.instance().onMessageSent(sipUri, bitmap, url); newId = LinphoneActivity.instance().onMessageSent(sipUri, bitmap, url);
} }
newId = checkId(newId); newId = checkId(newId);
latestImageMessages.put(newId, url); latestImageMessages.put(newId, url);
if (useLinphoneMessageStorage) if (useLinphoneMessageStorage)
url = saveImage(bitmap, newId, chatMessage); url = saveImage(bitmap, newId, chatMessage);
BubbleChat bubble = displayImageMessage(newId, bitmap, System.currentTimeMillis(), false, State.InProgress, messagesLayout, url); BubbleChat bubble = displayImageMessage(newId, bitmap, System.currentTimeMillis(), false, State.InProgress, messagesLayout, url);
bubble.setNativeMessageObject(chatMessage); bubble.setNativeMessageObject(chatMessage);
scrollToEnd(); scrollToEnd();
@ -679,13 +679,13 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC
LinphoneActivity.instance().displayCustomToast(getString(R.string.error_network_unreachable), Toast.LENGTH_LONG); LinphoneActivity.instance().displayCustomToast(getString(R.string.error_network_unreachable), Toast.LENGTH_LONG);
} }
} }
private LinphoneChatMessage getMessageForId(int id) { private LinphoneChatMessage getMessageForId(int id) {
LinphoneChatMessage msg = null; LinphoneChatMessage msg = null;
try { try {
msg = LinphoneActivity.instance().getChatStorage().getMessage(chatRoom, id); msg = LinphoneActivity.instance().getChatStorage().getMessage(chatRoom, id);
} catch (Exception e) {} } catch (Exception e) {}
if (msg == null) { if (msg == null) {
for (BubbleChat bubble : lastSentMessagesBubbles) { for (BubbleChat bubble : lastSentMessagesBubbles) {
if (bubble.getId() == id) { if (bubble.getId() == id) {
@ -693,10 +693,10 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC
} }
} }
} }
return msg; return msg;
} }
private void hideMessageBubble(int id) { private void hideMessageBubble(int id) {
for (int i = 0; i < messagesLayout.getChildCount(); i++) { for (int i = 0; i < messagesLayout.getChildCount(); i++) {
View v = messagesLayout.getChildAt(i); View v = messagesLayout.getChildAt(i);
@ -706,34 +706,34 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC
} }
} }
} }
private void resendMessage(int id) { private void resendMessage(int id) {
LinphoneChatMessage message = getMessageForId(id); LinphoneChatMessage message = getMessageForId(id);
if (message == null) if (message == null)
return; return;
LinphoneActivity.instance().getChatStorage().deleteMessage(chatRoom, id); LinphoneActivity.instance().getChatStorage().deleteMessage(chatRoom, id);
hideMessageBubble(id); hideMessageBubble(id);
if (message.getText() != null && message.getText().length() > 0) { if (message.getText() != null && message.getText().length() > 0) {
sendTextMessage(message.getText()); sendTextMessage(message.getText());
} else { } else {
sendImageMessage(message.getExternalBodyUrl(), null); sendImageMessage(message.getExternalBodyUrl(), null);
} }
} }
private void scrollToEnd() { private void scrollToEnd() {
messagesScrollView.postDelayed(new Runnable() { messagesScrollView.postDelayed(new Runnable() {
@Override @Override
public void run() { public void run() {
messagesScrollView.fullScroll(View.FOCUS_DOWN); messagesScrollView.fullScroll(View.FOCUS_DOWN);
//Only works if using liblinphone storage //Only works if using liblinphone storage
chatRoom.markAsRead(); chatRoom.markAsRead();
} }
}, 100); }, 100);
} }
private void copyTextMessageToClipboard(int id) { private void copyTextMessageToClipboard(int id) {
String msg = LinphoneActivity.instance().getChatStorage().getTextMessageForId(chatRoom, id); String msg = LinphoneActivity.instance().getChatStorage().getTextMessageForId(chatRoom, id);
if (msg != null) { if (msg != null) {
@ -797,7 +797,7 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC
} }
} }
} }
if (lastSentMessagesBubbles != null && lastSentMessagesBubbles.size() > 0) { if (lastSentMessagesBubbles != null && lastSentMessagesBubbles.size() > 0) {
for (BubbleChat bubble : lastSentMessagesBubbles) { for (BubbleChat bubble : lastSentMessagesBubbles) {
if (bubble.getNativeMessageObject() == finalMessage) { if (bubble.getNativeMessageObject() == finalMessage) {
@ -809,11 +809,11 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC
}); });
} }
} }
public String getSipUri() { public String getSipUri() {
return sipUri; return sipUri;
} }
private void pickImage() { private void pickImage() {
final List<Intent> cameraIntents = new ArrayList<Intent>(); final List<Intent> cameraIntents = new ArrayList<Intent>();
final Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); final Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
@ -831,7 +831,7 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC
startActivityForResult(chooserIntent, ADD_PHOTO); startActivityForResult(chooserIntent, ADD_PHOTO);
} }
public static Bitmap downloadImage(String stringUrl) { public static Bitmap downloadImage(String stringUrl) {
URL url; URL url;
Bitmap bm = null; Bitmap bm = null;
@ -840,23 +840,23 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC
URLConnection ucon = url.openConnection(); URLConnection ucon = url.openConnection();
InputStream is = ucon.getInputStream(); InputStream is = ucon.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is); BufferedInputStream bis = new BufferedInputStream(is);
ByteArrayBuffer baf = new ByteArrayBuffer(50); ByteArrayBuffer baf = new ByteArrayBuffer(50);
int current = 0; int current = 0;
while ((current = bis.read()) != -1) { while ((current = bis.read()) != -1) {
baf.append((byte) current); baf.append((byte) current);
} }
byte[] rawImage = baf.toByteArray(); byte[] rawImage = baf.toByteArray();
bm = BitmapFactory.decodeByteArray(rawImage, 0, rawImage.length); bm = BitmapFactory.decodeByteArray(rawImage, 0, rawImage.length);
bis.close(); bis.close();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
return bm; return bm;
} }
private void saveImage(int id) { private void saveImage(int id) {
byte[] rawImage = LinphoneActivity.instance().getChatStorage().getRawImageFromMessage(id); byte[] rawImage = LinphoneActivity.instance().getChatStorage().getRawImageFromMessage(id);
Bitmap bm = BitmapFactory.decodeByteArray(rawImage, 0, rawImage.length); Bitmap bm = BitmapFactory.decodeByteArray(rawImage, 0, rawImage.length);
@ -866,7 +866,7 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC
Toast.makeText(getActivity(), getString(R.string.image_not_saved), Toast.LENGTH_LONG).show(); Toast.makeText(getActivity(), getString(R.string.image_not_saved), Toast.LENGTH_LONG).show();
} }
} }
private String saveImage(Bitmap bm, int id, LinphoneChatMessage message) { private String saveImage(Bitmap bm, int id, LinphoneChatMessage message) {
try { try {
String path = Environment.getExternalStorageDirectory().toString(); String path = Environment.getExternalStorageDirectory().toString();
@ -875,17 +875,17 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC
path += "Pictures/"; path += "Pictures/";
File directory = new File(path); File directory = new File(path);
directory.mkdirs(); directory.mkdirs();
String filename = getString(R.string.picture_name_format).replace("%s", String.valueOf(id)); String filename = getString(R.string.picture_name_format).replace("%s", String.valueOf(id));
File file = new File(path, filename); File file = new File(path, filename);
OutputStream fOut = null; OutputStream fOut = null;
fOut = new FileOutputStream(file); fOut = new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.JPEG, 100, fOut); bm.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
fOut.flush(); fOut.flush();
fOut.close(); fOut.close();
if (useLinphoneMessageStorage) { if (useLinphoneMessageStorage) {
//Update url path in liblinphone database //Update url path in liblinphone database
if (message == null) { if (message == null) {
@ -900,7 +900,7 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC
message.setExternalBodyUrl(path + filename); message.setExternalBodyUrl(path + filename);
chatRoom.updateUrl(message); chatRoom.updateUrl(message);
} }
MediaStore.Images.Media.insertImage(getActivity().getContentResolver(),file.getAbsolutePath(),file.getName(),file.getName()); MediaStore.Images.Media.insertImage(getActivity().getContentResolver(),file.getAbsolutePath(),file.getName(),file.getName());
return file.getAbsolutePath(); return file.getAbsolutePath();
} catch (Exception e) { } catch (Exception e) {
@ -908,7 +908,7 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC
} }
return null; return null;
} }
private long hashBitmap(Bitmap bmp){ private long hashBitmap(Bitmap bmp){
long hash = 31; // Random prime number long hash = 31; // Random prime number
for(int x = 0; x < bmp.getWidth(); x++){ for(int x = 0; x < bmp.getWidth(); x++){
@ -918,38 +918,38 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC
} }
return hash; return hash;
} }
private String uploadImage(String filePath, Bitmap file, int compressorQuality, final int imageSize) { private String uploadImage(String filePath, Bitmap file, int compressorQuality, final int imageSize) {
String fileName; String fileName;
if (filePath != null) { if (filePath != null) {
File sourceFile = new File(filePath); File sourceFile = new File(filePath);
fileName = sourceFile.getName(); fileName = sourceFile.getName();
} else { } else {
fileName = getString(R.string.temp_photo_name_with_date).replace("%s", String.valueOf(System.currentTimeMillis())); fileName = getString(R.string.temp_photo_name_with_date).replace("%s", String.valueOf(System.currentTimeMillis()));
} }
if (getResources().getBoolean(R.bool.hash_images_as_name_before_upload)) { if (getResources().getBoolean(R.bool.hash_images_as_name_before_upload)) {
fileName = String.valueOf(hashBitmap(file)) + ".jpg"; fileName = String.valueOf(hashBitmap(file)) + ".jpg";
} }
String response = null; String response = null;
HttpURLConnection conn = null; HttpURLConnection conn = null;
try { try {
String lineEnd = "\r\n"; String lineEnd = "\r\n";
String twoHyphens = "--"; String twoHyphens = "--";
String boundary = "---------------------------14737809831466499882746641449"; String boundary = "---------------------------14737809831466499882746641449";
URL url = new URL(uploadServerUri); URL url = new URL(uploadServerUri);
conn = (HttpURLConnection) url.openConnection(); conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true); conn.setDoInput(true);
conn.setDoOutput(true); conn.setDoOutput(true);
conn.setUseCaches(false); conn.setUseCaches(false);
conn.setRequestMethod("POST"); conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive"); conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("ENCTYPE", "multipart/form-data"); conn.setRequestProperty("ENCTYPE", "multipart/form-data");
conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary); conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
conn.setRequestProperty("uploaded_file", fileName); conn.setRequestProperty("uploaded_file", fileName);
ProgressOutputStream pos = new ProgressOutputStream(conn.getOutputStream()); ProgressOutputStream pos = new ProgressOutputStream(conn.getOutputStream());
pos.setListener(new OutputStreamListener() { pos.setListener(new OutputStreamListener() {
@Override @Override
@ -959,20 +959,20 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC
} }
}); });
DataOutputStream dos = new DataOutputStream(pos); DataOutputStream dos = new DataOutputStream(pos);
dos.writeBytes(lineEnd + twoHyphens + boundary + lineEnd); dos.writeBytes(lineEnd + twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"userfile\"; filename=\""+ fileName + "\"" + lineEnd); dos.writeBytes("Content-Disposition: form-data; name=\"userfile\"; filename=\""+ fileName + "\"" + lineEnd);
dos.writeBytes("Content-Type: application/octet-stream" + lineEnd); dos.writeBytes("Content-Type: application/octet-stream" + lineEnd);
dos.writeBytes(lineEnd); dos.writeBytes(lineEnd);
file.compress(CompressFormat.JPEG, compressorQuality, dos); file.compress(CompressFormat.JPEG, compressorQuality, dos);
dos.writeBytes(lineEnd); dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
dos.flush(); dos.flush();
dos.close(); dos.close();
InputStream is = conn.getInputStream(); InputStream is = conn.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream();
@ -984,7 +984,7 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC
byte[] bytesReceived = baos.toByteArray(); byte[] bytesReceived = baos.toByteArray();
baos.close(); baos.close();
is.close(); is.close();
response = new String(bytesReceived); response = new String(bytesReceived);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
@ -993,10 +993,10 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC
conn.disconnect(); conn.disconnect();
} }
} }
return response; return response;
} }
public String getRealPathFromURI(Uri contentUri) { public String getRealPathFromURI(Uri contentUri) {
String[] proj = { MediaStore.Images.Media.DATA }; String[] proj = { MediaStore.Images.Media.DATA };
CursorLoader loader = new CursorLoader(getActivity(), contentUri, proj, null, null, null); CursorLoader loader = new CursorLoader(getActivity(), contentUri, proj, null, null, null);
@ -1009,7 +1009,7 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC
} }
return null; return null;
} }
private void showPopupMenuAskingImageSize(final String filePath, final Bitmap image) { private void showPopupMenuAskingImageSize(final String filePath, final Bitmap image) {
fileToUploadPath = filePath; fileToUploadPath = filePath;
imageToUpload = image; imageToUpload = image;
@ -1017,17 +1017,17 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC
sendImage.showContextMenu(); sendImage.showContextMenu();
} catch (Exception e) { e.printStackTrace(); }; } catch (Exception e) { e.printStackTrace(); };
} }
private void uploadAndSendImage(final String filePath, final Bitmap image, final ImageSize size) { private void uploadAndSendImage(final String filePath, final Bitmap image, final ImageSize size) {
uploadLayout.setVisibility(View.VISIBLE); uploadLayout.setVisibility(View.VISIBLE);
textLayout.setVisibility(View.GONE); textLayout.setVisibility(View.GONE);
uploadThread = new Thread(new Runnable() { uploadThread = new Thread(new Runnable() {
@Override @Override
public void run() { public void run() {
Bitmap bm = null; Bitmap bm = null;
String url = null; String url = null;
if (!uploadThread.isInterrupted()) { if (!uploadThread.isInterrupted()) {
if (filePath != null) { if (filePath != null) {
bm = BitmapFactory.decodeFile(filePath); bm = BitmapFactory.decodeFile(filePath);
@ -1043,7 +1043,7 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC
bm = image; bm = image;
} }
} }
// Rotate the bitmap if possible/needed, using EXIF data // Rotate the bitmap if possible/needed, using EXIF data
try { try {
if (filePath != null) { if (filePath != null) {
@ -1062,18 +1062,18 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
ByteArrayOutputStream outStream = new ByteArrayOutputStream(); ByteArrayOutputStream outStream = new ByteArrayOutputStream();
if (bm != null) { if (bm != null) {
bm.compress(CompressFormat.JPEG, COMPRESSOR_QUALITY, outStream); bm.compress(CompressFormat.JPEG, COMPRESSOR_QUALITY, outStream);
} }
if (!uploadThread.isInterrupted() && bm != null) { if (!uploadThread.isInterrupted() && bm != null) {
url = uploadImage(filePath, bm, COMPRESSOR_QUALITY, outStream.size()); url = uploadImage(filePath, bm, COMPRESSOR_QUALITY, outStream.size());
File file = new File(Environment.getExternalStorageDirectory(), getString(R.string.temp_photo_name)); File file = new File(Environment.getExternalStorageDirectory(), getString(R.string.temp_photo_name));
file.delete(); file.delete();
} }
if (!uploadThread.isInterrupted()) { if (!uploadThread.isInterrupted()) {
final Bitmap fbm = bm; final Bitmap fbm = bm;
final String furl = url; final String furl = url;
@ -1095,14 +1095,14 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC
}); });
uploadThread.start(); uploadThread.start();
} }
@Override @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) { public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == ADD_PHOTO && resultCode == Activity.RESULT_OK) { if (requestCode == ADD_PHOTO && resultCode == Activity.RESULT_OK) {
if (data != null && data.getExtras() != null && data.getExtras().get("data") != null) { if (data != null && data.getExtras() != null && data.getExtras().get("data") != null) {
Bitmap bm = (Bitmap) data.getExtras().get("data"); Bitmap bm = (Bitmap) data.getExtras().get("data");
showPopupMenuAskingImageSize(null, bm); showPopupMenuAskingImageSize(null, bm);
} }
else if (data != null && data.getData() != null) { else if (data != null && data.getData() != null) {
String filePath = getRealPathFromURI(data.getData()); String filePath = getRealPathFromURI(data.getData());
showPopupMenuAskingImageSize(filePath, null); showPopupMenuAskingImageSize(filePath, null);
@ -1123,24 +1123,24 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC
super.onActivityResult(requestCode, resultCode, data); super.onActivityResult(requestCode, resultCode, data);
} }
} }
class ProgressOutputStream extends OutputStream { class ProgressOutputStream extends OutputStream {
OutputStream outputStream; OutputStream outputStream;
private OutputStreamListener listener; private OutputStreamListener listener;
public ProgressOutputStream(OutputStream stream) { public ProgressOutputStream(OutputStream stream) {
outputStream = stream; outputStream = stream;
} }
public void setListener(OutputStreamListener listener) { public void setListener(OutputStreamListener listener) {
this.listener = listener; this.listener = listener;
} }
@Override @Override
public void write(int oneByte) throws IOException { public void write(int oneByte) throws IOException {
outputStream.write(oneByte); outputStream.write(oneByte);
} }
@Override @Override
public void write(byte[] buffer, int offset, int count) public void write(byte[] buffer, int offset, int count)
throws IOException { throws IOException {
@ -1148,11 +1148,11 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC
outputStream.write(buffer, offset, count); outputStream.write(buffer, offset, count);
} }
} }
interface OutputStreamListener { interface OutputStreamListener {
public void onBytesWrite(int count); public void onBytesWrite(int count);
} }
enum ImageSize { enum ImageSize {
SMALL, SMALL,
MEDIUM, MEDIUM,

View file

@ -811,7 +811,7 @@ public class LinphoneManager implements LinphoneCoreListener {
} }
private synchronized void allowSIPCalls() { private synchronized void allowSIPCalls() {
if (savedMaxCallWhileGsmIncall == 0) { if (savedMaxCallWhileGsmIncall == 0) {
Log.w("SIP calls are already allowed as no GSM call knowned to be running"); Log.w("SIP calls are already allowed as no GSM call known to be running");
return; return;
} }
mLc.setMaxCalls(savedMaxCallWhileGsmIncall); mLc.setMaxCalls(savedMaxCallWhileGsmIncall);