1
0
Fork 0
mirror of https://github.com/syncthing/syncthing-android.git synced 2024-11-23 12:51:16 +00:00

Fixed crash if config could not be opened.

This commit is contained in:
Felix Ableitner 2015-06-17 15:01:57 +02:00
parent 49ab457a91
commit 5a0870d21a
4 changed files with 50 additions and 15 deletions

View file

@ -71,6 +71,8 @@ public class MainActivity extends SyncthingActivity
new Date().getTime() > getFirstStartTime() + USAGE_REPORTING_DIALOG_DELAY && new Date().getTime() > getFirstStartTime() + USAGE_REPORTING_DIALOG_DELAY &&
getApi().getUsageReportAccepted() == RestApi.UsageReportSetting.UNDECIDED) { getApi().getUsageReportAccepted() == RestApi.UsageReportSetting.UNDECIDED) {
showUsageReportingDialog(); showUsageReportingDialog();
} else if (currentState == SyncthingService.State.ERROR) {
finish();
} else if (currentState != SyncthingService.State.ACTIVE && !isFinishing()) { } else if (currentState != SyncthingService.State.ACTIVE && !isFinishing()) {
if (currentState == SyncthingService.State.DISABLED) { if (currentState == SyncthingService.State.DISABLED) {
if (mLoadingDialog != null) { if (mLoadingDialog != null) {

View file

@ -131,7 +131,8 @@ public class SyncthingService extends Service implements
INIT, INIT,
STARTING, STARTING,
ACTIVE, ACTIVE,
DISABLED DISABLED,
ERROR
} }
private State mCurrentState = State.INIT; private State mCurrentState = State.INIT;
@ -208,14 +209,24 @@ public class SyncthingService extends Service implements
shutdown(); shutdown();
Log.i(TAG, "Starting syncthing according to current state and preferences"); Log.i(TAG, "Starting syncthing according to current state and preferences");
mConfig = null;
try {
mConfig = new ConfigXml(SyncthingService.this); mConfig = new ConfigXml(SyncthingService.this);
} catch (ConfigXml.OpenConfigException e) {
mCurrentState = State.ERROR;
Toast.makeText(this, R.string.config_create_failed, Toast.LENGTH_LONG).show();
}
if (mConfig != null) {
mCurrentState = State.STARTING; mCurrentState = State.STARTING;
registerOnWebGuiAvailableListener(mApi); registerOnWebGuiAvailableListener(mApi);
new PollWebGuiAvailableTaskImpl(getFilesDir() + "/" + HTTPS_CERT_FILE).execute(mConfig.getWebGuiUrl()); new PollWebGuiAvailableTaskImpl(getFilesDir() + "/" + HTTPS_CERT_FILE)
.execute(mConfig.getWebGuiUrl());
mRunnable = new SyncthingRunnable(this, SyncthingRunnable.Command.main); mRunnable = new SyncthingRunnable(this, SyncthingRunnable.Command.main);
new Thread(mRunnable).start(); new Thread(mRunnable).start();
updateNotification(); updateNotification();
} }
}
// Stop syncthing. // Stop syncthing.
else { else {
if (mCurrentState == State.DISABLED) if (mCurrentState == State.DISABLED)
@ -305,12 +316,24 @@ public class SyncthingService extends Service implements
@Override @Override
protected Pair<String, String> doInBackground(Void... voids) { protected Pair<String, String> doInBackground(Void... voids) {
try {
mConfig = new ConfigXml(SyncthingService.this); mConfig = new ConfigXml(SyncthingService.this);
return new Pair<>(mConfig.getWebGuiUrl(), mConfig.getApiKey()); return new Pair<>(mConfig.getWebGuiUrl(), mConfig.getApiKey());
} catch (ConfigXml.OpenConfigException e) {
return null;
}
} }
@Override @Override
protected void onPostExecute(Pair<String, String> urlAndKey) { protected void onPostExecute(Pair<String, String> urlAndKey) {
if (urlAndKey == null) {
Toast.makeText(SyncthingService.this, R.string.config_create_failed,
Toast.LENGTH_LONG).show();
mCurrentState = State.ERROR;
onApiChange();
return;
}
mApi = new RestApi(SyncthingService.this, urlAndKey.first, urlAndKey.second, mApi = new RestApi(SyncthingService.this, urlAndKey.first, urlAndKey.second,
mGuiUser, mGuiPassword, mGuiUser, mGuiPassword,
new RestApi.OnApiAvailableListener() { new RestApi.OnApiAvailableListener() {

View file

@ -2,9 +2,12 @@ package com.nutomic.syncthingandroid.util;
import android.content.Context; import android.content.Context;
import android.os.Environment; import android.os.Environment;
import android.os.Handler;
import android.os.Looper;
import android.util.Log; import android.util.Log;
import android.widget.Toast; import android.widget.Toast;
import com.nutomic.syncthingandroid.R;
import com.nutomic.syncthingandroid.syncthing.SyncthingRunnable; import com.nutomic.syncthingandroid.syncthing.SyncthingRunnable;
import org.w3c.dom.Document; import org.w3c.dom.Document;
@ -32,6 +35,9 @@ import javax.xml.transform.stream.StreamResult;
*/ */
public class ConfigXml { public class ConfigXml {
public class OpenConfigException extends RuntimeException {
}
private static final String TAG = "ConfigXml"; private static final String TAG = "ConfigXml";
/** /**
@ -41,11 +47,13 @@ public class ConfigXml {
private static final String INVALID_CONFIG_FILE = "config.xml.invalid"; private static final String INVALID_CONFIG_FILE = "config.xml.invalid";
private static final int OPEN_CONFIG_MAX_TRIES = 10;
private File mConfigFile; private File mConfigFile;
private Document mConfig; private Document mConfig;
public ConfigXml(Context context) { public ConfigXml(final Context context) throws OpenConfigException {
mConfigFile = getConfigFile(context); mConfigFile = getConfigFile(context);
boolean isFirstStart = !mConfigFile.exists(); boolean isFirstStart = !mConfigFile.exists();
if (isFirstStart) { if (isFirstStart) {
@ -53,7 +61,7 @@ public class ConfigXml {
generateKeysConfig(context); generateKeysConfig(context);
} }
for (int i = 0; i < 10 && mConfig == null; i++) { for (int i = 0; i < OPEN_CONFIG_MAX_TRIES && mConfig == null; i++) {
try { try {
DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
mConfig = db.parse(mConfigFile); mConfig = db.parse(mConfigFile);
@ -69,9 +77,8 @@ public class ConfigXml {
mConfigFile = getConfigFile(context); mConfigFile = getConfigFile(context);
} }
} }
if (mConfig == null) { if (mConfig == null)
Toast.makeText(context, "Failed to create a Syncthing config. Syncthing will not start!", Toast.LENGTH_LONG).show(); throw new OpenConfigException();
}
if (isFirstStart) { if (isFirstStart) {
changeDefaultFolder(); changeDefaultFolder();

View file

@ -396,6 +396,9 @@ Please report any problems you encounter via Github.</string>
<!-- Toast shown if folder observer fails to traverse a folder --> <!-- Toast shown if folder observer fails to traverse a folder -->
<string name="toast_folder_observer_stack_overflow">Directory tree too deep. Check for cyclic symlinks</string> <string name="toast_folder_observer_stack_overflow">Directory tree too deep. Check for cyclic symlinks</string>
<!-- Toast shown if syncthing failed to create a config -->
<string name="config_create_failed">Failed to create a Syncthing config. Please check the logs.</string>
<!-- RestApi --> <!-- RestApi -->