summaryrefslogtreecommitdiff
path: root/main/src/ui/java/de/blinkt/openvpn/fragments/ConnectionsAdapter.java
blob: 0e36a133b8f8d33cc16ce01c161f7a8755d32d63 (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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/*
 * Copyright (c) 2012-2016 Arne Schwabe
 * Distributed under the GNU GPL v2 with additional terms. For full terms see the file doc/LICENSE.txt
 */

package de.blinkt.openvpn.fragments;

import android.app.AlertDialog;
import android.content.Context;
import androidx.recyclerview.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;

import java.util.Arrays;

import de.blinkt.openvpn.R;
import de.blinkt.openvpn.VpnProfile;
import de.blinkt.openvpn.core.Connection;

public class ConnectionsAdapter extends RecyclerView.Adapter<ConnectionsAdapter.ConnectionsHolder> {
    private static final int TYPE_NORMAL = 0;
    private static final int TYPE_FOOTER = TYPE_NORMAL + 1;
    private final Context mContext;
    private final VpnProfile mProfile;
    private final Settings_Connections mConnectionFragment;
    private Connection[] mConnections;

    ConnectionsAdapter(Context c, Settings_Connections connections_fragments, VpnProfile vpnProfile) {
        mContext = c;
        mConnections = vpnProfile.mConnections;
        mProfile = vpnProfile;
        mConnectionFragment = connections_fragments;
    }

    @Override
    public ConnectionsAdapter.ConnectionsHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        LayoutInflater li = LayoutInflater.from(mContext);

        View card;
        if (viewType == TYPE_NORMAL) {
            card = li.inflate(R.layout.server_card, viewGroup, false);

        } else { // TYPE_FOOTER
            card = li.inflate(R.layout.server_footer, viewGroup, false);
        }
        return new ConnectionsHolder(card, this, viewType);

    }

    @Override
    public void onBindViewHolder(final ConnectionsAdapter.ConnectionsHolder cH, int position) {
        if (position == mConnections.length) {
            // Footer
            return;
        }
        final Connection connection = mConnections[position];

        cH.mConnection = null;

        cH.mPortNumberView.setText(connection.mServerPort);
        cH.mServerNameView.setText(connection.mServerName);
        cH.mPortNumberView.setText(connection.mServerPort);
        cH.mRemoteSwitch.setChecked(connection.mEnabled);


        cH.mProxyNameView.setText(connection.mProxyName);
        cH.mProxyPortNumberView.setText(connection.mProxyPort);

        cH.mConnectText.setText(String.valueOf(connection.getTimeout()));

        cH.mConnectSlider.setProgress(connection.getTimeout());


        cH.mProtoGroup.check(connection.mUseUdp ? R.id.udp_proto : R.id.tcp_proto);

        switch (connection.mProxyType) {
            case NONE:
                cH.mProxyGroup.check(R.id.proxy_none);
                break;
            case HTTP:
                cH.mProxyGroup.check(R.id.proxy_http);
                break;
            case SOCKS5:
                cH.mProxyGroup.check(R.id.proxy_socks);
                break;
            case ORBOT:
                cH.mProxyGroup.check(R.id.proxy_orbot);
                break;
        }

        cH.mProxyAuthCb.setChecked(connection.mUseProxyAuth);
        cH.mProxyAuthUser.setText(connection.mProxyAuthUser);
        cH.mProxyAuthPassword.setText(connection.mProxyAuthPassword);

        cH.mCustomOptionsLayout.setVisibility(connection.mUseCustomConfig ? View.VISIBLE : View.GONE);
        cH.mCustomOptionText.setText(connection.mCustomConfiguration);

        cH.mCustomOptionCB.setChecked(connection.mUseCustomConfig);
        cH.mConnection = connection;

        setVisibilityProxyServer(cH, connection);

    }

    private void setVisibilityProxyServer(ConnectionsHolder cH, Connection connection) {
        int visible = (connection.mProxyType == Connection.ProxyType.HTTP || connection.mProxyType == Connection.ProxyType.SOCKS5) ? View.VISIBLE : View.GONE;
        int authVisible = (connection.mProxyType == Connection.ProxyType.HTTP) ? View.VISIBLE : View.GONE;

        cH.mProxyNameView.setVisibility(visible);
        cH.mProxyPortNumberView.setVisibility(visible);
        cH.mProxyPortNameView.setVisibility(visible);
        cH.mProxyNameLabel.setVisibility(visible);

        cH.mProxyAuthLayout.setVisibility(authVisible);


    }

    private void removeRemote(int idx) {
        Connection[] mConnections2 = Arrays.copyOf(mConnections, mConnections.length - 1);
        for (int i = idx + 1; i < mConnections.length; i++) {
            mConnections2[i - 1] = mConnections[i];
        }
        mConnections = mConnections2;

    }

    @Override
    public int getItemCount() {
        return mConnections.length + 1; //for footer
    }

    @Override
    public int getItemViewType(int position) {
        if (position == mConnections.length)
            return TYPE_FOOTER;
        else
            return TYPE_NORMAL;
    }

    void addRemote() {
        mConnections = Arrays.copyOf(mConnections, mConnections.length + 1);
        mConnections[mConnections.length - 1] = new Connection();
        notifyItemInserted(mConnections.length - 1);
        displayWarningIfNoneEnabled();
    }

    void displayWarningIfNoneEnabled() {
        int showWarning = View.VISIBLE;
        for (Connection conn : mConnections) {
            if (conn.mEnabled)
                showWarning = View.GONE;
        }
        mConnectionFragment.setWarningVisible(showWarning);
    }

    void saveProfile() {
        mProfile.mConnections = mConnections;
    }

    static abstract class OnTextChangedWatcher implements TextWatcher {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }
    }

    class ConnectionsHolder extends RecyclerView.ViewHolder {
        private final EditText mServerNameView;
        private final EditText mPortNumberView;
        private final Switch mRemoteSwitch;
        private final RadioGroup mProtoGroup;
        private final EditText mCustomOptionText;
        private final CheckBox mCustomOptionCB;
        private final View mCustomOptionsLayout;
        private final ImageButton mDeleteButton;
        private final EditText mConnectText;
        private final SeekBar mConnectSlider;
        private final ConnectionsAdapter mConnectionsAdapter;
        private final RadioGroup mProxyGroup;
        private final EditText mProxyNameView;
        private final EditText mProxyPortNumberView;
        private final View mProxyPortNameView;
        private final View mProxyNameLabel;
        private final View mProxyAuthLayout;
        private final EditText mProxyAuthUser;
        private final EditText mProxyAuthPassword;
        private final CheckBox mProxyAuthCb;

        protected Connection mConnection; // Set to null on update


        ConnectionsHolder(View card, ConnectionsAdapter connectionsAdapter, int viewType) {
            super(card);
            mServerNameView = card.findViewById(R.id.servername);
            mPortNumberView = card.findViewById(R.id.portnumber);
            mRemoteSwitch = card.findViewById(R.id.remoteSwitch);
            mCustomOptionCB = card.findViewById(R.id.use_customoptions);
            mCustomOptionText = card.findViewById(R.id.customoptions);
            mProtoGroup = card.findViewById(R.id.udptcpradiogroup);
            mCustomOptionsLayout = card.findViewById(R.id.custom_options_layout);
            mDeleteButton = card.findViewById(R.id.remove_connection);
            mConnectSlider = card.findViewById(R.id.connect_silder);
            mConnectText = card.findViewById(R.id.connect_timeout);

            mProxyGroup = card.findViewById(R.id.proxyradiogroup);
            mProxyNameView = card.findViewById(R.id.proxyname);
            mProxyPortNumberView = card.findViewById(R.id.proxyport);
            mProxyPortNameView = card.findViewById(R.id.proxyport_label);
            mProxyNameLabel = card.findViewById(R.id.proxyserver_label);

            mProxyAuthLayout = card.findViewById(R.id.proxyauthlayout);
            mProxyAuthCb = card.findViewById(R.id.enable_proxy_auth);
            mProxyAuthUser = card.findViewById(R.id.proxyuser);
            mProxyAuthPassword = card.findViewById(R.id.proxypassword);

            mConnectionsAdapter = connectionsAdapter;

            if (viewType == TYPE_NORMAL)
                addListeners();
        }


        void addListeners() {
            mRemoteSwitch.setOnCheckedChangeListener((CompoundButton buttonView, boolean isChecked) -> {
                if (mConnection != null) {
                    mConnection.mEnabled = isChecked;
                    mConnectionsAdapter.displayWarningIfNoneEnabled();
                }
            });

            mProtoGroup.setOnCheckedChangeListener((group, checkedId) -> {
                if (mConnection != null) {
                    if (checkedId == R.id.udp_proto)
                        mConnection.mUseUdp = true;
                    else if (checkedId == R.id.tcp_proto)
                        mConnection.mUseUdp = false;
                }
            });

            mProxyGroup.setOnCheckedChangeListener((group, checkedId) -> {
                if (mConnection != null) {
                    switch (checkedId) {
                        case R.id.proxy_none:
                            mConnection.mProxyType = Connection.ProxyType.NONE;
                            break;
                        case R.id.proxy_http:
                            mConnection.mProxyType = Connection.ProxyType.HTTP;
                            break;
                        case R.id.proxy_socks:
                            mConnection.mProxyType = Connection.ProxyType.SOCKS5;
                            break;
                        case R.id.proxy_orbot:
                            mConnection.mProxyType = Connection.ProxyType.ORBOT;
                            break;
                    }
                    setVisibilityProxyServer(this, mConnection);
                }
            });

            mProxyAuthCb.setOnCheckedChangeListener((group, isChecked) ->
            {
                if (mConnection != null) {
                    mConnection.mUseProxyAuth = isChecked;
                    setVisibilityProxyServer(this, mConnection);
                }
            });

            mCustomOptionText.addTextChangedListener(new OnTextChangedWatcher() {
                @Override
                public void afterTextChanged(Editable s) {
                    if (mConnection != null)
                        mConnection.mCustomConfiguration = s.toString();
                }
            });


            mCustomOptionCB.setOnCheckedChangeListener((buttonView, isChecked) -> {
                if (mConnection != null) {
                    mConnection.mUseCustomConfig = isChecked;
                    mCustomOptionsLayout.setVisibility(mConnection.mUseCustomConfig ? View.VISIBLE : View.GONE);
                }
            });


            mServerNameView.addTextChangedListener(new OnTextChangedWatcher() {
                @Override
                public void afterTextChanged(Editable s) {
                    if (mConnection != null) {
                        mConnection.mServerName = s.toString();
                    }
                }

            });

            mPortNumberView.addTextChangedListener(new OnTextChangedWatcher() {
                @Override
                public void afterTextChanged(Editable s) {
                    if (mConnection != null) {
                        mConnection.mServerPort = s.toString();
                    }
                }
            });

            mProxyNameView.addTextChangedListener(new OnTextChangedWatcher() {
                @Override
                public void afterTextChanged(Editable s) {
                    if (mConnection != null) {
                        mConnection.mProxyName = s.toString();
                    }
                }

            });

            mProxyPortNumberView.addTextChangedListener(new OnTextChangedWatcher() {
                @Override
                public void afterTextChanged(Editable s) {
                    if (mConnection != null) {
                        mConnection.mProxyPort = s.toString();
                    }
                }
            });

            mProxyAuthPassword.addTextChangedListener(new OnTextChangedWatcher() {
                @Override
                public void afterTextChanged(Editable s) {
                    if (mConnection != null) {
                        mConnection.mProxyAuthPassword = s.toString();
                    }
                }
            });


            mProxyAuthUser.addTextChangedListener(new OnTextChangedWatcher() {
                @Override
                public void afterTextChanged(Editable s) {
                    if (mConnection != null) {
                        mConnection.mProxyAuthUser = s.toString();
                    }
                }
            });



            mCustomOptionText.addTextChangedListener(new OnTextChangedWatcher() {
                @Override
                public void afterTextChanged(Editable s) {
                    if (mConnection != null) {
                        mConnection.mCustomConfiguration = s.toString();
                    }
                }
            });

            mConnectSlider.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
                @Override
                public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                    if (fromUser && mConnection != null) {
                        mConnectText.setText(String.valueOf(progress));
                        mConnection.mConnectTimeout = progress;
                    }
                }

                @Override
                public void onStartTrackingTouch(SeekBar seekBar) {

                }

                @Override
                public void onStopTrackingTouch(SeekBar seekBar) {

                }
            });
            mConnectText.addTextChangedListener(new OnTextChangedWatcher() {
                @Override
                public void afterTextChanged(Editable s) {
                    if (mConnection != null) {
                        try {
                            int t = Integer.valueOf(String.valueOf(s));
                            mConnectSlider.setProgress(t);
                            mConnection.mConnectTimeout = t;
                        } catch (Exception ignored) {
                        }
                    }
                }
            });

            mDeleteButton.setOnClickListener(
                    v -> {
                        AlertDialog.Builder ab = new AlertDialog.Builder(mContext);
                        ab.setTitle(R.string.query_delete_remote);
                        ab.setPositiveButton(R.string.keep, null);
                        ab.setNegativeButton(R.string.delete, (dialog, which) -> {
                            removeRemote(getAdapterPosition());
                            notifyItemRemoved(getAdapterPosition());
                        });
                        ab.create().show();
                    }
            );


        }
    }
}