use of de.tum.in.tumcampusapp.component.ui.chat.model.ChatMessage in project TumCampusApp by TCA-Team.
the class ChatActivity method onMessagesLoaded.
private void onMessagesLoaded() {
final List<ChatMessage> msgs = chatMessageViewModel.getAll(currentChatRoom.getId());
// Update results in UI
runOnUiThread(() -> {
if (chatHistoryAdapter == null) {
chatHistoryAdapter = new ChatHistoryAdapter(ChatActivity.this, msgs, currentChatMember);
lvMessageHistory.setAdapter(chatHistoryAdapter);
} else {
chatHistoryAdapter.updateHistory(chatMessageViewModel.getAll(currentChatRoom.getId()));
}
// If all messages are loaded hide header view
if ((!msgs.isEmpty() && msgs.get(0).getPrevious() == 0) || chatHistoryAdapter.getCount() == 0) {
lvMessageHistory.removeHeaderView(bar);
} else {
loadingMore = false;
}
});
}
use of de.tum.in.tumcampusapp.component.ui.chat.model.ChatMessage in project TumCampusApp by TCA-Team.
the class ChatActivity method onItemLongClick.
/**
* Validates chat message if long clicked on an item
*
* @param parent ListView
* @param view View of the selected message
* @param position Index of the selected view
* @param id Id of the selected item
* @return True if the method consumed the on long click event
*/
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
if (mActionMode != null) {
return false;
}
// Calculate the proper position of the item without the header from pull to refresh
int positionActual = position - lvMessageHistory.getHeaderViewsCount();
// Get the correct message
ChatMessage message = chatHistoryAdapter.getItem(positionActual);
// TODO(jacqueline8711): If we are in a certain timespan and its the users own message allow editing
/*if ((System.currentTimeMillis() - message.getTimestampDate()
.getTime()) < ChatActivity.MAX_EDIT_TIMESPAN && message.getMember()
.getId() == currentChatMember.getId()) {
// Hide keyboard if opened
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(etMessage.getWindowToken(), 0);
// Start the CAB using the ActionMode.Callback defined above
mActionMode = this.startSupportActionMode(mActionModeCallback);
chatHistoryAdapter.mCheckedItem = message;
chatHistoryAdapter.notifyDataSetChanged();
} else {
this.showInfo(message);
}*/
this.showInfo(message);
return true;
}
use of de.tum.in.tumcampusapp.component.ui.chat.model.ChatMessage in project TumCampusApp by TCA-Team.
the class ChatHistoryAdapter method getView.
@Override
public View getView(int position, View convertView, ViewGroup parent) {
boolean outgoing = getItemViewType(position) == MSG_OUTGOING;
int layout = outgoing ? R.layout.activity_chat_history_row_outgoing : R.layout.activity_chat_history_row_incoming;
ChatMessage msg = getItem(position);
ViewHolder holder;
View listItem = convertView;
if (listItem == null) {
listItem = LayoutInflater.from(mContext).inflate(layout, parent, false);
holder = new ViewHolder();
// Set UI elements
holder.layout = listItem.findViewById(R.id.chatMessageLayout);
holder.tvMessage = listItem.findViewById(R.id.tvMessage);
holder.tvTimestamp = listItem.findViewById(R.id.tvTime);
if (outgoing) {
holder.pbSending = listItem.findViewById(R.id.progressBar);
holder.ivSent = listItem.findViewById(R.id.sentImage);
} else {
// We only got the user on receiving things
holder.tvUser = listItem.findViewById(R.id.tvUser);
}
listItem.setTag(holder);
} else {
holder = (ViewHolder) listItem.getTag();
}
holder.tvMessage.setText(msg.getText());
holder.tvTimestamp.setText(DateUtils.getTimeOrDayISO(msg.getTimestamp(), mContext));
if (!outgoing) {
holder.tvUser.setText(msg.getMember().getDisplayName());
} else {
// Set status for outgoing messages (ivSent is not null)
boolean sending = msg.getSendingStatus() == ChatMessage.STATUS_SENDING;
holder.ivSent.setVisibility(sending ? View.GONE : View.VISIBLE);
holder.pbSending.setVisibility(sending ? View.VISIBLE : View.GONE);
}
if (msg.getMember().getLrzId().equals("bot")) {
holder.tvUser.setText("");
holder.tvTimestamp.setText("");
}
if ((mCheckedItem != null && mCheckedItem.getId() == msg.getId() && (mCheckedItem.getSendingStatus() == msg.getSendingStatus())) || (mEditedItem != null && mEditedItem.getId() == msg.getId() && mEditedItem.getSendingStatus() == msg.getSendingStatus())) {
holder.layout.setBackgroundResource(R.drawable.bg_message_outgoing_selected);
} else if (outgoing) {
holder.layout.setBackgroundResource(R.drawable.bg_message_outgoing);
}
return listItem;
}
use of de.tum.in.tumcampusapp.component.ui.chat.model.ChatMessage in project TumCampusApp by TCA-Team.
the class SendMessageService method onHandleWork.
@Override
protected void onHandleWork(@NonNull Intent intent) {
TcaDb tcaDb = TcaDb.getInstance(this);
final CompositeDisposable mDisposable = new CompositeDisposable();
ChatMessageRemoteRepository remoteRepository = ChatMessageRemoteRepository.INSTANCE;
remoteRepository.setTumCabeClient(TUMCabeClient.getInstance(this));
ChatMessageLocalRepository localRepository = ChatMessageLocalRepository.INSTANCE;
localRepository.setDb(tcaDb);
ChatMessageViewModel chatMessageViewModel = new ChatMessageViewModel(localRepository, remoteRepository, mDisposable);
chatMessageViewModel.deleteOldEntries();
// Get all unsent messages from database
List<ChatMessage> unsentMsg = chatMessageViewModel.getUnsent();
if (unsentMsg.isEmpty()) {
return;
}
int numberOfAttempts = 0;
AuthenticationManager am = new AuthenticationManager(this);
// Try to send the message 5 times
while (numberOfAttempts < MAX_SEND_TRIES) {
try {
for (ChatMessage message : unsentMsg) {
// Generate signature and store it in the message
message.setSignature(am.sign(message.getText()));
// Send the message to the server
chatMessageViewModel.sendMessage(message.getRoom(), message, this.getApplicationContext());
Utils.logv("successfully sent message: " + message.getText());
}
// Exit the loop
return;
} catch (NoPrivateKey noPrivateKey) {
// Nothing can be done, just exit
return;
} catch (Exception e) {
Utils.log(e);
numberOfAttempts++;
}
// Sleep for five seconds, maybe the server is currently really busy
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
Utils.log(e);
}
}
}
use of de.tum.in.tumcampusapp.component.ui.chat.model.ChatMessage in project TumCampusApp by TCA-Team.
the class ChatMessageValidatorTestCase method testUnicodeValidMessageOneKey.
/**
* Tests that a unicode (european) message is correctly validated.
*/
@Test
public void testUnicodeValidMessageOneKey() {
validator = new ChatMessageValidator(buildPubkeyList(0, 1));
ChatMessage message = messageFixtures.get(1);
assertTrue(validator.validate(message));
}
Aggregations