private List < ChatConversation > limitNumberOfConversations ( List < ChatConversation > conversations ) { List < ChatConversation > noEmptyConversations = new ArrayList <> ( ) ; for ( ChatConversation c : conversations ) { if ( c . getLastRemoteEventId ( ) != null && c . getLastRemoteEventId ( ) >= 0 ) { noEmptyConversations . add ( c ) ; } } List < ChatConversation > limitedList ; if ( noEmptyConversations . size ( ) <= maxConversationsSynced ) { limitedList = noEmptyConversations ; } else { SortedMap < Long , ChatConversation > sorted = new TreeMap <> ( ) ; for ( ChatConversation conversation : noEmptyConversations ) { Long updatedOn = conversation . getUpdatedOn ( ) ; if ( updatedOn != null ) { sorted . put ( updatedOn , conversation ) ; } } limitedList = new ArrayList <> ( ) ; Object [ ] array = sorted . values ( ) . toArray ( ) ; for ( int i = 0 ; i < Math . min ( maxConversationsSynced , array . length ) ; i ++ ) { limitedList . add ( ( ChatConversation ) array [ i ] ) ; } } return limitedList ; }
Limits number of conversations to check and synchronise . Emty conversations wont be synchronised . The synchronisation will take place for twenty conversations updated most recently .