1
0
Fork 0
mirror of https://github.com/syncthing/syncthing-android.git synced 2024-11-23 04:41: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 &&
getApi().getUsageReportAccepted() == RestApi.UsageReportSetting.UNDECIDED) {
showUsageReportingDialog();
} else if (currentState == SyncthingService.State.ERROR) {
finish();
} else if (currentState != SyncthingService.State.ACTIVE && !isFinishing()) {
if (currentState == SyncthingService.State.DISABLED) {
if (mLoadingDialog != null) {

View file

@ -131,7 +131,8 @@ public class SyncthingService extends Service implements
INIT,
STARTING,
ACTIVE,
DISABLED
DISABLED,
ERROR
}
private State mCurrentState = State.INIT;
@ -208,14 +209,24 @@ public class SyncthingService extends Service implements
shutdown();
Log.i(TAG, "Starting syncthing according to current state and preferences");
mConfig = null;
try {
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;
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);
new Thread(mRunnable).start();
updateNotification();
}
}
// Stop syncthing.
else {
if (mCurrentState == State.DISABLED)
@ -305,12 +316,24 @@ public class SyncthingService extends Service implements
@Override
protected Pair<String, String> doInBackground(Void... voids) {
try {
mConfig = new ConfigXml(SyncthingService.this);
return new Pair<>(mConfig.getWebGuiUrl(), mConfig.getApiKey());
} catch (ConfigXml.OpenConfigException e) {
return null;
}
}
@Override
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,
mGuiUser, mGuiPassword,
new RestApi.OnApiAvailableListener() {

View file

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