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.ui.EditAccount.EditAccountListener;
9import eu.siacs.conversations.xmpp.OnTLSExceptionReceived;
10import eu.siacs.conversations.xmpp.XmppConnection;
11import android.app.Activity;
12import android.app.AlertDialog;
13import android.content.Context;
14import android.content.DialogInterface;
15import android.content.DialogInterface.OnClickListener;
16import android.content.Intent;
17import android.os.Bundle;
18import android.os.SystemClock;
19import android.view.ActionMode;
20import android.view.LayoutInflater;
21import android.view.Menu;
22import android.view.MenuInflater;
23import android.view.MenuItem;
24import android.view.View;
25import android.view.ViewGroup;
26import android.widget.AdapterView;
27import android.widget.AdapterView.OnItemClickListener;
28import android.widget.AdapterView.OnItemLongClickListener;
29import android.widget.ArrayAdapter;
30import android.widget.ListView;
31import android.widget.TextView;
32
33public class ManageAccountActivity extends XmppActivity {
34
35 protected boolean isActionMode = false;
36 protected ActionMode actionMode;
37 protected Account selectedAccountForActionMode = null;
38 protected ManageAccountActivity activity = this;
39
40 protected boolean firstrun = true;
41
42 protected List<Account> accountList = new ArrayList<Account>();
43 protected ListView accountListView;
44 protected ArrayAdapter<Account> accountListViewAdapter;
45 protected OnAccountListChangedListener accountChanged = new OnAccountListChangedListener() {
46
47 @Override
48 public void onAccountListChangedListener() {
49 accountList.clear();
50 accountList.addAll(xmppConnectionService.getAccounts());
51 runOnUiThread(new Runnable() {
52
53 @Override
54 public void run() {
55 accountListViewAdapter.notifyDataSetChanged();
56 }
57 });
58 }
59 };
60
61 protected OnTLSExceptionReceived tlsExceptionReceived = new OnTLSExceptionReceived() {
62
63 @Override
64 public void onTLSExceptionReceived(final String fingerprint, final Account account) {
65 activity.runOnUiThread(new Runnable() {
66
67 @Override
68 public void run() {
69 AlertDialog.Builder builder = new AlertDialog.Builder(activity);
70 builder.setTitle(getString(R.string.account_status_error));
71 builder.setIconAttribute(android.R.attr.alertDialogIcon);
72 View view = (View) getLayoutInflater().inflate(R.layout.cert_warning, null);
73 TextView sha = (TextView) view.findViewById(R.id.sha);
74 TextView hint = (TextView) view.findViewById(R.id.hint);
75 StringBuilder humanReadableSha = new StringBuilder();
76 humanReadableSha.append(fingerprint);
77 for(int i = 2; i < 59; i += 3) {
78 if ((i==14)||(i==29)||(i==44)) {
79 humanReadableSha.insert(i, "\n");
80 } else {
81 humanReadableSha.insert(i, ":");
82 }
83
84 }
85 hint.setText(getString(R.string.untrusted_cert_hint,account.getServer()));
86 sha.setText(humanReadableSha.toString());
87 builder.setView(view);
88 builder.setNegativeButton(getString(R.string.certif_no_trust), null);
89 builder.setPositiveButton(getString(R.string.certif_trust), new OnClickListener() {
90
91 @Override
92 public void onClick(DialogInterface dialog, int which) {
93 account.setSSLCertFingerprint(fingerprint);
94 activity.xmppConnectionService.updateAccount(account);
95 }
96 });
97 builder.create().show();
98 }
99 });
100
101 }
102 };
103
104 @Override
105 protected void onCreate(Bundle savedInstanceState) {
106
107 super.onCreate(savedInstanceState);
108
109 setContentView(R.layout.manage_accounts);
110
111 accountListView = (ListView) findViewById(R.id.account_list);
112 accountListViewAdapter = new ArrayAdapter<Account>(
113 getApplicationContext(), R.layout.account_row, this.accountList) {
114 @Override
115 public View getView(int position, View view, ViewGroup parent) {
116 Account account = getItem(position);
117 if (view == null) {
118 LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
119 view = (View) inflater.inflate(R.layout.account_row, null);
120 }
121 ((TextView) view.findViewById(R.id.account_jid))
122 .setText(account.getJid());
123 TextView statusView = (TextView) view
124 .findViewById(R.id.account_status);
125 switch (account.getStatus()) {
126 case Account.STATUS_DISABLED:
127 statusView.setText(getString(R.string.account_status_disabled));
128 statusView.setTextColor(0xFF1da9da);
129 break;
130 case Account.STATUS_ONLINE:
131 statusView.setText(getString(R.string.account_status_online));
132 statusView.setTextColor(0xFF83b600);
133 break;
134 case Account.STATUS_CONNECTING:
135 statusView.setText(getString(R.string.account_status_connecting));
136 statusView.setTextColor(0xFF1da9da);
137 break;
138 case Account.STATUS_OFFLINE:
139 statusView.setText(getString(R.string.account_status_offline));
140 statusView.setTextColor(0xFFe92727);
141 break;
142 case Account.STATUS_UNAUTHORIZED:
143 statusView.setText(getString(R.string.account_status_unauthorized));
144 statusView.setTextColor(0xFFe92727);
145 break;
146 case Account.STATUS_SERVER_NOT_FOUND:
147 statusView.setText(getString(R.string.account_status_not_found));
148 statusView.setTextColor(0xFFe92727);
149 break;
150 case Account.STATUS_NO_INTERNET:
151 statusView.setText(getString(R.string.account_status_no_internet));
152 statusView.setTextColor(0xFFe92727);
153 break;
154 case Account.STATUS_SERVER_REQUIRES_TLS:
155 statusView.setText(getString(R.string.account_status_requires_tls));
156 statusView.setTextColor(0xFFe92727);
157 break;
158 case Account.STATUS_TLS_ERROR:
159 statusView.setText(getString(R.string.account_status_error));
160 statusView.setTextColor(0xFFe92727);
161 break;
162 case Account.STATUS_REGISTRATION_FAILED:
163 statusView.setText(getString(R.string.account_status_regis_fail));
164 statusView.setTextColor(0xFFe92727);
165 break;
166 case Account.STATUS_REGISTRATION_CONFLICT:
167 statusView.setText(getString(R.string.account_status_regis_conflict));
168 statusView.setTextColor(0xFFe92727);
169 break;
170 case Account.STATUS_REGISTRATION_SUCCESSFULL:
171 statusView.setText(getString(R.string.account_status_regis_success));
172 statusView.setTextColor(0xFF83b600);
173 break;
174 case Account.STATUS_REGISTRATION_NOT_SUPPORTED:
175 statusView.setText(getString(R.string.account_status_regis_not_sup));
176 statusView.setTextColor(0xFFe92727);
177 break;
178 default:
179 statusView.setText("");
180 break;
181 }
182
183 return view;
184 }
185 };
186 final XmppActivity activity = this;
187 accountListView.setAdapter(this.accountListViewAdapter);
188 accountListView.setOnItemClickListener(new OnItemClickListener() {
189
190 @Override
191 public void onItemClick(AdapterView<?> arg0, View view,
192 int position, long arg3) {
193 if (!isActionMode) {
194 Account account = accountList.get(position);
195 if ((account.getStatus() == Account.STATUS_OFFLINE)||(account.getStatus() == Account.STATUS_TLS_ERROR)) {
196 activity.xmppConnectionService.reconnectAccount(accountList.get(position),true);
197 } else if (account.getStatus() == Account.STATUS_ONLINE) {
198 activity.startActivity(new Intent(activity.getApplicationContext(),StartConversation.class));
199 } else if (account.getStatus() != Account.STATUS_DISABLED) {
200 editAccount(account);
201 }
202 } else {
203 selectedAccountForActionMode = accountList.get(position);
204 actionMode.invalidate();
205 }
206 }
207 });
208 accountListView.setOnItemLongClickListener(new OnItemLongClickListener() {
209
210 @Override
211 public boolean onItemLongClick(AdapterView<?> arg0, View view,
212 int position, long arg3) {
213 if (!isActionMode) {
214 accountListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
215 accountListView.setItemChecked(position,true);
216 selectedAccountForActionMode = accountList.get(position);
217 actionMode = activity.startActionMode((new ActionMode.Callback() {
218
219 @Override
220 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
221 if (selectedAccountForActionMode.isOptionSet(Account.OPTION_DISABLED)) {
222 menu.findItem(R.id.mgmt_account_enable).setVisible(true);
223 menu.findItem(R.id.mgmt_account_disable).setVisible(false);
224 } else {
225 menu.findItem(R.id.mgmt_account_disable).setVisible(true);
226 menu.findItem(R.id.mgmt_account_enable).setVisible(false);
227 }
228 return true;
229 }
230
231 @Override
232 public void onDestroyActionMode(ActionMode mode) {
233 // TODO Auto-generated method stub
234
235 }
236
237 @Override
238 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
239 MenuInflater inflater = mode.getMenuInflater();
240 inflater.inflate(R.menu.manageaccounts_context, menu);
241 return true;
242 }
243
244 @Override
245 public boolean onActionItemClicked(final ActionMode mode, MenuItem item) {
246 if (item.getItemId()==R.id.mgmt_account_edit) {
247 editAccount(selectedAccountForActionMode);
248 } else if (item.getItemId()==R.id.mgmt_account_disable) {
249 selectedAccountForActionMode.setOption(Account.OPTION_DISABLED, true);
250 xmppConnectionService.updateAccount(selectedAccountForActionMode);
251 mode.finish();
252 } else if (item.getItemId()==R.id.mgmt_account_enable) {
253 selectedAccountForActionMode.setOption(Account.OPTION_DISABLED, false);
254 xmppConnectionService.updateAccount(selectedAccountForActionMode);
255 mode.finish();
256 } else if (item.getItemId()==R.id.mgmt_account_delete) {
257 AlertDialog.Builder builder = new AlertDialog.Builder(activity);
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), new OnClickListener() {
262
263 @Override
264 public void onClick(DialogInterface dialog, int which) {
265 xmppConnectionService.deleteAccount(selectedAccountForActionMode);
266 selectedAccountForActionMode = null;
267 mode.finish();
268 }
269 });
270 builder.setNegativeButton(getString(R.string.cancel),null);
271 builder.create().show();
272 } else if (item.getItemId()==R.id.mgmt_account_announce_pgp) {
273 if (activity.hasPgp()) {
274 mode.finish();
275 announcePgp(selectedAccountForActionMode,null);
276 } else {
277 activity.showInstallPgpDialog();
278 }
279 } else if (item.getItemId() == R.id.mgmt_otr_key) {
280 AlertDialog.Builder builder = new AlertDialog.Builder(activity);
281 builder.setTitle("OTR Fingerprint");
282 String fingerprintTxt = selectedAccountForActionMode.getOtrFingerprint(getApplicationContext());
283 View view = (View) getLayoutInflater().inflate(R.layout.otr_fingerprint, null);
284 if (fingerprintTxt!=null) {
285 TextView fingerprint = (TextView) view.findViewById(R.id.otr_fingerprint);
286 TextView noFingerprintView = (TextView) view.findViewById(R.id.otr_no_fingerprint);
287 fingerprint.setText(fingerprintTxt);
288 fingerprint.setVisibility(View.VISIBLE);
289 noFingerprintView.setVisibility(View.GONE);
290 }
291 builder.setView(view);
292 builder.setPositiveButton(getString(R.string.done), null);
293 builder.create().show();
294 } else if (item.getItemId() == R.id.mgmt_account_info) {
295 AlertDialog.Builder builder = new AlertDialog.Builder(activity);
296 builder.setTitle(getString(R.string.account_info));
297 if (selectedAccountForActionMode.getStatus() == Account.STATUS_ONLINE) {
298 XmppConnection xmpp = selectedAccountForActionMode.getXmppConnection();
299 long connectionAge = (SystemClock.elapsedRealtime() - xmpp.lastConnect) / 60000;
300 long sessionAge = (SystemClock.elapsedRealtime() - xmpp.lastSessionStarted) / 60000;
301 long connectionAgeHours = connectionAge / 60;
302 long sessionAgeHours = sessionAge / 60;
303 View view = (View) getLayoutInflater().inflate(R.layout.server_info, null);
304 TextView connection = (TextView) view.findViewById(R.id.connection);
305 TextView session = (TextView) view.findViewById(R.id.session);
306 TextView pcks_sent = (TextView) view.findViewById(R.id.pcks_sent);
307 TextView pcks_received = (TextView) view.findViewById(R.id.pcks_received);
308 TextView carbon = (TextView) view.findViewById(R.id.carbon);
309 TextView stream = (TextView) view.findViewById(R.id.stream);
310 TextView roster = (TextView) view.findViewById(R.id.roster);
311 TextView presences = (TextView) view.findViewById(R.id.number_presences);
312 presences.setText(selectedAccountForActionMode.countPresences()+"");
313 pcks_received.setText(""+xmpp.getReceivedStanzas());
314 pcks_sent.setText(""+xmpp.getSentStanzas());
315 if (connectionAgeHours >= 2) {
316 connection.setText(connectionAgeHours+" " + getString(R.string.hours));
317 } else {
318 connection.setText(connectionAge+" " + getString(R.string.mins));
319 }
320 if (xmpp.hasFeatureStreamManagment()) {
321 if (sessionAgeHours >= 2) {
322 session.setText(sessionAgeHours+" " + getString(R.string.hours));
323 } else {
324 session.setText(sessionAge+" " + getString(R.string.mins));
325 }
326 stream.setText(getString(R.string.yes));
327 } else {
328 stream.setText(getString(R.string.no));
329 session.setText(connection.getText());
330 }
331 if (xmpp.hasFeaturesCarbon()) {
332 carbon.setText(getString(R.string.yes));
333 } else {
334 carbon.setText(getString(R.string.no));
335 }
336 if (xmpp.hasFeatureRosterManagment()) {
337 roster.setText(getString(R.string.yes));
338 } else {
339 roster.setText(getString(R.string.no));
340 }
341 builder.setView(view);
342 } else {
343 builder.setMessage(getString(R.string.mgmt_account_account_offline));
344 }
345 builder.setPositiveButton(getString(R.string.hide), null);
346 builder.create().show();
347 }
348 return true;
349 }
350
351
352 }));
353 return true;
354 } else {
355 return false;
356 }
357 }
358 });
359 }
360
361 @Override
362 protected void onStop() {
363 if (xmppConnectionServiceBound) {
364 xmppConnectionService.removeOnAccountListChangedListener();
365 xmppConnectionService.removeOnTLSExceptionReceivedListener();
366 }
367 super.onStop();
368 }
369
370 @Override
371 void onBackendConnected() {
372 xmppConnectionService.setOnAccountListChangedListener(accountChanged);
373 xmppConnectionService.setOnTLSExceptionReceivedListener(tlsExceptionReceived);
374 this.accountList.clear();
375 this.accountList.addAll(xmppConnectionService.getAccounts());
376 accountListViewAdapter.notifyDataSetChanged();
377 if ((this.accountList.size() == 0)&&(this.firstrun)) {
378 getActionBar().setDisplayHomeAsUpEnabled(false);
379 getActionBar().setHomeButtonEnabled(false);
380 addAccount();
381 this.firstrun = false;
382 }
383 }
384
385 @Override
386 public boolean onCreateOptionsMenu(Menu menu) {
387 getMenuInflater().inflate(R.menu.manageaccounts, menu);
388 return true;
389 }
390
391 @Override
392 public boolean onOptionsItemSelected(MenuItem item) {
393 switch (item.getItemId()) {
394 case R.id.action_add_account:
395 addAccount();
396 break;
397 default:
398 break;
399 }
400 return super.onOptionsItemSelected(item);
401 }
402
403 @Override
404 public boolean onNavigateUp() {
405 if (xmppConnectionService.getConversations().size() == 0) {
406 Intent contactsIntent = new Intent(this, StartConversation.class);
407 contactsIntent.setFlags(
408 // if activity exists in stack, pop the stack and go back to it
409 Intent.FLAG_ACTIVITY_CLEAR_TOP |
410 // otherwise, make a new task for it
411 Intent.FLAG_ACTIVITY_NEW_TASK |
412 // don't use the new activity animation; finish animation runs instead
413 Intent.FLAG_ACTIVITY_NO_ANIMATION);
414 startActivity(contactsIntent);
415 finish();
416 return true;
417 } else {
418 return super.onNavigateUp();
419 }
420 }
421
422 private void editAccount(Account account) {
423 EditAccount dialog = new EditAccount();
424 dialog.setAccount(account);
425 dialog.setEditAccountListener(new EditAccountListener() {
426
427 @Override
428 public void onAccountEdited(Account account) {
429 xmppConnectionService.updateAccount(account);
430 if (actionMode != null) {
431 actionMode.finish();
432 }
433 }
434 });
435 dialog.show(getFragmentManager(), "edit_account");
436 dialog.setKnownHosts(xmppConnectionService.getKnownHosts(),this);
437
438 }
439
440 protected void addAccount() {
441 final Activity activity = this;
442 EditAccount dialog = new EditAccount();
443 dialog.setEditAccountListener(new EditAccountListener() {
444
445 @Override
446 public void onAccountEdited(Account account) {
447 xmppConnectionService.createAccount(account);
448 activity.getActionBar().setDisplayHomeAsUpEnabled(true);
449 activity.getActionBar().setHomeButtonEnabled(true);
450 }
451 });
452 dialog.show(getFragmentManager(), "add_account");
453 dialog.setKnownHosts(xmppConnectionService.getKnownHosts(),this);
454 }
455
456
457 @Override
458 public void onActionModeStarted(ActionMode mode) {
459 super.onActionModeStarted(mode);
460 this.isActionMode = true;
461 }
462
463 @Override
464 public void onActionModeFinished(ActionMode mode) {
465 super.onActionModeFinished(mode);
466 this.isActionMode = false;
467 accountListView.clearChoices();
468 accountListView.requestLayout();
469 accountListView.post(new Runnable() {
470 @Override
471 public void run() {
472 accountListView.setChoiceMode(ListView.CHOICE_MODE_NONE);
473 }
474 });
475 }
476
477 @Override
478 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
479 super.onActivityResult(requestCode, resultCode, data);
480 if (resultCode == RESULT_OK) {
481 if (requestCode == REQUEST_ANNOUNCE_PGP) {
482 announcePgp(selectedAccountForActionMode,null);
483 }
484 }
485 }
486}