Search in sources :

Example 11 with ChatMessage

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;
        }
    });
}
Also used : ChatHistoryAdapter(de.tum.in.tumcampusapp.component.ui.chat.adapter.ChatHistoryAdapter) ChatMessage(de.tum.in.tumcampusapp.component.ui.chat.model.ChatMessage)

Example 12 with ChatMessage

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;
}
Also used : ChatMessage(de.tum.in.tumcampusapp.component.ui.chat.model.ChatMessage)

Example 13 with ChatMessage

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;
}
Also used : ChatMessage(de.tum.in.tumcampusapp.component.ui.chat.model.ChatMessage) ImageView(android.widget.ImageView) TextView(android.widget.TextView) View(android.view.View)

Example 14 with ChatMessage

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);
        }
    }
}
Also used : AuthenticationManager(de.tum.in.tumcampusapp.api.app.AuthenticationManager) NoPrivateKey(de.tum.in.tumcampusapp.api.app.exception.NoPrivateKey) ChatMessageRemoteRepository(de.tum.in.tumcampusapp.component.ui.chat.repository.ChatMessageRemoteRepository) ChatMessage(de.tum.in.tumcampusapp.component.ui.chat.model.ChatMessage) ChatMessageViewModel(de.tum.in.tumcampusapp.component.ui.chat.ChatMessageViewModel) TcaDb(de.tum.in.tumcampusapp.database.TcaDb) CompositeDisposable(io.reactivex.disposables.CompositeDisposable) ChatMessageLocalRepository(de.tum.in.tumcampusapp.component.ui.chat.repository.ChatMessageLocalRepository)

Example 15 with ChatMessage

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));
}
Also used : ChatMessage(de.tum.in.tumcampusapp.component.ui.chat.model.ChatMessage) ChatMessageValidator(de.tum.in.tumcampusapp.component.ui.chat.ChatMessageValidator) Test(org.junit.Test)

Aggregations

ChatMessage (de.tum.in.tumcampusapp.component.ui.chat.model.ChatMessage)19 Test (org.junit.Test)11 ChatMessageValidator (de.tum.in.tumcampusapp.component.ui.chat.ChatMessageValidator)7 RSASigner (de.tum.in.tumcampusapp.utils.RSASigner)4 Intent (android.content.Intent)2 ChatPublicKey (de.tum.in.tumcampusapp.component.ui.chat.model.ChatPublicKey)2 PendingIntent (android.app.PendingIntent)1 View (android.view.View)1 ImageView (android.widget.ImageView)1 TextView (android.widget.TextView)1 Gson (com.google.gson.Gson)1 AuthenticationManager (de.tum.in.tumcampusapp.api.app.AuthenticationManager)1 NoPrivateKey (de.tum.in.tumcampusapp.api.app.exception.NoPrivateKey)1 ChatMessageViewModel (de.tum.in.tumcampusapp.component.ui.chat.ChatMessageViewModel)1 ChatRoomsActivity (de.tum.in.tumcampusapp.component.ui.chat.activity.ChatRoomsActivity)1 ChatHistoryAdapter (de.tum.in.tumcampusapp.component.ui.chat.adapter.ChatHistoryAdapter)1 ChatMessageLocalRepository (de.tum.in.tumcampusapp.component.ui.chat.repository.ChatMessageLocalRepository)1 ChatMessageRemoteRepository (de.tum.in.tumcampusapp.component.ui.chat.repository.ChatMessageRemoteRepository)1 MainActivity (de.tum.in.tumcampusapp.component.ui.overview.MainActivity)1 TcaDb (de.tum.in.tumcampusapp.database.TcaDb)1