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 mPassword.setEnabled(!Config.LOCK_SETTINGS);
678 mAccountJid.setEnabled(!Config.LOCK_SETTINGS);
679 mHostname.setEnabled(!Config.LOCK_SETTINGS);
680 mPort.setEnabled(!Config.LOCK_SETTINGS);
681 mPasswordConfirm.setEnabled(!Config.LOCK_SETTINGS);
682 mRegisterNew.setEnabled(!Config.LOCK_SETTINGS);
683
684 if (!mInitMode) {
685 this.mAvatar.setVisibility(View.VISIBLE);
686 this.mAvatar.setImageBitmap(avatarService().get(this.mAccount, getPixel(72)));
687 } else {
688 this.mAvatar.setVisibility(View.GONE);
689 }
690 if (this.mAccount.isOptionSet(Account.OPTION_REGISTER)) {
691 this.mRegisterNew.setVisibility(View.VISIBLE);
692 this.mRegisterNew.setChecked(true);
693 this.mPasswordConfirm.setText(this.mAccount.getPassword());
694 } else {
695 this.mRegisterNew.setVisibility(View.GONE);
696 this.mRegisterNew.setChecked(false);
697 }
698 if (this.mAccount.isOnlineAndConnected() && !this.mFetchingAvatar) {
699 Features features = this.mAccount.getXmppConnection().getFeatures();
700 this.mStats.setVisibility(View.VISIBLE);
701 boolean showOptimizingWarning = !xmppConnectionService.getPushManagementService().available(mAccount) && isOptimizingBattery();
702 this.mBatteryOptimizations.setVisibility(showOptimizingWarning ? View.VISIBLE : View.GONE);
703 this.mSessionEst.setText(UIHelper.readableTimeDifferenceFull(this, this.mAccount.getXmppConnection()
704 .getLastSessionEstablished()));
705 if (features.rosterVersioning()) {
706 this.mServerInfoRosterVersion.setText(R.string.server_info_available);
707 } else {
708 this.mServerInfoRosterVersion.setText(R.string.server_info_unavailable);
709 }
710 if (features.carbons()) {
711 this.mServerInfoCarbons.setText(R.string.server_info_available);
712 } else {
713 this.mServerInfoCarbons
714 .setText(R.string.server_info_unavailable);
715 }
716 if (features.mam()) {
717 this.mServerInfoMam.setText(R.string.server_info_available);
718 } else {
719 this.mServerInfoMam.setText(R.string.server_info_unavailable);
720 }
721 if (features.csi()) {
722 this.mServerInfoCSI.setText(R.string.server_info_available);
723 } else {
724 this.mServerInfoCSI.setText(R.string.server_info_unavailable);
725 }
726 if (features.blocking()) {
727 this.mServerInfoBlocking.setText(R.string.server_info_available);
728 } else {
729 this.mServerInfoBlocking.setText(R.string.server_info_unavailable);
730 }
731 if (features.sm()) {
732 this.mServerInfoSm.setText(R.string.server_info_available);
733 } else {
734 this.mServerInfoSm.setText(R.string.server_info_unavailable);
735 }
736 if (features.pep()) {
737 AxolotlService axolotlService = this.mAccount.getAxolotlService();
738 if (axolotlService != null && axolotlService.isPepBroken()) {
739 this.mServerInfoPep.setText(R.string.server_info_broken);
740 } else {
741 this.mServerInfoPep.setText(R.string.server_info_available);
742 }
743 } else {
744 this.mServerInfoPep.setText(R.string.server_info_unavailable);
745 }
746 if (features.httpUpload(0)) {
747 this.mServerInfoHttpUpload.setText(R.string.server_info_available);
748 } else {
749 this.mServerInfoHttpUpload.setText(R.string.server_info_unavailable);
750 }
751
752 this.mPushRow.setVisibility(xmppConnectionService.getPushManagementService().isStub() ? View.GONE : View.VISIBLE);
753
754 if (xmppConnectionService.getPushManagementService().available(mAccount)) {
755 this.mServerInfoPush.setText(R.string.server_info_available);
756 } else {
757 this.mServerInfoPush.setText(R.string.server_info_unavailable);
758 }
759 final String otrFingerprint = this.mAccount.getOtrFingerprint();
760 if (otrFingerprint != null) {
761 this.mOtrFingerprintBox.setVisibility(View.VISIBLE);
762 this.mOtrFingerprint.setText(CryptoHelper.prettifyFingerprint(otrFingerprint));
763 this.mOtrFingerprintToClipboardButton
764 .setVisibility(View.VISIBLE);
765 this.mOtrFingerprintToClipboardButton
766 .setOnClickListener(new View.OnClickListener() {
767
768 @Override
769 public void onClick(final View v) {
770
771 if (copyTextToClipboard(otrFingerprint, R.string.otr_fingerprint)) {
772 Toast.makeText(
773 EditAccountActivity.this,
774 R.string.toast_message_otr_fingerprint,
775 Toast.LENGTH_SHORT).show();
776 }
777 }
778 });
779 } else {
780 this.mOtrFingerprintBox.setVisibility(View.GONE);
781 }
782 final String axolotlFingerprint = this.mAccount.getAxolotlService().getOwnFingerprint();
783 if (axolotlFingerprint != null) {
784 this.mAxolotlFingerprintBox.setVisibility(View.VISIBLE);
785 this.mAxolotlFingerprint.setText(CryptoHelper.prettifyFingerprint(axolotlFingerprint.substring(2)));
786 this.mAxolotlFingerprintToClipboardButton
787 .setVisibility(View.VISIBLE);
788 this.mAxolotlFingerprintToClipboardButton
789 .setOnClickListener(new View.OnClickListener() {
790
791 @Override
792 public void onClick(final View v) {
793
794 if (copyTextToClipboard(axolotlFingerprint.substring(2), R.string.omemo_fingerprint)) {
795 Toast.makeText(
796 EditAccountActivity.this,
797 R.string.toast_message_omemo_fingerprint,
798 Toast.LENGTH_SHORT).show();
799 }
800 }
801 });
802 if (Config.SHOW_REGENERATE_AXOLOTL_KEYS_BUTTON) {
803 this.mRegenerateAxolotlKeyButton
804 .setVisibility(View.VISIBLE);
805 this.mRegenerateAxolotlKeyButton
806 .setOnClickListener(new View.OnClickListener() {
807
808 @Override
809 public void onClick(final View v) {
810 showRegenerateAxolotlKeyDialog();
811 }
812 });
813 }
814 } else {
815 this.mAxolotlFingerprintBox.setVisibility(View.GONE);
816 }
817 final String ownFingerprint = mAccount.getAxolotlService().getOwnFingerprint();
818 boolean hasKeys = false;
819 keys.removeAllViews();
820 for (final String fingerprint : mAccount.getAxolotlService().getFingerprintsForOwnSessions()) {
821 if (ownFingerprint.equals(fingerprint)) {
822 continue;
823 }
824 boolean highlight = fingerprint.equals(messageFingerprint);
825 hasKeys |= addFingerprintRow(keys, mAccount, fingerprint, highlight, null);
826 }
827 if (hasKeys) {
828 keysCard.setVisibility(View.VISIBLE);
829 } else {
830 keysCard.setVisibility(View.GONE);
831 }
832 } else {
833 if (this.mAccount.errorStatus()) {
834 final EditText errorTextField;
835 if (this.mAccount.getStatus() == Account.State.UNAUTHORIZED) {
836 errorTextField = this.mPassword;
837 } else if (mShowOptions
838 && this.mAccount.getStatus() == Account.State.SERVER_NOT_FOUND
839 && this.mHostname.getText().length() > 0) {
840 errorTextField = this.mHostname;
841 } else {
842 errorTextField = this.mAccountJid;
843 }
844 errorTextField.setError(getString(this.mAccount.getStatus().getReadableId()));
845 if (init || !accountInfoEdited()) {
846 errorTextField.requestFocus();
847 }
848 } else {
849 this.mAccountJid.setError(null);
850 this.mPassword.setError(null);
851 this.mHostname.setError(null);
852 }
853 this.mStats.setVisibility(View.GONE);
854 }
855 }
856
857 public void showRegenerateAxolotlKeyDialog() {
858 Builder builder = new Builder(this);
859 builder.setTitle("Regenerate Key");
860 builder.setIconAttribute(android.R.attr.alertDialogIcon);
861 builder.setMessage("Are you sure you want to regenerate your Identity Key? (This will also wipe all established sessions and contact Identity Keys)");
862 builder.setNegativeButton(getString(R.string.cancel), null);
863 builder.setPositiveButton("Yes",
864 new DialogInterface.OnClickListener() {
865 @Override
866 public void onClick(DialogInterface dialog, int which) {
867 mAccount.getAxolotlService().regenerateKeys(false);
868 }
869 });
870 builder.create().show();
871 }
872
873 public void showWipePepDialog() {
874 Builder builder = new Builder(this);
875 builder.setTitle(getString(R.string.clear_other_devices));
876 builder.setIconAttribute(android.R.attr.alertDialogIcon);
877 builder.setMessage(getString(R.string.clear_other_devices_desc));
878 builder.setNegativeButton(getString(R.string.cancel), null);
879 builder.setPositiveButton(getString(R.string.accept),
880 new DialogInterface.OnClickListener() {
881 @Override
882 public void onClick(DialogInterface dialog, int which) {
883 mAccount.getAxolotlService().wipeOtherPepDevices();
884 }
885 });
886 builder.create().show();
887 }
888
889 private void editMamPrefs() {
890 this.mFetchingMamPrefsToast = Toast.makeText(this, R.string.fetching_mam_prefs, Toast.LENGTH_LONG);
891 this.mFetchingMamPrefsToast.show();
892 xmppConnectionService.fetchMamPreferences(mAccount, this);
893 }
894
895 @Override
896 public void onKeyStatusUpdated(AxolotlService.FetchStatus report) {
897 refreshUi();
898 }
899
900 @Override
901 public void onCaptchaRequested(final Account account, final String id, final Data data,
902 final Bitmap captcha) {
903 final AlertDialog.Builder builder = new AlertDialog.Builder(this);
904 final ImageView view = new ImageView(this);
905 final LinearLayout layout = new LinearLayout(this);
906 final EditText input = new EditText(this);
907
908 view.setImageBitmap(captcha);
909 view.setScaleType(ImageView.ScaleType.FIT_CENTER);
910
911 input.setHint(getString(R.string.captcha_hint));
912
913 layout.setOrientation(LinearLayout.VERTICAL);
914 layout.addView(view);
915 layout.addView(input);
916
917 builder.setTitle(getString(R.string.captcha_required));
918 builder.setView(layout);
919
920 builder.setPositiveButton(getString(R.string.ok),
921 new DialogInterface.OnClickListener() {
922 @Override
923 public void onClick(DialogInterface dialog, int which) {
924 String rc = input.getText().toString();
925 data.put("username", account.getUsername());
926 data.put("password", account.getPassword());
927 data.put("ocr", rc);
928 data.submit();
929
930 if (xmppConnectionServiceBound) {
931 xmppConnectionService.sendCreateAccountWithCaptchaPacket(
932 account, id, data);
933 }
934 }
935 });
936 builder.setNegativeButton(getString(R.string.cancel), new DialogInterface.OnClickListener() {
937 @Override
938 public void onClick(DialogInterface dialog, int which) {
939 if (xmppConnectionService != null) {
940 xmppConnectionService.sendCreateAccountWithCaptchaPacket(account, null, null);
941 }
942 }
943 });
944
945 builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
946 @Override
947 public void onCancel(DialogInterface dialog) {
948 if (xmppConnectionService != null) {
949 xmppConnectionService.sendCreateAccountWithCaptchaPacket(account, null, null);
950 }
951 }
952 });
953
954 runOnUiThread(new Runnable() {
955 @Override
956 public void run() {
957 if ((mCaptchaDialog != null) && mCaptchaDialog.isShowing()) {
958 mCaptchaDialog.dismiss();
959 }
960 mCaptchaDialog = builder.create();
961 mCaptchaDialog.show();
962 }
963 });
964 }
965
966 public void onShowErrorToast(final int resId) {
967 runOnUiThread(new Runnable() {
968 @Override
969 public void run() {
970 Toast.makeText(EditAccountActivity.this, resId, Toast.LENGTH_SHORT).show();
971 }
972 });
973 }
974
975 @Override
976 public void onPreferencesFetched(final Element prefs) {
977 runOnUiThread(new Runnable() {
978 @Override
979 public void run() {
980 if (mFetchingMamPrefsToast != null) {
981 mFetchingMamPrefsToast.cancel();
982 }
983 AlertDialog.Builder builder = new Builder(EditAccountActivity.this);
984 builder.setTitle(R.string.server_side_mam_prefs);
985 String defaultAttr = prefs.getAttribute("default");
986 final List<String> defaults = Arrays.asList("never", "roster", "always");
987 final AtomicInteger choice = new AtomicInteger(Math.max(0,defaults.indexOf(defaultAttr)));
988 builder.setSingleChoiceItems(R.array.mam_prefs, choice.get(), new DialogInterface.OnClickListener() {
989 @Override
990 public void onClick(DialogInterface dialog, int which) {
991 choice.set(which);
992 }
993 });
994 builder.setNegativeButton(R.string.cancel, null);
995 builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
996 @Override
997 public void onClick(DialogInterface dialog, int which) {
998 prefs.setAttribute("default",defaults.get(choice.get()));
999 xmppConnectionService.pushMamPreferences(mAccount, prefs);
1000 }
1001 });
1002 builder.create().show();
1003 }
1004 });
1005 }
1006
1007 @Override
1008 public void onPreferencesFetchFailed() {
1009 runOnUiThread(new Runnable() {
1010 @Override
1011 public void run() {
1012 if (mFetchingMamPrefsToast != null) {
1013 mFetchingMamPrefsToast.cancel();
1014 }
1015 Toast.makeText(EditAccountActivity.this,R.string.unable_to_fetch_mam_prefs,Toast.LENGTH_LONG).show();
1016 }
1017 });
1018 }
1019}