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
|
/*
* Copyright (c) 2012-2014 Arne Schwabe
* Distributed under the GNU GPL v2. For full terms see the file doc/LICENSE.txt
*/
package de.blinkt.openvpn.fragments;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.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.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RadioGroup;
import android.widget.Switch;
import java.util.Arrays;
import de.blinkt.openvpn.R;
import de.blinkt.openvpn.VpnProfile;
import de.blinkt.openvpn.core.Connection;
/**
* Created by arne on 30.10.14.
*/
public class ConnectionsAdapter extends RecyclerView.Adapter<ConnectionsAdapter.ConnectionsHolder> {
private final Context mContext;
private final VpnProfile mProfile;
private final Settings_Connections mConnectionFragment;
private Connection[] mConnections;
public ConnectionsAdapter(Context c, Settings_Connections connections_fragments, VpnProfile vpnProfile)
{
mContext = c;
mConnections = vpnProfile.mConnections;
mProfile = vpnProfile;
mConnectionFragment = connections_fragments;
}
public static 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;
public ConnectionsHolder(View card) {
super(card);
mServerNameView = (EditText) card.findViewById(R.id.servername);
mPortNumberView = (EditText) card.findViewById(R.id.portnumber);
mRemoteSwitch = (Switch) card.findViewById (R.id.remoteSwitch);
mCustomOptionCB = (CheckBox) card.findViewById(R.id.use_customoptions);
mCustomOptionText = (EditText) card.findViewById(R.id.customoptions);
mProtoGroup = (RadioGroup) card.findViewById(R.id.udptcpradiogroup);
mCustomOptionsLayout = card.findViewById(R.id.custom_options_layout);
mDeleteButton = (ImageButton) card.findViewById(R.id.remove_connection);
}
}
@Override
public ConnectionsAdapter.ConnectionsHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
LayoutInflater li = LayoutInflater.from(mContext);
View card = li.inflate(R.layout.server_card, viewGroup, false);
return new ConnectionsHolder(card);
}
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) {
}
}
@Override
public void onBindViewHolder(final ConnectionsAdapter.ConnectionsHolder cH, final int i) {
final Connection connection = mConnections[i];
cH.mPortNumberView.setText(connection.mServerPort);
cH.mServerNameView.setText(connection.mServerName);
cH.mPortNumberView.setText(connection.mServerPort);
cH.mRemoteSwitch.setChecked(connection.mEnabled);
cH.mRemoteSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
connection.mEnabled = isChecked;
displayWarningifNoneEnabled();
}
});
cH.mProtoGroup.check(connection.mUseUdp ? R.id.udp_proto : R.id.tcp_proto);
cH.mProtoGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId == R.id.udp_proto)
connection.mUseUdp=true;
else if (checkedId == R.id.tcp_proto)
connection.mUseUdp=false;
}
});
cH.mCustomOptionsLayout.setVisibility(connection.mUseCustomConfig ? View.VISIBLE : View.GONE);
cH.mCustomOptionText.setText(connection.mCustomConfiguration);
cH.mCustomOptionCB.setChecked(connection.mUseCustomConfig);
cH.mCustomOptionCB.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
connection.mUseCustomConfig = isChecked;
cH.mCustomOptionsLayout.setVisibility(connection.mUseCustomConfig ? View.VISIBLE : View.GONE);
notifyItemChanged(i);
}
});
cH.mDeleteButton.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View 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, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
removeRemote(i);
notifyItemRemoved(i);
}
});
ab.create().show();
}
}
);
cH.mServerNameView.addTextChangedListener(new OnTextChangedWatcher() {
@Override
public void afterTextChanged(Editable s) {
connection.mServerName = s.toString();
}
});
cH.mPortNumberView.addTextChangedListener(new OnTextChangedWatcher() {
@Override
public void afterTextChanged(Editable s) {
connection.mServerPort = s.toString();
}
});
cH.mCustomOptionText.addTextChangedListener(new OnTextChangedWatcher() {
@Override
public void afterTextChanged(Editable s) {
connection.mCustomConfiguration = s.toString();
}
});
}
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;
}
public void displayWarningifNoneEnabled()
{
int showWarning = View.VISIBLE;
for(Connection conn:mConnections) {
if(conn.mEnabled)
showWarning= View.GONE;
}
mConnectionFragment.setWarningVisible(showWarning);
}
public void addRemote() {
mConnections = Arrays.copyOf(mConnections, mConnections.length+1);
mConnections[mConnections.length-1] = new Connection();
notifyItemInserted(mConnections.length-1);
displayWarningifNoneEnabled();
}
public void saveProfile() {
mProfile.mConnections= mConnections;
}
}
|