ManageAccountActivity.java

  1package eu.siacs.conversations.ui;
  2
  3import java.util.ArrayList;
  4import java.util.List;
  5
  6import eu.siacs.conversations.R;
  7import eu.siacs.conversations.crypto.PgpEngine;
  8import eu.siacs.conversations.crypto.PgpEngine.UserInputRequiredException;
  9import eu.siacs.conversations.entities.Account;
 10import eu.siacs.conversations.ui.EditAccount.EditAccountListener;
 11import eu.siacs.conversations.xmpp.OnTLSExceptionReceived;
 12import eu.siacs.conversations.xmpp.XmppConnection;
 13import android.app.Activity;
 14import android.app.AlertDialog;
 15import android.content.Context;
 16import android.content.DialogInterface;
 17import android.content.DialogInterface.OnClickListener;
 18import android.content.Intent;
 19import android.content.IntentSender.SendIntentException;
 20import android.os.Bundle;
 21import android.os.SystemClock;
 22import android.util.Log;
 23import android.view.ActionMode;
 24import android.view.LayoutInflater;
 25import android.view.Menu;
 26import android.view.MenuInflater;
 27import android.view.MenuItem;
 28import android.view.View;
 29import android.view.ViewGroup;
 30import android.widget.AdapterView;
 31import android.widget.AdapterView.OnItemClickListener;
 32import android.widget.AdapterView.OnItemLongClickListener;
 33import android.widget.ArrayAdapter;
 34import android.widget.ListView;
 35import android.widget.TextView;
 36
 37public class ManageAccountActivity extends XmppActivity {
 38
 39	public static final int REQUEST_ANNOUNCE_PGP = 0x73731;
 40	
 41	protected boolean isActionMode = false;
 42	protected ActionMode actionMode;
 43	protected Account selectedAccountForActionMode = null;
 44	protected ManageAccountActivity activity = this;
 45	
 46	protected List<Account> accountList = new ArrayList<Account>();
 47	protected ListView accountListView;
 48	protected ArrayAdapter<Account> accountListViewAdapter;
 49	protected OnAccountListChangedListener accountChanged = new OnAccountListChangedListener() {
 50
 51		@Override
 52		public void onAccountListChangedListener() {
 53			accountList.clear();
 54			accountList.addAll(xmppConnectionService.getAccounts());
 55			runOnUiThread(new Runnable() {
 56
 57				@Override
 58				public void run() {
 59					accountListViewAdapter.notifyDataSetChanged();
 60				}
 61			});
 62		}
 63	};
 64	
 65	protected OnTLSExceptionReceived tlsExceptionReceived = new OnTLSExceptionReceived() {
 66		
 67		@Override
 68		public void onTLSExceptionReceived(final String fingerprint, final Account account) {
 69			activity.runOnUiThread(new Runnable() {
 70				
 71				@Override
 72				public void run() {
 73					AlertDialog.Builder builder = new AlertDialog.Builder(activity);
 74					builder.setTitle("Untrusted Certificate");
 75					builder.setIconAttribute(android.R.attr.alertDialogIcon);
 76					View view = (View) getLayoutInflater().inflate(R.layout.cert_warning, null);
 77					TextView sha = (TextView) view.findViewById(R.id.sha);
 78					TextView hint = (TextView) view.findViewById(R.id.hint);
 79					StringBuilder humanReadableSha = new StringBuilder();
 80					humanReadableSha.append(fingerprint);
 81					for(int i = 2; i < 59; i += 3) {
 82						if ((i==14)||(i==29)||(i==44)) {
 83							humanReadableSha.insert(i, "\n");
 84						} else {
 85							humanReadableSha.insert(i, ":");
 86						}
 87						
 88					}
 89					hint.setText(getString(R.string.untrusted_cert_hint,account.getServer()));
 90					sha.setText(humanReadableSha.toString());
 91					builder.setView(view);
 92					builder.setNegativeButton("Don't connect", null);
 93					builder.setPositiveButton("Trust certificate", new OnClickListener() {
 94						
 95						@Override
 96						public void onClick(DialogInterface dialog, int which) {
 97							account.setSSLCertFingerprint(fingerprint);
 98							activity.xmppConnectionService.updateAccount(account);
 99						}
100					});
101					builder.create().show();
102				}
103			});
104			
105		}
106	};
107
108	@Override
109	protected void onCreate(Bundle savedInstanceState) {
110
111		super.onCreate(savedInstanceState);
112
113		setContentView(R.layout.manage_accounts);
114
115		accountListView = (ListView) findViewById(R.id.account_list);
116		accountListViewAdapter = new ArrayAdapter<Account>(
117				getApplicationContext(), R.layout.account_row, this.accountList) {
118			@Override
119			public View getView(int position, View view, ViewGroup parent) {
120				Account account = getItem(position);
121				if (view == null) {
122					LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
123					view = (View) inflater.inflate(R.layout.account_row, null);
124				}
125				((TextView) view.findViewById(R.id.account_jid))
126						.setText(account.getJid());
127				TextView statusView = (TextView) view
128						.findViewById(R.id.account_status);
129				switch (account.getStatus()) {
130				case Account.STATUS_DISABLED:
131					statusView.setText("temporarily disabled");
132					statusView.setTextColor(0xFF1da9da);
133					break;
134				case Account.STATUS_ONLINE:
135					statusView.setText("online");
136					statusView.setTextColor(0xFF83b600);
137					break;
138				case Account.STATUS_CONNECTING:
139					statusView.setText("connecting\u2026");
140					statusView.setTextColor(0xFF1da9da);
141					break;
142				case Account.STATUS_OFFLINE:
143					statusView.setText("offline");
144					statusView.setTextColor(0xFFe92727);
145					break;
146				case Account.STATUS_UNAUTHORIZED:
147					statusView.setText("unauthorized");
148					statusView.setTextColor(0xFFe92727);
149					break;
150				case Account.STATUS_SERVER_NOT_FOUND:
151					statusView.setText("server not found");
152					statusView.setTextColor(0xFFe92727);
153					break;
154				case Account.STATUS_NO_INTERNET:
155					statusView.setText("no internet");
156					statusView.setTextColor(0xFFe92727);
157					break;
158				case Account.STATUS_SERVER_REQUIRES_TLS:
159					statusView.setText("server requires TLS");
160					statusView.setTextColor(0xFFe92727);
161					break;
162				case Account.STATUS_TLS_ERROR:
163					statusView.setText("untrusted cerficate");
164					statusView.setTextColor(0xFFe92727);
165					break;
166				case Account.STATUS_REGISTRATION_FAILED:
167					statusView.setText("registration failed");
168					statusView.setTextColor(0xFFe92727);
169					break;
170				case Account.STATUS_REGISTRATION_CONFLICT:
171					statusView.setText("username already in use");
172					statusView.setTextColor(0xFFe92727);
173					break;
174				case  Account.STATUS_REGISTRATION_SUCCESSFULL:
175					statusView.setText("registration completed");
176					statusView.setTextColor(0xFF83b600);
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(),NewConversationActivity.class));
199					} else if (account.isOptionSet(Account.OPTION_REGISTER)) {
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("Are you sure?");
259								builder.setIconAttribute(android.R.attr.alertDialogIcon);
260								builder.setMessage("If you delete your account your entire conversation history will be lost");
261								builder.setPositiveButton("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("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									try {
276										xmppConnectionService.generatePgpAnnouncement(selectedAccountForActionMode);
277									} catch (PgpEngine.UserInputRequiredException e) {
278										try {
279											startIntentSenderForResult(e.getPendingIntent().getIntentSender(), REQUEST_ANNOUNCE_PGP, null, 0, 0, 0);
280										} catch (SendIntentException e1) {
281											Log.d("gultsch","sending intent failed");
282										}
283									}
284								}
285							} else if (item.getItemId() == R.id.mgmt_account_info) {
286								AlertDialog.Builder builder = new AlertDialog.Builder(activity);
287								builder.setTitle(getString(R.string.account_info));
288								if (selectedAccountForActionMode.getStatus() == Account.STATUS_ONLINE) {
289									XmppConnection xmpp = selectedAccountForActionMode.getXmppConnection();
290									long connectionAge = (SystemClock.elapsedRealtime() - xmpp.lastConnect) / 60000;
291									long sessionAge = (SystemClock.elapsedRealtime() - xmpp.lastSessionStarted) / 60000;
292									View view = (View) getLayoutInflater().inflate(R.layout.server_info, null);
293									TextView connection = (TextView) view.findViewById(R.id.connection);
294									TextView session = (TextView) view.findViewById(R.id.session);
295									TextView pcks_sent = (TextView) view.findViewById(R.id.pcks_sent);
296									TextView pcks_received = (TextView) view.findViewById(R.id.pcks_received);
297									TextView carbon = (TextView) view.findViewById(R.id.carbon);
298									TextView stream = (TextView) view.findViewById(R.id.stream);
299									TextView roster = (TextView) view.findViewById(R.id.roster);
300									pcks_received.setText(""+xmpp.getReceivedStanzas());
301									pcks_sent.setText(""+xmpp.getSentStanzas());
302									connection.setText(connectionAge+" mins");
303									if (xmpp.hasFeatureStreamManagment()) {
304										session.setText(sessionAge+" mins");
305										stream.setText("Yes");
306									} else {
307										stream.setText("No");
308										session.setText(connectionAge+" mins");
309									}
310									if (xmpp.hasFeaturesCarbon()) {
311										carbon.setText("Yes");
312									} else {
313										carbon.setText("No");
314									}
315									if (xmpp.hasFeatureRosterManagment()) {
316										roster.setText("Yes");
317									} else {
318										roster.setText("No");
319									}
320									builder.setView(view);
321								} else {
322									builder.setMessage("Account is offline");
323								}
324								builder.setPositiveButton("Hide", null);
325								builder.create().show();
326							}
327							return true;
328						}
329
330						
331					}));
332					return true;
333				} else {
334					return false;
335				}
336			}
337		});
338	}
339
340	@Override
341	protected void onStop() {
342		if (xmppConnectionServiceBound) {
343			xmppConnectionService.removeOnAccountListChangedListener();
344			xmppConnectionService.removeOnTLSExceptionReceivedListener();
345		}
346		super.onStop();
347	}
348
349	@Override
350	void onBackendConnected() {
351		xmppConnectionService.setOnAccountListChangedListener(accountChanged);
352		xmppConnectionService.setOnTLSExceptionReceivedListener(tlsExceptionReceived);
353		this.accountList.clear();
354		this.accountList.addAll(xmppConnectionService.getAccounts());
355		accountListViewAdapter.notifyDataSetChanged();
356		if (this.accountList.size() == 0) {
357			getActionBar().setDisplayHomeAsUpEnabled(false);
358			addAccount();
359		}
360	}
361
362	@Override
363	public boolean onCreateOptionsMenu(Menu menu) {
364		getMenuInflater().inflate(R.menu.manageaccounts, menu);
365		return true;
366	}
367
368	@Override
369	public boolean onOptionsItemSelected(MenuItem item) {
370		switch (item.getItemId()) {
371		case R.id.action_settings:
372			startActivity(new Intent(this, SettingsActivity.class));
373			break;
374		case R.id.action_add_account:
375			addAccount();
376			break;
377		default:
378			break;
379		}
380		return super.onOptionsItemSelected(item);
381	}
382
383	private void editAccount(Account account) {
384			EditAccount dialog = new EditAccount();
385			dialog.setAccount(account);
386			dialog.setEditAccountListener(new EditAccountListener() {
387
388				@Override
389				public void onAccountEdited(Account account) {
390					xmppConnectionService.updateAccount(account);
391					if (actionMode != null) { 
392						actionMode.finish();
393					}
394				}
395			});
396			dialog.show(getFragmentManager(), "edit_account");
397		
398	}
399	
400	protected void addAccount() {
401		final Activity activity = this;
402		EditAccount dialog = new EditAccount();
403		dialog.setEditAccountListener(new EditAccountListener() {
404
405			@Override
406			public void onAccountEdited(Account account) {
407				xmppConnectionService.createAccount(account);
408				activity.getActionBar().setDisplayHomeAsUpEnabled(true);
409			}
410		});
411		dialog.show(getFragmentManager(), "add_account");
412	}
413
414	
415	@Override
416	public void onActionModeStarted(ActionMode mode) {
417		super.onActionModeStarted(mode);
418		this.isActionMode = true;
419	}
420	
421	@Override
422	public void onActionModeFinished(ActionMode mode) {
423		super.onActionModeFinished(mode);
424		this.isActionMode = false;
425		accountListView.clearChoices();
426		accountListView.requestLayout();
427		accountListView.post(new Runnable() {
428            @Override
429            public void run() {
430                accountListView.setChoiceMode(ListView.CHOICE_MODE_NONE);
431            }
432        });
433	}
434	
435	 @Override
436	 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
437		 super.onActivityResult(requestCode, resultCode, data);
438		 if (resultCode == RESULT_OK) {
439			if (requestCode == REQUEST_ANNOUNCE_PGP) {
440				 try {
441					xmppConnectionService.generatePgpAnnouncement(selectedAccountForActionMode);
442				} catch (UserInputRequiredException e) {
443					Log.d("gultsch","already came back. ignoring");
444				}
445			 }
446		 }
447	 }
448}