ShareViaAccountActivity.java

  1package eu.siacs.conversations.ui;
  2
  3import android.os.Bundle;
  4import android.widget.ListView;
  5
  6import java.util.ArrayList;
  7import java.util.List;
  8
  9import eu.siacs.conversations.R;
 10import eu.siacs.conversations.entities.Account;
 11import eu.siacs.conversations.entities.Conversation;
 12import eu.siacs.conversations.ui.adapter.AccountAdapter;
 13import eu.siacs.conversations.utils.XmppUri;
 14import eu.siacs.conversations.xmpp.Jid;
 15
 16public class ShareViaAccountActivity extends XmppActivity {
 17    public static final String EXTRA_CONTACT = "contact";
 18    public static final String EXTRA_BODY = "body";
 19
 20    protected final List<Account> accountList = new ArrayList<>();
 21    protected ListView accountListView;
 22    protected AccountAdapter mAccountAdapter;
 23
 24    @Override
 25    protected void refreshUiReal() {
 26        synchronized (this.accountList) {
 27            accountList.clear();
 28            accountList.addAll(xmppConnectionService.getAccounts());
 29        }
 30        mAccountAdapter.notifyDataSetChanged();
 31    }
 32
 33    @Override
 34    protected void onCreate(Bundle savedInstanceState) {
 35        super.onCreate(savedInstanceState);
 36
 37        setContentView(R.layout.activity_manage_accounts);
 38        setSupportActionBar(findViewById(R.id.toolbar));
 39        configureActionBar(getSupportActionBar());
 40        accountListView = findViewById(R.id.account_list);
 41        this.mAccountAdapter = new AccountAdapter(this, accountList, false);
 42        accountListView.setAdapter(this.mAccountAdapter);
 43        accountListView.setOnItemClickListener((arg0, view, position, arg3) -> {
 44            final Account account = accountList.get(position);
 45            final String action = getAction();
 46            if (action != null && action.equals("command")) {
 47                startCommand(account, getJid(), new XmppUri(getIntent().getData()).getParameter("node"));
 48            } else {
 49                switchToConversation(getConversation(account), getBody(), false, null, false, false, action);
 50            }
 51            finish();
 52        });
 53    }
 54
 55    @Override
 56    protected void onBackendConnected() {
 57        final int numAccounts = xmppConnectionService.getAccounts().size();
 58
 59        if (numAccounts == 1) {
 60            final Account account = xmppConnectionService.getAccounts().get(0);
 61            final String action = getAction();
 62            if (action != null && action.equals("command")) {
 63                startCommand(account, getJid(), new XmppUri(getIntent().getData()).getParameter("node"));
 64            } else {
 65                switchToConversation(getConversation(account), getBody(), false, null, false, false, action);
 66            }
 67            finish();
 68        } else {
 69            refreshUiReal();
 70        }
 71    }
 72
 73    protected Conversation getConversation(Account account) {
 74        try {
 75            return xmppConnectionService.findOrCreateConversation(account, getJid(), false, false);
 76        } catch (IllegalArgumentException e) { }
 77        return null;
 78    }
 79
 80    protected String getAction() {
 81        if (getIntent().getData() == null) return null;
 82        XmppUri xmppUri = new XmppUri(getIntent().getData());
 83        if (xmppUri.isAction("message")) return "message";
 84        if (xmppUri.isAction("command")) return "command";
 85        return null;
 86    }
 87
 88    protected Jid getJid() {
 89        if (getIntent().getData() == null) {
 90            return Jid.of(getIntent().getStringExtra(EXTRA_CONTACT));
 91        } else {
 92            return new XmppUri(getIntent().getData()).getJid();
 93        }
 94    }
 95
 96    protected String getBody() {
 97        if (getIntent().getData() == null) {
 98            return getIntent().getStringExtra(EXTRA_BODY);
 99        } else {
100            return new XmppUri(getIntent().getData()).getBody();
101        }
102    }
103}