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