diff --git a/res/values/strings.xml b/res/values/strings.xml index cf7086d294244a89a007bc0b7155be01eca38dc9..820b61eea89565c2f58df5342ef65e995552abea 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -49,11 +49,13 @@ <string name="settings_enable">Enable</string> <string name="settings_label_subscriptions">I2P Addressbook</string> <string name="settings_desc_subscriptions">Subscription URLs</string> - <string name="settings_label_bandwidth">Bandwidth and network</string> + <string name="settings_label_bandwidth_net">Bandwidth and network</string> + <string name="settings_label_bandwidth">Bandwidth</string> <string name="settings_label_bw_inbound">Inbound speed</string> <string name="settings_desc_bw_inbound">Maximum inbound speed</string> <string name="settings_label_bw_outbound">Outbound speed</string> <string name="settings_desc_bw_outbound">Maximum outbound speed</string> + <string name="settings_label_network">Network</string> <string name="settings_label_hiddenMode">Participation</string> <string name="settings_desc_hiddenMode">Turn off hidden mode</string> <string name="settings_label_maxPartTunnels">Max participating tunnels</string> diff --git a/res/xml/settings1.xml b/res/xml/settings_advanced.xml similarity index 68% rename from res/xml/settings1.xml rename to res/xml/settings_advanced.xml index a6dd932bc0e2c64bd7c8e06158de4b6e987121e5..6091a296e6de632a0b2c3244575d5d96af853fbb 100644 --- a/res/xml/settings1.xml +++ b/res/xml/settings_advanced.xml @@ -1,98 +1,5 @@ -<PreferenceScreen - xmlns:android="http://schemas.android.com/apk/res/android" - android:key="settings1"> - - <Preference - android:title="@string/settings_label_subscriptions" - android:summary="@string/settings_desc_subscriptions" - android:key="addressbook_preferences"> - - <intent android:targetPackage="net.i2p.android.router" android:targetClass="net.i2p.android.router.activity.AddressbookSettingsActivity" /> - - </Preference> - - <!-- - Keys and the default values from current installation: - --> - - <PreferenceCategory - android:key="settings_bandwidth" - android:title="@string/settings_label_bandwidth"> - <!-- - i2np.bandwidth.inboundKBytesPerSecond=100 - --> - <com.hlidskialf.android.preference.SeekBarPreference - android:key="i2np.bandwidth.inboundKBytesPerSecond" - android:defaultValue="100" - android:max="1000" - android:title="@string/settings_label_bw_inbound" - android:summary="@string/settings_desc_bw_inbound" - android:dialogMessage="Maximum inbound speed" - android:text=" KBytes/sec" - /> - <!-- - i2np.bandwidth.outboundKBytesPerSecond=30 - --> - <com.hlidskialf.android.preference.SeekBarPreference - android:key="i2np.bandwidth.outboundKBytesPerSecond" - android:defaultValue="100" - android:max="1000" - android:title="@string/settings_label_bw_outbound" - android:summary="@string/settings_desc_bw_outbound" - android:dialogMessage="Maximum outbound speed" - android:text=" KBytes/sec" - /> - <CheckBoxPreference - android:key="router.hiddenMode" - android:defaultValue="false" - android:title="@string/settings_label_hiddenMode" - android:summary="@string/settings_desc_hiddenMode" - /> - <!-- - router.maxParticipatingTunnels=0 - --> - <com.hlidskialf.android.preference.SeekBarPreference - android:key="router.maxParticipatingTunnels" - android:defaultValue="20" - android:max="500" - android:title="@string/settings_label_maxPartTunnels" - android:summary="@string/settings_desc_maxPartTunnels" - android:dialogMessage="Maximum tunnels to participate in" - android:text=" Tunnels" - android:dependency="router.hiddenMode" - /> - <!-- - router.sharePercentage=10 - --> - <com.hlidskialf.android.preference.SeekBarPreference - android:key="router.sharePercentage" - android:defaultValue="80" - android:max="100" - android:title="@string/settings_label_sharePercent" - android:summary="@string/settings_desc_sharePercent" - android:dialogMessage="Percentage of bandwidth to share" - android:text="%" - android:dependency="router.hiddenMode" - android:shouldDisableView="true" - /> - - <!-- - i2np.upnp.enable=false - --> - <CheckBoxPreference - android:key="i2np.upnp.enable" - android:defaultValue="true" - android:title="UPnP" - android:summary="@string/settings_desc_upnp" - /> - </PreferenceCategory> - - - - <PreferenceCategory - android:key="settings_advanced" - android:title="@string/settings_label_advanced"> - +<?xml version="1.0" encoding="utf-8"?> +<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> <PreferenceScreen android:key="settings_transports" android:title="@string/settings_label_transports"> @@ -280,7 +187,6 @@ /> </PreferenceCategory> </PreferenceScreen> - </PreferenceCategory> <!-- diff --git a/res/xml/settings_headers.xml b/res/xml/settings_headers.xml new file mode 100644 index 0000000000000000000000000000000000000000..95f3c57df56cd1c149e99ceca32c4fd58e9c81c2 --- /dev/null +++ b/res/xml/settings_headers.xml @@ -0,0 +1,23 @@ +<?xml version="1.0" encoding="utf-8"?> +<preference-headers xmlns:android="http://schemas.android.com/apk/res/android"> + <header + android:fragment="net.i2p.android.router.activity.SettingsActivity$SettingsFragment" + android:title="@string/settings_label_bandwidth_net"> + <extra + android:name="settings" + android:value="net" /> + </header> + <header + android:title="@string/settings_label_subscriptions"> + <intent + android:targetPackage="net.i2p.android.router" + android:targetClass="net.i2p.android.router.activity.AddressbookSettingsActivity" /> + </header> + <header + android:fragment="net.i2p.android.router.activity.SettingsActivity$SettingsFragment" + android:title="@string/settings_label_advanced"> + <extra + android:name="settings" + android:value="advanced" /> + </header> +</preference-headers> \ No newline at end of file diff --git a/res/xml/settings_headers_legacy.xml b/res/xml/settings_headers_legacy.xml new file mode 100644 index 0000000000000000000000000000000000000000..86cb2d8a6825a9d18c974c14ac65a69369dda50a --- /dev/null +++ b/res/xml/settings_headers_legacy.xml @@ -0,0 +1,20 @@ +<?xml version="1.0" encoding="utf-8"?> +<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" > + <Preference android:title="@string/settings_label_bandwidth_net"> + <intent + android:targetPackage="net.i2p.android.router" + android:targetClass="net.i2p.android.router.activity.SettingsActivity" + android:action="net.i2p.android.router.PREFS_NET" /> + </Preference> + <Preference android:title="@string/settings_label_subscriptions"> + <intent + android:targetPackage="net.i2p.android.router" + android:targetClass="net.i2p.android.router.activity.AddressbookSettingsActivity" /> + </Preference> + <Preference android:title="@string/settings_label_advanced"> + <intent + android:targetPackage="net.i2p.android.router" + android:targetClass="net.i2p.android.router.activity.SettingsActivity" + android:action="net.i2p.android.router.PREFS_ADVANCED" /> + </Preference> +</PreferenceScreen> diff --git a/res/xml/settings_net.xml b/res/xml/settings_net.xml new file mode 100644 index 0000000000000000000000000000000000000000..76f6698da79020e447fe5fc1004b22003a73cdc7 --- /dev/null +++ b/res/xml/settings_net.xml @@ -0,0 +1,84 @@ +<?xml version="1.0" encoding="utf-8"?> +<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" > + <!-- + Keys and the default values from current installation: + --> + + <PreferenceCategory + android:key="settings_bandwidth" + android:title="@string/settings_label_bandwidth"> + <!-- + i2np.bandwidth.inboundKBytesPerSecond=100 + --> + <com.hlidskialf.android.preference.SeekBarPreference + android:key="i2np.bandwidth.inboundKBytesPerSecond" + android:defaultValue="100" + android:max="1000" + android:title="@string/settings_label_bw_inbound" + android:summary="@string/settings_desc_bw_inbound" + android:dialogMessage="Maximum inbound speed" + android:text=" KBytes/sec" + /> + <!-- + i2np.bandwidth.outboundKBytesPerSecond=30 + --> + <com.hlidskialf.android.preference.SeekBarPreference + android:key="i2np.bandwidth.outboundKBytesPerSecond" + android:defaultValue="100" + android:max="1000" + android:title="@string/settings_label_bw_outbound" + android:summary="@string/settings_desc_bw_outbound" + android:dialogMessage="Maximum outbound speed" + android:text=" KBytes/sec" + /> + </PreferenceCategory> + + <PreferenceCategory + android:key="settings_network" + android:title="@string/settings_label_network"> + <CheckBoxPreference + android:key="router.hiddenMode" + android:defaultValue="false" + android:title="@string/settings_label_hiddenMode" + android:summary="@string/settings_desc_hiddenMode" + /> + <!-- + router.maxParticipatingTunnels=0 + --> + <com.hlidskialf.android.preference.SeekBarPreference + android:key="router.maxParticipatingTunnels" + android:defaultValue="20" + android:max="500" + android:title="@string/settings_label_maxPartTunnels" + android:summary="@string/settings_desc_maxPartTunnels" + android:dialogMessage="Maximum tunnels to participate in" + android:text=" Tunnels" + android:dependency="router.hiddenMode" + /> + <!-- + router.sharePercentage=10 + --> + <com.hlidskialf.android.preference.SeekBarPreference + android:key="router.sharePercentage" + android:defaultValue="80" + android:max="100" + android:title="@string/settings_label_sharePercent" + android:summary="@string/settings_desc_sharePercent" + android:dialogMessage="Percentage of bandwidth to share" + android:text="%" + android:dependency="router.hiddenMode" + android:shouldDisableView="true" + /> + + <!-- + i2np.upnp.enable=false + --> + <CheckBoxPreference + android:key="i2np.upnp.enable" + android:defaultValue="true" + android:title="UPnP" + android:summary="@string/settings_desc_upnp" + /> + </PreferenceCategory> + +</PreferenceScreen> diff --git a/src/net/i2p/android/router/activity/SettingsActivity.java b/src/net/i2p/android/router/activity/SettingsActivity.java index 11f575fcc81b335d3370b7c77370c13c6db46a93..10d6649f9adcb4ca6e071c11ad3d66bad9c9bcb1 100644 --- a/src/net/i2p/android/router/activity/SettingsActivity.java +++ b/src/net/i2p/android/router/activity/SettingsActivity.java @@ -1,9 +1,12 @@ package net.i2p.android.router.activity; +import android.annotation.TargetApi; import android.content.SharedPreferences; +import android.os.Build; import android.os.Bundle; import android.preference.Preference; import android.preference.PreferenceActivity; +import android.preference.PreferenceFragment; import android.preference.PreferenceManager; import java.util.Iterator; import java.util.List; @@ -15,16 +18,35 @@ import net.i2p.router.RouterContext; import net.i2p.util.OrderedProperties; public class SettingsActivity extends PreferenceActivity { + // Actions for legacy settings + private static final String ACTION_PREFS_NET = "net.i2p.android.router.PREFS_NET"; + private static final String ACTION_PREFS_ADVANCED = "net.i2p.android.router.PREFS_ADVANCED"; + @SuppressWarnings("deprecation") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - addPreferencesFromResource(R.xml.settings1); + + String action = getIntent().getAction(); + if (action != null && action.equals(ACTION_PREFS_NET)) { + addPreferencesFromResource(R.xml.settings_net); + } else if (action != null && action.equals(ACTION_PREFS_ADVANCED)) { + addPreferencesFromResource(R.xml.settings_advanced); + } else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) { + // Load the legacy preferences headers + addPreferencesFromResource(R.xml.settings_headers_legacy); + } } + @TargetApi(Build.VERSION_CODES.HONEYCOMB) + @Override + public void onBuildHeaders(List<Header> target) { + loadHeadersFromResource(R.xml.settings_headers, target); + } @Override protected void onPause() { + // TODO: Rewrite this code to fix default setting and reduce duplication // Copy prefs Properties props = new OrderedProperties(); @@ -48,6 +70,8 @@ public class SettingsActivity extends PreferenceActivity { } else if(! x.startsWith("DO_NOT_SAVE")) { // Disabled? Preference findPreference = findPreference(x); + if (findPreference == null) + continue; if ( findPreference.isEnabled() ) { String string = all.get(x).toString(); props.setProperty(x, string); @@ -81,6 +105,74 @@ public class SettingsActivity extends PreferenceActivity { super.onSaveInstanceState(outState); } + @TargetApi(Build.VERSION_CODES.HONEYCOMB) + public static class SettingsFragment extends PreferenceFragment { + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + String settings = getArguments().getString("settings"); + if ("net".equals(settings)) { + addPreferencesFromResource(R.xml.settings_net); + } else if ("advanced".equals(settings)) { + addPreferencesFromResource(R.xml.settings_advanced); + } + } + + @Override + public void onPause() { + // TODO: Rewrite this code to fix default setting and reduce duplication + // Copy prefs + Properties props = new OrderedProperties(); + + SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity()); + Map<String, ?> all = preferences.getAll(); + Iterator<String> iterator = all.keySet().iterator(); + // get values from the Map and make them strings. + // This loop avoids needing to convert each one, or even know it's type, or if it exists yet. + while (iterator.hasNext()) { + String x = iterator.next(); + // special exception, we must invert the bool for this property only. + if(x.equals("router.hiddenMode")) { + Preference findPreference = findPreference(x); + String string = all.get(x).toString(); + String what="true"; + if(string.equals(what)) { + what="false"; + } + props.setProperty(x, what); + } else if(! x.startsWith("DO_NOT_SAVE")) { + // Disabled? + Preference findPreference = findPreference(x); + if (findPreference == null) + continue; + if ( findPreference.isEnabled() ) { + String string = all.get(x).toString(); + props.setProperty(x, string); + } else { + String summary[] = findPreference.getSummary().toString().split("default="); + String defaultval = summary[summary.length - 1].trim(); + if (defaultval.endsWith(")")) { + // strip the ")" off the tail end, this is the default value! + String string = defaultval.substring(0, defaultval.length() - 1); + Util.i("Resetting property '" + x + "' to default '" + string +"'"); + props.setProperty(x, string); + } + } + } + } + // Merge in new config settings, write the file. + InitActivities init = new InitActivities(getActivity()); + init.mergeResourceToFile(R.raw.router_config, "router.config", props); + // Apply new config if we are running. + List<RouterContext> contexts = RouterContext.listContexts(); + if ( !((contexts == null) || (contexts.isEmpty())) ) { + RouterContext _context = contexts.get(0); + _context.router().saveConfig(props, null); + } + super.onPause(); + } + } }