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