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(
74 R.menu.manageaccounts_context, menu);
75 AdapterView.AdapterContextMenuInfo acmi = (AdapterContextMenuInfo) menuInfo;
76 this.selectedAccount = accountList.get(acmi.position);
77 if (this.selectedAccount.isOptionSet(Account.OPTION_DISABLED)) {
78 menu.findItem(R.id.mgmt_account_disable).setVisible(false);
79 menu.findItem(R.id.mgmt_account_announce_pgp).setVisible(false);
80 menu.findItem(R.id.mgmt_account_publish_avatar).setVisible(false);
81 } else {
82 menu.findItem(R.id.mgmt_account_enable).setVisible(false);
83 }
84 menu.setHeaderTitle(this.selectedAccount.getJid().toBareJid().toString());
85 }
86
87 @Override
88 protected void onStop() {
89 if (xmppConnectionServiceBound) {
90 xmppConnectionService.removeOnAccountListChangedListener();
91 }
92 super.onStop();
93 }
94
95 @Override
96 void onBackendConnected() {
97 xmppConnectionService.setOnAccountListChangedListener(accountChanged);
98 this.accountList.clear();
99 this.accountList.addAll(xmppConnectionService.getAccounts());
100 mAccountAdapter.notifyDataSetChanged();
101 }
102
103 @Override
104 public boolean onCreateOptionsMenu(Menu menu) {
105 getMenuInflater().inflate(R.menu.manageaccounts, menu);
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 default:
140 break;
141 }
142 return super.onOptionsItemSelected(item);
143 }
144
145 @Override
146 public boolean onNavigateUp() {
147 if (xmppConnectionService.getConversations().size() == 0) {
148 Intent contactsIntent = new Intent(this,
149 StartConversationActivity.class);
150 contactsIntent.setFlags(
151 // if activity exists in stack, pop the stack and go back to it
152 Intent.FLAG_ACTIVITY_CLEAR_TOP |
153 // otherwise, make a new task for it
154 Intent.FLAG_ACTIVITY_NEW_TASK |
155 // don't use the new activity animation; finish
156 // animation runs instead
157 Intent.FLAG_ACTIVITY_NO_ANIMATION);
158 startActivity(contactsIntent);
159 finish();
160 return true;
161 } else {
162 return super.onNavigateUp();
163 }
164 }
165
166 private void publishAvatar(Account account) {
167 Intent intent = new Intent(getApplicationContext(),
168 PublishProfilePictureActivity.class);
169 intent.putExtra("account", account.getJid().toString());
170 startActivity(intent);
171 }
172
173 private void disableAccount(Account account) {
174 account.setOption(Account.OPTION_DISABLED, true);
175 xmppConnectionService.updateAccount(account);
176 }
177
178 private void enableAccount(Account account) {
179 account.setOption(Account.OPTION_DISABLED, false);
180 xmppConnectionService.updateAccount(account);
181 }
182
183 private void publishOpenPGPPublicKey(Account account) {
184 if (ManageAccountActivity.this.hasPgp()) {
185 announcePgp(account, null);
186 } else {
187 this.showInstallPgpDialog();
188 }
189 }
190
191 private void deleteAccount(final Account account) {
192 AlertDialog.Builder builder = new AlertDialog.Builder(
193 ManageAccountActivity.this);
194 builder.setTitle(getString(R.string.mgmt_account_are_you_sure));
195 builder.setIconAttribute(android.R.attr.alertDialogIcon);
196 builder.setMessage(getString(R.string.mgmt_account_delete_confirm_text));
197 builder.setPositiveButton(getString(R.string.delete),
198 new OnClickListener() {
199 @Override
200 public void onClick(DialogInterface dialog, int which) {
201 xmppConnectionService.deleteAccount(account);
202 selectedAccount = null;
203 }
204 });
205 builder.setNegativeButton(getString(R.string.cancel), null);
206 builder.create().show();
207 }
208
209 @Override
210 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
211 super.onActivityResult(requestCode, resultCode, data);
212 if (resultCode == RESULT_OK) {
213 if (requestCode == REQUEST_ANNOUNCE_PGP) {
214 announcePgp(selectedAccount, null);
215 }
216 }
217 }
218}