2019-03-31 15:12:01 +00:00
|
|
|
package eu.siacs.conversations.ui;
|
|
|
|
|
|
|
|
import android.app.AlertDialog;
|
|
|
|
import android.content.Context;
|
2019-09-24 15:29:34 +00:00
|
|
|
import android.content.Intent;
|
2019-03-31 15:12:01 +00:00
|
|
|
import android.content.SharedPreferences;
|
|
|
|
import android.databinding.DataBindingUtil;
|
2019-09-24 15:29:34 +00:00
|
|
|
import android.net.Uri;
|
2019-03-31 15:12:01 +00:00
|
|
|
import android.os.Bundle;
|
|
|
|
import android.support.v7.widget.Toolbar;
|
|
|
|
import android.text.Html;
|
|
|
|
import android.view.KeyEvent;
|
|
|
|
import android.view.Menu;
|
|
|
|
import android.view.MenuItem;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.inputmethod.InputMethodManager;
|
|
|
|
import android.widget.EditText;
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.concurrent.atomic.AtomicReference;
|
|
|
|
|
|
|
|
import eu.siacs.conversations.R;
|
|
|
|
import eu.siacs.conversations.databinding.ActivityChannelDiscoveryBinding;
|
|
|
|
import eu.siacs.conversations.entities.Account;
|
2019-04-26 10:26:49 +00:00
|
|
|
import eu.siacs.conversations.entities.Bookmark;
|
2019-03-31 15:12:01 +00:00
|
|
|
import eu.siacs.conversations.entities.Conversation;
|
2019-04-22 11:00:45 +00:00
|
|
|
import eu.siacs.conversations.http.services.MuclumbusService;
|
2019-04-24 11:25:54 +00:00
|
|
|
import eu.siacs.conversations.services.ChannelDiscoveryService;
|
2019-03-31 15:12:01 +00:00
|
|
|
import eu.siacs.conversations.ui.adapter.ChannelSearchResultAdapter;
|
2019-04-22 11:00:45 +00:00
|
|
|
import eu.siacs.conversations.ui.util.PendingItem;
|
2019-03-31 15:12:01 +00:00
|
|
|
import eu.siacs.conversations.ui.util.SoftKeyboardUtils;
|
|
|
|
import eu.siacs.conversations.utils.AccountUtils;
|
|
|
|
import rocks.xmpp.addr.Jid;
|
|
|
|
|
2019-04-24 11:25:54 +00:00
|
|
|
public class ChannelDiscoveryActivity extends XmppActivity implements MenuItem.OnActionExpandListener, TextView.OnEditorActionListener, ChannelDiscoveryService.OnChannelSearchResultsFound, ChannelSearchResultAdapter.OnChannelSearchResultSelected {
|
2019-03-31 15:12:01 +00:00
|
|
|
|
|
|
|
private static final String CHANNEL_DISCOVERY_OPT_IN = "channel_discovery_opt_in";
|
|
|
|
|
|
|
|
private final ChannelSearchResultAdapter adapter = new ChannelSearchResultAdapter();
|
2019-04-22 11:00:45 +00:00
|
|
|
private final PendingItem<String> mInitialSearchValue = new PendingItem<>();
|
2019-09-21 08:51:05 +00:00
|
|
|
private ActivityChannelDiscoveryBinding binding;
|
2019-04-22 11:00:45 +00:00
|
|
|
private MenuItem mMenuSearchView;
|
2019-03-31 15:12:01 +00:00
|
|
|
private EditText mSearchEditText;
|
|
|
|
|
|
|
|
private boolean optedIn = false;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void refreshUiReal() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
void onBackendConnected() {
|
|
|
|
if (optedIn) {
|
2019-04-22 11:00:45 +00:00
|
|
|
String query;
|
|
|
|
if (mMenuSearchView != null && mMenuSearchView.isActionViewExpanded()) {
|
|
|
|
query = mSearchEditText.getText().toString();
|
|
|
|
} else {
|
|
|
|
query = mInitialSearchValue.peek();
|
|
|
|
}
|
|
|
|
xmppConnectionService.discoverChannels(query, this);
|
2019-03-31 15:12:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onCreate(final Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
2019-04-24 11:25:54 +00:00
|
|
|
binding = DataBindingUtil.setContentView(this, R.layout.activity_channel_discovery);
|
2019-03-31 15:12:01 +00:00
|
|
|
setSupportActionBar((Toolbar) binding.toolbar);
|
|
|
|
configureActionBar(getSupportActionBar(), true);
|
|
|
|
binding.list.setAdapter(this.adapter);
|
|
|
|
this.adapter.setOnChannelSearchResultSelectedListener(this);
|
|
|
|
optedIn = getPreferences().getBoolean(CHANNEL_DISCOVERY_OPT_IN, false);
|
2019-04-22 11:00:45 +00:00
|
|
|
|
|
|
|
final String search = savedInstanceState == null ? null : savedInstanceState.getString("search");
|
|
|
|
if (search != null) {
|
|
|
|
mInitialSearchValue.push(search);
|
|
|
|
}
|
|
|
|
|
2019-03-31 15:12:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onCreateOptionsMenu(final Menu menu) {
|
|
|
|
getMenuInflater().inflate(R.menu.muc_users_activity, menu);
|
2019-04-22 11:00:45 +00:00
|
|
|
mMenuSearchView = menu.findItem(R.id.action_search);
|
|
|
|
final View mSearchView = mMenuSearchView.getActionView();
|
2019-03-31 15:12:01 +00:00
|
|
|
mSearchEditText = mSearchView.findViewById(R.id.search_field);
|
|
|
|
mSearchEditText.setHint(R.string.search_channels);
|
2019-04-22 11:00:45 +00:00
|
|
|
String initialSearchValue = mInitialSearchValue.pop();
|
|
|
|
if (initialSearchValue != null) {
|
|
|
|
mMenuSearchView.expandActionView();
|
|
|
|
mSearchEditText.append(initialSearchValue);
|
|
|
|
mSearchEditText.requestFocus();
|
2019-05-22 15:29:27 +00:00
|
|
|
if (optedIn && xmppConnectionService != null) {
|
2019-04-22 11:00:45 +00:00
|
|
|
xmppConnectionService.discoverChannels(initialSearchValue, this);
|
|
|
|
}
|
|
|
|
}
|
2019-03-31 15:12:01 +00:00
|
|
|
mSearchEditText.setOnEditorActionListener(this);
|
2019-04-22 11:00:45 +00:00
|
|
|
mMenuSearchView.setOnActionExpandListener(this);
|
2019-03-31 15:12:01 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onMenuItemActionExpand(MenuItem item) {
|
|
|
|
mSearchEditText.post(() -> {
|
|
|
|
mSearchEditText.requestFocus();
|
|
|
|
final InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
|
|
|
|
imm.showSoftInput(mSearchEditText, InputMethodManager.SHOW_IMPLICIT);
|
|
|
|
});
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onMenuItemActionCollapse(MenuItem item) {
|
|
|
|
final InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
|
|
|
|
imm.hideSoftInputFromWindow(mSearchEditText.getWindowToken(), InputMethodManager.HIDE_IMPLICIT_ONLY);
|
|
|
|
mSearchEditText.setText("");
|
2019-04-24 11:25:54 +00:00
|
|
|
toggleLoadingScreen();
|
2019-03-31 15:12:01 +00:00
|
|
|
if (optedIn) {
|
|
|
|
xmppConnectionService.discoverChannels(null, this);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-04-24 11:25:54 +00:00
|
|
|
private void toggleLoadingScreen() {
|
|
|
|
adapter.submitList(Collections.emptyList());
|
|
|
|
binding.progressBar.setVisibility(View.VISIBLE);
|
|
|
|
}
|
|
|
|
|
2019-03-31 15:12:01 +00:00
|
|
|
@Override
|
|
|
|
public void onStart() {
|
|
|
|
super.onStart();
|
|
|
|
if (!optedIn) {
|
|
|
|
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
|
|
|
|
builder.setTitle(R.string.channel_discovery_opt_in_title);
|
|
|
|
builder.setMessage(Html.fromHtml(getString(R.string.channel_discover_opt_in_message)));
|
|
|
|
builder.setNegativeButton(R.string.cancel, (dialog, which) -> finish());
|
|
|
|
builder.setPositiveButton(R.string.confirm, (dialog, which) -> optIn());
|
|
|
|
builder.setOnCancelListener(dialog -> finish());
|
|
|
|
final AlertDialog dialog = builder.create();
|
|
|
|
dialog.setCanceledOnTouchOutside(false);
|
|
|
|
dialog.show();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-22 11:00:45 +00:00
|
|
|
@Override
|
|
|
|
public void onSaveInstanceState(Bundle savedInstanceState) {
|
|
|
|
if (mMenuSearchView != null && mMenuSearchView.isActionViewExpanded()) {
|
|
|
|
savedInstanceState.putString("search", mSearchEditText != null ? mSearchEditText.getText().toString() : null);
|
|
|
|
}
|
|
|
|
super.onSaveInstanceState(savedInstanceState);
|
|
|
|
}
|
|
|
|
|
2019-03-31 15:12:01 +00:00
|
|
|
private void optIn() {
|
|
|
|
SharedPreferences preferences = getPreferences();
|
2019-04-26 10:26:49 +00:00
|
|
|
preferences.edit().putBoolean(CHANNEL_DISCOVERY_OPT_IN, true).apply();
|
2019-03-31 15:12:01 +00:00
|
|
|
optedIn = true;
|
|
|
|
xmppConnectionService.discoverChannels(null, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
|
|
|
|
if (optedIn) {
|
|
|
|
xmppConnectionService.discoverChannels(v.getText().toString(), this);
|
|
|
|
}
|
2019-04-24 11:25:54 +00:00
|
|
|
toggleLoadingScreen();
|
2019-03-31 15:12:01 +00:00
|
|
|
SoftKeyboardUtils.hideSoftKeyboard(this);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-04-22 11:00:45 +00:00
|
|
|
public void onChannelSearchResultsFound(List<MuclumbusService.Room> results) {
|
2019-04-24 11:25:54 +00:00
|
|
|
runOnUiThread(() -> {
|
|
|
|
adapter.submitList(results);
|
|
|
|
binding.list.setVisibility(View.VISIBLE);
|
|
|
|
binding.progressBar.setVisibility(View.GONE);
|
|
|
|
});
|
2019-03-31 15:12:01 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-04-22 11:00:45 +00:00
|
|
|
public void onChannelSearchResult(final MuclumbusService.Room result) {
|
2019-03-31 15:12:01 +00:00
|
|
|
List<String> accounts = AccountUtils.getEnabledAccounts(xmppConnectionService);
|
|
|
|
if (accounts.size() == 1) {
|
2019-04-26 10:26:49 +00:00
|
|
|
joinChannelSearchResult(accounts.get(0), result);
|
|
|
|
} else if (accounts.size() > 0) {
|
2019-03-31 15:12:01 +00:00
|
|
|
final AtomicReference<String> account = new AtomicReference<>(accounts.get(0));
|
|
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(this);
|
|
|
|
builder.setTitle(R.string.choose_account);
|
|
|
|
builder.setSingleChoiceItems(accounts.toArray(new CharSequence[0]), 0, (dialog, which) -> account.set(accounts.get(which)));
|
|
|
|
builder.setPositiveButton(R.string.join, (dialog, which) -> joinChannelSearchResult(account.get(), result));
|
|
|
|
builder.setNegativeButton(R.string.cancel, null);
|
|
|
|
builder.create().show();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-09-21 08:51:05 +00:00
|
|
|
@Override
|
|
|
|
public boolean onContextItemSelected(MenuItem item) {
|
|
|
|
final MuclumbusService.Room room = adapter.getCurrent();
|
|
|
|
if (room != null) {
|
|
|
|
switch (item.getItemId()) {
|
|
|
|
case R.id.share_with:
|
|
|
|
StartConversationActivity.shareAsChannel(this, room.address);
|
|
|
|
return true;
|
2019-09-24 15:29:34 +00:00
|
|
|
case R.id.open_join_dialog:
|
|
|
|
final Intent intent = new Intent(this, StartConversationActivity.class);
|
|
|
|
intent.setAction(Intent.ACTION_VIEW);
|
|
|
|
intent.putExtra("force_dialog", true);
|
|
|
|
intent.setData(Uri.parse(String.format("xmpp:%s?join", room.address)));
|
|
|
|
startActivity(intent);
|
|
|
|
return true;
|
2019-09-21 08:51:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-04-22 11:00:45 +00:00
|
|
|
public void joinChannelSearchResult(String accountJid, MuclumbusService.Room result) {
|
2019-04-26 10:26:49 +00:00
|
|
|
final boolean syncAutojoin = getBooleanPreference("autojoin", R.bool.autojoin);
|
2019-03-31 15:12:01 +00:00
|
|
|
Account account = xmppConnectionService.findAccountByJid(Jid.of(accountJid));
|
|
|
|
final Conversation conversation = xmppConnectionService.findOrCreateConversation(account, result.getRoom(), true, true, true);
|
2019-04-26 10:26:49 +00:00
|
|
|
if (conversation.getBookmark() != null) {
|
|
|
|
if (!conversation.getBookmark().autojoin() && syncAutojoin) {
|
|
|
|
conversation.getBookmark().setAutojoin(true);
|
|
|
|
xmppConnectionService.pushBookmarks(account);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
final Bookmark bookmark = new Bookmark(account, conversation.getJid().asBareJid());
|
|
|
|
bookmark.setAutojoin(syncAutojoin);
|
|
|
|
account.getBookmarks().add(bookmark);
|
|
|
|
xmppConnectionService.pushBookmarks(account);
|
|
|
|
}
|
2019-03-31 15:12:01 +00:00
|
|
|
switchToConversation(conversation);
|
|
|
|
}
|
|
|
|
}
|