Skip to content

Commit d7f6e36

Browse files
committed
UDP and NTCP ports, part 4
1 parent 5ef434e commit d7f6e36

File tree

3 files changed

+56
-165
lines changed

3 files changed

+56
-165
lines changed

app/src/main/java/net/i2p/android/router/SettingsActivity.java

Lines changed: 54 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -63,18 +63,21 @@ protected void onCreate(Bundle savedInstanceState) {
6363
addPreferencesFromResource(R.xml.settings_advanced);
6464
setupAdvancedSettings(this, getPreferenceScreen());
6565
}
66-
} else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
67-
// Load the legacy preferences headers
68-
addPreferencesFromResource(R.xml.settings_headers_legacy);
66+
} else {
6967
// Load any properties that the router might have changed on us.
70-
setupPreferences(this, getPreferenceManager(), Util.getRouterContext());
68+
setupPreferences(this, Util.getRouterContext());
69+
70+
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
71+
// Load the legacy preferences headers
72+
addPreferencesFromResource(R.xml.settings_headers_legacy);
73+
}
7174
}
7275

7376
mToolbar.setTitle(getTitle());
7477
}
7578

76-
protected static void setupPreferences(Context context, PreferenceManager mgr, RouterContext ctx) {
77-
if (mgr != null && ctx != null) {
79+
protected static void setupPreferences(Context context, RouterContext ctx) {
80+
if (ctx != null) {
7881
final String udpPortKey = context.getString(R.string.PROP_UDP_INTERNAL_PORT);
7982
final String ntcpPortKey = context.getString(R.string.PROP_I2NP_NTCP_PORT);
8083
final String ntcpAutoPortKey = context.getString(R.string.PROP_I2NP_NTCP_AUTO_PORT);
@@ -85,10 +88,16 @@ protected static void setupPreferences(Context context, PreferenceManager mgr, R
8588
if (ntcpPort < 0 && ntcpAutoPort)
8689
ntcpPort = udpPort;
8790

88-
SharedPreferences.Editor prefs = mgr.getSharedPreferences().edit();
89-
prefs.putInt(udpPortKey, udpPort);
90-
prefs.putInt(ntcpPortKey, ntcpPort);
91-
prefs.apply();
91+
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
92+
if (prefs.getInt(udpPortKey, -1) != udpPort ||
93+
prefs.getInt(ntcpPortKey, -1) != ntcpPort) {
94+
SharedPreferences.Editor editor = prefs.edit();
95+
editor.putInt(udpPortKey, udpPort);
96+
editor.putInt(ntcpPortKey, ntcpPort);
97+
// commit() instead of apply() because this needs to happen
98+
// before SettingsActivity loads its Preferences.
99+
editor.commit();
100+
}
92101
}
93102
}
94103

@@ -180,22 +189,42 @@ protected static void setupLoggingSettings(Context context, PreferenceScreen ps,
180189
}
181190
}
182191

183-
protected static void setupAdvancedSettings(Context context, PreferenceScreen ps) {
192+
protected static void setupAdvancedSettings(final Context context, PreferenceScreen ps) {
193+
final String udpEnableKey = context.getString(R.string.PROP_ENABLE_UDP);
194+
final String ntcpEnableKey = context.getString(R.string.PROP_ENABLE_NTCP);
184195
final String udpPortKey = context.getString(R.string.PROP_UDP_INTERNAL_PORT);
185196
final String ntcpPortKey = context.getString(R.string.PROP_I2NP_NTCP_PORT);
186197
final String ntcpAutoPortKey = context.getString(R.string.PROP_I2NP_NTCP_AUTO_PORT);
187198

199+
final CheckBoxPreference udpEnable = (CheckBoxPreference) ps.findPreference(udpEnableKey);
200+
final CheckBoxPreference ntcpEnable = (CheckBoxPreference) ps.findPreference(ntcpEnableKey);
188201
final PortPreference udpPort = (PortPreference) ps.findPreference(udpPortKey);
189202
final PortPreference ntcpPort = (PortPreference) ps.findPreference(ntcpPortKey);
190203
final CheckBoxPreference ntcpAutoPort = (CheckBoxPreference) ps.findPreference(ntcpAutoPortKey);
191204

192-
ntcpAutoPort.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
205+
udpEnable.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
193206
@Override
194207
public boolean onPreferenceChange(Preference preference, Object newValue) {
195208
final Boolean checked = (Boolean) newValue;
196-
if (checked)
197-
ntcpPort.setText(udpPort.getText());
198-
return true;
209+
if (checked || ntcpEnable.isChecked())
210+
return true;
211+
else {
212+
Toast.makeText(context, R.string.settings_need_transport_enabled, Toast.LENGTH_LONG).show();
213+
return false;
214+
}
215+
}
216+
});
217+
218+
ntcpEnable.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
219+
@Override
220+
public boolean onPreferenceChange(Preference preference, Object newValue) {
221+
final Boolean checked = (Boolean) newValue;
222+
if (checked || udpEnable.isChecked())
223+
return true;
224+
else {
225+
Toast.makeText(context, R.string.settings_need_transport_enabled, Toast.LENGTH_LONG).show();
226+
return false;
227+
}
199228
}
200229
});
201230

@@ -207,6 +236,16 @@ public boolean onPreferenceChange(Preference preference, Object newValue) {
207236
return true;
208237
}
209238
});
239+
240+
ntcpAutoPort.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
241+
@Override
242+
public boolean onPreferenceChange(Preference preference, Object newValue) {
243+
final Boolean checked = (Boolean) newValue;
244+
if (checked)
245+
ntcpPort.setText(udpPort.getText());
246+
return true;
247+
}
248+
});
210249
}
211250

212251
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@@ -217,8 +256,6 @@ public void onBuildHeaders(List<Header> target) {
217256
// be true for -sw720dp devices, false otherwise. For your curiosity, in
218257
// Nexus 7 it is false.
219258
loadHeadersFromResource(R.xml.settings_headers, target);
220-
// Load any properties that the router might have changed on us.
221-
setupPreferences(this, getPreferenceManager(), Util.getRouterContext());
222259
}
223260

224261
@Override

app/src/main/java/net/i2p/android/router/service/RouterService.java

Lines changed: 1 addition & 148 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,8 @@
1919
import net.i2p.router.Router;
2020
import net.i2p.router.RouterContext;
2121
import net.i2p.router.RouterLaunch;
22-
import net.i2p.util.OrderedProperties;
2322

24-
import java.io.File;
25-
import java.io.FileInputStream;
26-
import java.io.IOException;
27-
import java.io.InputStream;
2823
import java.text.DecimalFormat;
29-
import java.util.Properties;
30-
import java.util.Random;
3124

3225
/**
3326
* Runs the router
@@ -175,147 +168,7 @@ public void run() {
175168
//NativeBigInteger.main(null);
176169
//Util.d(MARKER + this + " JBigI speed test finished, launching router");
177170

178-
179-
// Before we launch, fix up any settings that need to be fixed here.
180-
// This should be done in the core, but as of this writing it isn't!
181-
182-
// Step one. Load the propertites.
183-
Properties props = new OrderedProperties();
184-
Properties oldprops = new OrderedProperties();
185-
String wrapName = _myDir + "/router.config";
186-
try {
187-
InputStream fin = new FileInputStream(new File(wrapName));
188-
DataHelper.loadProps(props, fin);
189-
} catch(IOException ioe) {
190-
// shouldn't happen...
191-
}
192-
oldprops.putAll(props);
193-
// Step two, check for any port settings, and copy for those that are missing.
194-
int UDPinbound;
195-
int UDPinlocal;
196-
int TCPinbound;
197-
int TCPinlocal;
198-
UDPinbound = Integer.parseInt(props.getProperty("i2np.udp.port", "-1"));
199-
UDPinlocal = Integer.parseInt(props.getProperty("i2np.udp.internalPort", "-1"));
200-
TCPinbound = Integer.parseInt(props.getProperty("i2np.ntcp.port", "-1"));
201-
TCPinlocal = Integer.parseInt(props.getProperty("i2np.ntcp.internalPort", "-1"));
202-
boolean hasUDPinbound = UDPinbound != -1;
203-
boolean hasUDPinlocal = UDPinlocal != -1;
204-
boolean hasTCPinbound = TCPinbound != -1;
205-
boolean hasTCPinlocal = TCPinlocal != -1;
206-
207-
// check and clear values based on these:
208-
boolean udp = Boolean.parseBoolean(props.getProperty("i2np.udp.enable", "false"));
209-
boolean tcp = Boolean.parseBoolean(props.getProperty("i2np.ntcp.enable", "false"));
210-
211-
// Fix if both are false.
212-
if(!(udp || tcp)) {
213-
// If both are not on, turn them both on.
214-
props.setProperty("i2np.udp.enable", "true");
215-
props.setProperty("i2np.ntcp.enable", "true");
216-
}
217-
218-
// Fix if we have local but no inbound
219-
if(!hasUDPinbound && hasUDPinlocal) {
220-
// if we got a local port and no external port, set it
221-
hasUDPinbound = true;
222-
UDPinbound = UDPinlocal;
223-
}
224-
if(!hasTCPinbound && hasTCPinlocal) {
225-
// if we got a local port and no external port, set it
226-
hasTCPinbound = true;
227-
TCPinbound = TCPinlocal;
228-
}
229-
230-
boolean anyUDP = hasUDPinbound || hasUDPinlocal;
231-
boolean anyTCP = hasTCPinbound || hasTCPinlocal;
232-
boolean anyport = anyUDP || anyTCP;
233-
234-
if(!anyport) {
235-
// generate one for UDPinbound, and fall thru.
236-
// FIX ME: Possibly not the best but should be OK.
237-
Random generator = new Random(System.currentTimeMillis());
238-
UDPinbound = generator.nextInt(55500) + 10000;
239-
anyUDP = true;
240-
}
241-
242-
// Copy missing port numbers
243-
if(anyUDP && !anyTCP) {
244-
TCPinbound = UDPinbound;
245-
TCPinlocal = UDPinlocal;
246-
}
247-
if(anyTCP && !anyUDP) {
248-
UDPinbound = TCPinbound;
249-
UDPinlocal = TCPinlocal;
250-
}
251-
// reset for a retest.
252-
hasUDPinbound = UDPinbound != -1;
253-
hasUDPinlocal = UDPinlocal != -1;
254-
hasTCPinbound = TCPinbound != -1;
255-
hasTCPinlocal = TCPinlocal != -1;
256-
anyUDP = hasUDPinbound || hasUDPinlocal;
257-
anyTCP = hasTCPinbound || hasTCPinlocal;
258-
boolean checkAnyUDP = anyUDP && udp;
259-
boolean checkAnyTCP = anyTCP && tcp;
260-
261-
// Enable things that need to be enabled.
262-
// Disable anything that needs to be disabled.
263-
if(!checkAnyUDP && !checkAnyTCP) {
264-
// enable the one(s) with values.
265-
if(anyUDP) {
266-
udp = true;
267-
}
268-
if(anyTCP) {
269-
tcp = true;
270-
}
271-
}
272-
273-
if(!udp) {
274-
props.setProperty("i2np.udp.enable", "false");
275-
props.remove("i2np.udp.port");
276-
props.remove("i2np.udp.internalPort");
277-
} else {
278-
props.setProperty("i2np.udp.enable", "true");
279-
if(hasUDPinbound) {
280-
props.setProperty("i2np.udp.port", Integer.toString(UDPinbound));
281-
} else {
282-
props.remove("i2np.udp.port");
283-
}
284-
if(hasUDPinlocal) {
285-
props.setProperty("i2np.udp.internalPort", Integer.toString(UDPinlocal));
286-
} else {
287-
props.remove("i2np.udp.internalPort");
288-
}
289-
}
290-
291-
if(!tcp) {
292-
props.setProperty("i2np.ntcp.enable", "false");
293-
props.remove("i2np.ntcp.port");
294-
props.remove("i2np.ntcp.internalPort");
295-
} else {
296-
props.setProperty("i2np.ntcp.enable", "true");
297-
if(hasTCPinbound) {
298-
props.setProperty("i2np.ntcp.port", Integer.toString(TCPinbound));
299-
} else {
300-
props.remove("i2np.ntcp.port");
301-
}
302-
if(hasTCPinlocal) {
303-
props.setProperty("i2np.ntcp.internalPort", Integer.toString(TCPinlocal));
304-
} else {
305-
props.remove("i2np.ntcp.internalPort");
306-
}
307-
}
308-
// WHEW! Now test for any changes.
309-
if(!props.equals(oldprops)) {
310-
// save fixed properties.
311-
try {
312-
DataHelper.storeProps(props, new File(wrapName));
313-
} catch(IOException ioe) {
314-
// shouldn't happen...
315-
}
316-
}
317-
318-
// _NOW_ launch the router!
171+
// Launch the router!
319172
RouterLaunch.main(null);
320173
synchronized(_stateLock) {
321174
if(_state != State.STARTING) {

app/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@
126126
<string name="settings_summ_expl_backupQuantity">%s tunnels</string>
127127
<string name="settings_desc_expl_backupQuantity">How many tunnel backups</string>
128128

129+
<string name="settings_need_transport_enabled">You must have at least one transport enabled</string>
129130
<string name="settings_router_restart_required">Please restart I2P to apply the changes</string>
130131

131132
<string name="menu_about">About</string>

0 commit comments

Comments
 (0)