EditAccountActivity.java

  1package eu.siacs.conversations.ui;
  2
  3import android.app.AlertDialog;
  4import android.app.AlertDialog.Builder;
  5import android.app.PendingIntent;
  6import android.content.DialogInterface;
  7import android.content.Intent;
  8import android.content.SharedPreferences;
  9import android.graphics.Bitmap;
 10import android.net.Uri;
 11import android.os.Bundle;
 12import android.provider.Settings;
 13import android.security.KeyChain;
 14import android.security.KeyChainAliasCallback;
 15import android.text.Editable;
 16import android.text.TextWatcher;
 17import android.view.Menu;
 18import android.view.MenuItem;
 19import android.view.View;
 20import android.view.View.OnClickListener;
 21import android.widget.AutoCompleteTextView;
 22import android.widget.Button;
 23import android.widget.CheckBox;
 24import android.widget.CompoundButton;
 25import android.widget.CompoundButton.OnCheckedChangeListener;
 26import android.widget.EditText;
 27import android.widget.ImageButton;
 28import android.widget.ImageView;
 29import android.widget.LinearLayout;
 30import android.widget.RelativeLayout;
 31import android.widget.TableLayout;
 32import android.widget.TextView;
 33import android.widget.Toast;
 34
 35import java.util.Arrays;
 36import java.util.List;
 37import java.util.Set;
 38import java.util.concurrent.atomic.AtomicInteger;
 39
 40import eu.siacs.conversations.Config;
 41import eu.siacs.conversations.R;
 42import eu.siacs.conversations.crypto.axolotl.AxolotlService;
 43import eu.siacs.conversations.entities.Account;
 44import eu.siacs.conversations.services.XmppConnectionService.OnCaptchaRequested;
 45import eu.siacs.conversations.services.XmppConnectionService;
 46import eu.siacs.conversations.services.XmppConnectionService.OnAccountUpdate;
 47import eu.siacs.conversations.ui.adapter.KnownHostsAdapter;
 48import eu.siacs.conversations.utils.CryptoHelper;
 49import eu.siacs.conversations.utils.UIHelper;
 50import eu.siacs.conversations.xml.Element;
 51import eu.siacs.conversations.xmpp.OnKeyStatusUpdated;
 52import eu.siacs.conversations.xmpp.XmppConnection.Features;
 53import eu.siacs.conversations.xmpp.forms.Data;
 54import eu.siacs.conversations.xmpp.jid.InvalidJidException;
 55import eu.siacs.conversations.xmpp.jid.Jid;
 56import eu.siacs.conversations.xmpp.pep.Avatar;
 57
 58public class EditAccountActivity extends XmppActivity implements OnAccountUpdate,
 59		OnKeyStatusUpdated, OnCaptchaRequested, KeyChainAliasCallback, XmppConnectionService.OnShowErrorToast, XmppConnectionService.OnMamPreferencesFetched {
 60
 61	private AutoCompleteTextView mAccountJid;
 62	private EditText mPassword;
 63	private EditText mPasswordConfirm;
 64	private CheckBox mRegisterNew;
 65	private Button mCancelButton;
 66	private Button mSaveButton;
 67	private Button mDisableBatterOptimizations;
 68	private TableLayout mMoreTable;
 69
 70	private LinearLayout mStats;
 71	private RelativeLayout mBatteryOptimizations;
 72	private TextView mServerInfoSm;
 73	private TextView mServerInfoRosterVersion;
 74	private TextView mServerInfoCarbons;
 75	private TextView mServerInfoMam;
 76	private TextView mServerInfoCSI;
 77	private TextView mServerInfoBlocking;
 78	private TextView mServerInfoPep;
 79	private TextView mServerInfoHttpUpload;
 80	private TextView mSessionEst;
 81	private TextView mOtrFingerprint;
 82	private TextView mAxolotlFingerprint;
 83	private TextView mAccountJidLabel;
 84	private ImageView mAvatar;
 85	private RelativeLayout mOtrFingerprintBox;
 86	private RelativeLayout mAxolotlFingerprintBox;
 87	private ImageButton mOtrFingerprintToClipboardButton;
 88	private ImageButton mAxolotlFingerprintToClipboardButton;
 89	private ImageButton mRegenerateAxolotlKeyButton;
 90	private LinearLayout keys;
 91	private LinearLayout keysCard;
 92	private LinearLayout mNamePort;
 93	private EditText mHostname;
 94	private EditText mPort;
 95	private AlertDialog mCaptchaDialog = null;
 96
 97	private Jid jidToEdit;
 98	private boolean mInitMode = false;
 99	private boolean mShowOptions = false;
100	private Account mAccount;
101	private String messageFingerprint;
102
103	private boolean mFetchingAvatar = false;
104
105	private final OnClickListener mSaveButtonClickListener = new OnClickListener() {
106
107		@Override
108		public void onClick(final View v) {
109			if (mInitMode && mAccount != null) {
110				mAccount.setOption(Account.OPTION_DISABLED, false);
111			}
112			if (mAccount != null && mAccount.getStatus() == Account.State.DISABLED && !accountInfoEdited()) {
113				mAccount.setOption(Account.OPTION_DISABLED, false);
114				xmppConnectionService.updateAccount(mAccount);
115				return;
116			}
117			final boolean registerNewAccount = mRegisterNew.isChecked() && !Config.DISALLOW_REGISTRATION_IN_UI;
118			if (Config.DOMAIN_LOCK != null && mAccountJid.getText().toString().contains("@")) {
119				mAccountJid.setError(getString(R.string.invalid_username));
120				mAccountJid.requestFocus();
121				return;
122			}
123			final Jid jid;
124			try {
125				if (Config.DOMAIN_LOCK != null) {
126					jid = Jid.fromParts(mAccountJid.getText().toString(), Config.DOMAIN_LOCK, null);
127				} else {
128					jid = Jid.fromString(mAccountJid.getText().toString());
129				}
130			} catch (final InvalidJidException e) {
131				if (Config.DOMAIN_LOCK != null) {
132					mAccountJid.setError(getString(R.string.invalid_username));
133				} else {
134					mAccountJid.setError(getString(R.string.invalid_jid));
135				}
136				mAccountJid.requestFocus();
137				return;
138			}
139			String hostname = null;
140			int numericPort = 5222;
141			if (mShowOptions) {
142				hostname = mHostname.getText().toString();
143				final String port = mPort.getText().toString();
144				if (hostname.contains(" ")) {
145					mHostname.setError(getString(R.string.not_valid_hostname));
146					mHostname.requestFocus();
147					return;
148				}
149				try {
150					numericPort = Integer.parseInt(port);
151					if (numericPort < 0 || numericPort > 65535) {
152						mPort.setError(getString(R.string.not_a_valid_port));
153						mPort.requestFocus();
154						return;
155					}
156
157				} catch (NumberFormatException e) {
158					mPort.setError(getString(R.string.not_a_valid_port));
159					mPort.requestFocus();
160					return;
161				}
162			}
163
164			if (jid.isDomainJid()) {
165				if (Config.DOMAIN_LOCK != null) {
166					mAccountJid.setError(getString(R.string.invalid_username));
167				} else {
168					mAccountJid.setError(getString(R.string.invalid_jid));
169				}
170				mAccountJid.requestFocus();
171				return;
172			}
173			final String password = mPassword.getText().toString();
174			final String passwordConfirm = mPasswordConfirm.getText().toString();
175			if (registerNewAccount) {
176				if (!password.equals(passwordConfirm)) {
177					mPasswordConfirm.setError(getString(R.string.passwords_do_not_match));
178					mPasswordConfirm.requestFocus();
179					return;
180				}
181			}
182			if (mAccount != null) {
183				mAccount.setJid(jid);
184				mAccount.setPort(numericPort);
185				mAccount.setHostname(hostname);
186				mAccountJid.setError(null);
187				mPasswordConfirm.setError(null);
188				mAccount.setPassword(password);
189				mAccount.setOption(Account.OPTION_REGISTER, registerNewAccount);
190				xmppConnectionService.updateAccount(mAccount);
191			} else {
192				if (xmppConnectionService.findAccountByJid(jid) != null) {
193					mAccountJid.setError(getString(R.string.account_already_exists));
194					mAccountJid.requestFocus();
195					return;
196				}
197				mAccount = new Account(jid.toBareJid(), password);
198				mAccount.setPort(numericPort);
199				mAccount.setHostname(hostname);
200				mAccount.setOption(Account.OPTION_USETLS, true);
201				mAccount.setOption(Account.OPTION_USECOMPRESSION, true);
202				mAccount.setOption(Account.OPTION_REGISTER, registerNewAccount);
203				xmppConnectionService.createAccount(mAccount);
204			}
205			mHostname.setError(null);
206			mPort.setError(null);
207			if (!mAccount.isOptionSet(Account.OPTION_DISABLED)
208					&& !registerNewAccount
209					&& !mInitMode) {
210				finish();
211			} else {
212				updateSaveButton();
213				updateAccountInformation(true);
214			}
215
216		}
217	};
218	private final OnClickListener mCancelButtonClickListener = new OnClickListener() {
219
220		@Override
221		public void onClick(final View v) {
222			finish();
223		}
224	};
225	private Toast mFetchingMamPrefsToast;
226
227	public void refreshUiReal() {
228		invalidateOptionsMenu();
229		if (mAccount != null
230				&& mAccount.getStatus() != Account.State.ONLINE
231				&& mFetchingAvatar) {
232			startActivity(new Intent(getApplicationContext(),
233					ManageAccountActivity.class));
234			finish();
235		} else if (mInitMode && mAccount != null && mAccount.getStatus() == Account.State.ONLINE) {
236			if (!mFetchingAvatar) {
237				mFetchingAvatar = true;
238				xmppConnectionService.checkForAvatar(mAccount, mAvatarFetchCallback);
239			}
240		} else {
241			updateSaveButton();
242		}
243		if (mAccount != null) {
244			updateAccountInformation(false);
245		}
246	}
247
248	@Override
249	public void onAccountUpdate() {
250		refreshUi();
251	}
252
253	private final UiCallback<Avatar> mAvatarFetchCallback = new UiCallback<Avatar>() {
254
255		@Override
256		public void userInputRequried(final PendingIntent pi, final Avatar avatar) {
257			finishInitialSetup(avatar);
258		}
259
260		@Override
261		public void success(final Avatar avatar) {
262			finishInitialSetup(avatar);
263		}
264
265		@Override
266		public void error(final int errorCode, final Avatar avatar) {
267			finishInitialSetup(avatar);
268		}
269	};
270	private final TextWatcher mTextWatcher = new TextWatcher() {
271
272		@Override
273		public void onTextChanged(final CharSequence s, final int start, final int before, final int count) {
274			updateSaveButton();
275		}
276
277		@Override
278		public void beforeTextChanged(final CharSequence s, final int start, final int count, final int after) {
279		}
280
281		@Override
282		public void afterTextChanged(final Editable s) {
283
284		}
285	};
286
287	private final OnClickListener mAvatarClickListener = new OnClickListener() {
288		@Override
289		public void onClick(final View view) {
290			if (mAccount != null) {
291				final Intent intent = new Intent(getApplicationContext(), PublishProfilePictureActivity.class);
292				intent.putExtra(EXTRA_ACCOUNT, mAccount.getJid().toBareJid().toString());
293				startActivity(intent);
294			}
295		}
296	};
297
298	protected void finishInitialSetup(final Avatar avatar) {
299		runOnUiThread(new Runnable() {
300
301			@Override
302			public void run() {
303				final Intent intent;
304				if (avatar != null) {
305					intent = new Intent(getApplicationContext(),
306							StartConversationActivity.class);
307					if (xmppConnectionService != null && xmppConnectionService.getAccounts().size() == 1) {
308						intent.putExtra("init", true);
309					}
310				} else {
311					intent = new Intent(getApplicationContext(),
312							PublishProfilePictureActivity.class);
313					intent.putExtra(EXTRA_ACCOUNT, mAccount.getJid().toBareJid().toString());
314					intent.putExtra("setup", true);
315				}
316				startActivity(intent);
317				finish();
318			}
319		});
320	}
321
322	@Override
323	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
324		super.onActivityResult(requestCode, resultCode, data);
325		if (requestCode == REQUEST_BATTERY_OP) {
326			updateAccountInformation(mAccount == null);
327		}
328	}
329
330	protected void updateSaveButton() {
331		if (accountInfoEdited() && !mInitMode) {
332			this.mSaveButton.setText(R.string.save);
333			this.mSaveButton.setEnabled(true);
334			this.mSaveButton.setTextColor(getPrimaryTextColor());
335		} else if (mAccount != null && (mAccount.getStatus() == Account.State.CONNECTING || mFetchingAvatar)) {
336			this.mSaveButton.setEnabled(false);
337			this.mSaveButton.setTextColor(getSecondaryTextColor());
338			this.mSaveButton.setText(R.string.account_status_connecting);
339		} else if (mAccount != null && mAccount.getStatus() == Account.State.DISABLED && !mInitMode) {
340			this.mSaveButton.setEnabled(true);
341			this.mSaveButton.setTextColor(getPrimaryTextColor());
342			this.mSaveButton.setText(R.string.enable);
343		} else {
344			this.mSaveButton.setEnabled(true);
345			this.mSaveButton.setTextColor(getPrimaryTextColor());
346			if (!mInitMode) {
347				if (mAccount != null && mAccount.isOnlineAndConnected()) {
348					this.mSaveButton.setText(R.string.save);
349					if (!accountInfoEdited()) {
350						this.mSaveButton.setEnabled(false);
351						this.mSaveButton.setTextColor(getSecondaryTextColor());
352					}
353				} else {
354					this.mSaveButton.setText(R.string.connect);
355				}
356			} else {
357				this.mSaveButton.setText(R.string.next);
358			}
359		}
360	}
361
362	protected boolean accountInfoEdited() {
363		if (this.mAccount == null) {
364			return false;
365		}
366		final String unmodified;
367		if (Config.DOMAIN_LOCK != null) {
368			unmodified = this.mAccount.getJid().getLocalpart();
369		} else {
370			unmodified = this.mAccount.getJid().toBareJid().toString();
371		}
372		return !unmodified.equals(this.mAccountJid.getText().toString()) ||
373				!this.mAccount.getPassword().equals(this.mPassword.getText().toString()) ||
374				!this.mAccount.getHostname().equals(this.mHostname.getText().toString()) ||
375				!String.valueOf(this.mAccount.getPort()).equals(this.mPort.getText().toString());
376	}
377
378	@Override
379	protected String getShareableUri() {
380		if (mAccount != null) {
381			return mAccount.getShareableUri();
382		} else {
383			return "";
384		}
385	}
386
387	@Override
388	protected void onCreate(final Bundle savedInstanceState) {
389		super.onCreate(savedInstanceState);
390		setContentView(R.layout.activity_edit_account);
391		this.mAccountJid = (AutoCompleteTextView) findViewById(R.id.account_jid);
392		this.mAccountJid.addTextChangedListener(this.mTextWatcher);
393		this.mAccountJidLabel = (TextView) findViewById(R.id.account_jid_label);
394		if (Config.DOMAIN_LOCK != null) {
395			this.mAccountJidLabel.setText(R.string.username);
396			this.mAccountJid.setHint(R.string.username_hint);
397		}
398		this.mPassword = (EditText) findViewById(R.id.account_password);
399		this.mPassword.addTextChangedListener(this.mTextWatcher);
400		this.mPasswordConfirm = (EditText) findViewById(R.id.account_password_confirm);
401		this.mAvatar = (ImageView) findViewById(R.id.avater);
402		this.mAvatar.setOnClickListener(this.mAvatarClickListener);
403		this.mRegisterNew = (CheckBox) findViewById(R.id.account_register_new);
404		this.mStats = (LinearLayout) findViewById(R.id.stats);
405		this.mBatteryOptimizations = (RelativeLayout) findViewById(R.id.battery_optimization);
406		this.mDisableBatterOptimizations = (Button) findViewById(R.id.batt_op_disable);
407		this.mDisableBatterOptimizations.setOnClickListener(new OnClickListener() {
408			@Override
409			public void onClick(View v) {
410				Intent intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
411				Uri uri = Uri.parse("package:"+getPackageName());
412				intent.setData(uri);
413				startActivityForResult(intent,REQUEST_BATTERY_OP);
414			}
415		});
416		this.mSessionEst = (TextView) findViewById(R.id.session_est);
417		this.mServerInfoRosterVersion = (TextView) findViewById(R.id.server_info_roster_version);
418		this.mServerInfoCarbons = (TextView) findViewById(R.id.server_info_carbons);
419		this.mServerInfoMam = (TextView) findViewById(R.id.server_info_mam);
420		this.mServerInfoCSI = (TextView) findViewById(R.id.server_info_csi);
421		this.mServerInfoBlocking = (TextView) findViewById(R.id.server_info_blocking);
422		this.mServerInfoSm = (TextView) findViewById(R.id.server_info_sm);
423		this.mServerInfoPep = (TextView) findViewById(R.id.server_info_pep);
424		this.mServerInfoHttpUpload = (TextView) findViewById(R.id.server_info_http_upload);
425		this.mOtrFingerprint = (TextView) findViewById(R.id.otr_fingerprint);
426		this.mOtrFingerprintBox = (RelativeLayout) findViewById(R.id.otr_fingerprint_box);
427		this.mOtrFingerprintToClipboardButton = (ImageButton) findViewById(R.id.action_copy_to_clipboard);
428		this.mAxolotlFingerprint = (TextView) findViewById(R.id.axolotl_fingerprint);
429		this.mAxolotlFingerprintBox = (RelativeLayout) findViewById(R.id.axolotl_fingerprint_box);
430		this.mAxolotlFingerprintToClipboardButton = (ImageButton) findViewById(R.id.action_copy_axolotl_to_clipboard);
431		this.mRegenerateAxolotlKeyButton = (ImageButton) findViewById(R.id.action_regenerate_axolotl_key);
432		this.keysCard = (LinearLayout) findViewById(R.id.other_device_keys_card);
433		this.keys = (LinearLayout) findViewById(R.id.other_device_keys);
434		this.mNamePort = (LinearLayout) findViewById(R.id.name_port);
435		this.mHostname = (EditText) findViewById(R.id.hostname);
436		this.mHostname.addTextChangedListener(mTextWatcher);
437		this.mPort = (EditText) findViewById(R.id.port);
438		this.mPort.setText("5222");
439		this.mPort.addTextChangedListener(mTextWatcher);
440		this.mSaveButton = (Button) findViewById(R.id.save_button);
441		this.mCancelButton = (Button) findViewById(R.id.cancel_button);
442		this.mSaveButton.setOnClickListener(this.mSaveButtonClickListener);
443		this.mCancelButton.setOnClickListener(this.mCancelButtonClickListener);
444		this.mMoreTable = (TableLayout) findViewById(R.id.server_info_more);
445		final OnCheckedChangeListener OnCheckedShowConfirmPassword = new OnCheckedChangeListener() {
446			@Override
447			public void onCheckedChanged(final CompoundButton buttonView,
448										 final boolean isChecked) {
449				if (isChecked) {
450					mPasswordConfirm.setVisibility(View.VISIBLE);
451				} else {
452					mPasswordConfirm.setVisibility(View.GONE);
453				}
454				updateSaveButton();
455			}
456		};
457		this.mRegisterNew.setOnCheckedChangeListener(OnCheckedShowConfirmPassword);
458		if (Config.DISALLOW_REGISTRATION_IN_UI) {
459			this.mRegisterNew.setVisibility(View.GONE);
460		}
461	}
462
463	@Override
464	public boolean onCreateOptionsMenu(final Menu menu) {
465		super.onCreateOptionsMenu(menu);
466		getMenuInflater().inflate(R.menu.editaccount, menu);
467		final MenuItem showQrCode = menu.findItem(R.id.action_show_qr_code);
468		final MenuItem showBlocklist = menu.findItem(R.id.action_show_block_list);
469		final MenuItem showMoreInfo = menu.findItem(R.id.action_server_info_show_more);
470		final MenuItem changePassword = menu.findItem(R.id.action_change_password_on_server);
471		final MenuItem clearDevices = menu.findItem(R.id.action_clear_devices);
472		final MenuItem renewCertificate = menu.findItem(R.id.action_renew_certificate);
473		final MenuItem mamPrefs = menu.findItem(R.id.action_mam_prefs);
474
475		renewCertificate.setVisible(mAccount != null && mAccount.getPrivateKeyAlias() != null);
476
477		if (mAccount != null && mAccount.isOnlineAndConnected()) {
478			if (!mAccount.getXmppConnection().getFeatures().blocking()) {
479				showBlocklist.setVisible(false);
480			}
481			if (!mAccount.getXmppConnection().getFeatures().register()) {
482				changePassword.setVisible(false);
483			}
484			mamPrefs.setVisible(mAccount.getXmppConnection().getFeatures().mam());
485			Set<Integer> otherDevices = mAccount.getAxolotlService().getOwnDeviceIds();
486			if (otherDevices == null || otherDevices.isEmpty()) {
487				clearDevices.setVisible(false);
488			}
489		} else {
490			showQrCode.setVisible(false);
491			showBlocklist.setVisible(false);
492			showMoreInfo.setVisible(false);
493			changePassword.setVisible(false);
494			clearDevices.setVisible(false);
495			mamPrefs.setVisible(false);
496		}
497		return true;
498	}
499
500	@Override
501	protected void onStart() {
502		super.onStart();
503		if (getIntent() != null) {
504			try {
505				this.jidToEdit = Jid.fromString(getIntent().getStringExtra("jid"));
506			} catch (final InvalidJidException | NullPointerException ignored) {
507				this.jidToEdit = null;
508			}
509			this.mInitMode = getIntent().getBooleanExtra("init", false) || this.jidToEdit == null;
510			this.messageFingerprint = getIntent().getStringExtra("fingerprint");
511			if (!mInitMode) {
512				this.mRegisterNew.setVisibility(View.GONE);
513				if (getActionBar() != null) {
514					getActionBar().setTitle(getString(R.string.account_details));
515				}
516			} else {
517				this.mAvatar.setVisibility(View.GONE);
518				if (getActionBar() != null) {
519					getActionBar().setTitle(R.string.action_add_account);
520				}
521			}
522		}
523		SharedPreferences preferences = getPreferences();
524		boolean useTor = Config.FORCE_ORBOT || preferences.getBoolean("use_tor", false);
525		this.mShowOptions = useTor || preferences.getBoolean("show_connection_options", false);
526		mHostname.setHint(useTor ? R.string.hostname_or_onion : R.string.hostname_example);
527		this.mNamePort.setVisibility(mShowOptions ? View.VISIBLE : View.GONE);
528	}
529
530	@Override
531	protected void onBackendConnected() {
532		if (this.jidToEdit != null) {
533			this.mAccount = xmppConnectionService.findAccountByJid(jidToEdit);
534			if (this.mAccount != null) {
535				if (this.mAccount.getPrivateKeyAlias() != null) {
536					this.mPassword.setHint(R.string.authenticate_with_certificate);
537					if (this.mInitMode) {
538						this.mPassword.requestFocus();
539					}
540				}
541				updateAccountInformation(true);
542			}
543		} else if (this.xmppConnectionService.getAccounts().size() == 0) {
544			if (getActionBar() != null) {
545				getActionBar().setDisplayHomeAsUpEnabled(false);
546				getActionBar().setDisplayShowHomeEnabled(false);
547				getActionBar().setHomeButtonEnabled(false);
548			}
549			this.mCancelButton.setEnabled(false);
550			this.mCancelButton.setTextColor(getSecondaryTextColor());
551		}
552		if (Config.DOMAIN_LOCK == null) {
553			final KnownHostsAdapter mKnownHostsAdapter = new KnownHostsAdapter(this,
554					android.R.layout.simple_list_item_1,
555					xmppConnectionService.getKnownHosts());
556			this.mAccountJid.setAdapter(mKnownHostsAdapter);
557		}
558		updateSaveButton();
559		invalidateOptionsMenu();
560	}
561
562	@Override
563	public boolean onOptionsItemSelected(final MenuItem item) {
564		switch (item.getItemId()) {
565			case R.id.action_show_block_list:
566				final Intent showBlocklistIntent = new Intent(this, BlocklistActivity.class);
567				showBlocklistIntent.putExtra(EXTRA_ACCOUNT, mAccount.getJid().toString());
568				startActivity(showBlocklistIntent);
569				break;
570			case R.id.action_server_info_show_more:
571				mMoreTable.setVisibility(item.isChecked() ? View.GONE : View.VISIBLE);
572				item.setChecked(!item.isChecked());
573				break;
574			case R.id.action_change_password_on_server:
575				final Intent changePasswordIntent = new Intent(this, ChangePasswordActivity.class);
576				changePasswordIntent.putExtra(EXTRA_ACCOUNT, mAccount.getJid().toString());
577				startActivity(changePasswordIntent);
578				break;
579			case R.id.action_mam_prefs:
580				editMamPrefs();
581				break;
582			case R.id.action_clear_devices:
583				showWipePepDialog();
584				break;
585			case R.id.action_renew_certificate:
586				renewCertificate();
587				break;
588		}
589		return super.onOptionsItemSelected(item);
590	}
591
592	private void renewCertificate() {
593		KeyChain.choosePrivateKeyAlias(this, this, null, null, null, -1, null);
594	}
595
596	@Override
597	public void alias(String alias) {
598		if (alias != null) {
599			xmppConnectionService.updateKeyInAccount(mAccount, alias);
600		}
601	}
602
603	private void updateAccountInformation(boolean init) {
604		if (init) {
605			this.mAccountJid.getEditableText().clear();
606			if (Config.DOMAIN_LOCK != null) {
607				this.mAccountJid.getEditableText().append(this.mAccount.getJid().getLocalpart());
608			} else {
609				this.mAccountJid.getEditableText().append(this.mAccount.getJid().toBareJid().toString());
610			}
611			this.mPassword.setText(this.mAccount.getPassword());
612			this.mHostname.setText("");
613			this.mHostname.getEditableText().append(this.mAccount.getHostname());
614			this.mPort.setText("");
615			this.mPort.getEditableText().append(String.valueOf(this.mAccount.getPort()));
616			this.mNamePort.setVisibility(mShowOptions ? View.VISIBLE : View.GONE);
617
618		}
619		if (!mInitMode) {
620			this.mAvatar.setVisibility(View.VISIBLE);
621			this.mAvatar.setImageBitmap(avatarService().get(this.mAccount, getPixel(72)));
622		}
623		if (this.mAccount.isOptionSet(Account.OPTION_REGISTER)) {
624			this.mRegisterNew.setVisibility(View.VISIBLE);
625			this.mRegisterNew.setChecked(true);
626			this.mPasswordConfirm.setText(this.mAccount.getPassword());
627		} else {
628			this.mRegisterNew.setVisibility(View.GONE);
629			this.mRegisterNew.setChecked(false);
630		}
631		if (this.mAccount.isOnlineAndConnected() && !this.mFetchingAvatar) {
632			this.mStats.setVisibility(View.VISIBLE);
633			this.mBatteryOptimizations.setVisibility(showBatteryOptimizationWarning() ? View.VISIBLE : View.GONE);
634			this.mSessionEst.setText(UIHelper.readableTimeDifferenceFull(this, this.mAccount.getXmppConnection()
635					.getLastSessionEstablished()));
636			Features features = this.mAccount.getXmppConnection().getFeatures();
637			if (features.rosterVersioning()) {
638				this.mServerInfoRosterVersion.setText(R.string.server_info_available);
639			} else {
640				this.mServerInfoRosterVersion.setText(R.string.server_info_unavailable);
641			}
642			if (features.carbons()) {
643				this.mServerInfoCarbons.setText(R.string.server_info_available);
644			} else {
645				this.mServerInfoCarbons
646						.setText(R.string.server_info_unavailable);
647			}
648			if (features.mam()) {
649				this.mServerInfoMam.setText(R.string.server_info_available);
650			} else {
651				this.mServerInfoMam.setText(R.string.server_info_unavailable);
652			}
653			if (features.csi()) {
654				this.mServerInfoCSI.setText(R.string.server_info_available);
655			} else {
656				this.mServerInfoCSI.setText(R.string.server_info_unavailable);
657			}
658			if (features.blocking()) {
659				this.mServerInfoBlocking.setText(R.string.server_info_available);
660			} else {
661				this.mServerInfoBlocking.setText(R.string.server_info_unavailable);
662			}
663			if (features.sm()) {
664				this.mServerInfoSm.setText(R.string.server_info_available);
665			} else {
666				this.mServerInfoSm.setText(R.string.server_info_unavailable);
667			}
668			if (features.pep()) {
669				AxolotlService axolotlService = this.mAccount.getAxolotlService();
670				if (axolotlService != null && axolotlService.isPepBroken()) {
671					this.mServerInfoPep.setText(R.string.server_info_broken);
672				} else {
673					this.mServerInfoPep.setText(R.string.server_info_available);
674				}
675			} else {
676				this.mServerInfoPep.setText(R.string.server_info_unavailable);
677			}
678			if (features.httpUpload()) {
679				this.mServerInfoHttpUpload.setText(R.string.server_info_available);
680			} else {
681				this.mServerInfoHttpUpload.setText(R.string.server_info_unavailable);
682			}
683			final String otrFingerprint = this.mAccount.getOtrFingerprint();
684			if (otrFingerprint != null) {
685				this.mOtrFingerprintBox.setVisibility(View.VISIBLE);
686				this.mOtrFingerprint.setText(CryptoHelper.prettifyFingerprint(otrFingerprint));
687				this.mOtrFingerprintToClipboardButton
688						.setVisibility(View.VISIBLE);
689				this.mOtrFingerprintToClipboardButton
690						.setOnClickListener(new View.OnClickListener() {
691
692							@Override
693							public void onClick(final View v) {
694
695								if (copyTextToClipboard(otrFingerprint, R.string.otr_fingerprint)) {
696									Toast.makeText(
697											EditAccountActivity.this,
698											R.string.toast_message_otr_fingerprint,
699											Toast.LENGTH_SHORT).show();
700								}
701							}
702						});
703			} else {
704				this.mOtrFingerprintBox.setVisibility(View.GONE);
705			}
706			final String axolotlFingerprint = this.mAccount.getAxolotlService().getOwnFingerprint();
707			if (axolotlFingerprint != null) {
708				this.mAxolotlFingerprintBox.setVisibility(View.VISIBLE);
709				this.mAxolotlFingerprint.setText(CryptoHelper.prettifyFingerprint(axolotlFingerprint.substring(2)));
710				this.mAxolotlFingerprintToClipboardButton
711						.setVisibility(View.VISIBLE);
712				this.mAxolotlFingerprintToClipboardButton
713						.setOnClickListener(new View.OnClickListener() {
714
715							@Override
716							public void onClick(final View v) {
717
718								if (copyTextToClipboard(axolotlFingerprint.substring(2), R.string.omemo_fingerprint)) {
719									Toast.makeText(
720											EditAccountActivity.this,
721											R.string.toast_message_omemo_fingerprint,
722											Toast.LENGTH_SHORT).show();
723								}
724							}
725						});
726				if (Config.SHOW_REGENERATE_AXOLOTL_KEYS_BUTTON) {
727					this.mRegenerateAxolotlKeyButton
728							.setVisibility(View.VISIBLE);
729					this.mRegenerateAxolotlKeyButton
730							.setOnClickListener(new View.OnClickListener() {
731
732								@Override
733								public void onClick(final View v) {
734									showRegenerateAxolotlKeyDialog();
735								}
736							});
737				}
738			} else {
739				this.mAxolotlFingerprintBox.setVisibility(View.GONE);
740			}
741			final String ownFingerprint = mAccount.getAxolotlService().getOwnFingerprint();
742			boolean hasKeys = false;
743			keys.removeAllViews();
744			for (final String fingerprint : mAccount.getAxolotlService().getFingerprintsForOwnSessions()) {
745				if (ownFingerprint.equals(fingerprint)) {
746					continue;
747				}
748				boolean highlight = fingerprint.equals(messageFingerprint);
749				hasKeys |= addFingerprintRow(keys, mAccount, fingerprint, highlight, null);
750			}
751			if (hasKeys) {
752				keysCard.setVisibility(View.VISIBLE);
753			} else {
754				keysCard.setVisibility(View.GONE);
755			}
756		} else {
757			if (this.mAccount.errorStatus()) {
758				final EditText errorTextField;
759				if (this.mAccount.getStatus() == Account.State.UNAUTHORIZED) {
760					errorTextField = this.mPassword;
761				} else if (mShowOptions
762						&& this.mAccount.getStatus() == Account.State.SERVER_NOT_FOUND
763						&& this.mHostname.getText().length() > 0) {
764					errorTextField = this.mHostname;
765				} else {
766					errorTextField = this.mAccountJid;
767				}
768				errorTextField.setError(getString(this.mAccount.getStatus().getReadableId()));
769				if (init || !accountInfoEdited()) {
770					errorTextField.requestFocus();
771				}
772			} else {
773				this.mAccountJid.setError(null);
774				this.mPassword.setError(null);
775				this.mHostname.setError(null);
776			}
777			this.mStats.setVisibility(View.GONE);
778		}
779	}
780
781	public void showRegenerateAxolotlKeyDialog() {
782		Builder builder = new Builder(this);
783		builder.setTitle("Regenerate Key");
784		builder.setIconAttribute(android.R.attr.alertDialogIcon);
785		builder.setMessage("Are you sure you want to regenerate your Identity Key? (This will also wipe all established sessions and contact Identity Keys)");
786		builder.setNegativeButton(getString(R.string.cancel), null);
787		builder.setPositiveButton("Yes",
788				new DialogInterface.OnClickListener() {
789					@Override
790					public void onClick(DialogInterface dialog, int which) {
791						mAccount.getAxolotlService().regenerateKeys(false);
792					}
793				});
794		builder.create().show();
795	}
796
797	public void showWipePepDialog() {
798		Builder builder = new Builder(this);
799		builder.setTitle(getString(R.string.clear_other_devices));
800		builder.setIconAttribute(android.R.attr.alertDialogIcon);
801		builder.setMessage(getString(R.string.clear_other_devices_desc));
802		builder.setNegativeButton(getString(R.string.cancel), null);
803		builder.setPositiveButton(getString(R.string.accept),
804				new DialogInterface.OnClickListener() {
805					@Override
806					public void onClick(DialogInterface dialog, int which) {
807						mAccount.getAxolotlService().wipeOtherPepDevices();
808					}
809				});
810		builder.create().show();
811	}
812
813	private void editMamPrefs() {
814		this.mFetchingMamPrefsToast = Toast.makeText(this, R.string.fetching_mam_prefs, Toast.LENGTH_LONG);
815		this.mFetchingMamPrefsToast.show();
816		xmppConnectionService.fetchMamPreferences(mAccount, this);
817	}
818
819	@Override
820	public void onKeyStatusUpdated(AxolotlService.FetchStatus report) {
821		refreshUi();
822	}
823
824	@Override
825	public void onCaptchaRequested(final Account account, final String id, final Data data,
826								   final Bitmap captcha) {
827		final AlertDialog.Builder builder = new AlertDialog.Builder(this);
828		final ImageView view = new ImageView(this);
829		final LinearLayout layout = new LinearLayout(this);
830		final EditText input = new EditText(this);
831
832		view.setImageBitmap(captcha);
833		view.setScaleType(ImageView.ScaleType.FIT_CENTER);
834
835		input.setHint(getString(R.string.captcha_hint));
836
837		layout.setOrientation(LinearLayout.VERTICAL);
838		layout.addView(view);
839		layout.addView(input);
840
841		builder.setTitle(getString(R.string.captcha_required));
842		builder.setView(layout);
843
844		builder.setPositiveButton(getString(R.string.ok),
845				new DialogInterface.OnClickListener() {
846					@Override
847					public void onClick(DialogInterface dialog, int which) {
848						String rc = input.getText().toString();
849						data.put("username", account.getUsername());
850						data.put("password", account.getPassword());
851						data.put("ocr", rc);
852						data.submit();
853
854						if (xmppConnectionServiceBound) {
855							xmppConnectionService.sendCreateAccountWithCaptchaPacket(
856									account, id, data);
857						}
858					}
859				});
860		builder.setNegativeButton(getString(R.string.cancel), new DialogInterface.OnClickListener() {
861			@Override
862			public void onClick(DialogInterface dialog, int which) {
863				if (xmppConnectionService != null) {
864					xmppConnectionService.sendCreateAccountWithCaptchaPacket(account, null, null);
865				}
866			}
867		});
868
869		builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
870			@Override
871			public void onCancel(DialogInterface dialog) {
872				if (xmppConnectionService != null) {
873					xmppConnectionService.sendCreateAccountWithCaptchaPacket(account, null, null);
874				}
875			}
876		});
877
878		runOnUiThread(new Runnable() {
879			@Override
880			public void run() {
881				if ((mCaptchaDialog != null) && mCaptchaDialog.isShowing()) {
882					mCaptchaDialog.dismiss();
883				}
884				mCaptchaDialog = builder.create();
885				mCaptchaDialog.show();
886			}
887		});
888	}
889
890	public void onShowErrorToast(final int resId) {
891		runOnUiThread(new Runnable() {
892			@Override
893			public void run() {
894				Toast.makeText(EditAccountActivity.this, resId, Toast.LENGTH_SHORT).show();
895			}
896		});
897	}
898
899	@Override
900	public void onPreferencesFetched(final Element prefs) {
901		runOnUiThread(new Runnable() {
902			@Override
903			public void run() {
904				if (mFetchingMamPrefsToast != null) {
905					mFetchingMamPrefsToast.cancel();
906				}
907				AlertDialog.Builder builder = new Builder(EditAccountActivity.this);
908				builder.setTitle(R.string.mam_prefs);
909				String defaultAttr = prefs.getAttribute("default");
910				final List<String> defaults = Arrays.asList("never", "roster", "always");
911				final AtomicInteger choice = new AtomicInteger(Math.max(0,defaults.indexOf(defaultAttr)));
912				builder.setSingleChoiceItems(R.array.mam_prefs, choice.get(), new DialogInterface.OnClickListener() {
913					@Override
914					public void onClick(DialogInterface dialog, int which) {
915						choice.set(which);
916					}
917				});
918				builder.setNegativeButton(R.string.cancel, null);
919				builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
920					@Override
921					public void onClick(DialogInterface dialog, int which) {
922						prefs.setAttribute("default",defaults.get(choice.get()));
923						xmppConnectionService.pushMamPreferences(mAccount, prefs);
924					}
925				});
926				builder.create().show();
927			}
928		});
929	}
930
931	@Override
932	public void onPreferencesFetchFailed() {
933		runOnUiThread(new Runnable() {
934			@Override
935			public void run() {
936				if (mFetchingMamPrefsToast != null) {
937					mFetchingMamPrefsToast.cancel();
938				}
939				Toast.makeText(EditAccountActivity.this,R.string.unable_to_fetch_mam_prefs,Toast.LENGTH_LONG).show();
940			}
941		});
942	}
943}