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