diff --git a/src/main/java/com/nutomic/syncthingandroid/syncthing/SyncthingService.java b/src/main/java/com/nutomic/syncthingandroid/syncthing/SyncthingService.java index a2eb507d..cd40633d 100644 --- a/src/main/java/com/nutomic/syncthingandroid/syncthing/SyncthingService.java +++ b/src/main/java/com/nutomic/syncthingandroid/syncthing/SyncthingService.java @@ -29,10 +29,8 @@ import org.apache.http.conn.HttpHostConnectException; import org.apache.http.impl.client.DefaultHttpClient; import java.io.File; -import java.io.FileOutputStream; import java.io.FilenameFilter; import java.io.IOException; -import java.io.InputStream; import java.lang.ref.WeakReference; import java.util.HashSet; import java.util.Iterator; @@ -62,11 +60,6 @@ public class SyncthingService extends Service { */ private static final long WEB_GUI_POLL_INTERVAL = 100; - /** - * File in the config folder that contains configuration. - */ - private static final String CONFIG_FILE = "config.xml"; - /** * Name of the public key file in the data directory. */ @@ -135,7 +128,7 @@ public class SyncthingService extends Service { new PostTask() { @Override protected void onPostExecute(Void aVoid) { - ConfigXml config = new ConfigXml(getConfigFile()); + ConfigXml config = new ConfigXml(SyncthingService.this); mApi = new RestApi(SyncthingService.this, config.getWebGuiUrl(), config.getApiKey()); registerOnWebGuiAvailableListener(mApi); @@ -261,8 +254,8 @@ public class SyncthingService extends Service { publicKey.renameTo(new File(getFilesDir(), PUBLIC_KEY_FILE)); File privateKey = new File(getApplicationInfo().dataDir, PRIVATE_KEY_FILE); privateKey.renameTo(new File(getFilesDir(), PRIVATE_KEY_FILE)); - File config = new File(getApplicationInfo().dataDir, CONFIG_FILE); - config.renameTo(new File(getFilesDir(), CONFIG_FILE)); + File config = new File(getApplicationInfo().dataDir, ConfigXml.CONFIG_FILE); + config.renameTo(new File(getFilesDir(), ConfigXml.CONFIG_FILE)); File oldStorageDir = new File(getApplicationInfo().dataDir); File[] files = oldStorageDir.listFiles(idxFilter); @@ -308,18 +301,16 @@ public class SyncthingService extends Service { private class StartupTask extends AsyncTask> { @Override protected Pair doInBackground(Void... voids) { + moveConfigFiles(); + ConfigXml config = new ConfigXml(SyncthingService.this); + config.updateIfNeeded(); + if (isFirstStart()) { Log.i(TAG, "App started for the first time. " + "Copying default config, keys will be generated automatically"); - copyDefaultConfig(); - } - - moveConfigFiles(); - ConfigXml config = new ConfigXml(getConfigFile()); - if (isFirstStart()) { config.createCameraRepo(); } - config.update(); + return new Pair(config.getWebGuiUrl(), config.getApiKey()); } @@ -329,7 +320,7 @@ public class SyncthingService extends Service { Log.i(TAG, "Web GUI will be available at " + mApi.getUrl()); // HACK: Make sure there is no syncthing binary left running from an improper - // shutdown (eg Play Store update). + // shutdown (eg Play Store updateIfNeeded). // NOTE: This will log an exception if syncthing is not actually running. mApi.shutdown(); updateState(); @@ -366,10 +357,6 @@ public class SyncthingService extends Service { } } - private File getConfigFile() { - return new File(getFilesDir(), CONFIG_FILE); - } - /** * Returns true if this service has not been started before (ie config.xml does not exist). * @@ -379,36 +366,6 @@ public class SyncthingService extends Service { return !new File(getFilesDir(), PUBLIC_KEY_FILE).exists(); } - /** - * Copies the default config file from res/raw/config_default.xml to (data folder)/config.xml. - */ - private void copyDefaultConfig() { - InputStream in = null; - FileOutputStream out = null; - try { - in = getResources().openRawResource(R.raw.config_default); - out = new FileOutputStream(getConfigFile()); - byte[] buff = new byte[1024]; - int read; - - while ((read = in.read(buff)) > 0) { - out.write(buff, 0, read); - } - } - catch (IOException e) { - throw new RuntimeException("Failed to write config file", e); - } - finally { - try { - in.close(); - out.close(); - } - catch (IOException e) { - Log.w(TAG, "Failed to close stream while copying config", e); - } - } - } - public RestApi getApi() { return mApi; } diff --git a/src/main/java/com/nutomic/syncthingandroid/util/ConfigXml.java b/src/main/java/com/nutomic/syncthingandroid/util/ConfigXml.java index 4d9d9e28..70894811 100644 --- a/src/main/java/com/nutomic/syncthingandroid/util/ConfigXml.java +++ b/src/main/java/com/nutomic/syncthingandroid/util/ConfigXml.java @@ -1,15 +1,20 @@ package com.nutomic.syncthingandroid.util; +import android.content.Context; import android.os.Environment; import android.util.Log; +import com.nutomic.syncthingandroid.R; + import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; import java.io.File; +import java.io.FileOutputStream; import java.io.IOException; +import java.io.InputStream; import java.util.Random; import javax.xml.parsers.DocumentBuilder; @@ -30,15 +35,23 @@ public class ConfigXml { private static final String TAG = "ConfigXml"; + /** + * File in the config folder that contains configuration. + */ + public static final String CONFIG_FILE = "config.xml"; + private File mConfigFile; private Document mConfig; - public ConfigXml(File configFile) { - mConfigFile = configFile; + public ConfigXml(Context context) { + mConfigFile = new File(context.getFilesDir(), CONFIG_FILE); + if (!mConfigFile.exists()) { + copyDefaultConfig(context); + } try { DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); - mConfig = db.parse(configFile); + mConfig = db.parse(mConfigFile); } catch (SAXException e) { throw new RuntimeException("Failed to parse config file", e); } catch (ParserConfigurationException e) { @@ -65,7 +78,7 @@ public class ConfigXml { * Coming from 0.3.0 and earlier, the ignorePerms flag is set to true on every repository. */ @SuppressWarnings("SdCardPath") - public void update() { + public void updateIfNeeded() { Log.i(TAG, "Checking for needed config updates"); boolean changed = false; Element options = (Element) mConfig.getDocumentElement() @@ -169,4 +182,34 @@ public class ConfigXml { } } + /** + * Copies the default config file from res/raw/config_default.xml to (data folder)/config.xml. + */ + private void copyDefaultConfig(Context context) { + InputStream in = null; + FileOutputStream out = null; + try { + in = context.getResources().openRawResource(R.raw.config_default); + out = new FileOutputStream(mConfigFile); + byte[] buff = new byte[1024]; + int read; + + while ((read = in.read(buff)) > 0) { + out.write(buff, 0, read); + } + } + catch (IOException e) { + throw new RuntimeException("Failed to write config file", e); + } + finally { + try { + in.close(); + out.close(); + } + catch (IOException e) { + Log.w(TAG, "Failed to close stream while copying config", e); + } + } + } + }