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.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 static final int REQUEST_DATA_SAVER = 0x37af244;
66 private AutoCompleteTextView mAccountJid;
67 private EditText mPassword;
68 private EditText mPasswordConfirm;
69 private CheckBox mRegisterNew;
70 private Button mCancelButton;
71 private Button mSaveButton;
72 private Button mDisableOsOptimizationsButton;
73 private TextView mDisableOsOptimizationsHeadline;
74 private TextView getmDisableOsOptimizationsBody;
75 private TableLayout mMoreTable;
76
77 private LinearLayout mStats;
78 private RelativeLayout mOsOptimizations;
79 private TextView mServerInfoSm;
80 private TextView mServerInfoRosterVersion;
81 private TextView mServerInfoCarbons;
82 private TextView mServerInfoMam;
83 private TextView mServerInfoCSI;
84 private TextView mServerInfoBlocking;
85 private TextView mServerInfoPep;
86 private TextView mServerInfoHttpUpload;
87 private TextView mServerInfoPush;
88 private TextView mSessionEst;
89 private TextView mOtrFingerprint;
90 private TextView mAxolotlFingerprint;
91 private TextView mOwnFingerprintDesc;
92 private TextView mAccountJidLabel;
93 private ImageView mAvatar;
94 private RelativeLayout mOtrFingerprintBox;
95 private RelativeLayout mAxolotlFingerprintBox;
96 private ImageButton mOtrFingerprintToClipboardButton;
97 private ImageButton mAxolotlFingerprintToClipboardButton;
98 private ImageButton mRegenerateAxolotlKeyButton;
99 private LinearLayout keys;
100 private LinearLayout keysCard;
101 private LinearLayout mNamePort;
102 private EditText mHostname;
103 private EditText mPort;
104 private AlertDialog mCaptchaDialog = null;
105
106 private Jid jidToEdit;
107 private boolean mInitMode = false;
108 private boolean mUsernameMode = Config.DOMAIN_LOCK != null;
109 private boolean mShowOptions = false;
110 private Account mAccount;
111 private String messageFingerprint;
112
113 private boolean mFetchingAvatar = false;
114
115 private final OnClickListener mSaveButtonClickListener = new OnClickListener() {
116
117 @Override
118 public void onClick(final View v) {
119 final String password = mPassword.getText().toString();
120 final String passwordConfirm = mPasswordConfirm.getText().toString();
121
122 if (!mInitMode && passwordChangedInMagicCreateMode()) {
123 gotoChangePassword(password);
124 return;
125 }
126 if (mInitMode && mAccount != null) {
127 mAccount.setOption(Account.OPTION_DISABLED, false);
128 }
129 if (mAccount != null && mAccount.getStatus() == Account.State.DISABLED && !accountInfoEdited()) {
130 mAccount.setOption(Account.OPTION_DISABLED, false);
131 if (!xmppConnectionService.updateAccount(mAccount)) {
132 Toast.makeText(EditAccountActivity.this,R.string.unable_to_update_account,Toast.LENGTH_SHORT).show();
133 }
134 return;
135 }
136 final boolean registerNewAccount = mRegisterNew.isChecked() && !Config.DISALLOW_REGISTRATION_IN_UI;
137 if (mUsernameMode && mAccountJid.getText().toString().contains("@")) {
138 mAccountJid.setError(getString(R.string.invalid_username));
139 mAccountJid.requestFocus();
140 return;
141 }
142 final Jid jid;
143 try {
144 if (mUsernameMode) {
145 jid = Jid.fromParts(mAccountJid.getText().toString(), getUserModeDomain(), null);
146 } else {
147 jid = Jid.fromString(mAccountJid.getText().toString());
148 }
149 } catch (final InvalidJidException e) {
150 if (mUsernameMode) {
151 mAccountJid.setError(getString(R.string.invalid_username));
152 } else {
153 mAccountJid.setError(getString(R.string.invalid_jid));
154 }
155 mAccountJid.requestFocus();
156 return;
157 }
158 String hostname = null;
159 int numericPort = 5222;
160 if (mShowOptions) {
161 hostname = mHostname.getText().toString().replaceAll("\\s","");
162 final String port = mPort.getText().toString().replaceAll("\\s","");
163 if (hostname.contains(" ")) {
164 mHostname.setError(getString(R.string.not_valid_hostname));
165 mHostname.requestFocus();
166 return;
167 }
168 try {
169 numericPort = Integer.parseInt(port);
170 if (numericPort < 0 || numericPort > 65535) {
171 mPort.setError(getString(R.string.not_a_valid_port));
172 mPort.requestFocus();
173 return;
174 }
175
176 } catch (NumberFormatException e) {
177 mPort.setError(getString(R.string.not_a_valid_port));
178 mPort.requestFocus();
179 return;
180 }
181 }
182
183 if (jid.isDomainJid()) {
184 if (mUsernameMode) {
185 mAccountJid.setError(getString(R.string.invalid_username));
186 } else {
187 mAccountJid.setError(getString(R.string.invalid_jid));
188 }
189 mAccountJid.requestFocus();
190 return;
191 }
192 if (registerNewAccount) {
193 if (!password.equals(passwordConfirm)) {
194 mPasswordConfirm.setError(getString(R.string.passwords_do_not_match));
195 mPasswordConfirm.requestFocus();
196 return;
197 }
198 }
199 if (mAccount != null) {
200 if (mInitMode && mAccount.isOptionSet(Account.OPTION_MAGIC_CREATE)) {
201 mAccount.setOption(Account.OPTION_MAGIC_CREATE, mAccount.getPassword().contains(password));
202 }
203 mAccount.setJid(jid);
204 mAccount.setPort(numericPort);
205 mAccount.setHostname(hostname);
206 mAccountJid.setError(null);
207 mPasswordConfirm.setError(null);
208 mAccount.setPassword(password);
209 mAccount.setOption(Account.OPTION_REGISTER, registerNewAccount);
210 if (!xmppConnectionService.updateAccount(mAccount)) {
211 Toast.makeText(EditAccountActivity.this,R.string.unable_to_update_account,Toast.LENGTH_SHORT).show();
212 return;
213 }
214 } else {
215 if (xmppConnectionService.findAccountByJid(jid) != null) {
216 mAccountJid.setError(getString(R.string.account_already_exists));
217 mAccountJid.requestFocus();
218 return;
219 }
220 mAccount = new Account(jid.toBareJid(), password);
221 mAccount.setPort(numericPort);
222 mAccount.setHostname(hostname);
223 mAccount.setOption(Account.OPTION_USETLS, true);
224 mAccount.setOption(Account.OPTION_USECOMPRESSION, true);
225 mAccount.setOption(Account.OPTION_REGISTER, registerNewAccount);
226 xmppConnectionService.createAccount(mAccount);
227 }
228 mHostname.setError(null);
229 mPort.setError(null);
230 if (!mAccount.isOptionSet(Account.OPTION_DISABLED)
231 && !registerNewAccount
232 && !mInitMode) {
233 finish();
234 } else {
235 updateSaveButton();
236 updateAccountInformation(true);
237 }
238
239 }
240 };
241 private final OnClickListener mCancelButtonClickListener = new OnClickListener() {
242
243 @Override
244 public void onClick(final View v) {
245 deleteMagicCreatedAccountAndReturnIfNecessary();
246 finish();
247 }
248 };
249 private Toast mFetchingMamPrefsToast;
250 private TableRow mPushRow;
251 private String mSavedInstanceAccount;
252 private boolean mSavedInstanceInit = false;
253
254 public void refreshUiReal() {
255 invalidateOptionsMenu();
256 if (mAccount != null
257 && mAccount.getStatus() != Account.State.ONLINE
258 && mFetchingAvatar) {
259 startActivity(new Intent(getApplicationContext(),
260 ManageAccountActivity.class));
261 finish();
262 } else if (mInitMode && mAccount != null && mAccount.getStatus() == Account.State.ONLINE) {
263 if (!mFetchingAvatar) {
264 mFetchingAvatar = true;
265 xmppConnectionService.checkForAvatar(mAccount, mAvatarFetchCallback);
266 }
267 }
268 if (mAccount != null) {
269 updateAccountInformation(false);
270 }
271 updateSaveButton();
272 }
273
274 @Override
275 public boolean onNavigateUp() {
276 deleteMagicCreatedAccountAndReturnIfNecessary();
277 return super.onNavigateUp();
278 }
279
280 @Override
281 public void onBackPressed() {
282 deleteMagicCreatedAccountAndReturnIfNecessary();
283 super.onBackPressed();
284 }
285
286 private void deleteMagicCreatedAccountAndReturnIfNecessary() {
287 if (Config.MAGIC_CREATE_DOMAIN != null
288 && mAccount != null
289 && mAccount.isOptionSet(Account.OPTION_MAGIC_CREATE)
290 && mAccount.isOptionSet(Account.OPTION_REGISTER)
291 && xmppConnectionService.getAccounts().size() == 1) {
292 xmppConnectionService.deleteAccount(mAccount);
293 startActivity(new Intent(EditAccountActivity.this, WelcomeActivity.class));
294 }
295 }
296
297 @Override
298 public void onAccountUpdate() {
299 refreshUi();
300 }
301
302 private final UiCallback<Avatar> mAvatarFetchCallback = new UiCallback<Avatar>() {
303
304 @Override
305 public void userInputRequried(final PendingIntent pi, final Avatar avatar) {
306 finishInitialSetup(avatar);
307 }
308
309 @Override
310 public void success(final Avatar avatar) {
311 finishInitialSetup(avatar);
312 }
313
314 @Override
315 public void error(final int errorCode, final Avatar avatar) {
316 finishInitialSetup(avatar);
317 }
318 };
319 private final TextWatcher mTextWatcher = new TextWatcher() {
320
321 @Override
322 public void onTextChanged(final CharSequence s, final int start, final int before, final int count) {
323 updateSaveButton();
324 }
325
326 @Override
327 public void beforeTextChanged(final CharSequence s, final int start, final int count, final int after) {
328 }
329
330 @Override
331 public void afterTextChanged(final Editable s) {
332
333 }
334 };
335
336 private final OnClickListener mAvatarClickListener = new OnClickListener() {
337 @Override
338 public void onClick(final View view) {
339 if (mAccount != null) {
340 final Intent intent = new Intent(getApplicationContext(), PublishProfilePictureActivity.class);
341 intent.putExtra(EXTRA_ACCOUNT, mAccount.getJid().toBareJid().toString());
342 startActivity(intent);
343 }
344 }
345 };
346
347 protected void finishInitialSetup(final Avatar avatar) {
348 runOnUiThread(new Runnable() {
349
350 @Override
351 public void run() {
352 final Intent intent;
353 final XmppConnection connection = mAccount.getXmppConnection();
354 final boolean wasFirstAccount = xmppConnectionService != null && xmppConnectionService.getAccounts().size() == 1;
355 if (avatar != null || (connection != null && !connection.getFeatures().pep())) {
356 intent = new Intent(getApplicationContext(), StartConversationActivity.class);
357 if (wasFirstAccount) {
358 intent.putExtra("init", true);
359 }
360 } else {
361 intent = new Intent(getApplicationContext(), PublishProfilePictureActivity.class);
362 intent.putExtra(EXTRA_ACCOUNT, mAccount.getJid().toBareJid().toString());
363 intent.putExtra("setup", true);
364 }
365 if (wasFirstAccount) {
366 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
367 }
368 startActivity(intent);
369 finish();
370 }
371 });
372 }
373
374 @Override
375 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
376 super.onActivityResult(requestCode, resultCode, data);
377 if (requestCode == REQUEST_BATTERY_OP || requestCode == REQUEST_DATA_SAVER) {
378 updateAccountInformation(mAccount == null);
379 }
380 }
381
382 protected void updateSaveButton() {
383 boolean accountInfoEdited = accountInfoEdited();
384
385 if (!mInitMode && passwordChangedInMagicCreateMode()) {
386 this.mSaveButton.setText(R.string.change_password);
387 this.mSaveButton.setEnabled(true);
388 this.mSaveButton.setTextColor(getPrimaryTextColor());
389 } else if (accountInfoEdited && !mInitMode) {
390 this.mSaveButton.setText(R.string.save);
391 this.mSaveButton.setEnabled(true);
392 this.mSaveButton.setTextColor(getPrimaryTextColor());
393 } else if (mAccount != null
394 && (mAccount.getStatus() == Account.State.CONNECTING || mAccount.getStatus() == Account.State.REGISTRATION_SUCCESSFUL|| mFetchingAvatar)) {
395 this.mSaveButton.setEnabled(false);
396 this.mSaveButton.setTextColor(getSecondaryTextColor());
397 this.mSaveButton.setText(R.string.account_status_connecting);
398 } else if (mAccount != null && mAccount.getStatus() == Account.State.DISABLED && !mInitMode) {
399 this.mSaveButton.setEnabled(true);
400 this.mSaveButton.setTextColor(getPrimaryTextColor());
401 this.mSaveButton.setText(R.string.enable);
402 } else {
403 this.mSaveButton.setEnabled(true);
404 this.mSaveButton.setTextColor(getPrimaryTextColor());
405 if (!mInitMode) {
406 if (mAccount != null && mAccount.isOnlineAndConnected()) {
407 this.mSaveButton.setText(R.string.save);
408 if (!accountInfoEdited) {
409 this.mSaveButton.setEnabled(false);
410 this.mSaveButton.setTextColor(getSecondaryTextColor());
411 }
412 } else {
413 this.mSaveButton.setText(R.string.connect);
414 }
415 } else {
416 this.mSaveButton.setText(R.string.next);
417 }
418 }
419 }
420
421 protected boolean accountInfoEdited() {
422 if (this.mAccount == null) {
423 return false;
424 }
425 return jidEdited() ||
426 !this.mAccount.getPassword().equals(this.mPassword.getText().toString()) ||
427 !this.mAccount.getHostname().equals(this.mHostname.getText().toString()) ||
428 !String.valueOf(this.mAccount.getPort()).equals(this.mPort.getText().toString());
429 }
430
431 protected boolean jidEdited() {
432 final String unmodified;
433 if (mUsernameMode) {
434 unmodified = this.mAccount.getJid().getLocalpart();
435 } else {
436 unmodified = this.mAccount.getJid().toBareJid().toString();
437 }
438 return !unmodified.equals(this.mAccountJid.getText().toString());
439 }
440
441 protected boolean passwordChangedInMagicCreateMode() {
442 return mAccount != null
443 && mAccount.isOptionSet(Account.OPTION_MAGIC_CREATE)
444 && !this.mAccount.getPassword().equals(this.mPassword.getText().toString())
445 && !this.jidEdited()
446 && mAccount.isOnlineAndConnected();
447 }
448
449 @Override
450 protected String getShareableUri() {
451 if (mAccount != null) {
452 return mAccount.getShareableUri();
453 } else {
454 return "";
455 }
456 }
457
458 @Override
459 protected void onCreate(final Bundle savedInstanceState) {
460 super.onCreate(savedInstanceState);
461 if (savedInstanceState != null) {
462 this.mSavedInstanceAccount = savedInstanceState.getString("account");
463 this.mSavedInstanceInit = savedInstanceState.getBoolean("initMode", false);
464 }
465 setContentView(R.layout.activity_edit_account);
466 this.mAccountJid = (AutoCompleteTextView) findViewById(R.id.account_jid);
467 this.mAccountJid.addTextChangedListener(this.mTextWatcher);
468 this.mAccountJidLabel = (TextView) findViewById(R.id.account_jid_label);
469 this.mPassword = (EditText) findViewById(R.id.account_password);
470 this.mPassword.addTextChangedListener(this.mTextWatcher);
471 this.mPasswordConfirm = (EditText) findViewById(R.id.account_password_confirm);
472 this.mAvatar = (ImageView) findViewById(R.id.avater);
473 this.mAvatar.setOnClickListener(this.mAvatarClickListener);
474 this.mRegisterNew = (CheckBox) findViewById(R.id.account_register_new);
475 this.mStats = (LinearLayout) findViewById(R.id.stats);
476 this.mOsOptimizations = (RelativeLayout) findViewById(R.id.os_optimization);
477 this.mDisableOsOptimizationsButton = (Button) findViewById(R.id.os_optimization_disable);
478 this.mDisableOsOptimizationsHeadline = (TextView) findViewById(R.id.os_optimization_headline);
479 this.getmDisableOsOptimizationsBody = (TextView) findViewById(R.id.os_optimization_body);
480 this.mSessionEst = (TextView) findViewById(R.id.session_est);
481 this.mServerInfoRosterVersion = (TextView) findViewById(R.id.server_info_roster_version);
482 this.mServerInfoCarbons = (TextView) findViewById(R.id.server_info_carbons);
483 this.mServerInfoMam = (TextView) findViewById(R.id.server_info_mam);
484 this.mServerInfoCSI = (TextView) findViewById(R.id.server_info_csi);
485 this.mServerInfoBlocking = (TextView) findViewById(R.id.server_info_blocking);
486 this.mServerInfoSm = (TextView) findViewById(R.id.server_info_sm);
487 this.mServerInfoPep = (TextView) findViewById(R.id.server_info_pep);
488 this.mServerInfoHttpUpload = (TextView) findViewById(R.id.server_info_http_upload);
489 this.mPushRow = (TableRow) findViewById(R.id.push_row);
490 this.mServerInfoPush = (TextView) findViewById(R.id.server_info_push);
491 this.mOtrFingerprint = (TextView) findViewById(R.id.otr_fingerprint);
492 this.mOtrFingerprintBox = (RelativeLayout) findViewById(R.id.otr_fingerprint_box);
493 this.mOtrFingerprintToClipboardButton = (ImageButton) findViewById(R.id.action_copy_to_clipboard);
494 this.mAxolotlFingerprint = (TextView) findViewById(R.id.axolotl_fingerprint);
495 this.mAxolotlFingerprintBox = (RelativeLayout) findViewById(R.id.axolotl_fingerprint_box);
496 this.mAxolotlFingerprintToClipboardButton = (ImageButton) findViewById(R.id.action_copy_axolotl_to_clipboard);
497 this.mRegenerateAxolotlKeyButton = (ImageButton) findViewById(R.id.action_regenerate_axolotl_key);
498 this.mOwnFingerprintDesc = (TextView) findViewById(R.id.own_fingerprint_desc);
499 this.keysCard = (LinearLayout) findViewById(R.id.other_device_keys_card);
500 this.keys = (LinearLayout) findViewById(R.id.other_device_keys);
501 this.mNamePort = (LinearLayout) findViewById(R.id.name_port);
502 this.mHostname = (EditText) findViewById(R.id.hostname);
503 this.mHostname.addTextChangedListener(mTextWatcher);
504 this.mPort = (EditText) findViewById(R.id.port);
505 this.mPort.setText("5222");
506 this.mPort.addTextChangedListener(mTextWatcher);
507 this.mSaveButton = (Button) findViewById(R.id.save_button);
508 this.mCancelButton = (Button) findViewById(R.id.cancel_button);
509 this.mSaveButton.setOnClickListener(this.mSaveButtonClickListener);
510 this.mCancelButton.setOnClickListener(this.mCancelButtonClickListener);
511 this.mMoreTable = (TableLayout) findViewById(R.id.server_info_more);
512 if (savedInstanceState != null && savedInstanceState.getBoolean("showMoreTable")) {
513 changeMoreTableVisibility(true);
514 }
515 final OnCheckedChangeListener OnCheckedShowConfirmPassword = new OnCheckedChangeListener() {
516 @Override
517 public void onCheckedChanged(final CompoundButton buttonView,
518 final boolean isChecked) {
519 if (isChecked) {
520 mPasswordConfirm.setVisibility(View.VISIBLE);
521 } else {
522 mPasswordConfirm.setVisibility(View.GONE);
523 }
524 updateSaveButton();
525 }
526 };
527 this.mRegisterNew.setOnCheckedChangeListener(OnCheckedShowConfirmPassword);
528 if (Config.DISALLOW_REGISTRATION_IN_UI) {
529 this.mRegisterNew.setVisibility(View.GONE);
530 }
531 }
532
533 @Override
534 public boolean onCreateOptionsMenu(final Menu menu) {
535 super.onCreateOptionsMenu(menu);
536 getMenuInflater().inflate(R.menu.editaccount, menu);
537 final MenuItem showQrCode = menu.findItem(R.id.action_show_qr_code);
538 final MenuItem showBlocklist = menu.findItem(R.id.action_show_block_list);
539 final MenuItem showMoreInfo = menu.findItem(R.id.action_server_info_show_more);
540 final MenuItem changePassword = menu.findItem(R.id.action_change_password_on_server);
541 final MenuItem showPassword = menu.findItem(R.id.action_show_password);
542 final MenuItem clearDevices = menu.findItem(R.id.action_clear_devices);
543 final MenuItem renewCertificate = menu.findItem(R.id.action_renew_certificate);
544 final MenuItem mamPrefs = menu.findItem(R.id.action_mam_prefs);
545 final MenuItem changePresence = menu.findItem(R.id.action_change_presence);
546 renewCertificate.setVisible(mAccount != null && mAccount.getPrivateKeyAlias() != null);
547
548 if (mAccount != null && mAccount.isOnlineAndConnected()) {
549 if (!mAccount.getXmppConnection().getFeatures().blocking()) {
550 showBlocklist.setVisible(false);
551 }
552 if (!mAccount.getXmppConnection().getFeatures().register()) {
553 changePassword.setVisible(false);
554 }
555 mamPrefs.setVisible(mAccount.getXmppConnection().getFeatures().mam());
556 Set<Integer> otherDevices = mAccount.getAxolotlService().getOwnDeviceIds();
557 if (otherDevices == null || otherDevices.isEmpty() || !Config.supportOmemo()) {
558 clearDevices.setVisible(false);
559 }
560 changePresence.setVisible(manuallyChangePresence());
561 } else {
562 showQrCode.setVisible(false);
563 showBlocklist.setVisible(false);
564 showMoreInfo.setVisible(false);
565 changePassword.setVisible(false);
566 clearDevices.setVisible(false);
567 mamPrefs.setVisible(false);
568 changePresence.setVisible(false);
569 }
570
571 if (mAccount != null) {
572 showPassword.setVisible(mAccount.isOptionSet(Account.OPTION_MAGIC_CREATE)
573 && !mAccount.isOptionSet(Account.OPTION_REGISTER));
574 } else {
575 showPassword.setVisible(false);
576 }
577 return super.onCreateOptionsMenu(menu);
578 }
579
580 @Override
581 public boolean onPrepareOptionsMenu(Menu menu) {
582 final MenuItem showMoreInfo = menu.findItem(R.id.action_server_info_show_more);
583 if (showMoreInfo.isVisible()) {
584 showMoreInfo.setChecked(mMoreTable.getVisibility() == View.VISIBLE);
585 }
586 return super.onPrepareOptionsMenu(menu);
587 }
588
589 @Override
590 protected void onStart() {
591 super.onStart();
592 final int theme = findTheme();
593 if (this.mTheme != theme) {
594 recreate();
595 } else if (getIntent() != null) {
596 try {
597 this.jidToEdit = Jid.fromString(getIntent().getStringExtra("jid"));
598 } catch (final InvalidJidException | NullPointerException ignored) {
599 this.jidToEdit = null;
600 }
601 boolean init = getIntent().getBooleanExtra("init", false);
602 this.mInitMode = init || this.jidToEdit == null;
603 this.messageFingerprint = getIntent().getStringExtra("fingerprint");
604 if (!mInitMode) {
605 this.mRegisterNew.setVisibility(View.GONE);
606 if (getActionBar() != null) {
607 getActionBar().setTitle(getString(R.string.account_details));
608 }
609 } else {
610 this.mAvatar.setVisibility(View.GONE);
611 ActionBar ab = getActionBar();
612 if (ab != null) {
613 if (init && Config.MAGIC_CREATE_DOMAIN == null) {
614 ab.setDisplayShowHomeEnabled(false);
615 ab.setDisplayHomeAsUpEnabled(false);
616 }
617 ab.setTitle(R.string.action_add_account);
618 }
619 }
620 }
621 SharedPreferences preferences = getPreferences();
622 boolean useTor = Config.FORCE_ORBOT || preferences.getBoolean("use_tor", false);
623 this.mShowOptions = useTor || preferences.getBoolean("show_connection_options", false);
624 mHostname.setHint(useTor ? R.string.hostname_or_onion : R.string.hostname_example);
625 this.mNamePort.setVisibility(mShowOptions ? View.VISIBLE : View.GONE);
626 }
627
628 @Override
629 public void onSaveInstanceState(final Bundle savedInstanceState) {
630 if (mAccount != null) {
631 savedInstanceState.putString("account", mAccount.getJid().toBareJid().toString());
632 savedInstanceState.putBoolean("initMode", mInitMode);
633 savedInstanceState.putBoolean("showMoreTable", mMoreTable.getVisibility() == View.VISIBLE);
634 }
635 super.onSaveInstanceState(savedInstanceState);
636 }
637
638 @Override
639 protected void onBackendConnected() {
640 boolean init = true;
641 if (mSavedInstanceAccount != null) {
642 try {
643 this.mAccount = xmppConnectionService.findAccountByJid(Jid.fromString(mSavedInstanceAccount));
644 this.mInitMode = mSavedInstanceInit;
645 init = false;
646 } catch (InvalidJidException e) {
647 this.mAccount = null;
648 }
649
650 } else if (this.jidToEdit != null) {
651 this.mAccount = xmppConnectionService.findAccountByJid(jidToEdit);
652 }
653
654 if (mAccount != null) {
655 this.mInitMode |= this.mAccount.isOptionSet(Account.OPTION_REGISTER);
656 this.mUsernameMode |= mAccount.isOptionSet(Account.OPTION_MAGIC_CREATE) && mAccount.isOptionSet(Account.OPTION_REGISTER);
657 if (this.mAccount.getPrivateKeyAlias() != null) {
658 this.mPassword.setHint(R.string.authenticate_with_certificate);
659 if (this.mInitMode) {
660 this.mPassword.requestFocus();
661 }
662 }
663 updateAccountInformation(init);
664 }
665
666
667 if (Config.MAGIC_CREATE_DOMAIN == null && this.xmppConnectionService.getAccounts().size() == 0) {
668 this.mCancelButton.setEnabled(false);
669 this.mCancelButton.setTextColor(getSecondaryTextColor());
670 }
671 if (mUsernameMode) {
672 this.mAccountJidLabel.setText(R.string.username);
673 this.mAccountJid.setHint(R.string.username_hint);
674 } else {
675 final KnownHostsAdapter mKnownHostsAdapter = new KnownHostsAdapter(this,
676 R.layout.simple_list_item,
677 xmppConnectionService.getKnownHosts());
678 this.mAccountJid.setAdapter(mKnownHostsAdapter);
679 }
680 updateSaveButton();
681 invalidateOptionsMenu();
682 }
683
684 private String getUserModeDomain() {
685 if (mAccount != null) {
686 return mAccount.getJid().getDomainpart();
687 } else {
688 return Config.DOMAIN_LOCK;
689 }
690 }
691
692 @Override
693 public boolean onOptionsItemSelected(final MenuItem item) {
694 switch (item.getItemId()) {
695 case R.id.action_show_block_list:
696 final Intent showBlocklistIntent = new Intent(this, BlocklistActivity.class);
697 showBlocklistIntent.putExtra(EXTRA_ACCOUNT, mAccount.getJid().toString());
698 startActivity(showBlocklistIntent);
699 break;
700 case R.id.action_server_info_show_more:
701 changeMoreTableVisibility(!item.isChecked());
702 break;
703 case R.id.action_change_password_on_server:
704 gotoChangePassword(null);
705 break;
706 case R.id.action_mam_prefs:
707 editMamPrefs();
708 break;
709 case R.id.action_clear_devices:
710 showWipePepDialog();
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
888 if (copyTextToClipboard(ownAxolotlFingerprint.substring(2), R.string.omemo_fingerprint)) {
889 Toast.makeText(
890 EditAccountActivity.this,
891 R.string.toast_message_omemo_fingerprint,
892 Toast.LENGTH_SHORT).show();
893 }
894 }
895 });
896 if (Config.SHOW_REGENERATE_AXOLOTL_KEYS_BUTTON) {
897 this.mRegenerateAxolotlKeyButton
898 .setVisibility(View.VISIBLE);
899 this.mRegenerateAxolotlKeyButton
900 .setOnClickListener(new View.OnClickListener() {
901
902 @Override
903 public void onClick(final View v) {
904 showRegenerateAxolotlKeyDialog();
905 }
906 });
907 }
908 } else {
909 this.mAxolotlFingerprintBox.setVisibility(View.GONE);
910 }
911 boolean hasKeys = false;
912 keys.removeAllViews();
913 for (final String fingerprint : mAccount.getAxolotlService().getFingerprintsForOwnSessions()) {
914 if (ownAxolotlFingerprint.equals(fingerprint)) {
915 continue;
916 }
917 boolean highlight = fingerprint.equals(messageFingerprint);
918 hasKeys |= addFingerprintRow(keys, mAccount, fingerprint, highlight, null);
919 }
920 if (hasKeys && Config.supportOmemo()) {
921 keysCard.setVisibility(View.VISIBLE);
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}