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