Mercury-IM/app/src/main/java/org/mercury_im/messenger/ui/chat/ChatActivity.java

213 lines
6.8 KiB
Java
Raw Normal View History

2019-05-13 03:19:17 +02:00
package org.mercury_im.messenger.ui.chat;
2019-04-22 04:54:02 +02:00
import android.os.Bundle;
2019-06-21 03:45:33 +02:00
import android.util.Log;
2019-08-11 03:32:13 +02:00
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
2019-04-22 04:54:02 +02:00
2019-06-24 01:41:17 +02:00
import androidx.annotation.NonNull;
2019-08-04 04:22:08 +02:00
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SearchView;
2019-06-24 01:41:17 +02:00
import androidx.appcompat.widget.Toolbar;
import androidx.lifecycle.ViewModelProvider;
2019-06-21 03:45:33 +02:00
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import org.jxmpp.jid.EntityBareJid;
import org.jxmpp.jid.impl.JidCreate;
import org.mercury_im.messenger.MercuryImApplication;
2019-05-13 03:19:17 +02:00
import org.mercury_im.messenger.R;
2019-06-21 03:45:33 +02:00
2019-12-21 16:30:14 +01:00
import java.util.UUID;
2019-12-06 15:52:50 +01:00
import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.schedulers.Schedulers;
2019-06-24 01:41:17 +02:00
public class ChatActivity extends AppCompatActivity
implements ChatInputFragment.OnChatInputActionListener, SearchView.OnQueryTextListener {
2019-06-21 03:45:33 +02:00
2019-06-24 01:41:17 +02:00
public static final String EXTRA_JID = "JID";
public static final String EXTRA_ACCOUNT = "ACCOUNT";
@BindView(R.id.toolbar)
Toolbar toolbar;
@BindView(R.id.recyclerView)
RecyclerView recyclerView;
2019-09-23 23:20:43 +02:00
private final MessagesRecyclerViewAdapter recyclerViewAdapter = new MessagesRecyclerViewAdapter();
2019-08-29 00:22:26 +02:00
2019-06-24 01:41:17 +02:00
private ChatViewModel chatViewModel;
2019-08-24 23:06:06 +02:00
private final CompositeDisposable disposable = new CompositeDisposable();
2019-06-21 03:45:33 +02:00
private EntityBareJid jid;
2019-04-22 04:54:02 +02:00
2019-12-21 16:30:14 +01:00
private UUID accountId;
2019-04-22 04:54:02 +02:00
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
2019-06-24 01:41:17 +02:00
Log.d("Mercury", "onCreate");
2019-04-22 04:54:02 +02:00
setContentView(R.layout.activity_chat);
2019-06-24 01:41:17 +02:00
ButterKnife.bind(this);
2019-06-21 03:45:33 +02:00
MercuryImApplication.getApplication().getAppComponent().inject(this);
if (savedInstanceState == null) {
savedInstanceState = getIntent().getExtras();
2019-06-21 04:44:59 +02:00
if (savedInstanceState == null) return;
2019-06-21 03:45:33 +02:00
}
2019-06-21 04:44:59 +02:00
2019-09-01 02:53:31 +02:00
setSupportActionBar(toolbar);
// Show back arrow
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
recyclerView.setAdapter(recyclerViewAdapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
2019-06-24 01:41:17 +02:00
String jidString = savedInstanceState.getString(EXTRA_JID);
2019-06-21 03:45:33 +02:00
if (jidString != null) {
jid = JidCreate.entityBareFromOrThrowUnchecked(jidString);
2019-09-11 01:40:32 +02:00
// JID will never change, so just set it once
getSupportActionBar().setSubtitle(jid.asUnescapedString());
2019-12-21 16:30:14 +01:00
accountId = UUID.fromString(savedInstanceState.getString(EXTRA_ACCOUNT));
2019-06-21 03:45:33 +02:00
chatViewModel = new ViewModelProvider(this).get(ChatViewModel.class);
2019-08-29 00:22:26 +02:00
chatViewModel.init(accountId, jid);
2019-09-11 01:40:32 +02:00
// Listen for updates to contact information and messages
2019-08-29 00:22:26 +02:00
observeViewModel(chatViewModel);
}
}
2019-06-21 03:45:33 +02:00
2019-08-29 00:22:26 +02:00
public void observeViewModel(ChatViewModel viewModel) {
viewModel.getContactDisplayName().observe(this,
name -> getSupportActionBar().setTitle(name));
2019-08-25 17:54:03 +02:00
2019-08-29 00:22:26 +02:00
viewModel.getMessages().observe(this, messageModels -> {
recyclerViewAdapter.updateMessages(messageModels);
recyclerView.scrollToPosition(messageModels.size() - 1);
});
2019-06-06 23:32:41 +02:00
}
2019-08-24 23:06:06 +02:00
public void onStop() {
super.onStop();
disposable.clear();
}
2019-08-11 03:32:13 +02:00
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_chat, menu);
final MenuItem searchItem = menu.findItem(R.id.action_search);
final SearchView searchView = (SearchView) searchItem.getActionView();
searchView.setOnQueryTextListener(this);
2019-08-11 03:32:13 +02:00
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
2019-09-02 01:06:17 +02:00
case R.id.action_debug:
2019-09-08 04:47:59 +02:00
Log.d("CHATACTIVITY", "Fetch MAM messages!");
chatViewModel.requestMamMessages()
2019-09-02 01:06:17 +02:00
.subscribeOn(Schedulers.io())
.subscribe();
break;
2019-09-01 02:55:50 +02:00
// menu_chat
case R.id.action_call:
case R.id.action_clear_history:
2019-09-01 02:55:50 +02:00
case R.id.action_notification_settings:
case R.id.action_delete_chat:
2019-09-01 02:55:50 +02:00
2019-09-11 01:40:32 +02:00
// long_click_message
2019-09-01 02:55:50 +02:00
case R.id.action_edit_message:
case R.id.action_reply_message:
case R.id.action_copy_message:
case R.id.action_forward_message:
case R.id.action_delete_message:
case R.id.action_message_details:
2019-09-11 01:40:32 +02:00
// short_click_message
2019-09-01 02:55:50 +02:00
case R.id.action_react_message:
Toast.makeText(this, R.string.not_yet_implemented, Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
2019-06-24 01:41:17 +02:00
@Override
protected void onSaveInstanceState(@NonNull Bundle outState) {
outState.putString(EXTRA_JID, jid.toString());
2019-12-21 16:30:14 +01:00
outState.putString(EXTRA_ACCOUNT, accountId.toString());
2019-06-24 01:41:17 +02:00
super.onSaveInstanceState(outState);
}
2019-06-06 23:32:41 +02:00
@Override
public void onButtonEmojiClicked() {
Toast.makeText(this, R.string.not_yet_implemented, Toast.LENGTH_SHORT).show();
}
2019-06-06 23:32:41 +02:00
@Override
public void onButtonMediaClicked() {
Toast.makeText(this, R.string.not_yet_implemented, Toast.LENGTH_SHORT).show();
2019-06-06 23:32:41 +02:00
}
@Override
public void onComposingBodyChanged(String body) {
}
@Override
public void onComposingBodySend(String body) {
2019-07-02 00:46:40 +02:00
String msg = body.trim();
if (msg.isEmpty()) {
return;
}
2019-08-24 23:06:06 +02:00
2019-12-06 15:52:50 +01:00
/*
2019-08-25 17:54:03 +02:00
// TODO: Improve by using rx
2019-06-21 03:45:33 +02:00
new Thread() {
@Override
public void run() {
try {
2019-08-25 17:54:03 +02:00
ChatManager.getInstanceFor(connectionCenter.getConnection(accountId).getConnection())
2019-07-02 00:46:40 +02:00
.chatWith(jid).send(msg);
2019-06-21 03:45:33 +02:00
} catch (SmackException.NotConnectedException e) {
Logger.getAnonymousLogger().log(Level.SEVERE,"NotConnectedException : \n" + e.getStackTrace().toString());
2019-06-21 03:45:33 +02:00
} catch (InterruptedException e) {
Logger.getAnonymousLogger().log(Level.SEVERE,"InterruptedException" + e.getStackTrace().toString());
2019-06-21 03:45:33 +02:00
}
}
}.start();
2019-12-06 15:52:50 +01:00
*/
2019-04-22 04:54:02 +02:00
}
@Override
public boolean onQueryTextSubmit(String query) {
// Ignore. Logic is in onQueryTextChange.
return false;
}
@Override
public boolean onQueryTextChange(String query) {
chatViewModel.queryTextChanged(query);
return false;
}
2019-09-01 02:53:31 +02:00
@Override
public boolean onSupportNavigateUp() {
// Go back when left arrow is pressed in toolbar
onBackPressed();
return true;
}
2019-04-22 04:54:02 +02:00
}