From 37da05ca9818ed22c9f3949f6c1290eae6b40ea8 Mon Sep 17 00:00:00 2001 From: str4d <str4d@mail.i2p> Date: Sun, 1 Jun 2014 02:50:15 +0000 Subject: [PATCH] Pull in changes to defaults while maintaining user settings --- .../i2p/android/router/InitActivities.java | 27 +++---- .../i2p/android/router/SettingsActivity.java | 61 ++-------------- src/net/i2p/android/router/util/Util.java | 72 +++++++++++++++++++ 3 files changed, 93 insertions(+), 67 deletions(-) diff --git a/src/net/i2p/android/router/InitActivities.java b/src/net/i2p/android/router/InitActivities.java index e00886c7f..d3531cd73 100644 --- a/src/net/i2p/android/router/InitActivities.java +++ b/src/net/i2p/android/router/InitActivities.java @@ -3,15 +3,18 @@ package net.i2p.android.router; import android.content.Context; import android.content.res.Resources; import android.os.Build; + import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; +import java.util.List; import java.util.Properties; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; + import net.i2p.android.router.R; import net.i2p.android.router.util.Util; import net.i2p.data.DataHelper; @@ -61,19 +64,24 @@ class InitActivities { void initialize() { 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.pid", myDir + "/tmp"); // Time disabled in default router.config // But lots of time problems on Android, not all carriers support NITZ // and there was no NTP before 3.0. Tablets should be fine? // 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 props.setProperty("time.disabled", "false"); mergeResourceToFile(R.raw.router_config, "router.config", props); - mergeResourceToFile(R.raw.logger_config, "logger.config", null); - mergeResourceToFile(R.raw.i2ptunnel_config, "i2ptunnel.config", null); + mergeResourceToFile(R.raw.logger_config, "logger.config", lProps.get(1)); + // 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 mergeResourceToFile(R.raw.hosts_txt, "hosts.txt", null); mergeResourceToFile(R.raw.more_hosts_txt, "hosts.txt", null); @@ -212,10 +220,8 @@ class InitActivities { /** * Load defaults from resource, - * then add props from file, + * then add props from settings, * 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 props local overrides or null @@ -224,13 +230,9 @@ class InitActivities { InputStream in = null; InputStream fin = null; - byte buf[] = new byte[4096]; try { in = ctx.getResources().openRawResource(resID); Properties props = new OrderedProperties(); - // keep user settings - //DataHelper.loadProps(props, in); - try { fin = new FileInputStream(new File(myDir, f)); DataHelper.loadProps(props, fin); @@ -239,9 +241,10 @@ class InitActivities { Util.d("Creating file " + f + " from resource"); } - // override user settings + // write in default settings DataHelper.loadProps(props, in); + // override with user settings if (overrides != null) props.putAll(overrides); File path = new File(myDir, f); diff --git a/src/net/i2p/android/router/SettingsActivity.java b/src/net/i2p/android/router/SettingsActivity.java index 1189b4154..9ce24aa10 100644 --- a/src/net/i2p/android/router/SettingsActivity.java +++ b/src/net/i2p/android/router/SettingsActivity.java @@ -172,59 +172,10 @@ public class SettingsActivity extends PreferenceActivity { @Override protected void onPause() { - // TODO: Rewrite this code to fix default setting - // Copy prefs - Properties props = new OrderedProperties(); + List<Properties> lProps = Util.getPropertiesFromPreferences(this); + Properties props = lProps.get(0); + 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. List<RouterContext> contexts = RouterContext.listContexts(); if ( !((contexts == null) || (contexts.isEmpty())) ) { @@ -246,12 +197,12 @@ public class SettingsActivity extends PreferenceActivity { super.onPause(); } - private void saveLoggingChanges(I2PAppContext ctx, Map<String, String> logSettings) { + private void saveLoggingChanges(I2PAppContext ctx, Properties logSettings) { boolean shouldSave = false; - for (String key : logSettings.keySet()) { + for (Object key : logSettings.keySet()) { if ("logger.defaultLevel".equals(key)) { - String defaultLevel = logSettings.get(key); + String defaultLevel = (String) logSettings.get(key); String oldDefault = ctx.logManager().getDefaultLimit(); if (!defaultLevel.equals(oldDefault)) { shouldSave = true; diff --git a/src/net/i2p/android/router/util/Util.java b/src/net/i2p/android/router/util/Util.java index 9a190fefa..48c2f2630 100644 --- a/src/net/i2p/android/router/util/Util.java +++ b/src/net/i2p/android/router/util/Util.java @@ -1,12 +1,22 @@ 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.SharedPreferences; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.os.Build; +import android.preference.PreferenceManager; import net.i2p.I2PAppContext; +import net.i2p.util.OrderedProperties; public abstract class Util { private static final boolean _isEmulator = Build.MODEL.equals("sdk"); @@ -108,4 +118,66 @@ public abstract class Util { 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; + } } -- GitLab