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