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 onStart() {
57 super.onStart();
58 final int theme = findTheme();
59 if (this.mTheme != theme) {
60 recreate();
61 }
62 }
63
64 @Override
65 void onBackendConnected() {
66 final int numAccounts = xmppConnectionService.getAccounts().size();
67
68 if (numAccounts == 1) {
69 final Account account = xmppConnectionService.getAccounts().get(0);
70 final String action = getAction();
71 if (action != null && action.equals("command")) {
72 startCommand(account, getJid(), new XmppUri(getIntent().getData()).getParameter("node"));
73 } else {
74 switchToConversation(getConversation(account), getBody(), false, null, false, false, action);
75 }
76 finish();
77 } else {
78 refreshUiReal();
79 }
80 }
81
82 protected Conversation getConversation(Account account) {
83 try {
84 return xmppConnectionService.findOrCreateConversation(account, getJid(), false, false);
85 } catch (IllegalArgumentException e) { }
86 return null;
87 }
88
89 protected String getAction() {
90 if (getIntent().getData() == null) return null;
91 XmppUri xmppUri = new XmppUri(getIntent().getData());
92 if (xmppUri.isAction("message")) return "message";
93 if (xmppUri.isAction("command")) return "command";
94 return null;
95 }
96
97 protected Jid getJid() {
98 if (getIntent().getData() == null) {
99 return Jid.of(getIntent().getStringExtra(EXTRA_CONTACT));
100 } else {
101 return new XmppUri(getIntent().getData()).getJid();
102 }
103 }
104
105 protected String getBody() {
106 if (getIntent().getData() == null) {
107 return getIntent().getStringExtra(EXTRA_BODY);
108 } else {
109 return new XmppUri(getIntent().getData()).getBody();
110 }
111 }
112}