EditAccountActivity.java

   1package eu.siacs.conversations.ui;
   2
   3import android.app.ActionBar;
   4import android.app.AlertDialog;
   5import android.app.AlertDialog.Builder;
   6import android.app.PendingIntent;
   7import android.content.ActivityNotFoundException;
   8import android.content.DialogInterface;
   9import android.content.Intent;
  10import android.content.SharedPreferences;
  11import android.graphics.Bitmap;
  12import android.net.Uri;
  13import android.os.Bundle;
  14import android.provider.Settings;
  15import android.security.KeyChain;
  16import android.security.KeyChainAliasCallback;
  17import android.text.Editable;
  18import android.text.TextWatcher;
  19import android.view.Menu;
  20import android.view.MenuItem;
  21import android.view.View;
  22import android.view.View.OnClickListener;
  23import android.widget.AutoCompleteTextView;
  24import android.widget.Button;
  25import android.widget.CheckBox;
  26import android.widget.CompoundButton;
  27import android.widget.CompoundButton.OnCheckedChangeListener;
  28import android.widget.EditText;
  29import android.widget.ImageButton;
  30import android.widget.ImageView;
  31import android.widget.LinearLayout;
  32import android.widget.RelativeLayout;
  33import android.widget.TableLayout;
  34import android.widget.TableRow;
  35import android.widget.TextView;
  36import android.widget.Toast;
  37
  38import java.util.Arrays;
  39import java.util.List;
  40import java.util.Set;
  41import java.util.concurrent.atomic.AtomicInteger;
  42
  43import eu.siacs.conversations.Config;
  44import eu.siacs.conversations.OmemoActivity;
  45import eu.siacs.conversations.R;
  46import eu.siacs.conversations.crypto.axolotl.AxolotlService;
  47import eu.siacs.conversations.crypto.axolotl.XmppAxolotlSession;
  48import eu.siacs.conversations.entities.Account;
  49import eu.siacs.conversations.services.BarcodeProvider;
  50import eu.siacs.conversations.services.XmppConnectionService.OnCaptchaRequested;
  51import eu.siacs.conversations.services.XmppConnectionService;
  52import eu.siacs.conversations.services.XmppConnectionService.OnAccountUpdate;
  53import eu.siacs.conversations.ui.adapter.KnownHostsAdapter;
  54import eu.siacs.conversations.utils.CryptoHelper;
  55import eu.siacs.conversations.utils.UIHelper;
  56import eu.siacs.conversations.xml.Element;
  57import eu.siacs.conversations.xmpp.OnKeyStatusUpdated;
  58import eu.siacs.conversations.xmpp.XmppConnection;
  59import eu.siacs.conversations.xmpp.XmppConnection.Features;
  60import eu.siacs.conversations.xmpp.forms.Data;
  61import eu.siacs.conversations.xmpp.jid.InvalidJidException;
  62import eu.siacs.conversations.xmpp.jid.Jid;
  63import eu.siacs.conversations.xmpp.pep.Avatar;
  64
  65public class EditAccountActivity extends OmemoActivity implements OnAccountUpdate,
  66		OnKeyStatusUpdated, OnCaptchaRequested, KeyChainAliasCallback, XmppConnectionService.OnShowErrorToast, XmppConnectionService.OnMamPreferencesFetched {
  67
  68	private static final int REQUEST_DATA_SAVER = 0x37af244;
  69	private AutoCompleteTextView mAccountJid;
  70	private EditText mPassword;
  71	private EditText mPasswordConfirm;
  72	private CheckBox mRegisterNew;
  73	private Button mCancelButton;
  74	private Button mSaveButton;
  75	private Button mDisableOsOptimizationsButton;
  76	private TextView mDisableOsOptimizationsHeadline;
  77	private TextView getmDisableOsOptimizationsBody;
  78	private TableLayout mMoreTable;
  79
  80	private LinearLayout mStats;
  81	private RelativeLayout mOsOptimizations;
  82	private TextView mServerInfoSm;
  83	private TextView mServerInfoRosterVersion;
  84	private TextView mServerInfoCarbons;
  85	private TextView mServerInfoMam;
  86	private TextView mServerInfoCSI;
  87	private TextView mServerInfoBlocking;
  88	private TextView mServerInfoPep;
  89	private TextView mServerInfoHttpUpload;
  90	private TextView mServerInfoPush;
  91	private TextView mSessionEst;
  92	private TextView mOtrFingerprint;
  93	private TextView mAxolotlFingerprint;
  94	private TextView mOwnFingerprintDesc;
  95	private TextView mAccountJidLabel;
  96	private ImageView mAvatar;
  97	private RelativeLayout mOtrFingerprintBox;
  98	private RelativeLayout mAxolotlFingerprintBox;
  99	private ImageButton mOtrFingerprintToClipboardButton;
 100	private ImageButton mAxolotlFingerprintToClipboardButton;
 101	private ImageButton mRegenerateAxolotlKeyButton;
 102	private LinearLayout keys;
 103	private LinearLayout keysCard;
 104	private LinearLayout mNamePort;
 105	private EditText mHostname;
 106	private EditText mPort;
 107	private AlertDialog mCaptchaDialog = null;
 108
 109	private Jid jidToEdit;
 110	private boolean mInitMode = false;
 111	private boolean mUsernameMode = Config.DOMAIN_LOCK != null;
 112	private boolean mShowOptions = false;
 113	private Account mAccount;
 114	private String messageFingerprint;
 115
 116	private boolean mFetchingAvatar = false;
 117
 118	private final OnClickListener mSaveButtonClickListener = new OnClickListener() {
 119
 120		@Override
 121		public void onClick(final View v) {
 122			final String password = mPassword.getText().toString();
 123			final String passwordConfirm = mPasswordConfirm.getText().toString();
 124
 125			if (!mInitMode && passwordChangedInMagicCreateMode()) {
 126				gotoChangePassword(password);
 127				return;
 128			}
 129			if (mInitMode && mAccount != null) {
 130				mAccount.setOption(Account.OPTION_DISABLED, false);
 131			}
 132			if (mAccount != null && mAccount.getStatus() == Account.State.DISABLED && !accountInfoEdited()) {
 133				mAccount.setOption(Account.OPTION_DISABLED, false);
 134				if (!xmppConnectionService.updateAccount(mAccount)) {
 135					Toast.makeText(EditAccountActivity.this,R.string.unable_to_update_account,Toast.LENGTH_SHORT).show();
 136				}
 137				return;
 138			}
 139			final boolean registerNewAccount = mRegisterNew.isChecked() && !Config.DISALLOW_REGISTRATION_IN_UI;
 140			if (mUsernameMode && mAccountJid.getText().toString().contains("@")) {
 141				mAccountJid.setError(getString(R.string.invalid_username));
 142				mAccountJid.requestFocus();
 143				return;
 144			}
 145			final Jid jid;
 146			try {
 147				if (mUsernameMode) {
 148					jid = Jid.fromParts(mAccountJid.getText().toString(), getUserModeDomain(), null);
 149				} else {
 150					jid = Jid.fromString(mAccountJid.getText().toString());
 151				}
 152			} catch (final InvalidJidException e) {
 153				if (mUsernameMode) {
 154					mAccountJid.setError(getString(R.string.invalid_username));
 155				} else {
 156					mAccountJid.setError(getString(R.string.invalid_jid));
 157				}
 158				mAccountJid.requestFocus();
 159				return;
 160			}
 161			String hostname = null;
 162			int numericPort = 5222;
 163			if (mShowOptions) {
 164				hostname = mHostname.getText().toString().replaceAll("\\s","");
 165				final String port = mPort.getText().toString().replaceAll("\\s","");
 166				if (hostname.contains(" ")) {
 167					mHostname.setError(getString(R.string.not_valid_hostname));
 168					mHostname.requestFocus();
 169					return;
 170				}
 171				try {
 172					numericPort = Integer.parseInt(port);
 173					if (numericPort < 0 || numericPort > 65535) {
 174						mPort.setError(getString(R.string.not_a_valid_port));
 175						mPort.requestFocus();
 176						return;
 177					}
 178
 179				} catch (NumberFormatException e) {
 180					mPort.setError(getString(R.string.not_a_valid_port));
 181					mPort.requestFocus();
 182					return;
 183				}
 184			}
 185
 186			if (jid.isDomainJid()) {
 187				if (mUsernameMode) {
 188					mAccountJid.setError(getString(R.string.invalid_username));
 189				} else {
 190					mAccountJid.setError(getString(R.string.invalid_jid));
 191				}
 192				mAccountJid.requestFocus();
 193				return;
 194			}
 195			if (registerNewAccount) {
 196				if (!password.equals(passwordConfirm)) {
 197					mPasswordConfirm.setError(getString(R.string.passwords_do_not_match));
 198					mPasswordConfirm.requestFocus();
 199					return;
 200				}
 201			}
 202			if (mAccount != null) {
 203				if (mInitMode && mAccount.isOptionSet(Account.OPTION_MAGIC_CREATE)) {
 204					mAccount.setOption(Account.OPTION_MAGIC_CREATE, mAccount.getPassword().contains(password));
 205				}
 206				mAccount.setJid(jid);
 207				mAccount.setPort(numericPort);
 208				mAccount.setHostname(hostname);
 209				mAccountJid.setError(null);
 210				mPasswordConfirm.setError(null);
 211				mAccount.setPassword(password);
 212				mAccount.setOption(Account.OPTION_REGISTER, registerNewAccount);
 213				if (!xmppConnectionService.updateAccount(mAccount)) {
 214					Toast.makeText(EditAccountActivity.this,R.string.unable_to_update_account,Toast.LENGTH_SHORT).show();
 215					return;
 216				}
 217			} else {
 218				if (xmppConnectionService.findAccountByJid(jid) != null) {
 219					mAccountJid.setError(getString(R.string.account_already_exists));
 220					mAccountJid.requestFocus();
 221					return;
 222				}
 223				mAccount = new Account(jid.toBareJid(), password);
 224				mAccount.setPort(numericPort);
 225				mAccount.setHostname(hostname);
 226				mAccount.setOption(Account.OPTION_USETLS, true);
 227				mAccount.setOption(Account.OPTION_USECOMPRESSION, true);
 228				mAccount.setOption(Account.OPTION_REGISTER, registerNewAccount);
 229				xmppConnectionService.createAccount(mAccount);
 230			}
 231			mHostname.setError(null);
 232			mPort.setError(null);
 233			if (!mAccount.isOptionSet(Account.OPTION_DISABLED)
 234					&& !registerNewAccount
 235					&& !mInitMode) {
 236				finish();
 237			} else {
 238				updateSaveButton();
 239				updateAccountInformation(true);
 240			}
 241
 242		}
 243	};
 244	private final OnClickListener mCancelButtonClickListener = new OnClickListener() {
 245
 246		@Override
 247		public void onClick(final View v) {
 248			deleteMagicCreatedAccountAndReturnIfNecessary();
 249			finish();
 250		}
 251	};
 252	private Toast mFetchingMamPrefsToast;
 253	private TableRow mPushRow;
 254	private String mSavedInstanceAccount;
 255	private boolean mSavedInstanceInit = false;
 256	private Button mClearDevicesButton;
 257
 258	public void refreshUiReal() {
 259		invalidateOptionsMenu();
 260		if (mAccount != null
 261				&& mAccount.getStatus() != Account.State.ONLINE
 262				&& mFetchingAvatar) {
 263			startActivity(new Intent(getApplicationContext(),
 264					ManageAccountActivity.class));
 265			finish();
 266		} else if (mInitMode && mAccount != null && mAccount.getStatus() == Account.State.ONLINE) {
 267			if (!mFetchingAvatar) {
 268				mFetchingAvatar = true;
 269				xmppConnectionService.checkForAvatar(mAccount, mAvatarFetchCallback);
 270			}
 271		}
 272		if (mAccount != null) {
 273			updateAccountInformation(false);
 274		}
 275		updateSaveButton();
 276	}
 277
 278	@Override
 279	public boolean onNavigateUp() {
 280		deleteMagicCreatedAccountAndReturnIfNecessary();
 281		return super.onNavigateUp();
 282	}
 283
 284	@Override
 285	public void onBackPressed() {
 286		deleteMagicCreatedAccountAndReturnIfNecessary();
 287		super.onBackPressed();
 288	}
 289
 290	private void deleteMagicCreatedAccountAndReturnIfNecessary() {
 291		if (Config.MAGIC_CREATE_DOMAIN != null
 292				&& mAccount != null
 293				&& mAccount.isOptionSet(Account.OPTION_MAGIC_CREATE)
 294				&& mAccount.isOptionSet(Account.OPTION_REGISTER)
 295				&& xmppConnectionService.getAccounts().size() == 1) {
 296			xmppConnectionService.deleteAccount(mAccount);
 297			startActivity(new Intent(EditAccountActivity.this, WelcomeActivity.class));
 298		}
 299	}
 300
 301	@Override
 302	public void onAccountUpdate() {
 303		refreshUi();
 304	}
 305
 306	private final UiCallback<Avatar> mAvatarFetchCallback = new UiCallback<Avatar>() {
 307
 308		@Override
 309		public void userInputRequried(final PendingIntent pi, final Avatar avatar) {
 310			finishInitialSetup(avatar);
 311		}
 312
 313		@Override
 314		public void success(final Avatar avatar) {
 315			finishInitialSetup(avatar);
 316		}
 317
 318		@Override
 319		public void error(final int errorCode, final Avatar avatar) {
 320			finishInitialSetup(avatar);
 321		}
 322	};
 323	private final TextWatcher mTextWatcher = new TextWatcher() {
 324
 325		@Override
 326		public void onTextChanged(final CharSequence s, final int start, final int before, final int count) {
 327			updateSaveButton();
 328		}
 329
 330		@Override
 331		public void beforeTextChanged(final CharSequence s, final int start, final int count, final int after) {
 332		}
 333
 334		@Override
 335		public void afterTextChanged(final Editable s) {
 336
 337		}
 338	};
 339
 340	private final OnClickListener mAvatarClickListener = new OnClickListener() {
 341		@Override
 342		public void onClick(final View view) {
 343			if (mAccount != null) {
 344				final Intent intent = new Intent(getApplicationContext(), PublishProfilePictureActivity.class);
 345				intent.putExtra(EXTRA_ACCOUNT, mAccount.getJid().toBareJid().toString());
 346				startActivity(intent);
 347			}
 348		}
 349	};
 350
 351	protected void finishInitialSetup(final Avatar avatar) {
 352		runOnUiThread(new Runnable() {
 353
 354			@Override
 355			public void run() {
 356				final Intent intent;
 357				final XmppConnection connection = mAccount.getXmppConnection();
 358				final boolean wasFirstAccount = xmppConnectionService != null && xmppConnectionService.getAccounts().size() == 1;
 359				if (avatar != null || (connection != null && !connection.getFeatures().pep())) {
 360					intent = new Intent(getApplicationContext(), StartConversationActivity.class);
 361					if (wasFirstAccount) {
 362						intent.putExtra("init", true);
 363					}
 364				} else {
 365					intent = new Intent(getApplicationContext(), PublishProfilePictureActivity.class);
 366					intent.putExtra(EXTRA_ACCOUNT, mAccount.getJid().toBareJid().toString());
 367					intent.putExtra("setup", true);
 368				}
 369				if (wasFirstAccount) {
 370					intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
 371				}
 372				startActivity(intent);
 373				finish();
 374			}
 375		});
 376	}
 377
 378	@Override
 379	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
 380		super.onActivityResult(requestCode, resultCode, data);
 381		if (requestCode == REQUEST_BATTERY_OP || requestCode == REQUEST_DATA_SAVER) {
 382			updateAccountInformation(mAccount == null);
 383		}
 384	}
 385
 386	protected void updateSaveButton() {
 387		boolean accountInfoEdited = accountInfoEdited();
 388
 389		if (!mInitMode && passwordChangedInMagicCreateMode()) {
 390			this.mSaveButton.setText(R.string.change_password);
 391			this.mSaveButton.setEnabled(true);
 392			this.mSaveButton.setTextColor(getPrimaryTextColor());
 393		} else if (accountInfoEdited && !mInitMode) {
 394			this.mSaveButton.setText(R.string.save);
 395			this.mSaveButton.setEnabled(true);
 396			this.mSaveButton.setTextColor(getPrimaryTextColor());
 397		} else if (mAccount != null
 398				&& (mAccount.getStatus() == Account.State.CONNECTING || mAccount.getStatus() == Account.State.REGISTRATION_SUCCESSFUL|| mFetchingAvatar)) {
 399			this.mSaveButton.setEnabled(false);
 400			this.mSaveButton.setTextColor(getSecondaryTextColor());
 401			this.mSaveButton.setText(R.string.account_status_connecting);
 402		} else if (mAccount != null && mAccount.getStatus() == Account.State.DISABLED && !mInitMode) {
 403			this.mSaveButton.setEnabled(true);
 404			this.mSaveButton.setTextColor(getPrimaryTextColor());
 405			this.mSaveButton.setText(R.string.enable);
 406		} else {
 407			this.mSaveButton.setEnabled(true);
 408			this.mSaveButton.setTextColor(getPrimaryTextColor());
 409			if (!mInitMode) {
 410				if (mAccount != null && mAccount.isOnlineAndConnected()) {
 411					this.mSaveButton.setText(R.string.save);
 412					if (!accountInfoEdited) {
 413						this.mSaveButton.setEnabled(false);
 414						this.mSaveButton.setTextColor(getSecondaryTextColor());
 415					}
 416				} else {
 417					this.mSaveButton.setText(R.string.connect);
 418				}
 419			} else {
 420				this.mSaveButton.setText(R.string.next);
 421			}
 422		}
 423	}
 424
 425	protected boolean accountInfoEdited() {
 426		if (this.mAccount == null) {
 427			return false;
 428		}
 429		return jidEdited() ||
 430				!this.mAccount.getPassword().equals(this.mPassword.getText().toString()) ||
 431				!this.mAccount.getHostname().equals(this.mHostname.getText().toString()) ||
 432				!String.valueOf(this.mAccount.getPort()).equals(this.mPort.getText().toString());
 433	}
 434
 435	protected boolean jidEdited() {
 436		final String unmodified;
 437		if (mUsernameMode) {
 438			unmodified = this.mAccount.getJid().getLocalpart();
 439		} else {
 440			unmodified = this.mAccount.getJid().toBareJid().toString();
 441		}
 442		return !unmodified.equals(this.mAccountJid.getText().toString());
 443	}
 444
 445	protected boolean passwordChangedInMagicCreateMode() {
 446		return mAccount != null
 447				&& mAccount.isOptionSet(Account.OPTION_MAGIC_CREATE)
 448				&& !this.mAccount.getPassword().equals(this.mPassword.getText().toString())
 449				&& !this.jidEdited()
 450				&& mAccount.isOnlineAndConnected();
 451	}
 452
 453	@Override
 454	protected String getShareableUri() {
 455		if (mAccount != null) {
 456			return mAccount.getShareableUri();
 457		} else {
 458			return "";
 459		}
 460	}
 461
 462	@Override
 463	protected void onCreate(final Bundle savedInstanceState) {
 464		super.onCreate(savedInstanceState);
 465		if (savedInstanceState != null) {
 466			this.mSavedInstanceAccount = savedInstanceState.getString("account");
 467			this.mSavedInstanceInit = savedInstanceState.getBoolean("initMode", false);
 468		}
 469		setContentView(R.layout.activity_edit_account);
 470		this.mAccountJid = (AutoCompleteTextView) findViewById(R.id.account_jid);
 471		this.mAccountJid.addTextChangedListener(this.mTextWatcher);
 472		this.mAccountJidLabel = (TextView) findViewById(R.id.account_jid_label);
 473		this.mPassword = (EditText) findViewById(R.id.account_password);
 474		this.mPassword.addTextChangedListener(this.mTextWatcher);
 475		this.mPasswordConfirm = (EditText) findViewById(R.id.account_password_confirm);
 476		this.mAvatar = (ImageView) findViewById(R.id.avater);
 477		this.mAvatar.setOnClickListener(this.mAvatarClickListener);
 478		this.mRegisterNew = (CheckBox) findViewById(R.id.account_register_new);
 479		this.mStats = (LinearLayout) findViewById(R.id.stats);
 480		this.mOsOptimizations = (RelativeLayout) findViewById(R.id.os_optimization);
 481		this.mDisableOsOptimizationsButton = (Button) findViewById(R.id.os_optimization_disable);
 482		this.mDisableOsOptimizationsHeadline = (TextView) findViewById(R.id.os_optimization_headline);
 483		this.getmDisableOsOptimizationsBody = (TextView) findViewById(R.id.os_optimization_body);
 484		this.mSessionEst = (TextView) findViewById(R.id.session_est);
 485		this.mServerInfoRosterVersion = (TextView) findViewById(R.id.server_info_roster_version);
 486		this.mServerInfoCarbons = (TextView) findViewById(R.id.server_info_carbons);
 487		this.mServerInfoMam = (TextView) findViewById(R.id.server_info_mam);
 488		this.mServerInfoCSI = (TextView) findViewById(R.id.server_info_csi);
 489		this.mServerInfoBlocking = (TextView) findViewById(R.id.server_info_blocking);
 490		this.mServerInfoSm = (TextView) findViewById(R.id.server_info_sm);
 491		this.mServerInfoPep = (TextView) findViewById(R.id.server_info_pep);
 492		this.mServerInfoHttpUpload = (TextView) findViewById(R.id.server_info_http_upload);
 493		this.mPushRow = (TableRow) findViewById(R.id.push_row);
 494		this.mServerInfoPush = (TextView) findViewById(R.id.server_info_push);
 495		this.mOtrFingerprint = (TextView) findViewById(R.id.otr_fingerprint);
 496		this.mOtrFingerprintBox = (RelativeLayout) findViewById(R.id.otr_fingerprint_box);
 497		this.mOtrFingerprintToClipboardButton = (ImageButton) findViewById(R.id.action_copy_to_clipboard);
 498		this.mAxolotlFingerprint = (TextView) findViewById(R.id.axolotl_fingerprint);
 499		this.mAxolotlFingerprintBox = (RelativeLayout) findViewById(R.id.axolotl_fingerprint_box);
 500		this.mAxolotlFingerprintToClipboardButton = (ImageButton) findViewById(R.id.action_copy_axolotl_to_clipboard);
 501		this.mRegenerateAxolotlKeyButton = (ImageButton) findViewById(R.id.action_regenerate_axolotl_key);
 502		this.mOwnFingerprintDesc = (TextView) findViewById(R.id.own_fingerprint_desc);
 503		this.keysCard = (LinearLayout) findViewById(R.id.other_device_keys_card);
 504		this.keys = (LinearLayout) findViewById(R.id.other_device_keys);
 505		this.mNamePort = (LinearLayout) findViewById(R.id.name_port);
 506		this.mHostname = (EditText) findViewById(R.id.hostname);
 507		this.mHostname.addTextChangedListener(mTextWatcher);
 508		this.mClearDevicesButton = (Button) findViewById(R.id.clear_devices);
 509		this.mClearDevicesButton.setOnClickListener(new OnClickListener() {
 510			@Override
 511			public void onClick(View v) {
 512				showWipePepDialog();
 513			}
 514		});
 515		this.mPort = (EditText) findViewById(R.id.port);
 516		this.mPort.setText("5222");
 517		this.mPort.addTextChangedListener(mTextWatcher);
 518		this.mSaveButton = (Button) findViewById(R.id.save_button);
 519		this.mCancelButton = (Button) findViewById(R.id.cancel_button);
 520		this.mSaveButton.setOnClickListener(this.mSaveButtonClickListener);
 521		this.mCancelButton.setOnClickListener(this.mCancelButtonClickListener);
 522		this.mMoreTable = (TableLayout) findViewById(R.id.server_info_more);
 523		if (savedInstanceState != null && savedInstanceState.getBoolean("showMoreTable")) {
 524			changeMoreTableVisibility(true);
 525		}
 526		final OnCheckedChangeListener OnCheckedShowConfirmPassword = new OnCheckedChangeListener() {
 527			@Override
 528			public void onCheckedChanged(final CompoundButton buttonView,
 529										 final boolean isChecked) {
 530				if (isChecked) {
 531					mPasswordConfirm.setVisibility(View.VISIBLE);
 532				} else {
 533					mPasswordConfirm.setVisibility(View.GONE);
 534				}
 535				updateSaveButton();
 536			}
 537		};
 538		this.mRegisterNew.setOnCheckedChangeListener(OnCheckedShowConfirmPassword);
 539		if (Config.DISALLOW_REGISTRATION_IN_UI) {
 540			this.mRegisterNew.setVisibility(View.GONE);
 541		}
 542	}
 543
 544	@Override
 545	public boolean onCreateOptionsMenu(final Menu menu) {
 546		super.onCreateOptionsMenu(menu);
 547		getMenuInflater().inflate(R.menu.editaccount, menu);
 548		final MenuItem showQrCode = menu.findItem(R.id.action_show_qr_code);
 549		final MenuItem showBlocklist = menu.findItem(R.id.action_show_block_list);
 550		final MenuItem showMoreInfo = menu.findItem(R.id.action_server_info_show_more);
 551		final MenuItem changePassword = menu.findItem(R.id.action_change_password_on_server);
 552		final MenuItem showPassword = menu.findItem(R.id.action_show_password);
 553		final MenuItem renewCertificate = menu.findItem(R.id.action_renew_certificate);
 554		final MenuItem mamPrefs = menu.findItem(R.id.action_mam_prefs);
 555		final MenuItem changePresence = menu.findItem(R.id.action_change_presence);
 556		renewCertificate.setVisible(mAccount != null && mAccount.getPrivateKeyAlias() != null);
 557
 558		if (mAccount != null && mAccount.isOnlineAndConnected()) {
 559			if (!mAccount.getXmppConnection().getFeatures().blocking()) {
 560				showBlocklist.setVisible(false);
 561			}
 562			if (!mAccount.getXmppConnection().getFeatures().register()) {
 563				changePassword.setVisible(false);
 564			}
 565			mamPrefs.setVisible(mAccount.getXmppConnection().getFeatures().mam());
 566			changePresence.setVisible(manuallyChangePresence());
 567		} else {
 568			showQrCode.setVisible(false);
 569			showBlocklist.setVisible(false);
 570			showMoreInfo.setVisible(false);
 571			changePassword.setVisible(false);
 572			mamPrefs.setVisible(false);
 573			changePresence.setVisible(false);
 574		}
 575
 576		if (mAccount != null) {
 577			showPassword.setVisible(mAccount.isOptionSet(Account.OPTION_MAGIC_CREATE)
 578					&& !mAccount.isOptionSet(Account.OPTION_REGISTER));
 579		} else {
 580			showPassword.setVisible(false);
 581		}
 582		return super.onCreateOptionsMenu(menu);
 583	}
 584
 585	@Override
 586	public boolean onPrepareOptionsMenu(Menu menu) {
 587		final MenuItem showMoreInfo = menu.findItem(R.id.action_server_info_show_more);
 588		if (showMoreInfo.isVisible()) {
 589			showMoreInfo.setChecked(mMoreTable.getVisibility() == View.VISIBLE);
 590		}
 591		return super.onPrepareOptionsMenu(menu);
 592	}
 593
 594	@Override
 595	protected void onStart() {
 596		super.onStart();
 597		final int theme = findTheme();
 598		if (this.mTheme != theme) {
 599			recreate();
 600		} else if (getIntent() != null) {
 601			try {
 602				this.jidToEdit = Jid.fromString(getIntent().getStringExtra("jid"));
 603			} catch (final InvalidJidException | NullPointerException ignored) {
 604				this.jidToEdit = null;
 605			}
 606			boolean init = getIntent().getBooleanExtra("init", false);
 607			this.mInitMode = init || this.jidToEdit == null;
 608			this.messageFingerprint = getIntent().getStringExtra("fingerprint");
 609			if (!mInitMode) {
 610				this.mRegisterNew.setVisibility(View.GONE);
 611				if (getActionBar() != null) {
 612					getActionBar().setTitle(getString(R.string.account_details));
 613				}
 614			} else {
 615				this.mAvatar.setVisibility(View.GONE);
 616				ActionBar ab = getActionBar();
 617				if (ab != null) {
 618					if (init && Config.MAGIC_CREATE_DOMAIN == null) {
 619						ab.setDisplayShowHomeEnabled(false);
 620						ab.setDisplayHomeAsUpEnabled(false);
 621					}
 622					ab.setTitle(R.string.action_add_account);
 623				}
 624			}
 625		}
 626		SharedPreferences preferences = getPreferences();
 627		boolean useTor = Config.FORCE_ORBOT || preferences.getBoolean("use_tor", false);
 628		this.mShowOptions = useTor || preferences.getBoolean("show_connection_options", false);
 629		mHostname.setHint(useTor ? R.string.hostname_or_onion : R.string.hostname_example);
 630		this.mNamePort.setVisibility(mShowOptions ? View.VISIBLE : View.GONE);
 631	}
 632
 633	@Override
 634	public void onSaveInstanceState(final Bundle savedInstanceState) {
 635		if (mAccount != null) {
 636			savedInstanceState.putString("account", mAccount.getJid().toBareJid().toString());
 637			savedInstanceState.putBoolean("initMode", mInitMode);
 638			savedInstanceState.putBoolean("showMoreTable", mMoreTable.getVisibility() == View.VISIBLE);
 639		}
 640		super.onSaveInstanceState(savedInstanceState);
 641	}
 642
 643	protected void onBackendConnected() {
 644		boolean init = true;
 645		if (mSavedInstanceAccount != null) {
 646			try {
 647				this.mAccount = xmppConnectionService.findAccountByJid(Jid.fromString(mSavedInstanceAccount));
 648				this.mInitMode = mSavedInstanceInit;
 649				init = false;
 650			} catch (InvalidJidException e) {
 651				this.mAccount = null;
 652			}
 653
 654		} else if (this.jidToEdit != null) {
 655			this.mAccount = xmppConnectionService.findAccountByJid(jidToEdit);
 656		}
 657
 658		if (mAccount != null) {
 659			this.mInitMode |= this.mAccount.isOptionSet(Account.OPTION_REGISTER);
 660			this.mUsernameMode |= mAccount.isOptionSet(Account.OPTION_MAGIC_CREATE) && mAccount.isOptionSet(Account.OPTION_REGISTER);
 661			if (this.mAccount.getPrivateKeyAlias() != null) {
 662				this.mPassword.setHint(R.string.authenticate_with_certificate);
 663				if (this.mInitMode) {
 664					this.mPassword.requestFocus();
 665				}
 666			}
 667			updateAccountInformation(init);
 668		}
 669
 670
 671		if (Config.MAGIC_CREATE_DOMAIN == null && this.xmppConnectionService.getAccounts().size() == 0) {
 672			this.mCancelButton.setEnabled(false);
 673			this.mCancelButton.setTextColor(getSecondaryTextColor());
 674		}
 675		if (mUsernameMode) {
 676			this.mAccountJidLabel.setText(R.string.username);
 677			this.mAccountJid.setHint(R.string.username_hint);
 678		} else {
 679			final KnownHostsAdapter mKnownHostsAdapter = new KnownHostsAdapter(this,
 680					R.layout.simple_list_item,
 681					xmppConnectionService.getKnownHosts());
 682			this.mAccountJid.setAdapter(mKnownHostsAdapter);
 683		}
 684		updateSaveButton();
 685		invalidateOptionsMenu();
 686	}
 687
 688	private String getUserModeDomain() {
 689		if (mAccount != null) {
 690			return mAccount.getJid().getDomainpart();
 691		} else {
 692			return Config.DOMAIN_LOCK;
 693		}
 694	}
 695
 696	@Override
 697	public boolean onOptionsItemSelected(final MenuItem item) {
 698		switch (item.getItemId()) {
 699			case R.id.action_show_block_list:
 700				final Intent showBlocklistIntent = new Intent(this, BlocklistActivity.class);
 701				showBlocklistIntent.putExtra(EXTRA_ACCOUNT, mAccount.getJid().toString());
 702				startActivity(showBlocklistIntent);
 703				break;
 704			case R.id.action_server_info_show_more:
 705				changeMoreTableVisibility(!item.isChecked());
 706				break;
 707			case R.id.action_share_barcode:
 708				shareBarcode();
 709				break;
 710			case R.id.action_share_http:
 711				shareLink(true);
 712				break;
 713			case R.id.action_share_uri:
 714				shareLink(false);
 715				break;
 716			case R.id.action_change_password_on_server:
 717				gotoChangePassword(null);
 718				break;
 719			case R.id.action_mam_prefs:
 720				editMamPrefs();
 721				break;
 722			case R.id.action_renew_certificate:
 723				renewCertificate();
 724				break;
 725			case R.id.action_change_presence:
 726				changePresence();
 727				break;
 728			case R.id.action_show_password:
 729				showPassword();
 730				break;
 731		}
 732		return super.onOptionsItemSelected(item);
 733	}
 734
 735	private void shareLink(boolean http) {
 736		Intent intent = new Intent(Intent.ACTION_SEND);
 737		intent.setType("text/plain");
 738		String text;
 739		if (http) {
 740			text = "https://conversations.im/i/"+mAccount.getJid().toBareJid().toString();
 741		} else {
 742			text = mAccount.getShareableUri();
 743		}
 744		intent.putExtra(Intent.EXTRA_TEXT,text);
 745		startActivity(Intent.createChooser(intent, getText(R.string.share_with)));
 746	}
 747
 748	private void shareBarcode() {
 749		Intent intent = new Intent(Intent.ACTION_SEND);
 750		intent.putExtra(Intent.EXTRA_STREAM,BarcodeProvider.getUriForAccount(mAccount));
 751		intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
 752		intent.setType("image/png");
 753		startActivity(Intent.createChooser(intent, getText(R.string.share_with)));
 754	}
 755
 756	private void changeMoreTableVisibility(boolean visible) {
 757		mMoreTable.setVisibility(visible ? View.VISIBLE : View.GONE);
 758	}
 759
 760	private void gotoChangePassword(String newPassword) {
 761		final Intent changePasswordIntent = new Intent(this, ChangePasswordActivity.class);
 762		changePasswordIntent.putExtra(EXTRA_ACCOUNT, mAccount.getJid().toString());
 763		if (newPassword != null) {
 764			changePasswordIntent.putExtra("password", newPassword);
 765		}
 766		startActivity(changePasswordIntent);
 767	}
 768
 769	private void renewCertificate() {
 770		KeyChain.choosePrivateKeyAlias(this, this, null, null, null, -1, null);
 771	}
 772
 773	private void changePresence() {
 774		Intent intent = new Intent(this, SetPresenceActivity.class);
 775		intent.putExtra(SetPresenceActivity.EXTRA_ACCOUNT,mAccount.getJid().toBareJid().toString());
 776		startActivity(intent);
 777	}
 778
 779	@Override
 780	public void alias(String alias) {
 781		if (alias != null) {
 782			xmppConnectionService.updateKeyInAccount(mAccount, alias);
 783		}
 784	}
 785
 786	private void updateAccountInformation(boolean init) {
 787		if (init) {
 788			this.mAccountJid.getEditableText().clear();
 789			if (mUsernameMode) {
 790				this.mAccountJid.getEditableText().append(this.mAccount.getJid().getLocalpart());
 791			} else {
 792				this.mAccountJid.getEditableText().append(this.mAccount.getJid().toBareJid().toString());
 793			}
 794			this.mPassword.setText(this.mAccount.getPassword());
 795			this.mHostname.setText("");
 796			this.mHostname.getEditableText().append(this.mAccount.getHostname());
 797			this.mPort.setText("");
 798			this.mPort.getEditableText().append(String.valueOf(this.mAccount.getPort()));
 799			this.mNamePort.setVisibility(mShowOptions ? View.VISIBLE : View.GONE);
 800
 801		}
 802
 803		if (!mInitMode) {
 804			this.mAvatar.setVisibility(View.VISIBLE);
 805			this.mAvatar.setImageBitmap(avatarService().get(this.mAccount, getPixel(72)));
 806		} else {
 807			this.mAvatar.setVisibility(View.GONE);
 808		}
 809		if (this.mAccount.isOptionSet(Account.OPTION_REGISTER)) {
 810			this.mRegisterNew.setVisibility(View.VISIBLE);
 811			this.mRegisterNew.setChecked(true);
 812			this.mPasswordConfirm.setText(this.mAccount.getPassword());
 813		} else {
 814			this.mRegisterNew.setVisibility(View.GONE);
 815			this.mRegisterNew.setChecked(false);
 816		}
 817		if (this.mAccount.isOnlineAndConnected() && !this.mFetchingAvatar) {
 818			Features features = this.mAccount.getXmppConnection().getFeatures();
 819			this.mStats.setVisibility(View.VISIBLE);
 820			boolean showBatteryWarning = !xmppConnectionService.getPushManagementService().available(mAccount) && isOptimizingBattery();
 821			boolean showDataSaverWarning = isAffectedByDataSaver();
 822			showOsOptimizationWarning(showBatteryWarning,showDataSaverWarning);
 823			this.mSessionEst.setText(UIHelper.readableTimeDifferenceFull(this, this.mAccount.getXmppConnection()
 824					.getLastSessionEstablished()));
 825			if (features.rosterVersioning()) {
 826				this.mServerInfoRosterVersion.setText(R.string.server_info_available);
 827			} else {
 828				this.mServerInfoRosterVersion.setText(R.string.server_info_unavailable);
 829			}
 830			if (features.carbons()) {
 831				this.mServerInfoCarbons.setText(R.string.server_info_available);
 832			} else {
 833				this.mServerInfoCarbons
 834						.setText(R.string.server_info_unavailable);
 835			}
 836			if (features.mam()) {
 837				this.mServerInfoMam.setText(R.string.server_info_available);
 838			} else {
 839				this.mServerInfoMam.setText(R.string.server_info_unavailable);
 840			}
 841			if (features.csi()) {
 842				this.mServerInfoCSI.setText(R.string.server_info_available);
 843			} else {
 844				this.mServerInfoCSI.setText(R.string.server_info_unavailable);
 845			}
 846			if (features.blocking()) {
 847				this.mServerInfoBlocking.setText(R.string.server_info_available);
 848			} else {
 849				this.mServerInfoBlocking.setText(R.string.server_info_unavailable);
 850			}
 851			if (features.sm()) {
 852				this.mServerInfoSm.setText(R.string.server_info_available);
 853			} else {
 854				this.mServerInfoSm.setText(R.string.server_info_unavailable);
 855			}
 856			if (features.pep()) {
 857				AxolotlService axolotlService = this.mAccount.getAxolotlService();
 858				if (axolotlService != null && axolotlService.isPepBroken()) {
 859					this.mServerInfoPep.setText(R.string.server_info_broken);
 860				} else {
 861					this.mServerInfoPep.setText(R.string.server_info_available);
 862				}
 863			} else {
 864				this.mServerInfoPep.setText(R.string.server_info_unavailable);
 865			}
 866			if (features.httpUpload(0)) {
 867				this.mServerInfoHttpUpload.setText(R.string.server_info_available);
 868			} else {
 869				this.mServerInfoHttpUpload.setText(R.string.server_info_unavailable);
 870			}
 871
 872			this.mPushRow.setVisibility(xmppConnectionService.getPushManagementService().isStub() ? View.GONE : View.VISIBLE);
 873
 874			if (xmppConnectionService.getPushManagementService().available(mAccount)) {
 875				this.mServerInfoPush.setText(R.string.server_info_available);
 876			} else {
 877				this.mServerInfoPush.setText(R.string.server_info_unavailable);
 878			}
 879			final String otrFingerprint = this.mAccount.getOtrFingerprint();
 880			if (otrFingerprint != null && Config.supportOtr()) {
 881				this.mOtrFingerprintBox.setVisibility(View.VISIBLE);
 882				this.mOtrFingerprint.setText(CryptoHelper.prettifyFingerprint(otrFingerprint));
 883				this.mOtrFingerprintToClipboardButton
 884						.setVisibility(View.VISIBLE);
 885				this.mOtrFingerprintToClipboardButton
 886						.setOnClickListener(new View.OnClickListener() {
 887
 888							@Override
 889							public void onClick(final View v) {
 890
 891								if (copyTextToClipboard(otrFingerprint, R.string.otr_fingerprint)) {
 892									Toast.makeText(
 893											EditAccountActivity.this,
 894											R.string.toast_message_otr_fingerprint,
 895											Toast.LENGTH_SHORT).show();
 896								}
 897							}
 898						});
 899			} else {
 900				this.mOtrFingerprintBox.setVisibility(View.GONE);
 901			}
 902			final String ownAxolotlFingerprint = this.mAccount.getAxolotlService().getOwnFingerprint();
 903			if (ownAxolotlFingerprint != null && Config.supportOmemo()) {
 904				this.mAxolotlFingerprintBox.setVisibility(View.VISIBLE);
 905				if (ownAxolotlFingerprint.equals(messageFingerprint)) {
 906					this.mOwnFingerprintDesc.setTextColor(getResources().getColor(R.color.accent));
 907				} else {
 908					this.mOwnFingerprintDesc.setTextColor(getSecondaryTextColor());
 909				}
 910				this.mAxolotlFingerprint.setText(CryptoHelper.prettifyFingerprint(ownAxolotlFingerprint.substring(2)));
 911				this.mAxolotlFingerprintToClipboardButton
 912						.setVisibility(View.VISIBLE);
 913				this.mAxolotlFingerprintToClipboardButton
 914						.setOnClickListener(new View.OnClickListener() {
 915
 916							@Override
 917							public void onClick(final View v) {
 918								copyOmemoFingerprint(ownAxolotlFingerprint);
 919							}
 920						});
 921				if (Config.SHOW_REGENERATE_AXOLOTL_KEYS_BUTTON) {
 922					this.mRegenerateAxolotlKeyButton
 923							.setVisibility(View.VISIBLE);
 924					this.mRegenerateAxolotlKeyButton
 925							.setOnClickListener(new View.OnClickListener() {
 926
 927								@Override
 928								public void onClick(final View v) {
 929									showRegenerateAxolotlKeyDialog();
 930								}
 931							});
 932				}
 933			} else {
 934				this.mAxolotlFingerprintBox.setVisibility(View.GONE);
 935			}
 936			boolean hasKeys = false;
 937			keys.removeAllViews();
 938			for(XmppAxolotlSession session : mAccount.getAxolotlService().findOwnSessions()) {
 939				if (!session.getTrust().isCompromised()) {
 940					boolean highlight = session.getFingerprint().equals(messageFingerprint);
 941					addFingerprintRow(keys,session,highlight);
 942					hasKeys = true;
 943				}
 944			}
 945			if (hasKeys && Config.supportOmemo()) {
 946				keysCard.setVisibility(View.VISIBLE);
 947				Set<Integer> otherDevices = mAccount.getAxolotlService().getOwnDeviceIds();
 948				if (otherDevices == null || otherDevices.isEmpty()) {
 949					mClearDevicesButton.setVisibility(View.GONE);
 950				} else {
 951					mClearDevicesButton.setVisibility(View.VISIBLE);
 952				}
 953			} else {
 954				keysCard.setVisibility(View.GONE);
 955			}
 956		} else {
 957			if (this.mAccount.errorStatus()) {
 958				final EditText errorTextField;
 959				if (this.mAccount.getStatus() == Account.State.UNAUTHORIZED) {
 960					errorTextField = this.mPassword;
 961				} else if (mShowOptions
 962						&& this.mAccount.getStatus() == Account.State.SERVER_NOT_FOUND
 963						&& this.mHostname.getText().length() > 0) {
 964					errorTextField = this.mHostname;
 965				} else {
 966					errorTextField = this.mAccountJid;
 967				}
 968				errorTextField.setError(getString(this.mAccount.getStatus().getReadableId()));
 969				if (init || !accountInfoEdited()) {
 970					errorTextField.requestFocus();
 971				}
 972			} else {
 973				this.mAccountJid.setError(null);
 974				this.mPassword.setError(null);
 975				this.mHostname.setError(null);
 976			}
 977			this.mStats.setVisibility(View.GONE);
 978		}
 979	}
 980
 981	private void showOsOptimizationWarning(boolean showBatteryWarning, boolean showDataSaverWarning) {
 982		this.mOsOptimizations.setVisibility(showBatteryWarning || showDataSaverWarning ? View.VISIBLE : View.GONE);
 983		if (showDataSaverWarning) {
 984			this.mDisableOsOptimizationsHeadline.setText(R.string.data_saver_enabled);
 985			this.getmDisableOsOptimizationsBody.setText(R.string.data_saver_enabled_explained);
 986			this.mDisableOsOptimizationsButton.setText(R.string.allow);
 987			this.mDisableOsOptimizationsButton.setOnClickListener(new OnClickListener() {
 988				@Override
 989				public void onClick(View v) {
 990					Intent intent = new Intent(Settings.ACTION_IGNORE_BACKGROUND_DATA_RESTRICTIONS_SETTINGS);
 991					Uri uri = Uri.parse("package:"+getPackageName());
 992					intent.setData(uri);
 993					try {
 994						startActivityForResult(intent, REQUEST_DATA_SAVER);
 995					} catch (ActivityNotFoundException e) {
 996						Toast.makeText(EditAccountActivity.this, R.string.device_does_not_support_data_saver, Toast.LENGTH_SHORT).show();
 997					}
 998				}
 999			});
1000		} else if (showBatteryWarning) {
1001			this.mDisableOsOptimizationsButton.setText(R.string.disable);
1002			this.mDisableOsOptimizationsHeadline.setText(R.string.battery_optimizations_enabled);
1003			this.getmDisableOsOptimizationsBody.setText(R.string.battery_optimizations_enabled_explained);
1004			this.mDisableOsOptimizationsButton.setOnClickListener(new OnClickListener() {
1005				@Override
1006				public void onClick(View v) {
1007					Intent intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
1008					Uri uri = Uri.parse("package:"+getPackageName());
1009					intent.setData(uri);
1010					try {
1011						startActivityForResult(intent, REQUEST_BATTERY_OP);
1012					} catch (ActivityNotFoundException e) {
1013						Toast.makeText(EditAccountActivity.this, R.string.device_does_not_support_battery_op, Toast.LENGTH_SHORT).show();
1014					}
1015				}
1016			});
1017		}
1018	}
1019
1020	public void showRegenerateAxolotlKeyDialog() {
1021		Builder builder = new Builder(this);
1022		builder.setTitle("Regenerate Key");
1023		builder.setIconAttribute(android.R.attr.alertDialogIcon);
1024		builder.setMessage("Are you sure you want to regenerate your Identity Key? (This will also wipe all established sessions and contact Identity Keys)");
1025		builder.setNegativeButton(getString(R.string.cancel), null);
1026		builder.setPositiveButton("Yes",
1027				new DialogInterface.OnClickListener() {
1028					@Override
1029					public void onClick(DialogInterface dialog, int which) {
1030						mAccount.getAxolotlService().regenerateKeys(false);
1031					}
1032				});
1033		builder.create().show();
1034	}
1035
1036	public void showWipePepDialog() {
1037		Builder builder = new Builder(this);
1038		builder.setTitle(getString(R.string.clear_other_devices));
1039		builder.setIconAttribute(android.R.attr.alertDialogIcon);
1040		builder.setMessage(getString(R.string.clear_other_devices_desc));
1041		builder.setNegativeButton(getString(R.string.cancel), null);
1042		builder.setPositiveButton(getString(R.string.accept),
1043				new DialogInterface.OnClickListener() {
1044					@Override
1045					public void onClick(DialogInterface dialog, int which) {
1046						mAccount.getAxolotlService().wipeOtherPepDevices();
1047					}
1048				});
1049		builder.create().show();
1050	}
1051
1052	private void editMamPrefs() {
1053		this.mFetchingMamPrefsToast = Toast.makeText(this, R.string.fetching_mam_prefs, Toast.LENGTH_LONG);
1054		this.mFetchingMamPrefsToast.show();
1055		xmppConnectionService.fetchMamPreferences(mAccount, this);
1056	}
1057
1058	private void showPassword() {
1059		AlertDialog.Builder builder = new AlertDialog.Builder(this);
1060		View view = getLayoutInflater().inflate(R.layout.dialog_show_password, null);
1061		TextView password = (TextView) view.findViewById(R.id.password);
1062		password.setText(mAccount.getPassword());
1063		builder.setTitle(R.string.password);
1064		builder.setView(view);
1065		builder.setPositiveButton(R.string.cancel, null);
1066		builder.create().show();
1067	}
1068
1069	@Override
1070	public void onKeyStatusUpdated(AxolotlService.FetchStatus report) {
1071		refreshUi();
1072	}
1073
1074	@Override
1075	public void onCaptchaRequested(final Account account, final String id, final Data data, final Bitmap captcha) {
1076		runOnUiThread(new Runnable() {
1077			@Override
1078			public void run() {
1079				if ((mCaptchaDialog != null) && mCaptchaDialog.isShowing()) {
1080					mCaptchaDialog.dismiss();
1081				}
1082				final AlertDialog.Builder builder = new AlertDialog.Builder(EditAccountActivity.this);
1083				final View view = getLayoutInflater().inflate(R.layout.captcha, null);
1084				final ImageView imageView = (ImageView) view.findViewById(R.id.captcha);
1085				final EditText input = (EditText) view.findViewById(R.id.input);
1086				imageView.setImageBitmap(captcha);
1087
1088				builder.setTitle(getString(R.string.captcha_required));
1089				builder.setView(view);
1090
1091				builder.setPositiveButton(getString(R.string.ok),
1092						new DialogInterface.OnClickListener() {
1093							@Override
1094							public void onClick(DialogInterface dialog, int which) {
1095								String rc = input.getText().toString();
1096								data.put("username", account.getUsername());
1097								data.put("password", account.getPassword());
1098								data.put("ocr", rc);
1099								data.submit();
1100
1101								if (xmppConnectionServiceBound) {
1102									xmppConnectionService.sendCreateAccountWithCaptchaPacket(
1103											account, id, data);
1104								}
1105							}
1106						});
1107				builder.setNegativeButton(getString(R.string.cancel), new DialogInterface.OnClickListener() {
1108					@Override
1109					public void onClick(DialogInterface dialog, int which) {
1110						if (xmppConnectionService != null) {
1111							xmppConnectionService.sendCreateAccountWithCaptchaPacket(account, null, null);
1112						}
1113					}
1114				});
1115
1116				builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
1117					@Override
1118					public void onCancel(DialogInterface dialog) {
1119						if (xmppConnectionService != null) {
1120							xmppConnectionService.sendCreateAccountWithCaptchaPacket(account, null, null);
1121						}
1122					}
1123				});
1124				mCaptchaDialog = builder.create();
1125				mCaptchaDialog.show();
1126			}
1127		});
1128	}
1129
1130	public void onShowErrorToast(final int resId) {
1131		runOnUiThread(new Runnable() {
1132			@Override
1133			public void run() {
1134				Toast.makeText(EditAccountActivity.this, resId, Toast.LENGTH_SHORT).show();
1135			}
1136		});
1137	}
1138
1139	@Override
1140	public void onPreferencesFetched(final Element prefs) {
1141		runOnUiThread(new Runnable() {
1142			@Override
1143			public void run() {
1144				if (mFetchingMamPrefsToast != null) {
1145					mFetchingMamPrefsToast.cancel();
1146				}
1147				AlertDialog.Builder builder = new Builder(EditAccountActivity.this);
1148				builder.setTitle(R.string.server_side_mam_prefs);
1149				String defaultAttr = prefs.getAttribute("default");
1150				final List<String> defaults = Arrays.asList("never", "roster", "always");
1151				final AtomicInteger choice = new AtomicInteger(Math.max(0,defaults.indexOf(defaultAttr)));
1152				builder.setSingleChoiceItems(R.array.mam_prefs, choice.get(), new DialogInterface.OnClickListener() {
1153					@Override
1154					public void onClick(DialogInterface dialog, int which) {
1155						choice.set(which);
1156					}
1157				});
1158				builder.setNegativeButton(R.string.cancel, null);
1159				builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1160					@Override
1161					public void onClick(DialogInterface dialog, int which) {
1162						prefs.setAttribute("default",defaults.get(choice.get()));
1163						xmppConnectionService.pushMamPreferences(mAccount, prefs);
1164					}
1165				});
1166				builder.create().show();
1167			}
1168		});
1169	}
1170
1171	@Override
1172	public void onPreferencesFetchFailed() {
1173		runOnUiThread(new Runnable() {
1174			@Override
1175			public void run() {
1176				if (mFetchingMamPrefsToast != null) {
1177					mFetchingMamPrefsToast.cancel();
1178				}
1179				Toast.makeText(EditAccountActivity.this,R.string.unable_to_fetch_mam_prefs,Toast.LENGTH_LONG).show();
1180			}
1181		});
1182	}
1183}