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;
|
2019-09-24 15:29:34 +00:00
|
|
|
import android.net.Uri;
|
2019-03-31 15:12:01 +00:00
|
|
|
import android.os.Bundle;
|
2019-11-02 08:43:37 +00:00
|
|
|
import android.preference.PreferenceManager;
|
2019-03-31 15:12:01 +00:00
|
|
|
import android.text.Html;
|
2019-11-02 16:00:23 +00:00
|
|
|
import android.text.method.LinkMovementMethod;
|
2019-03-31 15:12:01 +00:00
|
|
|
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;
|
2020-03-31 17:46:05 +00:00
|
|
|
import android.widget.Toast;
|
2019-03-31 15:12:01 +00:00
|
|
|
|
2021-01-23 08:25:34 +00:00
|
|
|
import androidx.databinding.DataBindingUtil;
|
|
|
|
|
2019-03-31 15:12:01 +00:00
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.concurrent.atomic.AtomicReference;
|
|
|
|
|
2019-10-01 10:24:57 +00:00
|
|
|
import eu.siacs.conversations.Config;
|
2019-03-31 15:12:01 +00:00
|
|
|
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-11-02 08:43:37 +00:00
|
|
|
import eu.siacs.conversations.entities.Room;
|
2019-04-24 11:25:54 +00:00
|
|
|
import eu.siacs.conversations.services.ChannelDiscoveryService;
|
2019-11-09 09:20:58 +00:00
|
|
|
import eu.siacs.conversations.services.QuickConversationsService;
|
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;
|
2019-10-10 10:54:43 +00:00
|
|
|
import eu.siacs.conversations.ui.util.StyledAttributes;
|
2019-03-31 15:12:01 +00:00
|
|
|
import eu.siacs.conversations.utils.AccountUtils;
|
2020-05-15 15:06:16 +00:00
|
|
|
import eu.siacs.conversations.xmpp.Jid;
|
2019-03-31 15:12:01 +00:00
|
|
|
|
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;
|
|
|
|
|
2019-11-02 08:43:37 +00:00
|
|
|
private ChannelDiscoveryService.Method method = ChannelDiscoveryService.Method.LOCAL_SERVER;
|
|
|
|
|
2019-03-31 15:12:01 +00:00
|
|
|
private boolean optedIn = false;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void refreshUiReal() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
void onBackendConnected() {
|
2019-11-02 08:43:37 +00:00
|
|
|
if (optedIn || method == ChannelDiscoveryService.Method.LOCAL_SERVER) {
|
|
|
|
final String query;
|
2019-04-22 11:00:45 +00:00
|
|
|
if (mMenuSearchView != null && mMenuSearchView.isActionViewExpanded()) {
|
|
|
|
query = mSearchEditText.getText().toString();
|
|
|
|
} else {
|
|
|
|
query = mInitialSearchValue.peek();
|
|
|
|
}
|
2019-11-02 08:43:37 +00:00
|
|
|
toggleLoadingScreen();
|
|
|
|
xmppConnectionService.discoverChannels(query, this.method, 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);
|
2021-01-23 08:25:34 +00:00
|
|
|
setSupportActionBar(binding.toolbar);
|
2019-03-31 15:12:01 +00:00
|
|
|
configureActionBar(getSupportActionBar(), true);
|
|
|
|
binding.list.setAdapter(this.adapter);
|
|
|
|
this.adapter.setOnChannelSearchResultSelectedListener(this);
|
2019-11-02 08:43:37 +00:00
|
|
|
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-11-02 08:43:37 +00:00
|
|
|
}
|
2019-04-22 11:00:45 +00:00
|
|
|
|
2019-11-02 08:43:37 +00:00
|
|
|
private static ChannelDiscoveryService.Method getMethod(final Context c) {
|
2019-11-09 09:20:58 +00:00
|
|
|
if (QuickConversationsService.isQuicksy()) {
|
|
|
|
return ChannelDiscoveryService.Method.JABBER_NETWORK;
|
|
|
|
}
|
2019-11-02 08:43:37 +00:00
|
|
|
final SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(c);
|
|
|
|
final String m = p.getString("channel_discovery_method", c.getString(R.string.default_channel_discovery));
|
|
|
|
try {
|
|
|
|
return ChannelDiscoveryService.Method.valueOf(m);
|
|
|
|
} catch (IllegalArgumentException e) {
|
|
|
|
return ChannelDiscoveryService.Method.JABBER_NETWORK;
|
|
|
|
}
|
2019-03-31 15:12:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onCreateOptionsMenu(final Menu menu) {
|
2019-11-02 08:43:37 +00:00
|
|
|
getMenuInflater().inflate(R.menu.channel_discovery_activity, menu);
|
|
|
|
AccountUtils.showHideMenuItems(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-11-02 08:43:37 +00:00
|
|
|
final String initialSearchValue = mInitialSearchValue.pop();
|
2019-04-22 11:00:45 +00:00
|
|
|
if (initialSearchValue != null) {
|
|
|
|
mMenuSearchView.expandActionView();
|
|
|
|
mSearchEditText.append(initialSearchValue);
|
|
|
|
mSearchEditText.requestFocus();
|
2019-11-02 08:43:37 +00:00
|
|
|
if ((optedIn || method == ChannelDiscoveryService.Method.LOCAL_SERVER) && xmppConnectionService != null) {
|
|
|
|
xmppConnectionService.discoverChannels(initialSearchValue, this.method, this);
|
2019-04-22 11:00:45 +00:00
|
|
|
}
|
|
|
|
}
|
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-11-02 08:43:37 +00:00
|
|
|
if (optedIn || method == ChannelDiscoveryService.Method.LOCAL_SERVER) {
|
|
|
|
xmppConnectionService.discoverChannels(null, this.method, this);
|
2019-03-31 15:12:01 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-04-24 11:25:54 +00:00
|
|
|
private void toggleLoadingScreen() {
|
|
|
|
adapter.submitList(Collections.emptyList());
|
|
|
|
binding.progressBar.setVisibility(View.VISIBLE);
|
2019-10-10 10:54:43 +00:00
|
|
|
binding.list.setBackgroundColor(StyledAttributes.getColor(this, R.attr.color_background_primary));
|
2019-04-24 11:25:54 +00:00
|
|
|
}
|
|
|
|
|
2019-03-31 15:12:01 +00:00
|
|
|
@Override
|
|
|
|
public void onStart() {
|
|
|
|
super.onStart();
|
2019-11-02 08:43:37 +00:00
|
|
|
this.method = getMethod(this);
|
|
|
|
if (!optedIn && method == ChannelDiscoveryService.Method.JABBER_NETWORK) {
|
2019-03-31 15:12:01 +00:00
|
|
|
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();
|
2019-11-02 16:00:23 +00:00
|
|
|
dialog.setOnShowListener(d -> {
|
|
|
|
final TextView textView = dialog.findViewById(android.R.id.message);
|
|
|
|
if (textView == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
textView.setMovementMethod(LinkMovementMethod.getInstance());
|
|
|
|
});
|
2019-03-31 15:12:01 +00:00
|
|
|
dialog.setCanceledOnTouchOutside(false);
|
|
|
|
dialog.show();
|
2019-11-08 11:18:39 +00:00
|
|
|
holdLoading();
|
2019-03-31 15:12:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-08 11:18:39 +00:00
|
|
|
private void holdLoading() {
|
|
|
|
adapter.submitList(Collections.emptyList());
|
|
|
|
binding.progressBar.setVisibility(View.GONE);
|
|
|
|
binding.list.setBackgroundColor(StyledAttributes.getColor(this, R.attr.color_background_primary));
|
|
|
|
}
|
|
|
|
|
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;
|
2019-11-08 11:18:39 +00:00
|
|
|
toggleLoadingScreen();
|
2019-11-02 08:43:37 +00:00
|
|
|
xmppConnectionService.discoverChannels(null, this.method, this);
|
2019-03-31 15:12:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
|
2019-11-02 08:43:37 +00:00
|
|
|
if (optedIn || method == ChannelDiscoveryService.Method.LOCAL_SERVER) {
|
2019-10-11 17:39:45 +00:00
|
|
|
toggleLoadingScreen();
|
|
|
|
SoftKeyboardUtils.hideSoftKeyboard(this);
|
2019-11-02 08:43:37 +00:00
|
|
|
xmppConnectionService.discoverChannels(v.getText().toString(), this.method, this);
|
2019-03-31 15:12:01 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-11-02 08:43:37 +00:00
|
|
|
public void onChannelSearchResultsFound(final List<Room> results) {
|
2019-04-24 11:25:54 +00:00
|
|
|
runOnUiThread(() -> {
|
|
|
|
adapter.submitList(results);
|
|
|
|
binding.progressBar.setVisibility(View.GONE);
|
2019-10-10 10:54:43 +00:00
|
|
|
if (results.size() == 0) {
|
|
|
|
binding.list.setBackground(StyledAttributes.getDrawable(this, R.attr.activity_primary_background_no_results));
|
|
|
|
} else {
|
|
|
|
binding.list.setBackgroundColor(StyledAttributes.getColor(this, R.attr.color_background_primary));
|
|
|
|
}
|
2019-04-24 11:25:54 +00:00
|
|
|
});
|
2019-03-31 15:12:01 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-11-02 08:43:37 +00:00
|
|
|
public void onChannelSearchResult(final Room result) {
|
2020-03-31 17:46:05 +00:00
|
|
|
final List<String> accounts = AccountUtils.getEnabledAccounts(xmppConnectionService);
|
2019-03-31 15:12:01 +00:00
|
|
|
if (accounts.size() == 1) {
|
2019-04-26 10:26:49 +00:00
|
|
|
joinChannelSearchResult(accounts.get(0), result);
|
2020-03-31 17:46:05 +00:00
|
|
|
} else if (accounts.size() == 0) {
|
2020-03-31 17:49:08 +00:00
|
|
|
Toast.makeText(this, R.string.please_enable_an_account, Toast.LENGTH_LONG).show();
|
2020-03-31 17:46:05 +00:00
|
|
|
} else {
|
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) {
|
2019-11-02 08:43:37 +00:00
|
|
|
final Room room = adapter.getCurrent();
|
2019-09-21 08:51:05 +00:00
|
|
|
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-11-02 08:43:37 +00:00
|
|
|
public void joinChannelSearchResult(String selectedAccount, Room result) {
|
2020-06-15 17:17:21 +00:00
|
|
|
final Jid jid = Config.DOMAIN_LOCK == null ? Jid.ofEscaped(selectedAccount) : Jid.ofLocalAndDomainEscaped(selectedAccount, Config.DOMAIN_LOCK);
|
2019-10-01 10:24:57 +00:00
|
|
|
final boolean syncAutoJoin = getBooleanPreference("autojoin", R.bool.autojoin);
|
|
|
|
final Account account = xmppConnectionService.findAccountByJid(jid);
|
2019-03-31 15:12:01 +00:00
|
|
|
final Conversation conversation = xmppConnectionService.findOrCreateConversation(account, result.getRoom(), true, true, true);
|
2019-09-27 22:32:29 +00:00
|
|
|
Bookmark bookmark = conversation.getBookmark();
|
|
|
|
if (bookmark != null) {
|
2019-10-07 07:51:03 +00:00
|
|
|
if (!bookmark.autojoin() && syncAutoJoin) {
|
|
|
|
bookmark.setAutojoin(true);
|
2019-09-27 22:32:29 +00:00
|
|
|
xmppConnectionService.createBookmark(account, bookmark);
|
2019-04-26 10:26:49 +00:00
|
|
|
}
|
|
|
|
} else {
|
2019-09-27 22:32:29 +00:00
|
|
|
bookmark = new Bookmark(account, conversation.getJid().asBareJid());
|
2019-10-07 07:51:03 +00:00
|
|
|
bookmark.setAutojoin(syncAutoJoin);
|
2019-09-27 22:32:29 +00:00
|
|
|
xmppConnectionService.createBookmark(account, bookmark);
|
2019-04-26 10:26:49 +00:00
|
|
|
}
|
2019-03-31 15:12:01 +00:00
|
|
|
switchToConversation(conversation);
|
|
|
|
}
|
|
|
|
}
|