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