Improve memory in chat

This commit is contained in:
Sylvain Berfini 2016-05-10 18:02:23 +02:00
parent 8e9f7aa1d4
commit a90fa14b7f

View file

@ -338,17 +338,24 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC
ArrayList<LinphoneChatMessage> history; ArrayList<LinphoneChatMessage> history;
Context context; Context context;
public ChatMessageAdapter(Context context, LinphoneChatMessage[] history) { public ChatMessageAdapter(Context context) {
this.history = new ArrayList<LinphoneChatMessage>(Arrays.asList(history));
this.context = context; this.context = context;
refreshHistory();
} }
public void destroy() { public void destroy() {
history.clear();
history = null; history = null;
notifyDataSetInvalidated();
} }
public void refreshHistory() { public void refreshHistory() {
this.history = new ArrayList<LinphoneChatMessage>(Arrays.asList(chatRoom.getHistory())); if (history != null) {
history.clear();
}
LinphoneChatMessage[] messages = chatRoom.getHistory();
history = new ArrayList<LinphoneChatMessage>(Arrays.asList(messages));
messages = null;
} }
public void addMessage(LinphoneChatMessage message) { public void addMessage(LinphoneChatMessage message) {
@ -373,12 +380,12 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC
@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.get(position); LinphoneChatMessage message = history.get(position);
RelativeLayout rlayout = new RelativeLayout(context);
BubbleChat bubble = new BubbleChat(context, message, contact); BubbleChat bubble = new BubbleChat(context, message, contact);
View v = bubble.getView(); View v = bubble.getView();
registerForContextMenu(v); registerForContextMenu(v);
RelativeLayout rlayout = new RelativeLayout(context);
CheckBox deleteChatBubble = (CheckBox) v.findViewById(R.id.delete_message); CheckBox deleteChatBubble = (CheckBox) v.findViewById(R.id.delete_message);
@ -470,9 +477,13 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC
public void dispayMessageList() { public void dispayMessageList() {
messagesList.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE); messagesList.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);
if(chatRoom != null) { if(chatRoom != null) {
if (adapter != null) adapter.destroy(); if (adapter != null) {
adapter = new ChatMessageAdapter(getActivity(), chatRoom.getHistory()); adapter.refreshHistory();
messagesList.setAdapter(adapter); adapter.notifyDataSetChanged();
} else {
adapter = new ChatMessageAdapter(getActivity());
messagesList.setAdapter(adapter);
}
} }
} }
@ -541,6 +552,13 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC
} }
return true; return true;
} }
@Override
public void onDestroy() {
if (adapter != null) adapter.destroy();
adapter = null;
super.onDestroy();
}
@Override @Override
public void onPause() { public void onPause() {