diff --git a/src/androidTest/java/com/nutomic/syncthingandroid/test/syncthing/PollWebGuiAvailableTaskTest.java b/src/androidTest/java/com/nutomic/syncthingandroid/test/syncthing/PollWebGuiAvailableTaskTest.java index 4fd42255..b858d273 100644 --- a/src/androidTest/java/com/nutomic/syncthingandroid/test/syncthing/PollWebGuiAvailableTaskTest.java +++ b/src/androidTest/java/com/nutomic/syncthingandroid/test/syncthing/PollWebGuiAvailableTaskTest.java @@ -23,7 +23,6 @@ public class PollWebGuiAvailableTaskTest extends AndroidTestCase { super.setUp(); mConfig = new ConfigXml(new MockContext(getContext())); - mConfig.updateIfNeeded(); } @Override diff --git a/src/androidTest/java/com/nutomic/syncthingandroid/test/syncthing/RestApiTest.java b/src/androidTest/java/com/nutomic/syncthingandroid/test/syncthing/RestApiTest.java index f9dd0a4f..2fba8936 100644 --- a/src/androidTest/java/com/nutomic/syncthingandroid/test/syncthing/RestApiTest.java +++ b/src/androidTest/java/com/nutomic/syncthingandroid/test/syncthing/RestApiTest.java @@ -32,8 +32,7 @@ public class RestApiTest extends AndroidTestCase { getContext().getApplicationInfo().dataDir + "/" + SyncthingService.BINARY_NAME); mConfig = new ConfigXml(new MockContext(getContext())); - mConfig.createCameraFolder(); - mConfig.updateIfNeeded(); + mConfig.changeDefaultFolder(); final CountDownLatch latch = new CountDownLatch(2); new PollWebGuiAvailableTask() { diff --git a/src/androidTest/java/com/nutomic/syncthingandroid/test/util/ConfigXmlTest.java b/src/androidTest/java/com/nutomic/syncthingandroid/test/util/ConfigXmlTest.java index 7262e6cc..997f5b92 100644 --- a/src/androidTest/java/com/nutomic/syncthingandroid/test/util/ConfigXmlTest.java +++ b/src/androidTest/java/com/nutomic/syncthingandroid/test/util/ConfigXmlTest.java @@ -4,9 +4,13 @@ import android.test.AndroidTestCase; import android.test.suitebuilder.annotation.MediumTest; import android.test.suitebuilder.annotation.SmallTest; +import com.nutomic.syncthingandroid.syncthing.SyncthingService; import com.nutomic.syncthingandroid.test.MockContext; import com.nutomic.syncthingandroid.util.ConfigXml; +import java.io.File; +import java.io.IOException; + public class ConfigXmlTest extends AndroidTestCase { private MockContext mContext; @@ -44,22 +48,9 @@ public class ConfigXmlTest extends AndroidTestCase { public void testCreateCameraFolder() { long oldTime = ConfigXml.getConfigFile(mContext).lastModified(); long oldSize = ConfigXml.getConfigFile(mContext).length(); - mConfig.createCameraFolder(); + mConfig.changeDefaultFolder(); assertNotSame(oldTime, ConfigXml.getConfigFile(mContext).lastModified()); assertNotSame(oldSize, ConfigXml.getConfigFile(mContext).lastModified()); } - /** - * Same as {@link #testCreateCameraFolder()}. - */ - @MediumTest - public void testUpdateIfNeeded() { - long oldTime = ConfigXml.getConfigFile(mContext).lastModified(); - long oldSize = ConfigXml.getConfigFile(mContext).length(); - mConfig.updateIfNeeded(); - assertNotSame(oldTime, ConfigXml.getConfigFile(mContext).lastModified()); - assertNotSame(oldSize, ConfigXml.getConfigFile(mContext).lastModified()); - assertNotNull(mConfig.getApiKey()); - } - } diff --git a/src/main/java/com/nutomic/syncthingandroid/syncthing/SyncthingService.java b/src/main/java/com/nutomic/syncthingandroid/syncthing/SyncthingService.java index b4791869..2285ae25 100644 --- a/src/main/java/com/nutomic/syncthingandroid/syncthing/SyncthingService.java +++ b/src/main/java/com/nutomic/syncthingandroid/syncthing/SyncthingService.java @@ -77,8 +77,6 @@ public class SyncthingService extends Service { private LinkedList mObservers = new LinkedList<>(); - private SyncthingRunnable mSyncthingRunnable; - private final SyncthingServiceBinder mBinder = new SyncthingServiceBinder(this); /** @@ -182,7 +180,6 @@ public class SyncthingService extends Service { Log.i(TAG, "Starting syncthing according to current state and preferences"); mConfig = new ConfigXml(SyncthingService.this); - mConfig.updateIfNeeded(); mCurrentState = State.STARTING; registerOnWebGuiAvailableListener(mApi); new PollWebGuiAvailableTaskImpl().execute(mConfig.getWebGuiUrl()); @@ -270,14 +267,8 @@ public class SyncthingService extends Service { @Override protected Pair doInBackground(Void... voids) { moveConfigFiles(); - mConfig = new ConfigXml(SyncthingService.this); - mConfig.updateIfNeeded(); - if (isFirstStart()) { - Log.i(TAG, "App started for the first time. " + - "Copying default config, keys will be generated automatically"); - mConfig.createCameraFolder(); - } + mConfig = new ConfigXml(SyncthingService.this); return new Pair<>(mConfig.getWebGuiUrl(), mConfig.getApiKey()); } diff --git a/src/main/java/com/nutomic/syncthingandroid/util/ConfigXml.java b/src/main/java/com/nutomic/syncthingandroid/util/ConfigXml.java index 82ba0c6d..f798f1e1 100644 --- a/src/main/java/com/nutomic/syncthingandroid/util/ConfigXml.java +++ b/src/main/java/com/nutomic/syncthingandroid/util/ConfigXml.java @@ -4,7 +4,8 @@ import android.content.Context; import android.os.Environment; import android.util.Log; -import com.nutomic.syncthingandroid.R; +import com.nutomic.syncthingandroid.syncthing.SyncthingRunnable; +import com.nutomic.syncthingandroid.syncthing.SyncthingService; import org.w3c.dom.Document; import org.w3c.dom.Element; @@ -13,9 +14,7 @@ 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; @@ -47,15 +46,25 @@ public class ConfigXml { public ConfigXml(Context context) { mConfigFile = getConfigFile(context); - if (!mConfigFile.exists()) { - copyDefaultConfig(context); + boolean isFirstStart = !mConfigFile.exists(); + if (isFirstStart) { + Log.i(TAG, "App started for the first time. Generating keys and config."); + new SyncthingRunnable(context, context.getApplicationInfo().dataDir + "/" + + SyncthingService.BINARY_NAME + " -generate='" + context.getFilesDir() + "'") + .run(); } + try { DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); mConfig = db.parse(mConfigFile); } catch (SAXException | ParserConfigurationException | IOException e) { throw new RuntimeException("Failed to open config file", e); } + + if (isFirstStart) { + changeDefaultFolder(); + } + updateIfNeeded(); } public static File getConfigFile(Context context) { @@ -79,7 +88,7 @@ public class ConfigXml { * Coming from 0.3.0 and earlier, the ignorePerms flag is set to true on every folder. */ @SuppressWarnings("SdCardPath") - public void updateIfNeeded() { + private void updateIfNeeded() { Log.i(TAG, "Checking for needed config updates"); boolean changed = false; Element options = (Element) mConfig.getDocumentElement() @@ -191,17 +200,15 @@ public class ConfigXml { } /** - * Creates a folder for the default camera folder. + * Change default folder id to camera and path to camera folder path. */ - public void createCameraFolder() { - Element cameraFolder = mConfig.createElement("folder"); - cameraFolder.setAttribute("id", "camera"); - cameraFolder.setAttribute("directory", Environment + public void changeDefaultFolder() { + Element folder = (Element) mConfig.getDocumentElement() + .getElementsByTagName("folder").item(0); + folder.setAttribute("id", "camera"); + folder.setAttribute("path", Environment .getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsolutePath()); - cameraFolder.setAttribute("ro", "true"); - cameraFolder.setAttribute("ignorePerms", "true"); - mConfig.getDocumentElement().appendChild(cameraFolder); - + folder.setAttribute("ro", "true"); saveChanges(); } @@ -221,31 +228,4 @@ 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); - } - } - } - } diff --git a/src/main/res/raw/config_default.xml b/src/main/res/raw/config_default.xml deleted file mode 100644 index 03ab8196..00000000 --- a/src/main/res/raw/config_default.xml +++ /dev/null @@ -1,17 +0,0 @@ - - -
127.0.0.1:8080
-
- - 0.0.0.0:22000 - 194.126.249.5:22026 - true - true - 16 - 0 - 60 - 1000 - false - true - -