optimize ChatMessage handling and remove dead code

This commit is contained in:
Simon Morlat 2016-05-10 16:04:35 +02:00
parent c32a058b9b
commit 8e9f7aa1d4
4 changed files with 19 additions and 93 deletions

View file

@ -22,6 +22,7 @@ import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
@ -334,60 +335,44 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC
} }
class ChatMessageAdapter extends BaseAdapter { class ChatMessageAdapter extends BaseAdapter {
LinphoneChatMessage[] history; ArrayList<LinphoneChatMessage> history;
Context context; Context context;
public ChatMessageAdapter(Context context, LinphoneChatMessage[] history) { public ChatMessageAdapter(Context context, LinphoneChatMessage[] history) {
this.history = history; this.history = new ArrayList<LinphoneChatMessage>(Arrays.asList(history));
this.context = context; this.context = context;
} }
public void destroy() { public void destroy() {
this.history = null; history = null;
} }
public void refreshHistory() { public void refreshHistory() {
this.history = null; this.history = new ArrayList<LinphoneChatMessage>(Arrays.asList(chatRoom.getHistory()));
this.history = chatRoom.getHistory();
} }
public void addMessage(LinphoneChatMessage message) { public void addMessage(LinphoneChatMessage message) {
LinphoneChatMessage[] newHist = new LinphoneChatMessage[getCount() +1]; history.add(message);
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;
} }
@Override @Override
public int getCount() { public int getCount() {
return history.length; return history.size();
} }
@Override @Override
public LinphoneChatMessage getItem(int position) { public LinphoneChatMessage getItem(int position) {
return history[position]; return history.get(position);
} }
@Override @Override
public long getItemId(int position) { public long getItemId(int position) {
return history[position].getStorageId(); return history.get(position).getStorageId();
} }
@Override @Override
public View getView(final int position, View convertView, ViewGroup parent) { 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); BubbleChat bubble = new BubbleChat(context, message, contact);
View v = bubble.getView(); View v = bubble.getView();
@ -753,7 +738,7 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC
if (newChatConversation) { if (newChatConversation) {
exitNewConversationMode(lAddress.asStringUriOnly()); exitNewConversationMode(lAddress.asStringUriOnly());
} else { } else {
invalidate(); displayBubbleChat(message);
} }
Log.i("Sent message current status: " + message.getStatus()); Log.i("Sent message current status: " + message.getStatus());

View file

@ -280,64 +280,14 @@ public class ChatStorage {
return drafts; return drafts;
} }
public List<ChatMessage> getMessages(String correspondent) {
List<ChatMessage> chatMessages = new ArrayList<ChatMessage>();
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) { public String getTextMessageForId(LinphoneChatRoom chatroom, int id) {
String message = null; String message = null;
if (useNativeAPI) { if (useNativeAPI) {
LinphoneChatMessage[] history = chatroom.getHistory(); LinphoneChatMessage msg = getMessage(chatroom, id);
for (LinphoneChatMessage msg : history) {
if (msg.getStorageId() == id) { if (msg != null) {
message = msg.getText(); message = msg.getText();
break;
}
} }
} else { } else {
Cursor c = db.query(TABLE_NAME, null, "id LIKE " + id, null, null, null, null); 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) { public void deleteMessage(LinphoneChatRoom chatroom, int id) {
if (useNativeAPI) { if (useNativeAPI) {
LinphoneChatMessage[] history = chatroom.getHistory(); LinphoneChatMessage msg = getMessage(chatroom, id);
for (LinphoneChatMessage message : history) { if (msg != null){
if (message.getStorageId() == id) { chatroom.deleteMessage(msg);
chatroom.deleteMessage(message);
break;
}
} }
} else { } else {
db.delete(TABLE_NAME, "id LIKE " + id, null); db.delete(TABLE_NAME, "id LIKE " + id, null);

View file

@ -864,10 +864,6 @@ public class LinphoneActivity extends Activity implements OnClickListener, Conta
return getChatStorage().getDrafts(); return getChatStorage().getDrafts();
} }
public List<ChatMessage> getChatMessages(String correspondent) {
return getChatStorage().getMessages(correspondent);
}
public void removeFromChatList(String sipUri) { public void removeFromChatList(String sipUri) {
getChatStorage().removeDiscussion(sipUri); getChatStorage().removeDiscussion(sipUri);
} }
@ -876,8 +872,6 @@ public class LinphoneActivity extends Activity implements OnClickListener, Conta
getChatStorage().deleteDraft(sipUri); getChatStorage().deleteDraft(sipUri);
} }
public void updateMissedChatCount() { public void updateMissedChatCount() {
displayMissedChats(getUnreadMessageCount()); displayMissedChats(getUnreadMessageCount());
} }

@ -1 +1 @@
Subproject commit 392271334671ff505926215847f42ae79a136e88 Subproject commit fa00efddbb22cda2dabf6e0629de27ad40bbe45b