From 8e9f7aa1d4f264076654a22e6d07b0735168bb6d Mon Sep 17 00:00:00 2001 From: Simon Morlat Date: Tue, 10 May 2016 16:04:35 +0200 Subject: [PATCH] optimize ChatMessage handling and remove dead code --- src/org/linphone/ChatFragment.java | 37 +++++--------- src/org/linphone/ChatStorage.java | 67 +++----------------------- src/org/linphone/LinphoneActivity.java | 6 --- submodules/linphone | 2 +- 4 files changed, 19 insertions(+), 93 deletions(-) diff --git a/src/org/linphone/ChatFragment.java b/src/org/linphone/ChatFragment.java index feac6ef48..91a8408bd 100644 --- a/src/org/linphone/ChatFragment.java +++ b/src/org/linphone/ChatFragment.java @@ -22,6 +22,7 @@ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import java.util.Locale; @@ -334,60 +335,44 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC } class ChatMessageAdapter extends BaseAdapter { - LinphoneChatMessage[] history; + ArrayList history; Context context; public ChatMessageAdapter(Context context, LinphoneChatMessage[] history) { - this.history = history; + this.history = new ArrayList(Arrays.asList(history)); this.context = context; } public void destroy() { - this.history = null; + history = null; } public void refreshHistory() { - this.history = null; - this.history = chatRoom.getHistory(); + this.history = new ArrayList(Arrays.asList(chatRoom.getHistory())); } public void addMessage(LinphoneChatMessage message) { - LinphoneChatMessage[] newHist = new LinphoneChatMessage[getCount() +1]; - for(int i=0; i< getCount(); i++){ - newHist[i] = this.history[i]; - } - newHist[getCount()] = message; - this.history = newHist; - } - - public void removeMessage(LinphoneChatMessage message) { - LinphoneChatMessage[] newHist = new LinphoneChatMessage[getCount() -1]; - for(int i=0; i< getCount(); i++){ - if(this.history[i].getStorageId() != newHist[i].getStorageId()) - newHist[i] = this.history[i]; - } - newHist[getCount()] = message; - this.history = newHist; + history.add(message); } @Override public int getCount() { - return history.length; + return history.size(); } @Override public LinphoneChatMessage getItem(int position) { - return history[position]; + return history.get(position); } @Override public long getItemId(int position) { - return history[position].getStorageId(); + return history.get(position).getStorageId(); } @Override public View getView(final int position, View convertView, ViewGroup parent) { - LinphoneChatMessage message = history[position]; + LinphoneChatMessage message = history.get(position); BubbleChat bubble = new BubbleChat(context, message, contact); View v = bubble.getView(); @@ -753,7 +738,7 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC if (newChatConversation) { exitNewConversationMode(lAddress.asStringUriOnly()); } else { - invalidate(); + displayBubbleChat(message); } Log.i("Sent message current status: " + message.getStatus()); diff --git a/src/org/linphone/ChatStorage.java b/src/org/linphone/ChatStorage.java index c7a5a36bc..e379e06c3 100644 --- a/src/org/linphone/ChatStorage.java +++ b/src/org/linphone/ChatStorage.java @@ -280,64 +280,14 @@ public class ChatStorage { return drafts; } - public List getMessages(String correspondent) { - List chatMessages = new ArrayList(); - - if (!useNativeAPI) { - Cursor c = db.query(TABLE_NAME, null, "remoteContact LIKE \"" + correspondent + "\"", null, null, null, "id ASC"); - - while (c.moveToNext()) { - try { - String message, timestamp, url; - int id = c.getInt(c.getColumnIndex("id")); - int direction = c.getInt(c.getColumnIndex("direction")); - message = c.getString(c.getColumnIndex("message")); - timestamp = c.getString(c.getColumnIndex("time")); - int status = c.getInt(c.getColumnIndex("status")); - byte[] rawImage = c.getBlob(c.getColumnIndex("image")); - int read = c.getInt(c.getColumnIndex("read")); - url = c.getString(c.getColumnIndex("url")); - - ChatMessage chatMessage = new ChatMessage(id, message, rawImage, timestamp, direction == INCOMING, status, read == READ); - chatMessage.setUrl(url); - chatMessages.add(chatMessage); - } catch (Exception e) { - e.printStackTrace(); - } - } - c.close(); - } else { - LinphoneChatRoom room = LinphoneManager.getLc().getOrCreateChatRoom(correspondent); - LinphoneChatMessage[] history = room.getHistory(); - for (int i = 0; i < history.length; i++) { - LinphoneChatMessage message = history[i]; - - Bitmap bm = null; - String url = message.getExternalBodyUrl(); - if (url != null && !url.startsWith("http")) { - bm = BitmapFactory.decodeFile(url); - } - ChatMessage chatMessage = new ChatMessage(i+1, message.getText(), bm, - String.valueOf(message.getTime()), !message.isOutgoing(), - message.getStatus().toInt(), message.isRead()); - chatMessage.setUrl(url); - chatMessages.add(chatMessage); - } - } - - return chatMessages; - } - public String getTextMessageForId(LinphoneChatRoom chatroom, int id) { String message = null; if (useNativeAPI) { - LinphoneChatMessage[] history = chatroom.getHistory(); - for (LinphoneChatMessage msg : history) { - if (msg.getStorageId() == id) { - message = msg.getText(); - break; - } + LinphoneChatMessage msg = getMessage(chatroom, id); + + if (msg != null) { + message = msg.getText(); } } else { Cursor c = db.query(TABLE_NAME, null, "id LIKE " + id, null, null, null, null); @@ -410,12 +360,9 @@ public class ChatStorage { public void deleteMessage(LinphoneChatRoom chatroom, int id) { if (useNativeAPI) { - LinphoneChatMessage[] history = chatroom.getHistory(); - for (LinphoneChatMessage message : history) { - if (message.getStorageId() == id) { - chatroom.deleteMessage(message); - break; - } + LinphoneChatMessage msg = getMessage(chatroom, id); + if (msg != null){ + chatroom.deleteMessage(msg); } } else { db.delete(TABLE_NAME, "id LIKE " + id, null); diff --git a/src/org/linphone/LinphoneActivity.java b/src/org/linphone/LinphoneActivity.java index 7559e5bcf..b85e24116 100644 --- a/src/org/linphone/LinphoneActivity.java +++ b/src/org/linphone/LinphoneActivity.java @@ -864,10 +864,6 @@ public class LinphoneActivity extends Activity implements OnClickListener, Conta return getChatStorage().getDrafts(); } - public List getChatMessages(String correspondent) { - return getChatStorage().getMessages(correspondent); - } - public void removeFromChatList(String sipUri) { getChatStorage().removeDiscussion(sipUri); } @@ -876,8 +872,6 @@ public class LinphoneActivity extends Activity implements OnClickListener, Conta getChatStorage().deleteDraft(sipUri); } - - public void updateMissedChatCount() { displayMissedChats(getUnreadMessageCount()); } diff --git a/submodules/linphone b/submodules/linphone index 392271334..fa00efddb 160000 --- a/submodules/linphone +++ b/submodules/linphone @@ -1 +1 @@ -Subproject commit 392271334671ff505926215847f42ae79a136e88 +Subproject commit fa00efddbb22cda2dabf6e0629de27ad40bbe45b