1
0
Fork 0
mirror of https://github.com/syncthing/syncthing-android.git synced 2024-11-26 14:21:16 +00:00

Use syncthing -generate flag instead of copying own config file.

This commit is contained in:
Felix Ableitner 2014-10-26 03:32:23 +03:00
parent 84355c2dcf
commit df48d75127
6 changed files with 29 additions and 86 deletions

View file

@ -23,7 +23,6 @@ public class PollWebGuiAvailableTaskTest extends AndroidTestCase {
super.setUp();
mConfig = new ConfigXml(new MockContext(getContext()));
mConfig.updateIfNeeded();
}
@Override

View file

@ -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() {

View file

@ -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());
}
}

View file

@ -77,8 +77,6 @@ public class SyncthingService extends Service {
private LinkedList<FolderObserver> 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<String, String> 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());
}

View file

@ -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);
}
}
}
}

View file

@ -1,17 +0,0 @@
<configuration version="2">
<gui enabled="true">
<address>127.0.0.1:8080</address>
</gui>
<options>
<listenAddress>0.0.0.0:22000</listenAddress>
<globalAnnounceServer>194.126.249.5:22026</globalAnnounceServer>
<globalAnnounceEnabled>true</globalAnnounceEnabled>
<localAnnounceEnabled>true</localAnnounceEnabled>
<parallelRequests>16</parallelRequests>
<maxSendKbps>0</maxSendKbps>
<reconnectionIntervalS>60</reconnectionIntervalS>
<maxChangeKbps>1000</maxChangeKbps>
<startBrowser>false</startBrowser>
<upnpEnabled>true</upnpEnabled>
</options>
</configuration>