I2P Address: [http://git.idk.i2p]

Skip to content
Snippets Groups Projects
Commit 37da05ca authored by str4d's avatar str4d
Browse files

Pull in changes to defaults while maintaining user settings

parent f003bbbf
No related branches found
No related tags found
No related merge requests found
...@@ -3,15 +3,18 @@ package net.i2p.android.router; ...@@ -3,15 +3,18 @@ package net.i2p.android.router;
import android.content.Context; import android.content.Context;
import android.content.res.Resources; import android.content.res.Resources;
import android.os.Build; import android.os.Build;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.List;
import java.util.Properties; import java.util.Properties;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream; import java.util.zip.ZipInputStream;
import net.i2p.android.router.R; import net.i2p.android.router.R;
import net.i2p.android.router.util.Util; import net.i2p.android.router.util.Util;
import net.i2p.data.DataHelper; import net.i2p.data.DataHelper;
...@@ -61,19 +64,24 @@ class InitActivities { ...@@ -61,19 +64,24 @@ class InitActivities {
void initialize() { void initialize() {
if (checkNewVersion()) { if (checkNewVersion()) {
Properties props = new Properties(); List<Properties> lProps = Util.getPropertiesFromPreferences(ctx);
Properties props = lProps.get(0);
props.setProperty("i2p.dir.temp", myDir + "/tmp"); props.setProperty("i2p.dir.temp", myDir + "/tmp");
props.setProperty("i2p.dir.pid", myDir + "/tmp"); props.setProperty("i2p.dir.pid", myDir + "/tmp");
// Time disabled in default router.config // Time disabled in default router.config
// But lots of time problems on Android, not all carriers support NITZ // But lots of time problems on Android, not all carriers support NITZ
// and there was no NTP before 3.0. Tablets should be fine? // and there was no NTP before 3.0. Tablets should be fine?
// Phones in airplane mode with wifi enabled still a problem. // Phones in airplane mode with wifi enabled still a problem.
// Deactivated phones in airplane mode definatly won't have correct time. // Deactivated phones in airplane mode definitely won't have correct time.
if (Build.VERSION.SDK_INT < 11) // Honeycomb 3.0 if (Build.VERSION.SDK_INT < 11) // Honeycomb 3.0
props.setProperty("time.disabled", "false"); props.setProperty("time.disabled", "false");
mergeResourceToFile(R.raw.router_config, "router.config", props); mergeResourceToFile(R.raw.router_config, "router.config", props);
mergeResourceToFile(R.raw.logger_config, "logger.config", null); mergeResourceToFile(R.raw.logger_config, "logger.config", lProps.get(1));
mergeResourceToFile(R.raw.i2ptunnel_config, "i2ptunnel.config", null); // This is not needed for now, i2ptunnel.config only contains tunnel
// settings, which can now be configured manually. We don't want to
// overwrite the user's tunnels.
//mergeResourceToFile(R.raw.i2ptunnel_config, "i2ptunnel.config", null);
// FIXME this is a memory hog to merge this way // FIXME this is a memory hog to merge this way
mergeResourceToFile(R.raw.hosts_txt, "hosts.txt", null); mergeResourceToFile(R.raw.hosts_txt, "hosts.txt", null);
mergeResourceToFile(R.raw.more_hosts_txt, "hosts.txt", null); mergeResourceToFile(R.raw.more_hosts_txt, "hosts.txt", null);
...@@ -212,10 +220,8 @@ class InitActivities { ...@@ -212,10 +220,8 @@ class InitActivities {
/** /**
* Load defaults from resource, * Load defaults from resource,
* then add props from file, * then add props from settings,
* and write back * and write back
* For now, do it backwards so we can override with new apks.
* When we have user configurable stuff, switch it back.
* *
* @param f relative to base dir * @param f relative to base dir
* @param props local overrides or null * @param props local overrides or null
...@@ -224,13 +230,9 @@ class InitActivities { ...@@ -224,13 +230,9 @@ class InitActivities {
InputStream in = null; InputStream in = null;
InputStream fin = null; InputStream fin = null;
byte buf[] = new byte[4096];
try { try {
in = ctx.getResources().openRawResource(resID); in = ctx.getResources().openRawResource(resID);
Properties props = new OrderedProperties(); Properties props = new OrderedProperties();
// keep user settings
//DataHelper.loadProps(props, in);
try { try {
fin = new FileInputStream(new File(myDir, f)); fin = new FileInputStream(new File(myDir, f));
DataHelper.loadProps(props, fin); DataHelper.loadProps(props, fin);
...@@ -239,9 +241,10 @@ class InitActivities { ...@@ -239,9 +241,10 @@ class InitActivities {
Util.d("Creating file " + f + " from resource"); Util.d("Creating file " + f + " from resource");
} }
// override user settings // write in default settings
DataHelper.loadProps(props, in); DataHelper.loadProps(props, in);
// override with user settings
if (overrides != null) if (overrides != null)
props.putAll(overrides); props.putAll(overrides);
File path = new File(myDir, f); File path = new File(myDir, f);
......
...@@ -172,59 +172,10 @@ public class SettingsActivity extends PreferenceActivity { ...@@ -172,59 +172,10 @@ public class SettingsActivity extends PreferenceActivity {
@Override @Override
protected void onPause() { protected void onPause() {
// TODO: Rewrite this code to fix default setting List<Properties> lProps = Util.getPropertiesFromPreferences(this);
// Copy prefs Properties props = lProps.get(0);
Properties props = new OrderedProperties(); Properties logSettings = lProps.get(1);
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
// List to store stats for graphing
List<String> statSummaries = new ArrayList<String>();
// List to store Log settings
Map<String, String> logSettings = new HashMap<String, String>();
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();
if ( x.startsWith("i2pandroid.")) // Skip over UI-related I2P Android settings
continue;
else if ( x.startsWith("stat.summaries.")) {
String stat = x.substring("stat.summaries.".length());
String checked = all.get(x).toString();
if (checked.equals("true")) {
statSummaries.add(stat);
}
} else if ( x.startsWith("logger.")) {
logSettings.put(x, all.get(x).toString());
} else if (
x.equals("router.hiddenMode") ||
x.equals("i2cp.disableInterface")) {
// special exception, we must invert the bool for these properties only.
String string = all.get(x).toString();
String what="true";
if(string.equals(what)) {
what="false";
}
props.setProperty(x, what);
} else {
String string = all.get(x).toString();
props.setProperty(x, string);
}
}
if (statSummaries.isEmpty()) {
props.setProperty("stat.summaries", "");
} else {
Iterator<String> iter = statSummaries.iterator();
StringBuilder buf = new StringBuilder(iter.next());
while (iter.hasNext()) {
buf.append(",").append(iter.next());
}
props.setProperty("stat.summaries", buf.toString());
}
// Apply new config if we are running. // Apply new config if we are running.
List<RouterContext> contexts = RouterContext.listContexts(); List<RouterContext> contexts = RouterContext.listContexts();
if ( !((contexts == null) || (contexts.isEmpty())) ) { if ( !((contexts == null) || (contexts.isEmpty())) ) {
...@@ -246,12 +197,12 @@ public class SettingsActivity extends PreferenceActivity { ...@@ -246,12 +197,12 @@ public class SettingsActivity extends PreferenceActivity {
super.onPause(); super.onPause();
} }
private void saveLoggingChanges(I2PAppContext ctx, Map<String, String> logSettings) { private void saveLoggingChanges(I2PAppContext ctx, Properties logSettings) {
boolean shouldSave = false; boolean shouldSave = false;
for (String key : logSettings.keySet()) { for (Object key : logSettings.keySet()) {
if ("logger.defaultLevel".equals(key)) { if ("logger.defaultLevel".equals(key)) {
String defaultLevel = logSettings.get(key); String defaultLevel = (String) logSettings.get(key);
String oldDefault = ctx.logManager().getDefaultLimit(); String oldDefault = ctx.logManager().getDefaultLimit();
if (!defaultLevel.equals(oldDefault)) { if (!defaultLevel.equals(oldDefault)) {
shouldSave = true; shouldSave = true;
......
package net.i2p.android.router.util; package net.i2p.android.router.util;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo; import android.content.pm.PackageInfo;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.net.ConnectivityManager; import android.net.ConnectivityManager;
import android.net.NetworkInfo; import android.net.NetworkInfo;
import android.os.Build; import android.os.Build;
import android.preference.PreferenceManager;
import net.i2p.I2PAppContext; import net.i2p.I2PAppContext;
import net.i2p.util.OrderedProperties;
public abstract class Util { public abstract class Util {
private static final boolean _isEmulator = Build.MODEL.equals("sdk"); private static final boolean _isEmulator = Build.MODEL.equals("sdk");
...@@ -108,4 +118,66 @@ public abstract class Util { ...@@ -108,4 +118,66 @@ public abstract class Util {
android.util.Log.d(ANDROID_TAG, m); android.util.Log.d(ANDROID_TAG, m);
} }
} }
public static List<Properties> getPropertiesFromPreferences(Context context) {
List<Properties> pList = new ArrayList<Properties>();
// Copy prefs
Properties routerProps = new OrderedProperties();
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
// List to store stats for graphing
List<String> statSummaries = new ArrayList<String>();
// List to store Log settings
Properties logSettings = new OrderedProperties();
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();
if ( x.startsWith("i2pandroid.")) // Skip over UI-related I2P Android settings
continue;
else if ( x.startsWith("stat.summaries.")) {
String stat = x.substring("stat.summaries.".length());
String checked = all.get(x).toString();
if (checked.equals("true")) {
statSummaries.add(stat);
}
} else if ( x.startsWith("logger.")) {
logSettings.put(x, all.get(x).toString());
} else if (
x.equals("router.hiddenMode") ||
x.equals("i2cp.disableInterface")) {
// special exception, we must invert the bool for these properties only.
String string = all.get(x).toString();
String what="true";
if(string.equals(what)) {
what="false";
}
routerProps.setProperty(x, what);
} else {
String string = all.get(x).toString();
routerProps.setProperty(x, string);
}
}
if (statSummaries.isEmpty()) {
routerProps.setProperty("stat.summaries", "");
} else {
Iterator<String> iter = statSummaries.iterator();
StringBuilder buf = new StringBuilder(iter.next());
while (iter.hasNext()) {
buf.append(",").append(iter.next());
}
routerProps.setProperty("stat.summaries", buf.toString());
}
pList.add(routerProps);
pList.add(logSettings);
return pList;
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment