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