Reduced number of time ChatRoom.getUnreadMessageCount is called

This commit is contained in:
Sylvain Berfini 2018-03-13 11:01:44 +01:00
parent 6be8df9ff8
commit 17920ed822
6 changed files with 63 additions and 40 deletions

View file

@ -139,6 +139,8 @@ import java.io.IOException;
import java.io.InputStream;
import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
@ -200,6 +202,7 @@ public class LinphoneManager implements CoreListener, SensorEventListener, Accou
public String wizardLoginViewDomain = null;
protected LinphoneManager(final Context c) {
mUnreadChatsPerRoom = new HashMap();
sExited = false;
echoTesterIsRunning = false;
mServiceContext = c;
@ -236,6 +239,7 @@ public class LinphoneManager implements CoreListener, SensorEventListener, Accou
private final String mFriendsDatabaseFile;
private byte[] mUploadingImage;
private Timer mTimer;
private Map<String, Integer> mUnreadChatsPerRoom;
private void routeAudioToSpeakerHelper(boolean speakerOn) {
Log.w("Routing audio to " + (speakerOn ? "speaker" : "earpiece") + ", disabling bluetooth audio route");
@ -784,6 +788,8 @@ public class LinphoneManager implements CoreListener, SensorEventListener, Accou
accountCreator = LinphoneManager.getLc().createAccountCreator(LinphonePreferences.instance().getXmlrpcUrl());
accountCreator.setListener(this);
callGsmON = false;
updateMissedChatCount();
}
public void setHandsetMode(Boolean on){
@ -1089,6 +1095,8 @@ public class LinphoneManager implements CoreListener, SensorEventListener, Accou
return;
}
increaseUnreadCountForChatRoom(cr);
Address from = message.getFromAddress();
LinphoneContact contact = ContactsManager.getInstance().findContactFromAddress(from);
String textMessage = (message.getFileTransferInformation() != null) ? getString(R.string.content_description_incoming_file) : message.getTextContent();
@ -1748,4 +1756,45 @@ public class LinphoneManager implements CoreListener, SensorEventListener, Accou
public void onUpdateAccount(AccountCreator accountCreator, AccountCreator.Status status, String resp) {
}
private void updateMissedChatCount() {
for (ChatRoom cr : LinphoneManager.getLc().getChatRooms()) {
updateUnreadCountForChatRoom(cr, cr.getUnreadMessagesCount());
}
}
public int getUnreadMessageCount() {
int count = 0;
for (Integer unread : mUnreadChatsPerRoom.values()) {
count += unread;
}
return count;
}
public void updateUnreadCountForChatRoom(String key, Integer value) {
mUnreadChatsPerRoom.put(key, value);
}
public void updateUnreadCountForChatRoom(ChatRoom cr, Integer value) {
String key = cr.getPeerAddress().asStringUriOnly();
updateUnreadCountForChatRoom(key, value);
}
public int getUnreadCountForChatRoom(ChatRoom cr) {
String key = cr.getPeerAddress().asStringUriOnly();
if (mUnreadChatsPerRoom.containsKey(key)) {
return mUnreadChatsPerRoom.get(key);
}
return 0;
}
private void increaseUnreadCountForChatRoom(ChatRoom cr) {
String key = cr.getPeerAddress().asStringUriOnly();
if (mUnreadChatsPerRoom.containsKey(key)) {
mUnreadChatsPerRoom.put(key, mUnreadChatsPerRoom.get(key) + 1);
} else {
mUnreadChatsPerRoom.put(key, 1);
}
}
}

View file

@ -30,7 +30,6 @@ import org.linphone.contacts.LinphoneContact;
import org.linphone.core.Address;
import org.linphone.core.Call;
import org.linphone.core.Call.State;
import org.linphone.core.Call.Status;
import org.linphone.core.Core;
import org.linphone.core.GlobalState;
import org.linphone.core.RegistrationState;

View file

@ -233,7 +233,7 @@ public class LinphoneActivity extends LinphoneGenericActivity implements OnClick
mListener = new CoreListenerStub(){
@Override
public void onMessageReceived(Core lc, ChatRoom cr, ChatMessage message) {
displayMissedChats(getUnreadMessageCount());
displayMissedChats(LinphoneManager.getInstance().getUnreadMessageCount());
}
@Override
@ -666,30 +666,6 @@ public class LinphoneActivity extends LinphoneGenericActivity implements OnClick
startActivity(new Intent(LinphoneActivity.this, InAppPurchaseActivity.class));
}
public int getUnreadMessageCount() {
int count = 0;
ChatRoom[] chats = LinphoneManager.getLc().getChatRooms();
for (ChatRoom chatroom : chats) {
count += chatroom.getUnreadMessagesCount();
}
return count;
}
public void displayInfoChat(List<ContactAddress> list) {
Bundle extras = new Bundle();
ArrayList<String> listUri = new ArrayList<String>();
if (LinphoneManager.isInstanciated() && LinphoneManager.getLc() != null
&& LinphoneManager.getLc().getDefaultProxyConfig() != null
&& LinphoneManager.getLc().getDefaultProxyConfig().getIdentityAddress() != null) {
listUri.add(LinphoneManager.getLc().getDefaultProxyConfig().getIdentityAddress().asStringUriOnly());
}
for (ContactAddress ca : list) {
listUri.add(ca.getAddressAsDisplayableString());
}
extras.putStringArrayList("contactsSelected", listUri);
changeCurrentFragment(FragmentsAvailable.INFO_GROUP_CHAT, extras);
}
private void displayChat(String sipUri, String message, String fileUri, String pictureUri, String thumbnailUri, String displayName, Address lAddress) {
Bundle extras = new Bundle();
extras.putString("SipUri", sipUri);
@ -728,7 +704,8 @@ public class LinphoneActivity extends LinphoneGenericActivity implements OnClick
LinphoneService.instance().resetMessageNotifCount();
LinphoneService.instance().removeMessageNotification();
displayMissedChats(getUnreadMessageCount());
LinphoneManager.getInstance().updateUnreadCountForChatRoom(sipUri, 0);
displayMissedChats(LinphoneManager.getInstance().getUnreadMessageCount());
}
public void goToChatGroupInfos(String address, ArrayList<ContactAddress> contacts, String subject, boolean isEditionEnabled, boolean isGoBack) {
@ -789,7 +766,8 @@ public class LinphoneActivity extends LinphoneGenericActivity implements OnClick
LinphoneService.instance().resetMessageNotifCount();
LinphoneService.instance().removeMessageNotification();
displayMissedChats(getUnreadMessageCount());
LinphoneManager.getInstance().updateUnreadCountForChatRoom(sipUri, 0);
displayMissedChats(LinphoneManager.getInstance().getUnreadMessageCount());
}
@Override
@ -912,8 +890,8 @@ public class LinphoneActivity extends LinphoneGenericActivity implements OnClick
return statusFragment;
}
public void updateMissedChatCount() {
displayMissedChats(getUnreadMessageCount());
public void refreshMissedChatCountDisplay() {
displayMissedChats(LinphoneManager.getInstance().getUnreadMessageCount());
}
public void displayMissedCalls(final int missedCallsCount) {
@ -1433,8 +1411,7 @@ public class LinphoneActivity extends LinphoneGenericActivity implements OnClick
isTrialAccount();
}
updateMissedChatCount();
displayMissedChats(LinphoneManager.getInstance().getUnreadMessageCount());
displayMissedCalls(LinphoneManager.getLc().getMissedCallsCount());
LinphoneManager.getInstance().changeStatusToOnline();

View file

@ -1573,11 +1573,7 @@ public class CallActivity extends LinphoneGenericActivity implements OnClickList
}
private void displayMissedChats() {
int count = 0;
ChatRoom[] chats = LinphoneManager.getLc().getChatRooms();
for (ChatRoom chatroom : chats) {
count += chatroom.getUnreadMessagesCount();
}
int count = LinphoneManager.getInstance().getUnreadMessageCount();
if (count > 0) {
missedChats.setText(count + "");

View file

@ -162,7 +162,7 @@ public class ChatRoomsAdapter extends ListSelectionAdapter {
chatRoom.setUserData(holder);
}
int unreadMessagesCount = chatRoom.getUnreadMessagesCount();
int unreadMessagesCount = LinphoneManager.getInstance().getUnreadCountForChatRoom(chatRoom);
ChatMessage lastMessage = chatRoom.getLastMessageInHistory();
holder.lastMessageView.setText("");
holder.lastMessageSenderView.setText("");

View file

@ -398,7 +398,8 @@ public class GroupChatFragment extends Fragment implements ChatRoomListener, Con
}
mChatRoom.addListener(this);
mChatRoom.markAsRead();
LinphoneActivity.instance().updateMissedChatCount();
LinphoneManager.getInstance().updateUnreadCountForChatRoom(mChatRoom, 0);
LinphoneActivity.instance().refreshMissedChatCountDisplay();
mRemoteParticipantAddress = mRemoteSipAddress;
if (mChatRoom.hasCapability(ChatRoomCapabilities.OneToOne.toInt()) && mChatRoom.getParticipants().length > 0) {
@ -653,7 +654,8 @@ public class GroupChatFragment extends Fragment implements ChatRoomListener, Con
@Override
public void onChatMessageReceived(ChatRoom cr, EventLog event) {
cr.markAsRead();
LinphoneActivity.instance().updateMissedChatCount();
LinphoneManager.getInstance().updateUnreadCountForChatRoom(mChatRoom, 0);
LinphoneActivity.instance().refreshMissedChatCountDisplay();
ChatMessage msg = event.getChatMessage();
if (msg.getErrorInfo() != null && msg.getErrorInfo().getReason() == Reason.UnsupportedContent) {