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