From 5e3f097aa433ac22ef974662068b41512ac18990 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Mon, 12 May 2014 16:33:53 +0200 Subject: [PATCH] Create default config on first start. --- res/raw/config_default.xml | 18 +++++++ .../syncthingandroid/WebGuiActivity.java | 47 +++++++++++++++++-- 2 files changed, 62 insertions(+), 3 deletions(-) create mode 100644 res/raw/config_default.xml diff --git a/res/raw/config_default.xml b/res/raw/config_default.xml new file mode 100644 index 00000000..1886a83d --- /dev/null +++ b/res/raw/config_default.xml @@ -0,0 +1,18 @@ + + +
127.0.0.1:8080
+
+ + :22000 + announce.syncthing.net:22025 + true + true + 16 + 0 + 60 + 60 + 1000 + false + true + +
diff --git a/src/com/nutomic/syncthingandroid/WebGuiActivity.java b/src/com/nutomic/syncthingandroid/WebGuiActivity.java index a60cffc9..de5bd24a 100644 --- a/src/com/nutomic/syncthingandroid/WebGuiActivity.java +++ b/src/com/nutomic/syncthingandroid/WebGuiActivity.java @@ -5,6 +5,7 @@ import android.app.AlertDialog; import android.content.Intent; import android.os.Bundle; import android.os.Handler; +import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; @@ -14,6 +15,9 @@ import android.widget.ProgressBar; import android.widget.TextView; import java.io.File; +import java.io.FileOutputStream; +import java.io.InputStream; +import java.io.IOException; public class WebGuiActivity extends Activity { @@ -21,8 +25,6 @@ public class WebGuiActivity extends Activity { /** * URL of the local syncthing web UI. - * - * TODO: read this out from native code. */ private static final String SYNCTHING_URL = "http://127.0.0.1:8080"; @@ -38,6 +40,11 @@ public class WebGuiActivity extends Activity { */ private static final String CERT_FILE = "cert.pem"; + /** + * File in the config folder that contains configuration. + */ + private static final String CONFIG_FILE = "config.xml"; + private WebView mWebView; private View mLoadingView; @@ -87,8 +94,10 @@ public class WebGuiActivity extends Activity { mWebView.setWebViewClient(mWebViewClient); mWebView.loadUrl(SYNCTHING_URL); + // Handle first start. if (!new File(CONFIG_FOLDER, CERT_FILE).exists()) { - // First start. + copyDefaultConfig(); + TextView loadingText = (TextView) mLoadingView.findViewById(R.id.loading_text); loadingText.setText(R.string.web_gui_creating_key); new AlertDialog.Builder(this) @@ -119,5 +128,37 @@ public class WebGuiActivity extends Activity { return super.onOptionsItemSelected(item); } } + + /** + * Copies the default config file from res/raw/config_default.xml to CONFIG_FOLDER/CONFIG_FILE. + */ + private void copyDefaultConfig() { + File config = new File(CONFIG_FOLDER, CONFIG_FILE); + InputStream in = null; + FileOutputStream out = null; + try { + in = getResources().openRawResource(R.raw.config_default); + out = new FileOutputStream(config); + byte[] buff = new byte[1024]; + int read = 0; + + while ((read = in.read(buff)) > 0) { + out.write(buff, 0, read); + } + } + catch (IOException e) { + Log.w(TAG, "Failed to write config file", e); + config.delete(); + } + finally { + try { + in.close(); + out.close(); + } + catch (IOException e) { + Log.w(TAG, "Failed to close stream while copying config", e); + } + } + } }