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 android.app.Activity;
 13import android.app.AlertDialog;
 14import android.content.Context;
 15import android.content.DialogInterface;
 16import android.content.DialogInterface.OnClickListener;
 17import android.content.Intent;
 18import android.content.IntentSender.SendIntentException;
 19import android.os.Bundle;
 20import android.util.Log;
 21import android.view.ActionMode;
 22import android.view.LayoutInflater;
 23import android.view.Menu;
 24import android.view.MenuInflater;
 25import android.view.MenuItem;
 26import android.view.View;
 27import android.view.ViewGroup;
 28import android.widget.AdapterView;
 29import android.widget.AdapterView.OnItemClickListener;
 30import android.widget.AdapterView.OnItemLongClickListener;
 31import android.widget.ArrayAdapter;
 32import android.widget.ListView;
 33import android.widget.TextView;
 34
 35public class ManageAccountActivity extends XmppActivity {
 36
 37	public static final int REQUEST_ANNOUNCE_PGP = 0x73731;
 38	
 39	protected boolean isActionMode = false;
 40	protected ActionMode actionMode;
 41	protected Account selectedAccountForActionMode = null;
 42	protected ManageAccountActivity activity = this;
 43	
 44	protected List<Account> accountList = new ArrayList<Account>();
 45	protected ListView accountListView;
 46	protected ArrayAdapter<Account> accountListViewAdapter;
 47	protected OnAccountListChangedListener accountChanged = new OnAccountListChangedListener() {
 48
 49		@Override
 50		public void onAccountListChangedListener() {
 51			Log.d("xmppService", "ui on account list changed listener");
 52			accountList.clear();
 53			accountList.addAll(xmppConnectionService.getAccounts());
 54			runOnUiThread(new Runnable() {
 55
 56				@Override
 57				public void run() {
 58					accountListViewAdapter.notifyDataSetChanged();
 59				}
 60			});
 61		}
 62	};
 63	
 64	protected OnTLSExceptionReceived tlsExceptionReceived = new OnTLSExceptionReceived() {
 65		
 66		@Override
 67		public void onTLSExceptionReceived(final String fingerprint, final Account account) {
 68			activity.runOnUiThread(new Runnable() {
 69				
 70				@Override
 71				public void run() {
 72					AlertDialog.Builder builder = new AlertDialog.Builder(activity);
 73					builder.setTitle("Untrusted Certificate");
 74					builder.setIconAttribute(android.R.attr.alertDialogIcon);
 75					View view = (View) getLayoutInflater().inflate(R.layout.cert_warning, null);
 76					TextView sha = (TextView) view.findViewById(R.id.sha);
 77					TextView hint = (TextView) view.findViewById(R.id.hint);
 78					StringBuilder humanReadableSha = new StringBuilder();
 79					humanReadableSha.append(fingerprint);
 80					for(int i = 2; i < 59; i += 3) {
 81						Log.d("gultsch","insert into "+i);
 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));
184					} else if (account.getStatus() == Account.STATUS_ONLINE) {
185						activity.startActivity(new Intent(activity.getApplicationContext(),NewConversationActivity.class));
186					}
187					
188					Log.d("gultsch","clicked on account "+accountList.get(position).getJid());
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 dialog = new EditAccount();
235								dialog.setAccount(selectedAccountForActionMode);
236								dialog.setEditAccountListener(new EditAccountListener() {
237				
238									@Override
239									public void onAccountEdited(Account account) {
240										xmppConnectionService.updateAccount(account);
241										actionMode.finish();
242									}
243								});
244								dialog.show(getFragmentManager(), "edit_account");
245							} else if (item.getItemId()==R.id.mgmt_account_disable) {
246								selectedAccountForActionMode.setOption(Account.OPTION_DISABLED, true);
247								xmppConnectionService.updateAccount(selectedAccountForActionMode);
248								mode.finish();
249							} else if (item.getItemId()==R.id.mgmt_account_enable) {
250								selectedAccountForActionMode.setOption(Account.OPTION_DISABLED, false);
251								xmppConnectionService.updateAccount(selectedAccountForActionMode);
252								mode.finish();
253							} else if (item.getItemId()==R.id.mgmt_account_delete) {
254								AlertDialog.Builder builder = new AlertDialog.Builder(activity);
255								builder.setTitle("Are you sure?");
256								builder.setIconAttribute(android.R.attr.alertDialogIcon);
257								builder.setMessage("If you delete your account your entire conversation history will be lost");
258								builder.setPositiveButton("Delete", new OnClickListener() {
259									
260									@Override
261									public void onClick(DialogInterface dialog, int which) {
262										xmppConnectionService.deleteAccount(selectedAccountForActionMode);
263										selectedAccountForActionMode = null;
264										mode.finish();
265									}
266								});
267								builder.setNegativeButton("Cancel",null);
268								builder.create().show();
269							} else if (item.getItemId()==R.id.mgmt_account_announce_pgp) {
270								if (activity.hasPgp()) {
271									mode.finish();
272									try {
273										xmppConnectionService.generatePgpAnnouncement(selectedAccountForActionMode);
274									} catch (PgpEngine.UserInputRequiredException e) {
275										try {
276											startIntentSenderForResult(e.getPendingIntent().getIntentSender(), REQUEST_ANNOUNCE_PGP, null, 0, 0, 0);
277										} catch (SendIntentException e1) {
278											Log.d("gultsch","sending intent failed");
279										}
280									}
281								}
282							}
283							return true;
284						}
285					}));
286					return true;
287				} else {
288					return false;
289				}
290			}
291		});
292	}
293
294	@Override
295	protected void onStop() {
296		if (xmppConnectionServiceBound) {
297			xmppConnectionService.removeOnAccountListChangedListener();
298			xmppConnectionService.removeOnTLSExceptionReceivedListener();
299		}
300		super.onStop();
301	}
302
303	@Override
304	void onBackendConnected() {
305		xmppConnectionService.setOnAccountListChangedListener(accountChanged);
306		xmppConnectionService.setOnTLSExceptionReceivedListener(tlsExceptionReceived);
307		this.accountList.clear();
308		this.accountList.addAll(xmppConnectionService.getAccounts());
309		accountListViewAdapter.notifyDataSetChanged();
310		if (this.accountList.size() == 0) {
311			getActionBar().setDisplayHomeAsUpEnabled(false);
312			addAccount();
313		}
314	}
315
316	@Override
317	public boolean onCreateOptionsMenu(Menu menu) {
318		getMenuInflater().inflate(R.menu.manageaccounts, menu);
319		return true;
320	}
321
322	@Override
323	public boolean onOptionsItemSelected(MenuItem item) {
324		switch (item.getItemId()) {
325		case R.id.action_settings:
326			startActivity(new Intent(this, SettingsActivity.class));
327			break;
328		case R.id.action_add_account:
329			addAccount();
330			break;
331		default:
332			break;
333		}
334		return super.onOptionsItemSelected(item);
335	}
336
337	protected void addAccount() {
338		final Activity activity = this;
339		EditAccount dialog = new EditAccount();
340		dialog.setEditAccountListener(new EditAccountListener() {
341
342			@Override
343			public void onAccountEdited(Account account) {
344				xmppConnectionService.createAccount(account);
345				activity.getActionBar().setDisplayHomeAsUpEnabled(true);
346			}
347		});
348		dialog.show(getFragmentManager(), "add_account");
349	}
350
351	
352	@Override
353	public void onActionModeStarted(ActionMode mode) {
354		super.onActionModeStarted(mode);
355		this.isActionMode = true;
356	}
357	
358	@Override
359	public void onActionModeFinished(ActionMode mode) {
360		super.onActionModeFinished(mode);
361		this.isActionMode = false;
362		accountListView.clearChoices();
363		accountListView.requestLayout();
364		accountListView.post(new Runnable() {
365            @Override
366            public void run() {
367                accountListView.setChoiceMode(ListView.CHOICE_MODE_NONE);
368            }
369        });
370	}
371	
372	 @Override
373	 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
374		 super.onActivityResult(requestCode, resultCode, data);
375		 if (resultCode == RESULT_OK) {
376			if (requestCode == REQUEST_ANNOUNCE_PGP) {
377				 try {
378					xmppConnectionService.generatePgpAnnouncement(selectedAccountForActionMode);
379				} catch (UserInputRequiredException e) {
380					Log.d("gultsch","already came back. ignoring");
381				}
382			 }
383		 }
384	 }
385}