aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/pixart/messenger/ui/MagicCreateActivity.java
blob: 76dc844993c1410508500749aae9654f8ac23709 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package de.pixart.messenger.ui;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import java.security.SecureRandom;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import de.pixart.messenger.Config;
import de.pixart.messenger.R;
import de.pixart.messenger.entities.Account;
import rocks.xmpp.addr.Jid;

public class MagicCreateActivity extends XmppActivity implements TextWatcher, AdapterView.OnItemSelectedListener {

    private TextView mFullJidDisplay;
    private EditText mUsername;
    private Spinner mServer;
    private SecureRandom mRandom;
    String domain = null;

    private static final String CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456780+-/#$!?";
    private static final int PW_LENGTH = 10;

    @Override
    protected void refreshUiReal() {

    }

    @Override
    void onBackendConnected() {

    }

    @Override
    public void onStart() {
        super.onStart();
        final int theme = findTheme();
        if (this.mTheme != theme) {
            recreate();
        }
    }

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        if (getResources().getBoolean(R.bool.portrait_only)) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_magic_create);
        setSupportActionBar(findViewById(R.id.toolbar));
        configureActionBar(getSupportActionBar());
        mFullJidDisplay = findViewById(R.id.full_jid);
        final List<String> domains = Arrays.asList(getResources().getStringArray(R.array.domains));
        Collections.sort(domains, String::compareToIgnoreCase);
        final ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_selectable_list_item, domains);
        mUsername = findViewById(R.id.username);
        mServer = findViewById(R.id.server);
        mServer.setAdapter(adapter);
        mServer.setOnItemSelectedListener(this);
        adapter.setDropDownViewResource(android.R.layout.select_dialog_singlechoice);
        mRandom = new SecureRandom();
        Button next = findViewById(R.id.create_account);
        next.setOnClickListener(v -> {
            try {
                String username = mUsername.getText().toString();
                if (domain == null) {
                    domain = Config.MAGIC_CREATE_DOMAIN;
                }
                Jid jid = Jid.of(username.toLowerCase(), domain, null);
                if (!jid.getEscapedLocal().equals(jid.getLocal()) || username.length() < 3) {
                    mUsername.setError(getString(R.string.invalid_username));
                    mUsername.requestFocus();
                } else {
                    mUsername.setError(null);
                    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);
                    intent.putExtra("jid", account.getJid().asBareJid().toString());
                    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_LONG).show();
                    WelcomeActivity.addInviteUri(intent, getIntent());
                    startActivity(intent);
                    overridePendingTransition(R.animator.fade_in, R.animator.fade_out);
                    finish();
                    overridePendingTransition(R.animator.fade_in, R.animator.fade_out);
                }
            } catch (IllegalArgumentException e) {
                mUsername.setError(getString(R.string.invalid_username));
                mUsername.requestFocus();
            }
        });
        mUsername.addTextChangedListener(this);
    }

    private String createPassword() {
        StringBuilder builder = new StringBuilder(PW_LENGTH);
        for (int i = 0; i < PW_LENGTH; ++i) {
            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) {
        generateJID(s.toString());
    }

    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
        generateJID(mUsername.getText().toString());
    }

    @Override
    public void onNothingSelected(AdapterView<?> adapterView) {
        generateJID(mUsername.getText().toString());
    }

    private void generateJID (String s) {
        domain = mServer.getSelectedItem().toString();
        if (s.trim().length() > 0) {
            try {
                mFullJidDisplay.setVisibility(View.VISIBLE);
                if (domain == null) {
                    domain = Config.MAGIC_CREATE_DOMAIN;
                }
                Jid jid = Jid.of(s.toLowerCase(), domain, null);
                mFullJidDisplay.setText(getString(R.string.your_full_jid_will_be, jid.toEscapedString()));
            } catch (IllegalArgumentException e) {
                mFullJidDisplay.setVisibility(View.INVISIBLE);
            }

        } else {
            mFullJidDisplay.setVisibility(View.INVISIBLE);
        }
    }
}