1package eu.siacs.conversations.ui;
2
3import android.content.Intent;
4import android.os.Bundle;
5import android.text.Editable;
6import android.text.TextWatcher;
7import android.view.View;
8import android.widget.Button;
9import android.widget.EditText;
10import android.widget.TextView;
11import android.widget.Toast;
12
13import java.security.SecureRandom;
14
15import eu.siacs.conversations.Config;
16import eu.siacs.conversations.R;
17import eu.siacs.conversations.entities.Account;
18import eu.siacs.conversations.xmpp.jid.InvalidJidException;
19import eu.siacs.conversations.xmpp.jid.Jid;
20
21public class MagicCreateActivity extends XmppActivity implements TextWatcher {
22
23 private TextView mFullJidDisplay;
24 private EditText mUsername;
25 private SecureRandom mRandom;
26
27 private static final String CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456780+-/#$!?";
28 private static final int PW_LENGTH = 10;
29
30 @Override
31 protected void refreshUiReal() {
32
33 }
34
35 @Override
36 void onBackendConnected() {
37
38 }
39
40 @Override
41 protected void onCreate(final Bundle savedInstanceState) {
42 super.onCreate(savedInstanceState);
43 setContentView(R.layout.magic_create);
44 mFullJidDisplay = (TextView) findViewById(R.id.full_jid);
45 mUsername = (EditText) findViewById(R.id.username);
46 mRandom = new SecureRandom();
47 Button next = (Button) findViewById(R.id.create_account);
48 next.setOnClickListener(new View.OnClickListener() {
49 @Override
50 public void onClick(View v) {
51 String username = mUsername.getText().toString();
52 if (username.contains("@") || username.length() < 3) {
53 mUsername.setError(getString(R.string.invalid_username));
54 mUsername.requestFocus();
55 } else {
56 mUsername.setError(null);
57 try {
58 Jid jid = Jid.fromParts(username.toLowerCase(), Config.MAGIC_CREATE_DOMAIN, null);
59 Account account = xmppConnectionService.findAccountByJid(jid);
60 if (account == null) {
61 account = new Account(jid, createPassword());
62 account.setOption(Account.OPTION_REGISTER, true);
63 account.setOption(Account.OPTION_DISABLED, true);
64 account.setOption(Account.OPTION_MAGIC_CREATE, true);
65 xmppConnectionService.createAccount(account);
66 }
67 Intent intent = new Intent(MagicCreateActivity.this, EditAccountActivity.class);
68 intent.putExtra("jid", account.getJid().toBareJid().toString());
69 intent.putExtra("init", true);
70 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
71 Toast.makeText(MagicCreateActivity.this, R.string.secure_password_generated, Toast.LENGTH_SHORT).show();
72 startActivity(intent);
73 } catch (InvalidJidException e) {
74 mUsername.setError(getString(R.string.invalid_username));
75 mUsername.requestFocus();
76 }
77 }
78 }
79 });
80 mUsername.addTextChangedListener(this);
81 }
82
83 private String createPassword() {
84 StringBuilder builder = new StringBuilder(PW_LENGTH);
85 for(int i = 0; i < PW_LENGTH; ++i) {
86 builder.append(CHARS.charAt(mRandom.nextInt(CHARS.length() - 1)));
87 }
88 return builder.toString();
89 }
90
91 @Override
92 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
93
94 }
95
96 @Override
97 public void onTextChanged(CharSequence s, int start, int before, int count) {
98
99 }
100
101 @Override
102 public void afterTextChanged(Editable s) {
103 if (s.toString().trim().length() > 0) {
104 try {
105 mFullJidDisplay.setVisibility(View.VISIBLE);
106 Jid jid = Jid.fromParts(s.toString().toLowerCase(), Config.MAGIC_CREATE_DOMAIN, null);
107 mFullJidDisplay.setText(getString(R.string.your_full_jid_will_be, jid.toString()));
108 } catch (InvalidJidException e) {
109 mFullJidDisplay.setVisibility(View.INVISIBLE);
110 }
111
112 } else {
113 mFullJidDisplay.setVisibility(View.INVISIBLE);
114 }
115 }
116}