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				default:
167					break;
168				}
169
170				return view;
171			}
172		};
173		final XmppActivity activity = this;
174		accountListView.setAdapter(this.accountListViewAdapter);
175		accountListView.setOnItemClickListener(new OnItemClickListener() {
176
177			@Override
178			public void onItemClick(AdapterView<?> arg0, View view,
179					int position, long arg3) {
180				if (!isActionMode) {
181					Account account = accountList.get(position);
182					if ((account.getStatus() != Account.STATUS_ONLINE)&&(account.getStatus() != Account.STATUS_CONNECTING)&&(!account.isOptionSet(Account.OPTION_DISABLED))) {
183						activity.xmppConnectionService.reconnectAccount(accountList.get(position),true);
184					} else if (account.getStatus() == Account.STATUS_ONLINE) {
185						activity.startActivity(new Intent(activity.getApplicationContext(),NewConversationActivity.class));
186					} else if (account.isOptionSet(Account.OPTION_REGISTER)) {
187						editAccount(account);
188					}
189				} else {
190					selectedAccountForActionMode = accountList.get(position);
191					actionMode.invalidate();
192				}
193			}
194		});
195		accountListView.setOnItemLongClickListener(new OnItemLongClickListener() {
196
197			@Override
198			public boolean onItemLongClick(AdapterView<?> arg0, View view,
199					int position, long arg3) {
200				if (!isActionMode) {
201					accountListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
202					accountListView.setItemChecked(position,true);
203					selectedAccountForActionMode = accountList.get(position);
204					actionMode = activity.startActionMode((new ActionMode.Callback() {
205						
206						@Override
207						public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
208							if (selectedAccountForActionMode.isOptionSet(Account.OPTION_DISABLED)) {
209					        	menu.findItem(R.id.mgmt_account_enable).setVisible(true);
210					        	menu.findItem(R.id.mgmt_account_disable).setVisible(false);
211					        } else {
212					        	menu.findItem(R.id.mgmt_account_disable).setVisible(true);
213					        	menu.findItem(R.id.mgmt_account_enable).setVisible(false);
214					        }
215							return true;
216						}
217						
218						@Override
219						public void onDestroyActionMode(ActionMode mode) {
220							// TODO Auto-generated method stub
221							
222						}
223						
224						@Override
225						public boolean onCreateActionMode(ActionMode mode, Menu menu) {
226							MenuInflater inflater = mode.getMenuInflater();
227					        inflater.inflate(R.menu.manageaccounts_context, menu);
228							return true;
229						}
230						
231						@Override
232						public boolean onActionItemClicked(final ActionMode mode, MenuItem item) {
233							if (item.getItemId()==R.id.mgmt_account_edit) {
234								editAccount(selectedAccountForActionMode);
235							} else if (item.getItemId()==R.id.mgmt_account_disable) {
236								selectedAccountForActionMode.setOption(Account.OPTION_DISABLED, true);
237								xmppConnectionService.updateAccount(selectedAccountForActionMode);
238								mode.finish();
239							} else if (item.getItemId()==R.id.mgmt_account_enable) {
240								selectedAccountForActionMode.setOption(Account.OPTION_DISABLED, false);
241								xmppConnectionService.updateAccount(selectedAccountForActionMode);
242								mode.finish();
243							} else if (item.getItemId()==R.id.mgmt_account_delete) {
244								AlertDialog.Builder builder = new AlertDialog.Builder(activity);
245								builder.setTitle("Are you sure?");
246								builder.setIconAttribute(android.R.attr.alertDialogIcon);
247								builder.setMessage("If you delete your account your entire conversation history will be lost");
248								builder.setPositiveButton("Delete", new OnClickListener() {
249									
250									@Override
251									public void onClick(DialogInterface dialog, int which) {
252										xmppConnectionService.deleteAccount(selectedAccountForActionMode);
253										selectedAccountForActionMode = null;
254										mode.finish();
255									}
256								});
257								builder.setNegativeButton("Cancel",null);
258								builder.create().show();
259							} else if (item.getItemId()==R.id.mgmt_account_announce_pgp) {
260								if (activity.hasPgp()) {
261									mode.finish();
262									try {
263										xmppConnectionService.generatePgpAnnouncement(selectedAccountForActionMode);
264									} catch (PgpEngine.UserInputRequiredException e) {
265										try {
266											startIntentSenderForResult(e.getPendingIntent().getIntentSender(), REQUEST_ANNOUNCE_PGP, null, 0, 0, 0);
267										} catch (SendIntentException e1) {
268											Log.d("gultsch","sending intent failed");
269										}
270									}
271								}
272							} else if (item.getItemId() == R.id.mgmt_account_info) {
273								AlertDialog.Builder builder = new AlertDialog.Builder(activity);
274								builder.setTitle(getString(R.string.account_info));
275								if (selectedAccountForActionMode.getStatus() == Account.STATUS_ONLINE) {
276									XmppConnection xmpp = selectedAccountForActionMode.getXmppConnection();
277									long connectionAge = (SystemClock.elapsedRealtime() - xmpp.lastConnect) / 60000;
278									long sessionAge = (SystemClock.elapsedRealtime() - xmpp.lastSessionStarted) / 60000;
279									View view = (View) getLayoutInflater().inflate(R.layout.server_info, null);
280									TextView connection = (TextView) view.findViewById(R.id.connection);
281									TextView session = (TextView) view.findViewById(R.id.session);
282									TextView pcks_sent = (TextView) view.findViewById(R.id.pcks_sent);
283									TextView pcks_received = (TextView) view.findViewById(R.id.pcks_received);
284									pcks_received.setText(""+xmpp.getReceivedStanzas());
285									pcks_sent.setText(""+xmpp.getSentStanzas());
286									connection.setText(connectionAge+" mins");
287									session.setText(sessionAge+" mins");
288									builder.setView(view);
289								} else {
290									builder.setMessage("Account is offline");
291								}
292								builder.setPositiveButton("Hide", null);
293								builder.create().show();
294							}
295							return true;
296						}
297
298						
299					}));
300					return true;
301				} else {
302					return false;
303				}
304			}
305		});
306	}
307
308	@Override
309	protected void onStop() {
310		if (xmppConnectionServiceBound) {
311			xmppConnectionService.removeOnAccountListChangedListener();
312			xmppConnectionService.removeOnTLSExceptionReceivedListener();
313		}
314		super.onStop();
315	}
316
317	@Override
318	void onBackendConnected() {
319		xmppConnectionService.setOnAccountListChangedListener(accountChanged);
320		xmppConnectionService.setOnTLSExceptionReceivedListener(tlsExceptionReceived);
321		this.accountList.clear();
322		this.accountList.addAll(xmppConnectionService.getAccounts());
323		accountListViewAdapter.notifyDataSetChanged();
324		if (this.accountList.size() == 0) {
325			getActionBar().setDisplayHomeAsUpEnabled(false);
326			addAccount();
327		}
328	}
329
330	@Override
331	public boolean onCreateOptionsMenu(Menu menu) {
332		getMenuInflater().inflate(R.menu.manageaccounts, menu);
333		return true;
334	}
335
336	@Override
337	public boolean onOptionsItemSelected(MenuItem item) {
338		switch (item.getItemId()) {
339		case R.id.action_settings:
340			startActivity(new Intent(this, SettingsActivity.class));
341			break;
342		case R.id.action_add_account:
343			addAccount();
344			break;
345		default:
346			break;
347		}
348		return super.onOptionsItemSelected(item);
349	}
350
351	private void editAccount(Account account) {
352			EditAccount dialog = new EditAccount();
353			dialog.setAccount(account);
354			dialog.setEditAccountListener(new EditAccountListener() {
355
356				@Override
357				public void onAccountEdited(Account account) {
358					xmppConnectionService.updateAccount(account);
359					actionMode.finish();
360				}
361			});
362			dialog.show(getFragmentManager(), "edit_account");
363		
364	}
365	
366	protected void addAccount() {
367		final Activity activity = this;
368		EditAccount dialog = new EditAccount();
369		dialog.setEditAccountListener(new EditAccountListener() {
370
371			@Override
372			public void onAccountEdited(Account account) {
373				xmppConnectionService.createAccount(account);
374				activity.getActionBar().setDisplayHomeAsUpEnabled(true);
375			}
376		});
377		dialog.show(getFragmentManager(), "add_account");
378	}
379
380	
381	@Override
382	public void onActionModeStarted(ActionMode mode) {
383		super.onActionModeStarted(mode);
384		this.isActionMode = true;
385	}
386	
387	@Override
388	public void onActionModeFinished(ActionMode mode) {
389		super.onActionModeFinished(mode);
390		this.isActionMode = false;
391		accountListView.clearChoices();
392		accountListView.requestLayout();
393		accountListView.post(new Runnable() {
394            @Override
395            public void run() {
396                accountListView.setChoiceMode(ListView.CHOICE_MODE_NONE);
397            }
398        });
399	}
400	
401	 @Override
402	 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
403		 super.onActivityResult(requestCode, resultCode, data);
404		 if (resultCode == RESULT_OK) {
405			if (requestCode == REQUEST_ANNOUNCE_PGP) {
406				 try {
407					xmppConnectionService.generatePgpAnnouncement(selectedAccountForActionMode);
408				} catch (UserInputRequiredException e) {
409					Log.d("gultsch","already came back. ignoring");
410				}
411			 }
412		 }
413	 }
414}