1package eu.siacs.conversations.ui;
2
3import android.app.AlertDialog;
4import android.content.DialogInterface;
5import android.content.DialogInterface.OnClickListener;
6import android.content.Intent;
7import android.os.Bundle;
8import android.view.ContextMenu;
9import android.view.ContextMenu.ContextMenuInfo;
10import android.view.Menu;
11import android.view.MenuItem;
12import android.view.View;
13import android.widget.AdapterView;
14import android.widget.AdapterView.AdapterContextMenuInfo;
15import android.widget.AdapterView.OnItemClickListener;
16import android.widget.ListView;
17
18import java.util.ArrayList;
19import java.util.List;
20
21import eu.siacs.conversations.R;
22import eu.siacs.conversations.entities.Account;
23import eu.siacs.conversations.services.XmppConnectionService.OnAccountUpdate;
24import eu.siacs.conversations.ui.adapter.AccountAdapter;
25
26public class ManageAccountActivity extends XmppActivity implements OnAccountUpdate {
27
28 protected Account selectedAccount = null;
29
30 protected final List<Account> accountList = new ArrayList<>();
31 protected ListView accountListView;
32 protected AccountAdapter mAccountAdapter;
33
34 @Override
35 public void onAccountUpdate() {
36 refreshUi();
37 }
38
39 @Override
40 protected void refreshUiReal() {
41 synchronized (this.accountList) {
42 accountList.clear();
43 accountList.addAll(xmppConnectionService.getAccounts());
44 }
45 invalidateOptionsMenu();
46 mAccountAdapter.notifyDataSetChanged();
47 }
48
49 @Override
50 protected void onCreate(Bundle savedInstanceState) {
51
52 super.onCreate(savedInstanceState);
53
54 setContentView(R.layout.manage_accounts);
55
56 accountListView = (ListView) findViewById(R.id.account_list);
57 this.mAccountAdapter = new AccountAdapter(this, accountList);
58 accountListView.setAdapter(this.mAccountAdapter);
59 accountListView.setOnItemClickListener(new OnItemClickListener() {
60
61 @Override
62 public void onItemClick(AdapterView<?> arg0, View view,
63 int position, long arg3) {
64 switchToAccount(accountList.get(position));
65 }
66 });
67 registerForContextMenu(accountListView);
68 }
69
70 @Override
71 public void onCreateContextMenu(ContextMenu menu, View v,
72 ContextMenuInfo menuInfo) {
73 super.onCreateContextMenu(menu, v, menuInfo);
74 ManageAccountActivity.this.getMenuInflater().inflate(
75 R.menu.manageaccounts_context, menu);
76 AdapterView.AdapterContextMenuInfo acmi = (AdapterContextMenuInfo) menuInfo;
77 this.selectedAccount = accountList.get(acmi.position);
78 if (this.selectedAccount.isOptionSet(Account.OPTION_DISABLED)) {
79 menu.findItem(R.id.mgmt_account_disable).setVisible(false);
80 menu.findItem(R.id.mgmt_account_announce_pgp).setVisible(false);
81 menu.findItem(R.id.mgmt_account_publish_avatar).setVisible(false);
82 } else {
83 menu.findItem(R.id.mgmt_account_enable).setVisible(false);
84 }
85 menu.setHeaderTitle(this.selectedAccount.getJid().toBareJid().toString());
86 }
87
88 @Override
89 void onBackendConnected() {
90 this.accountList.clear();
91 this.accountList.addAll(xmppConnectionService.getAccounts());
92 mAccountAdapter.notifyDataSetChanged();
93 }
94
95 @Override
96 public boolean onCreateOptionsMenu(Menu menu) {
97 getMenuInflater().inflate(R.menu.manageaccounts, menu);
98 MenuItem enableAll = menu.findItem(R.id.action_enable_all);
99 if (!accountsLeftToEnable()) {
100 enableAll.setVisible(false);
101 }
102 MenuItem disableAll = menu.findItem(R.id.action_disable_all);
103 if (!accountsLeftToDisable()) {
104 disableAll.setVisible(false);
105 }
106 return true;
107 }
108
109 @Override
110 public boolean onContextItemSelected(MenuItem item) {
111 switch (item.getItemId()) {
112 case R.id.mgmt_account_publish_avatar:
113 publishAvatar(selectedAccount);
114 return true;
115 case R.id.mgmt_account_disable:
116 disableAccount(selectedAccount);
117 return true;
118 case R.id.mgmt_account_enable:
119 enableAccount(selectedAccount);
120 return true;
121 case R.id.mgmt_account_delete:
122 deleteAccount(selectedAccount);
123 return true;
124 case R.id.mgmt_account_announce_pgp:
125 publishOpenPGPPublicKey(selectedAccount);
126 return true;
127 default:
128 return super.onContextItemSelected(item);
129 }
130 }
131
132 @Override
133 public boolean onOptionsItemSelected(MenuItem item) {
134 switch (item.getItemId()) {
135 case R.id.action_add_account:
136 startActivity(new Intent(getApplicationContext(),
137 EditAccountActivity.class));
138 break;
139 case R.id.action_disable_all:
140 disableAllAccounts();
141 break;
142 case R.id.action_enable_all:
143 enableAllAccounts();
144 break;
145 default:
146 break;
147 }
148 return super.onOptionsItemSelected(item);
149 }
150
151 @Override
152 public boolean onNavigateUp() {
153 if (xmppConnectionService.getConversations().size() == 0) {
154 Intent contactsIntent = new Intent(this,
155 StartConversationActivity.class);
156 contactsIntent.setFlags(
157 // if activity exists in stack, pop the stack and go back to it
158 Intent.FLAG_ACTIVITY_CLEAR_TOP |
159 // otherwise, make a new task for it
160 Intent.FLAG_ACTIVITY_NEW_TASK |
161 // don't use the new activity animation; finish
162 // animation runs instead
163 Intent.FLAG_ACTIVITY_NO_ANIMATION);
164 startActivity(contactsIntent);
165 finish();
166 return true;
167 } else {
168 return super.onNavigateUp();
169 }
170 }
171
172 public void onClickTglAccountState(Account account, boolean enable) {
173 if (enable) {
174 enableAccount(account);
175 } else {
176 disableAccount(account);
177 }
178 }
179
180 private void publishAvatar(Account account) {
181 Intent intent = new Intent(getApplicationContext(),
182 PublishProfilePictureActivity.class);
183 intent.putExtra("account", account.getJid().toString());
184 startActivity(intent);
185 }
186
187 private void disableAllAccounts() {
188 List<Account> list = new ArrayList<>();
189 synchronized (this.accountList) {
190 for (Account account : this.accountList) {
191 if (!account.isOptionSet(Account.OPTION_DISABLED)) {
192 list.add(account);
193 }
194 }
195 }
196 for(Account account : list) {
197 disableAccount(account);
198 }
199 }
200
201 private boolean accountsLeftToDisable() {
202 synchronized (this.accountList) {
203 for (Account account : this.accountList) {
204 if (!account.isOptionSet(Account.OPTION_DISABLED)) {
205 return true;
206 }
207 }
208 return false;
209 }
210 }
211
212 private boolean accountsLeftToEnable() {
213 synchronized (this.accountList) {
214 for (Account account : this.accountList) {
215 if (account.isOptionSet(Account.OPTION_DISABLED)) {
216 return true;
217 }
218 }
219 return false;
220 }
221 }
222
223 private void enableAllAccounts() {
224 List<Account> list = new ArrayList<>();
225 synchronized (this.accountList) {
226 for (Account account : this.accountList) {
227 if (account.isOptionSet(Account.OPTION_DISABLED)) {
228 list.add(account);
229 }
230 }
231 }
232 for(Account account : list) {
233 enableAccount(account);
234 }
235 }
236
237 private void disableAccount(Account account) {
238 account.setOption(Account.OPTION_DISABLED, true);
239 xmppConnectionService.updateAccount(account);
240 }
241
242 private void enableAccount(Account account) {
243 account.setOption(Account.OPTION_DISABLED, false);
244 xmppConnectionService.updateAccount(account);
245 }
246
247 private void publishOpenPGPPublicKey(Account account) {
248 if (ManageAccountActivity.this.hasPgp()) {
249 announcePgp(account, null);
250 } else {
251 this.showInstallPgpDialog();
252 }
253 }
254
255 private void deleteAccount(final Account account) {
256 AlertDialog.Builder builder = new AlertDialog.Builder(
257 ManageAccountActivity.this);
258 builder.setTitle(getString(R.string.mgmt_account_are_you_sure));
259 builder.setIconAttribute(android.R.attr.alertDialogIcon);
260 builder.setMessage(getString(R.string.mgmt_account_delete_confirm_text));
261 builder.setPositiveButton(getString(R.string.delete),
262 new OnClickListener() {
263 @Override
264 public void onClick(DialogInterface dialog, int which) {
265 xmppConnectionService.deleteAccount(account);
266 selectedAccount = null;
267 }
268 });
269 builder.setNegativeButton(getString(R.string.cancel), null);
270 builder.create().show();
271 }
272
273 @Override
274 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
275 super.onActivityResult(requestCode, resultCode, data);
276 if (resultCode == RESULT_OK) {
277 if (requestCode == REQUEST_ANNOUNCE_PGP) {
278 announcePgp(selectedAccount, null);
279 }
280 }
281 }
282}