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