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 if (avatar != null || (connection != null && !connection.getFeatures().pep())) {
319 intent = new Intent(getApplicationContext(), StartConversationActivity.class);
320 if (xmppConnectionService != null && xmppConnectionService.getAccounts().size() == 1) {
321 intent.putExtra("init", true);
322 }
323 } else {
324 intent = new Intent(getApplicationContext(), PublishProfilePictureActivity.class);
325 intent.putExtra(EXTRA_ACCOUNT, mAccount.getJid().toBareJid().toString());
326 intent.putExtra("setup", true);
327 }
328 startActivity(intent);
329 finish();
330 }
331 });
332 }
333
334 @Override
335 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
336 super.onActivityResult(requestCode, resultCode, data);
337 if (requestCode == REQUEST_BATTERY_OP) {
338 updateAccountInformation(mAccount == null);
339 }
340 }
341
342 protected void updateSaveButton() {
343 boolean accountInfoEdited = accountInfoEdited();
344
345 if (!mInitMode && passwordChangedInMagicCreateMode()) {
346 this.mSaveButton.setText(R.string.change_password);
347 this.mSaveButton.setEnabled(true);
348 this.mSaveButton.setTextColor(getPrimaryTextColor());
349 } else if (accountInfoEdited && !mInitMode) {
350 this.mSaveButton.setText(R.string.save);
351 this.mSaveButton.setEnabled(true);
352 this.mSaveButton.setTextColor(getPrimaryTextColor());
353 } else if (mAccount != null
354 && (mAccount.getStatus() == Account.State.CONNECTING || mAccount.getStatus() == Account.State.REGISTRATION_SUCCESSFUL|| mFetchingAvatar)) {
355 this.mSaveButton.setEnabled(false);
356 this.mSaveButton.setTextColor(getSecondaryTextColor());
357 this.mSaveButton.setText(R.string.account_status_connecting);
358 } else if (mAccount != null && mAccount.getStatus() == Account.State.DISABLED && !mInitMode) {
359 this.mSaveButton.setEnabled(true);
360 this.mSaveButton.setTextColor(getPrimaryTextColor());
361 this.mSaveButton.setText(R.string.enable);
362 } else {
363 this.mSaveButton.setEnabled(true);
364 this.mSaveButton.setTextColor(getPrimaryTextColor());
365 if (!mInitMode) {
366 if (mAccount != null && mAccount.isOnlineAndConnected()) {
367 this.mSaveButton.setText(R.string.save);
368 if (!accountInfoEdited) {
369 this.mSaveButton.setEnabled(false);
370 this.mSaveButton.setTextColor(getSecondaryTextColor());
371 }
372 } else {
373 this.mSaveButton.setText(R.string.connect);
374 }
375 } else {
376 this.mSaveButton.setText(R.string.next);
377 }
378 }
379 }
380
381 protected boolean accountInfoEdited() {
382 if (this.mAccount == null) {
383 return false;
384 }
385 return jidEdited() ||
386 !this.mAccount.getPassword().equals(this.mPassword.getText().toString()) ||
387 !this.mAccount.getHostname().equals(this.mHostname.getText().toString()) ||
388 !String.valueOf(this.mAccount.getPort()).equals(this.mPort.getText().toString());
389 }
390
391 protected boolean jidEdited() {
392 final String unmodified;
393 if (Config.DOMAIN_LOCK != null) {
394 unmodified = this.mAccount.getJid().getLocalpart();
395 } else {
396 unmodified = this.mAccount.getJid().toBareJid().toString();
397 }
398 return !unmodified.equals(this.mAccountJid.getText().toString());
399 }
400
401 protected boolean passwordChangedInMagicCreateMode() {
402 return mAccount != null
403 && mAccount.isOptionSet(Account.OPTION_MAGIC_CREATE)
404 && !this.mAccount.getPassword().equals(this.mPassword.getText().toString())
405 && !this.jidEdited()
406 && mAccount.isOnlineAndConnected();
407 }
408
409 @Override
410 protected String getShareableUri() {
411 if (mAccount != null) {
412 return mAccount.getShareableUri();
413 } else {
414 return "";
415 }
416 }
417
418 @Override
419 protected void onCreate(final Bundle savedInstanceState) {
420 super.onCreate(savedInstanceState);
421 setContentView(R.layout.activity_edit_account);
422 this.mAccountJid = (AutoCompleteTextView) findViewById(R.id.account_jid);
423 this.mAccountJid.addTextChangedListener(this.mTextWatcher);
424 this.mAccountJidLabel = (TextView) findViewById(R.id.account_jid_label);
425 if (Config.DOMAIN_LOCK != null) {
426 this.mAccountJidLabel.setText(R.string.username);
427 this.mAccountJid.setHint(R.string.username_hint);
428 }
429 this.mPassword = (EditText) findViewById(R.id.account_password);
430 this.mPassword.addTextChangedListener(this.mTextWatcher);
431 this.mPasswordConfirm = (EditText) findViewById(R.id.account_password_confirm);
432 this.mAvatar = (ImageView) findViewById(R.id.avater);
433 this.mAvatar.setOnClickListener(this.mAvatarClickListener);
434 this.mRegisterNew = (CheckBox) findViewById(R.id.account_register_new);
435 this.mStats = (LinearLayout) findViewById(R.id.stats);
436 this.mBatteryOptimizations = (RelativeLayout) findViewById(R.id.battery_optimization);
437 this.mDisableBatterOptimizations = (Button) findViewById(R.id.batt_op_disable);
438 this.mDisableBatterOptimizations.setOnClickListener(new OnClickListener() {
439 @Override
440 public void onClick(View v) {
441 Intent intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
442 Uri uri = Uri.parse("package:"+getPackageName());
443 intent.setData(uri);
444 startActivityForResult(intent,REQUEST_BATTERY_OP);
445 }
446 });
447 this.mSessionEst = (TextView) findViewById(R.id.session_est);
448 this.mServerInfoRosterVersion = (TextView) findViewById(R.id.server_info_roster_version);
449 this.mServerInfoCarbons = (TextView) findViewById(R.id.server_info_carbons);
450 this.mServerInfoMam = (TextView) findViewById(R.id.server_info_mam);
451 this.mServerInfoCSI = (TextView) findViewById(R.id.server_info_csi);
452 this.mServerInfoBlocking = (TextView) findViewById(R.id.server_info_blocking);
453 this.mServerInfoSm = (TextView) findViewById(R.id.server_info_sm);
454 this.mServerInfoPep = (TextView) findViewById(R.id.server_info_pep);
455 this.mServerInfoHttpUpload = (TextView) findViewById(R.id.server_info_http_upload);
456 this.mPushRow = (TableRow) findViewById(R.id.push_row);
457 this.mServerInfoPush = (TextView) findViewById(R.id.server_info_push);
458 this.mOtrFingerprint = (TextView) findViewById(R.id.otr_fingerprint);
459 this.mOtrFingerprintBox = (RelativeLayout) findViewById(R.id.otr_fingerprint_box);
460 this.mOtrFingerprintToClipboardButton = (ImageButton) findViewById(R.id.action_copy_to_clipboard);
461 this.mAxolotlFingerprint = (TextView) findViewById(R.id.axolotl_fingerprint);
462 this.mAxolotlFingerprintBox = (RelativeLayout) findViewById(R.id.axolotl_fingerprint_box);
463 this.mAxolotlFingerprintToClipboardButton = (ImageButton) findViewById(R.id.action_copy_axolotl_to_clipboard);
464 this.mRegenerateAxolotlKeyButton = (ImageButton) findViewById(R.id.action_regenerate_axolotl_key);
465 this.keysCard = (LinearLayout) findViewById(R.id.other_device_keys_card);
466 this.keys = (LinearLayout) findViewById(R.id.other_device_keys);
467 this.mNamePort = (LinearLayout) findViewById(R.id.name_port);
468 this.mHostname = (EditText) findViewById(R.id.hostname);
469 this.mHostname.addTextChangedListener(mTextWatcher);
470 this.mPort = (EditText) findViewById(R.id.port);
471 this.mPort.setText("5222");
472 this.mPort.addTextChangedListener(mTextWatcher);
473 this.mSaveButton = (Button) findViewById(R.id.save_button);
474 this.mCancelButton = (Button) findViewById(R.id.cancel_button);
475 this.mSaveButton.setOnClickListener(this.mSaveButtonClickListener);
476 this.mCancelButton.setOnClickListener(this.mCancelButtonClickListener);
477 this.mMoreTable = (TableLayout) findViewById(R.id.server_info_more);
478 final OnCheckedChangeListener OnCheckedShowConfirmPassword = new OnCheckedChangeListener() {
479 @Override
480 public void onCheckedChanged(final CompoundButton buttonView,
481 final boolean isChecked) {
482 if (isChecked) {
483 mPasswordConfirm.setVisibility(View.VISIBLE);
484 } else {
485 mPasswordConfirm.setVisibility(View.GONE);
486 }
487 updateSaveButton();
488 }
489 };
490 this.mRegisterNew.setOnCheckedChangeListener(OnCheckedShowConfirmPassword);
491 if (Config.DISALLOW_REGISTRATION_IN_UI) {
492 this.mRegisterNew.setVisibility(View.GONE);
493 }
494 }
495
496 @Override
497 public boolean onCreateOptionsMenu(final Menu menu) {
498 super.onCreateOptionsMenu(menu);
499 getMenuInflater().inflate(R.menu.editaccount, menu);
500 final MenuItem showQrCode = menu.findItem(R.id.action_show_qr_code);
501 final MenuItem showBlocklist = menu.findItem(R.id.action_show_block_list);
502 final MenuItem showMoreInfo = menu.findItem(R.id.action_server_info_show_more);
503 final MenuItem changePassword = menu.findItem(R.id.action_change_password_on_server);
504 final MenuItem clearDevices = menu.findItem(R.id.action_clear_devices);
505 final MenuItem renewCertificate = menu.findItem(R.id.action_renew_certificate);
506 final MenuItem mamPrefs = menu.findItem(R.id.action_mam_prefs);
507 final MenuItem changePresence = menu.findItem(R.id.action_change_presence);
508 renewCertificate.setVisible(mAccount != null && mAccount.getPrivateKeyAlias() != null);
509
510 if (mAccount != null && mAccount.isOnlineAndConnected()) {
511 if (!mAccount.getXmppConnection().getFeatures().blocking()) {
512 showBlocklist.setVisible(false);
513 }
514 if (!mAccount.getXmppConnection().getFeatures().register()) {
515 changePassword.setVisible(false);
516 }
517 mamPrefs.setVisible(mAccount.getXmppConnection().getFeatures().mam());
518 Set<Integer> otherDevices = mAccount.getAxolotlService().getOwnDeviceIds();
519 if (otherDevices == null || otherDevices.isEmpty()) {
520 clearDevices.setVisible(false);
521 }
522 changePresence.setVisible(manuallyChangePresence());
523 } else {
524 showQrCode.setVisible(false);
525 showBlocklist.setVisible(false);
526 showMoreInfo.setVisible(false);
527 changePassword.setVisible(false);
528 clearDevices.setVisible(false);
529 mamPrefs.setVisible(false);
530 changePresence.setVisible(false);
531 }
532 return super.onCreateOptionsMenu(menu);
533 }
534
535 @Override
536 protected void onStart() {
537 super.onStart();
538 if (getIntent() != null) {
539 try {
540 this.jidToEdit = Jid.fromString(getIntent().getStringExtra("jid"));
541 } catch (final InvalidJidException | NullPointerException ignored) {
542 this.jidToEdit = null;
543 }
544 this.mInitMode = getIntent().getBooleanExtra("init", false) || this.jidToEdit == null;
545 this.messageFingerprint = getIntent().getStringExtra("fingerprint");
546 if (!mInitMode) {
547 this.mRegisterNew.setVisibility(View.GONE);
548 if (getActionBar() != null) {
549 getActionBar().setTitle(getString(R.string.account_details));
550 }
551 } else {
552 this.mAvatar.setVisibility(View.GONE);
553 if (getActionBar() != null) {
554 getActionBar().setTitle(R.string.action_add_account);
555 }
556 }
557 }
558 SharedPreferences preferences = getPreferences();
559 boolean useTor = Config.FORCE_ORBOT || preferences.getBoolean("use_tor", false);
560 this.mShowOptions = useTor || preferences.getBoolean("show_connection_options", false);
561 mHostname.setHint(useTor ? R.string.hostname_or_onion : R.string.hostname_example);
562 this.mNamePort.setVisibility(mShowOptions ? View.VISIBLE : View.GONE);
563 }
564
565 @Override
566 protected void onBackendConnected() {
567 if (this.jidToEdit != null) {
568 this.mAccount = xmppConnectionService.findAccountByJid(jidToEdit);
569 if (this.mAccount != null) {
570 this.mInitMode |= this.mAccount.isOptionSet(Account.OPTION_REGISTER);
571 if (this.mAccount.getPrivateKeyAlias() != null) {
572 this.mPassword.setHint(R.string.authenticate_with_certificate);
573 if (this.mInitMode) {
574 this.mPassword.requestFocus();
575 }
576 }
577 updateAccountInformation(true);
578 }
579 }
580 if (this.xmppConnectionService.getAccounts().size() == 0
581 || this.mAccount == xmppConnectionService.getPendingAccount()) {
582 if (getActionBar() != null) {
583 getActionBar().setDisplayHomeAsUpEnabled(false);
584 getActionBar().setDisplayShowHomeEnabled(false);
585 getActionBar().setHomeButtonEnabled(false);
586 }
587 this.mCancelButton.setEnabled(false);
588 this.mCancelButton.setTextColor(getSecondaryTextColor());
589 }
590 if (Config.DOMAIN_LOCK == null) {
591 final KnownHostsAdapter mKnownHostsAdapter = new KnownHostsAdapter(this,
592 android.R.layout.simple_list_item_1,
593 xmppConnectionService.getKnownHosts());
594 this.mAccountJid.setAdapter(mKnownHostsAdapter);
595 }
596 updateSaveButton();
597 invalidateOptionsMenu();
598 }
599
600 @Override
601 public boolean onOptionsItemSelected(final MenuItem item) {
602 switch (item.getItemId()) {
603 case R.id.action_show_block_list:
604 final Intent showBlocklistIntent = new Intent(this, BlocklistActivity.class);
605 showBlocklistIntent.putExtra(EXTRA_ACCOUNT, mAccount.getJid().toString());
606 startActivity(showBlocklistIntent);
607 break;
608 case R.id.action_server_info_show_more:
609 mMoreTable.setVisibility(item.isChecked() ? View.GONE : View.VISIBLE);
610 item.setChecked(!item.isChecked());
611 break;
612 case R.id.action_change_password_on_server:
613 gotoChangePassword(null);
614 break;
615 case R.id.action_mam_prefs:
616 editMamPrefs();
617 break;
618 case R.id.action_clear_devices:
619 showWipePepDialog();
620 break;
621 case R.id.action_renew_certificate:
622 renewCertificate();
623 break;
624 case R.id.action_change_presence:
625 changePresence();
626 break;
627 }
628 return super.onOptionsItemSelected(item);
629 }
630
631 private void gotoChangePassword(String newPassword) {
632 final Intent changePasswordIntent = new Intent(this, ChangePasswordActivity.class);
633 changePasswordIntent.putExtra(EXTRA_ACCOUNT, mAccount.getJid().toString());
634 if (newPassword != null) {
635 changePasswordIntent.putExtra("password", newPassword);
636 }
637 startActivity(changePasswordIntent);
638 }
639
640 private void renewCertificate() {
641 KeyChain.choosePrivateKeyAlias(this, this, null, null, null, -1, null);
642 }
643
644 private void changePresence() {
645 Intent intent = new Intent(this, SetPresenceActivity.class);
646 intent.putExtra(SetPresenceActivity.EXTRA_ACCOUNT,mAccount.getJid().toBareJid().toString());
647 startActivity(intent);
648 }
649
650 @Override
651 public void alias(String alias) {
652 if (alias != null) {
653 xmppConnectionService.updateKeyInAccount(mAccount, alias);
654 }
655 }
656
657 private void updateAccountInformation(boolean init) {
658 if (init) {
659 this.mAccountJid.getEditableText().clear();
660 if (Config.DOMAIN_LOCK != null) {
661 this.mAccountJid.getEditableText().append(this.mAccount.getJid().getLocalpart());
662 } else {
663 this.mAccountJid.getEditableText().append(this.mAccount.getJid().toBareJid().toString());
664 }
665 this.mPassword.setText(this.mAccount.getPassword());
666 this.mHostname.setText("");
667 this.mHostname.getEditableText().append(this.mAccount.getHostname());
668 this.mPort.setText("");
669 this.mPort.getEditableText().append(String.valueOf(this.mAccount.getPort()));
670 this.mNamePort.setVisibility(mShowOptions ? View.VISIBLE : View.GONE);
671
672 }
673 mPassword.setEnabled(!Config.LOCK_SETTINGS);
674 mAccountJid.setEnabled(!Config.LOCK_SETTINGS);
675 mHostname.setEnabled(!Config.LOCK_SETTINGS);
676 mPort.setEnabled(!Config.LOCK_SETTINGS);
677 mPasswordConfirm.setEnabled(!Config.LOCK_SETTINGS);
678 mRegisterNew.setEnabled(!Config.LOCK_SETTINGS);
679
680 if (!mInitMode) {
681 this.mAvatar.setVisibility(View.VISIBLE);
682 this.mAvatar.setImageBitmap(avatarService().get(this.mAccount, getPixel(72)));
683 } else {
684 this.mAvatar.setVisibility(View.GONE);
685 }
686 if (this.mAccount.isOptionSet(Account.OPTION_REGISTER)) {
687 this.mRegisterNew.setVisibility(View.VISIBLE);
688 this.mRegisterNew.setChecked(true);
689 this.mPasswordConfirm.setText(this.mAccount.getPassword());
690 } else {
691 this.mRegisterNew.setVisibility(View.GONE);
692 this.mRegisterNew.setChecked(false);
693 }
694 if (this.mAccount.isOnlineAndConnected() && !this.mFetchingAvatar) {
695 Features features = this.mAccount.getXmppConnection().getFeatures();
696 this.mStats.setVisibility(View.VISIBLE);
697 boolean showOptimizingWarning = !xmppConnectionService.getPushManagementService().available(mAccount) && isOptimizingBattery();
698 this.mBatteryOptimizations.setVisibility(showOptimizingWarning ? View.VISIBLE : View.GONE);
699 this.mSessionEst.setText(UIHelper.readableTimeDifferenceFull(this, this.mAccount.getXmppConnection()
700 .getLastSessionEstablished()));
701 if (features.rosterVersioning()) {
702 this.mServerInfoRosterVersion.setText(R.string.server_info_available);
703 } else {
704 this.mServerInfoRosterVersion.setText(R.string.server_info_unavailable);
705 }
706 if (features.carbons()) {
707 this.mServerInfoCarbons.setText(R.string.server_info_available);
708 } else {
709 this.mServerInfoCarbons
710 .setText(R.string.server_info_unavailable);
711 }
712 if (features.mam()) {
713 this.mServerInfoMam.setText(R.string.server_info_available);
714 } else {
715 this.mServerInfoMam.setText(R.string.server_info_unavailable);
716 }
717 if (features.csi()) {
718 this.mServerInfoCSI.setText(R.string.server_info_available);
719 } else {
720 this.mServerInfoCSI.setText(R.string.server_info_unavailable);
721 }
722 if (features.blocking()) {
723 this.mServerInfoBlocking.setText(R.string.server_info_available);
724 } else {
725 this.mServerInfoBlocking.setText(R.string.server_info_unavailable);
726 }
727 if (features.sm()) {
728 this.mServerInfoSm.setText(R.string.server_info_available);
729 } else {
730 this.mServerInfoSm.setText(R.string.server_info_unavailable);
731 }
732 if (features.pep()) {
733 AxolotlService axolotlService = this.mAccount.getAxolotlService();
734 if (axolotlService != null && axolotlService.isPepBroken()) {
735 this.mServerInfoPep.setText(R.string.server_info_broken);
736 } else {
737 this.mServerInfoPep.setText(R.string.server_info_available);
738 }
739 } else {
740 this.mServerInfoPep.setText(R.string.server_info_unavailable);
741 }
742 if (features.httpUpload(0)) {
743 this.mServerInfoHttpUpload.setText(R.string.server_info_available);
744 } else {
745 this.mServerInfoHttpUpload.setText(R.string.server_info_unavailable);
746 }
747
748 this.mPushRow.setVisibility(xmppConnectionService.getPushManagementService().isStub() ? View.GONE : View.VISIBLE);
749
750 if (xmppConnectionService.getPushManagementService().available(mAccount)) {
751 this.mServerInfoPush.setText(R.string.server_info_available);
752 } else {
753 this.mServerInfoPush.setText(R.string.server_info_unavailable);
754 }
755 final String otrFingerprint = this.mAccount.getOtrFingerprint();
756 if (otrFingerprint != null) {
757 this.mOtrFingerprintBox.setVisibility(View.VISIBLE);
758 this.mOtrFingerprint.setText(CryptoHelper.prettifyFingerprint(otrFingerprint));
759 this.mOtrFingerprintToClipboardButton
760 .setVisibility(View.VISIBLE);
761 this.mOtrFingerprintToClipboardButton
762 .setOnClickListener(new View.OnClickListener() {
763
764 @Override
765 public void onClick(final View v) {
766
767 if (copyTextToClipboard(otrFingerprint, R.string.otr_fingerprint)) {
768 Toast.makeText(
769 EditAccountActivity.this,
770 R.string.toast_message_otr_fingerprint,
771 Toast.LENGTH_SHORT).show();
772 }
773 }
774 });
775 } else {
776 this.mOtrFingerprintBox.setVisibility(View.GONE);
777 }
778 final String axolotlFingerprint = this.mAccount.getAxolotlService().getOwnFingerprint();
779 if (axolotlFingerprint != null) {
780 this.mAxolotlFingerprintBox.setVisibility(View.VISIBLE);
781 this.mAxolotlFingerprint.setText(CryptoHelper.prettifyFingerprint(axolotlFingerprint.substring(2)));
782 this.mAxolotlFingerprintToClipboardButton
783 .setVisibility(View.VISIBLE);
784 this.mAxolotlFingerprintToClipboardButton
785 .setOnClickListener(new View.OnClickListener() {
786
787 @Override
788 public void onClick(final View v) {
789
790 if (copyTextToClipboard(axolotlFingerprint.substring(2), R.string.omemo_fingerprint)) {
791 Toast.makeText(
792 EditAccountActivity.this,
793 R.string.toast_message_omemo_fingerprint,
794 Toast.LENGTH_SHORT).show();
795 }
796 }
797 });
798 if (Config.SHOW_REGENERATE_AXOLOTL_KEYS_BUTTON) {
799 this.mRegenerateAxolotlKeyButton
800 .setVisibility(View.VISIBLE);
801 this.mRegenerateAxolotlKeyButton
802 .setOnClickListener(new View.OnClickListener() {
803
804 @Override
805 public void onClick(final View v) {
806 showRegenerateAxolotlKeyDialog();
807 }
808 });
809 }
810 } else {
811 this.mAxolotlFingerprintBox.setVisibility(View.GONE);
812 }
813 final String ownFingerprint = mAccount.getAxolotlService().getOwnFingerprint();
814 boolean hasKeys = false;
815 keys.removeAllViews();
816 for (final String fingerprint : mAccount.getAxolotlService().getFingerprintsForOwnSessions()) {
817 if (ownFingerprint.equals(fingerprint)) {
818 continue;
819 }
820 boolean highlight = fingerprint.equals(messageFingerprint);
821 hasKeys |= addFingerprintRow(keys, mAccount, fingerprint, highlight, null);
822 }
823 if (hasKeys) {
824 keysCard.setVisibility(View.VISIBLE);
825 } else {
826 keysCard.setVisibility(View.GONE);
827 }
828 } else {
829 if (this.mAccount.errorStatus()) {
830 final EditText errorTextField;
831 if (this.mAccount.getStatus() == Account.State.UNAUTHORIZED) {
832 errorTextField = this.mPassword;
833 } else if (mShowOptions
834 && this.mAccount.getStatus() == Account.State.SERVER_NOT_FOUND
835 && this.mHostname.getText().length() > 0) {
836 errorTextField = this.mHostname;
837 } else {
838 errorTextField = this.mAccountJid;
839 }
840 errorTextField.setError(getString(this.mAccount.getStatus().getReadableId()));
841 if (init || !accountInfoEdited()) {
842 errorTextField.requestFocus();
843 }
844 } else {
845 this.mAccountJid.setError(null);
846 this.mPassword.setError(null);
847 this.mHostname.setError(null);
848 }
849 this.mStats.setVisibility(View.GONE);
850 }
851 }
852
853 public void showRegenerateAxolotlKeyDialog() {
854 Builder builder = new Builder(this);
855 builder.setTitle("Regenerate Key");
856 builder.setIconAttribute(android.R.attr.alertDialogIcon);
857 builder.setMessage("Are you sure you want to regenerate your Identity Key? (This will also wipe all established sessions and contact Identity Keys)");
858 builder.setNegativeButton(getString(R.string.cancel), null);
859 builder.setPositiveButton("Yes",
860 new DialogInterface.OnClickListener() {
861 @Override
862 public void onClick(DialogInterface dialog, int which) {
863 mAccount.getAxolotlService().regenerateKeys(false);
864 }
865 });
866 builder.create().show();
867 }
868
869 public void showWipePepDialog() {
870 Builder builder = new Builder(this);
871 builder.setTitle(getString(R.string.clear_other_devices));
872 builder.setIconAttribute(android.R.attr.alertDialogIcon);
873 builder.setMessage(getString(R.string.clear_other_devices_desc));
874 builder.setNegativeButton(getString(R.string.cancel), null);
875 builder.setPositiveButton(getString(R.string.accept),
876 new DialogInterface.OnClickListener() {
877 @Override
878 public void onClick(DialogInterface dialog, int which) {
879 mAccount.getAxolotlService().wipeOtherPepDevices();
880 }
881 });
882 builder.create().show();
883 }
884
885 private void editMamPrefs() {
886 this.mFetchingMamPrefsToast = Toast.makeText(this, R.string.fetching_mam_prefs, Toast.LENGTH_LONG);
887 this.mFetchingMamPrefsToast.show();
888 xmppConnectionService.fetchMamPreferences(mAccount, this);
889 }
890
891 @Override
892 public void onKeyStatusUpdated(AxolotlService.FetchStatus report) {
893 refreshUi();
894 }
895
896 @Override
897 public void onCaptchaRequested(final Account account, final String id, final Data data,
898 final Bitmap captcha) {
899 final AlertDialog.Builder builder = new AlertDialog.Builder(this);
900 final ImageView view = new ImageView(this);
901 final LinearLayout layout = new LinearLayout(this);
902 final EditText input = new EditText(this);
903
904 view.setImageBitmap(captcha);
905 view.setScaleType(ImageView.ScaleType.FIT_CENTER);
906
907 input.setHint(getString(R.string.captcha_hint));
908
909 layout.setOrientation(LinearLayout.VERTICAL);
910 layout.addView(view);
911 layout.addView(input);
912
913 builder.setTitle(getString(R.string.captcha_required));
914 builder.setView(layout);
915
916 builder.setPositiveButton(getString(R.string.ok),
917 new DialogInterface.OnClickListener() {
918 @Override
919 public void onClick(DialogInterface dialog, int which) {
920 String rc = input.getText().toString();
921 data.put("username", account.getUsername());
922 data.put("password", account.getPassword());
923 data.put("ocr", rc);
924 data.submit();
925
926 if (xmppConnectionServiceBound) {
927 xmppConnectionService.sendCreateAccountWithCaptchaPacket(
928 account, id, data);
929 }
930 }
931 });
932 builder.setNegativeButton(getString(R.string.cancel), new DialogInterface.OnClickListener() {
933 @Override
934 public void onClick(DialogInterface dialog, int which) {
935 if (xmppConnectionService != null) {
936 xmppConnectionService.sendCreateAccountWithCaptchaPacket(account, null, null);
937 }
938 }
939 });
940
941 builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
942 @Override
943 public void onCancel(DialogInterface dialog) {
944 if (xmppConnectionService != null) {
945 xmppConnectionService.sendCreateAccountWithCaptchaPacket(account, null, null);
946 }
947 }
948 });
949
950 runOnUiThread(new Runnable() {
951 @Override
952 public void run() {
953 if ((mCaptchaDialog != null) && mCaptchaDialog.isShowing()) {
954 mCaptchaDialog.dismiss();
955 }
956 mCaptchaDialog = builder.create();
957 mCaptchaDialog.show();
958 }
959 });
960 }
961
962 public void onShowErrorToast(final int resId) {
963 runOnUiThread(new Runnable() {
964 @Override
965 public void run() {
966 Toast.makeText(EditAccountActivity.this, resId, Toast.LENGTH_SHORT).show();
967 }
968 });
969 }
970
971 @Override
972 public void onPreferencesFetched(final Element prefs) {
973 runOnUiThread(new Runnable() {
974 @Override
975 public void run() {
976 if (mFetchingMamPrefsToast != null) {
977 mFetchingMamPrefsToast.cancel();
978 }
979 AlertDialog.Builder builder = new Builder(EditAccountActivity.this);
980 builder.setTitle(R.string.server_side_mam_prefs);
981 String defaultAttr = prefs.getAttribute("default");
982 final List<String> defaults = Arrays.asList("never", "roster", "always");
983 final AtomicInteger choice = new AtomicInteger(Math.max(0,defaults.indexOf(defaultAttr)));
984 builder.setSingleChoiceItems(R.array.mam_prefs, choice.get(), new DialogInterface.OnClickListener() {
985 @Override
986 public void onClick(DialogInterface dialog, int which) {
987 choice.set(which);
988 }
989 });
990 builder.setNegativeButton(R.string.cancel, null);
991 builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
992 @Override
993 public void onClick(DialogInterface dialog, int which) {
994 prefs.setAttribute("default",defaults.get(choice.get()));
995 xmppConnectionService.pushMamPreferences(mAccount, prefs);
996 }
997 });
998 builder.create().show();
999 }
1000 });
1001 }
1002
1003 @Override
1004 public void onPreferencesFetchFailed() {
1005 runOnUiThread(new Runnable() {
1006 @Override
1007 public void run() {
1008 if (mFetchingMamPrefsToast != null) {
1009 mFetchingMamPrefsToast.cancel();
1010 }
1011 Toast.makeText(EditAccountActivity.this,R.string.unable_to_fetch_mam_prefs,Toast.LENGTH_LONG).show();
1012 }
1013 });
1014 }
1015}