From 5a0870d21ad5e88259f539a12b3b4c70e219fbdd Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Wed, 17 Jun 2015 15:01:57 +0200 Subject: [PATCH] Fixed crash if config could not be opened. --- .../activities/MainActivity.java | 2 + .../syncthing/SyncthingService.java | 43 ++++++++++++++----- .../syncthingandroid/util/ConfigXml.java | 17 +++++--- src/main/res/values/strings.xml | 3 ++ 4 files changed, 50 insertions(+), 15 deletions(-) diff --git a/src/main/java/com/nutomic/syncthingandroid/activities/MainActivity.java b/src/main/java/com/nutomic/syncthingandroid/activities/MainActivity.java index 6a4b0ff4..77c245b7 100644 --- a/src/main/java/com/nutomic/syncthingandroid/activities/MainActivity.java +++ b/src/main/java/com/nutomic/syncthingandroid/activities/MainActivity.java @@ -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) { diff --git a/src/main/java/com/nutomic/syncthingandroid/syncthing/SyncthingService.java b/src/main/java/com/nutomic/syncthingandroid/syncthing/SyncthingService.java index 395f914d..ffc5dc88 100644 --- a/src/main/java/com/nutomic/syncthingandroid/syncthing/SyncthingService.java +++ b/src/main/java/com/nutomic/syncthingandroid/syncthing/SyncthingService.java @@ -131,7 +131,8 @@ public class SyncthingService extends Service implements INIT, STARTING, ACTIVE, - DISABLED + DISABLED, + ERROR } private State mCurrentState = State.INIT; @@ -208,13 +209,23 @@ public class SyncthingService extends Service implements shutdown(); Log.i(TAG, "Starting syncthing according to current state and preferences"); - mConfig = new ConfigXml(SyncthingService.this); - mCurrentState = State.STARTING; - registerOnWebGuiAvailableListener(mApi); - new PollWebGuiAvailableTaskImpl(getFilesDir() + "/" + HTTPS_CERT_FILE).execute(mConfig.getWebGuiUrl()); - mRunnable = new SyncthingRunnable(this, SyncthingRunnable.Command.main); - new Thread(mRunnable).start(); - updateNotification(); + 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()); + mRunnable = new SyncthingRunnable(this, SyncthingRunnable.Command.main); + new Thread(mRunnable).start(); + updateNotification(); + } } // Stop syncthing. else { @@ -305,12 +316,24 @@ public class SyncthingService extends Service implements @Override protected Pair doInBackground(Void... voids) { - mConfig = new ConfigXml(SyncthingService.this); - return new Pair<>(mConfig.getWebGuiUrl(), mConfig.getApiKey()); + try { + mConfig = new ConfigXml(SyncthingService.this); + return new Pair<>(mConfig.getWebGuiUrl(), mConfig.getApiKey()); + } catch (ConfigXml.OpenConfigException e) { + return null; + } } @Override protected void onPostExecute(Pair 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() { diff --git a/src/main/java/com/nutomic/syncthingandroid/util/ConfigXml.java b/src/main/java/com/nutomic/syncthingandroid/util/ConfigXml.java index f1e050c1..f334dd1f 100644 --- a/src/main/java/com/nutomic/syncthingandroid/util/ConfigXml.java +++ b/src/main/java/com/nutomic/syncthingandroid/util/ConfigXml.java @@ -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(); diff --git a/src/main/res/values/strings.xml b/src/main/res/values/strings.xml index a2054f45..44a2ca59 100644 --- a/src/main/res/values/strings.xml +++ b/src/main/res/values/strings.xml @@ -396,6 +396,9 @@ Please report any problems you encounter via Github. Directory tree too deep. Check for cyclic symlinks + + Failed to create a Syncthing config. Please check the logs. +