2016-04-19 16:03:24 +00:00
|
|
|
package eu.siacs.conversations.ui;
|
|
|
|
|
|
|
|
import android.content.Intent;
|
2016-08-26 14:05:38 +00:00
|
|
|
import android.content.pm.ActivityInfo;
|
2016-04-19 16:03:24 +00:00
|
|
|
import android.os.Bundle;
|
|
|
|
import android.text.Editable;
|
|
|
|
import android.text.TextWatcher;
|
|
|
|
import android.view.View;
|
|
|
|
import android.widget.Button;
|
|
|
|
import android.widget.EditText;
|
|
|
|
import android.widget.TextView;
|
2016-04-26 21:23:48 +00:00
|
|
|
import android.widget.Toast;
|
2016-04-19 16:03:24 +00:00
|
|
|
|
|
|
|
import java.security.SecureRandom;
|
|
|
|
|
|
|
|
import eu.siacs.conversations.Config;
|
|
|
|
import eu.siacs.conversations.R;
|
|
|
|
import eu.siacs.conversations.entities.Account;
|
2018-03-05 17:30:40 +00:00
|
|
|
import rocks.xmpp.addr.Jid;
|
2016-04-19 16:03:24 +00:00
|
|
|
|
|
|
|
public class MagicCreateActivity extends XmppActivity implements TextWatcher {
|
|
|
|
|
|
|
|
private TextView mFullJidDisplay;
|
|
|
|
private EditText mUsername;
|
|
|
|
private SecureRandom mRandom;
|
|
|
|
|
|
|
|
private static final String CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456780+-/#$!?";
|
|
|
|
private static final int PW_LENGTH = 10;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void refreshUiReal() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
void onBackendConnected() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-06-30 19:22:35 +00:00
|
|
|
@Override
|
|
|
|
public void onStart() {
|
|
|
|
super.onStart();
|
|
|
|
final int theme = findTheme();
|
|
|
|
if (this.mTheme != theme) {
|
|
|
|
recreate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-19 16:03:24 +00:00
|
|
|
@Override
|
|
|
|
protected void onCreate(final Bundle savedInstanceState) {
|
2016-08-26 14:05:38 +00:00
|
|
|
if (getResources().getBoolean(R.bool.portrait_only)) {
|
|
|
|
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
|
|
|
|
}
|
2016-04-19 16:03:24 +00:00
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
setContentView(R.layout.magic_create);
|
2018-03-18 08:30:22 +00:00
|
|
|
setSupportActionBar(findViewById(R.id.toolbar));
|
|
|
|
configureActionBar(getSupportActionBar());
|
2017-12-16 19:38:14 +00:00
|
|
|
mFullJidDisplay = findViewById(R.id.full_jid);
|
|
|
|
mUsername = findViewById(R.id.username);
|
2016-04-19 16:03:24 +00:00
|
|
|
mRandom = new SecureRandom();
|
2017-12-16 19:38:14 +00:00
|
|
|
Button next = findViewById(R.id.create_account);
|
|
|
|
next.setOnClickListener(v -> {
|
2018-03-11 17:32:16 +00:00
|
|
|
try {
|
|
|
|
String username = mUsername.getText().toString();
|
|
|
|
Jid jid = Jid.of(username.toLowerCase(), Config.MAGIC_CREATE_DOMAIN, null);
|
|
|
|
if (!jid.getEscapedLocal().equals(jid.getLocal())|| username.length() < 3) {
|
|
|
|
mUsername.setError(getString(R.string.invalid_username));
|
|
|
|
mUsername.requestFocus();
|
|
|
|
} else {
|
|
|
|
mUsername.setError(null);
|
2017-12-16 19:38:14 +00:00
|
|
|
Account account = xmppConnectionService.findAccountByJid(jid);
|
|
|
|
if (account == null) {
|
|
|
|
account = new Account(jid, createPassword());
|
|
|
|
account.setOption(Account.OPTION_REGISTER, true);
|
|
|
|
account.setOption(Account.OPTION_DISABLED, true);
|
|
|
|
account.setOption(Account.OPTION_MAGIC_CREATE, true);
|
|
|
|
xmppConnectionService.createAccount(account);
|
|
|
|
}
|
|
|
|
Intent intent = new Intent(MagicCreateActivity.this, EditAccountActivity.class);
|
2018-03-05 17:30:40 +00:00
|
|
|
intent.putExtra("jid", account.getJid().asBareJid().toString());
|
2017-12-16 19:38:14 +00:00
|
|
|
intent.putExtra("init", true);
|
|
|
|
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
|
|
|
|
Toast.makeText(MagicCreateActivity.this, R.string.secure_password_generated, Toast.LENGTH_SHORT).show();
|
2018-02-14 15:55:45 +00:00
|
|
|
WelcomeActivity.addInviteUri(intent, getIntent());
|
2017-12-16 19:38:14 +00:00
|
|
|
startActivity(intent);
|
2016-04-19 16:03:24 +00:00
|
|
|
}
|
2018-03-11 17:32:16 +00:00
|
|
|
} catch (IllegalArgumentException e) {
|
|
|
|
mUsername.setError(getString(R.string.invalid_username));
|
|
|
|
mUsername.requestFocus();
|
2016-04-19 16:03:24 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
mUsername.addTextChangedListener(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
private String createPassword() {
|
|
|
|
StringBuilder builder = new StringBuilder(PW_LENGTH);
|
2017-12-16 19:38:14 +00:00
|
|
|
for (int i = 0; i < PW_LENGTH; ++i) {
|
2016-04-19 16:03:24 +00:00
|
|
|
builder.append(CHARS.charAt(mRandom.nextInt(CHARS.length() - 1)));
|
|
|
|
}
|
|
|
|
return builder.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onTextChanged(CharSequence s, int start, int before, int count) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void afterTextChanged(Editable s) {
|
|
|
|
if (s.toString().trim().length() > 0) {
|
|
|
|
try {
|
|
|
|
mFullJidDisplay.setVisibility(View.VISIBLE);
|
2018-03-05 17:30:40 +00:00
|
|
|
Jid jid = Jid.of(s.toString().toLowerCase(), Config.MAGIC_CREATE_DOMAIN, null);
|
2018-03-11 17:32:16 +00:00
|
|
|
mFullJidDisplay.setText(getString(R.string.your_full_jid_will_be, jid.toEscapedString()));
|
2018-03-05 17:30:40 +00:00
|
|
|
} catch (IllegalArgumentException e) {
|
2016-04-19 16:03:24 +00:00
|
|
|
mFullJidDisplay.setVisibility(View.INVISIBLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
mFullJidDisplay.setVisibility(View.INVISIBLE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|