From 77b7f3a299e8908d2043daabb207c0de21148e62 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Mon, 26 May 2014 17:30:49 +0200 Subject: [PATCH] Read web gui url from config instead of hardcoding it. --- .../syncthingandroid/SettingsActivity.java | 7 +++- .../syncthingandroid/WebGuiActivity.java | 2 +- .../syncthingandroid/syncthing/GetTask.java | 4 +- .../syncthingandroid/syncthing/PostTask.java | 4 +- .../syncthingandroid/syncthing/RestApi.java | 22 +++++++++-- .../syncthing/SyncthingService.java | 37 +++++++++++++------ 6 files changed, 54 insertions(+), 22 deletions(-) diff --git a/src/main/java/com/nutomic/syncthingandroid/SettingsActivity.java b/src/main/java/com/nutomic/syncthingandroid/SettingsActivity.java index 160aa7cd..e17a658d 100644 --- a/src/main/java/com/nutomic/syncthingandroid/SettingsActivity.java +++ b/src/main/java/com/nutomic/syncthingandroid/SettingsActivity.java @@ -26,13 +26,16 @@ public class SettingsActivity extends PreferenceActivity { private SyncthingService mSyncthingService; + /** + * Binds to service and sets version name. The version name can not be retrieved if the service + * is just started (as we don't wait until the api is up). + */ private ServiceConnection mSyncthingServiceConnection = new ServiceConnection() { public void onServiceConnected(ComponentName className, IBinder service) { SyncthingServiceBinder binder = (SyncthingServiceBinder) service; mSyncthingService = binder.getService(); - final PreferenceScreen screen = getPreferenceScreen(); - final Preference version = screen.findPreference(SYNCTHING_VERSION_KEY); + Preference version = getPreferenceScreen().findPreference(SYNCTHING_VERSION_KEY); version.setSummary(mSyncthingService.getApi().getVersion()); } diff --git a/src/main/java/com/nutomic/syncthingandroid/WebGuiActivity.java b/src/main/java/com/nutomic/syncthingandroid/WebGuiActivity.java index 66fd4025..4002551c 100644 --- a/src/main/java/com/nutomic/syncthingandroid/WebGuiActivity.java +++ b/src/main/java/com/nutomic/syncthingandroid/WebGuiActivity.java @@ -99,7 +99,7 @@ public class WebGuiActivity extends Activity implements SyncthingService.OnWebGu */ @Override public void onWebGuiAvailable() { - mWebView.loadUrl(SyncthingService.SYNCTHING_URL); + mWebView.loadUrl(mSyncthingService.getApi().getUrl()); } @Override diff --git a/src/main/java/com/nutomic/syncthingandroid/syncthing/GetTask.java b/src/main/java/com/nutomic/syncthingandroid/syncthing/GetTask.java index 31d8c541..cc066337 100644 --- a/src/main/java/com/nutomic/syncthingandroid/syncthing/GetTask.java +++ b/src/main/java/com/nutomic/syncthingandroid/syncthing/GetTask.java @@ -25,12 +25,12 @@ public class GetTask extends AsyncTask { /** * URI to call to get version name. */ - public static final String URI_VERSION = SyncthingService.SYNCTHING_URL + "/rest/version"; + public static final String URI_VERSION = "/rest/version"; @Override protected String doInBackground(String... uri) { HttpClient httpclient = new DefaultHttpClient(); - HttpGet get = new HttpGet(uri[0]); + HttpGet get = new HttpGet(uri[0] + uri[1]); String responseString = null; try { HttpResponse response = httpclient.execute(get); diff --git a/src/main/java/com/nutomic/syncthingandroid/syncthing/PostTask.java b/src/main/java/com/nutomic/syncthingandroid/syncthing/PostTask.java index a0e58a19..a44790e1 100644 --- a/src/main/java/com/nutomic/syncthingandroid/syncthing/PostTask.java +++ b/src/main/java/com/nutomic/syncthingandroid/syncthing/PostTask.java @@ -20,12 +20,12 @@ public class PostTask extends AsyncTask { /** * URI to call for shutdown. */ - public static final String URI_SHUTDOWN = SyncthingService.SYNCTHING_URL + "/rest/shutdown"; + public static final String URI_SHUTDOWN = "/rest/shutdown"; @Override protected Void doInBackground(String... uri) { HttpClient httpclient = new DefaultHttpClient(); - HttpPost post = new HttpPost(uri[0]); + HttpPost post = new HttpPost(uri[0] + uri[1]); String responseString = null; try { HttpResponse response = httpclient.execute(post); diff --git a/src/main/java/com/nutomic/syncthingandroid/syncthing/RestApi.java b/src/main/java/com/nutomic/syncthingandroid/syncthing/RestApi.java index bbd5a124..190bd56c 100644 --- a/src/main/java/com/nutomic/syncthingandroid/syncthing/RestApi.java +++ b/src/main/java/com/nutomic/syncthingandroid/syncthing/RestApi.java @@ -13,8 +13,18 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener { private String mVersion; - public RestApi(Context context) { + private String mUrl; + + public RestApi(Context context, String url) { mContext = context; + mUrl = url; + } + + /** + * Returns the full URL of the web gui. + */ + public String getUrl() { + return mUrl; } @Override @@ -26,15 +36,21 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener { ? versionName : mContext.getString(R.string.syncthing_version_error); } - }.execute(GetTask.URI_VERSION); + }.execute(mUrl, GetTask.URI_VERSION); } + /** + * Returns the version name, or a (text) error message on failure. + */ public String getVersion() { return mVersion; } + /** + * Stops syncthing. You should probably use SyncthingService.stopService() instead. + */ public void shutdown() { - new PostTask().execute(PostTask.URI_SHUTDOWN); + new PostTask().execute(mUrl, PostTask.URI_SHUTDOWN); } } diff --git a/src/main/java/com/nutomic/syncthingandroid/syncthing/SyncthingService.java b/src/main/java/com/nutomic/syncthingandroid/syncthing/SyncthingService.java index 1ff3527b..98913931 100644 --- a/src/main/java/com/nutomic/syncthingandroid/syncthing/SyncthingService.java +++ b/src/main/java/com/nutomic/syncthingandroid/syncthing/SyncthingService.java @@ -68,13 +68,6 @@ public class SyncthingService extends Service { */ private static final String CONFIG_FILE = "config.xml"; - /** - * URL of the local syncthing web UI. - * - * TODO: read from config. - */ - public static final String SYNCTHING_URL = "http://127.0.0.1:8080"; - private RestApi mApi; private final SyncthingServiceBinder mBinder = new SyncthingServiceBinder(this); @@ -192,7 +185,7 @@ public class SyncthingService extends Service { protected Void doInBackground(Void... voids) { int status = 0; HttpClient httpclient = new DefaultHttpClient(); - HttpHead head = new HttpHead(SYNCTHING_URL); + HttpHead head = new HttpHead(mApi.getUrl()); do { try { Thread.sleep(WEB_GUI_POLL_INTERVAL); @@ -206,10 +199,10 @@ public class SyncthingService extends Service { // so we ignore and continue. } catch (IOException e) { - Log.d(TAG, "Failed to poll for web interface", e); + Log.w(TAG, "Failed to poll for web interface", e); } catch (InterruptedException e) { - Log.d(TAG, "Failed to poll for web interface", e); + Log.w(TAG, "Failed to poll for web interface", e); } } while(status != HttpStatus.SC_OK); return null; @@ -241,8 +234,27 @@ public class SyncthingService extends Service { n.flags |= Notification.FLAG_ONGOING_EVENT; startForeground(NOTIFICATION_ID, n); - mApi = new RestApi(this); - registerOnWebGuiAvailableListener(mApi); + String syncthingUrl = null; + try { + DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); + Document d = db.parse(getConfigFile()); + Element options = (Element) + d.getDocumentElement().getElementsByTagName("gui").item(0); + syncthingUrl = options.getElementsByTagName("address").item(0).getTextContent(); + } + catch (SAXException e) { + throw new RuntimeException("Failed to read gui url, aborting", e); + } + catch (ParserConfigurationException e) { + throw new RuntimeException("Failed to read gui url, aborting", e); + } + catch (IOException e) { + throw new RuntimeException("Failed to read gui url, aborting", e); + } + finally { + mApi = new RestApi(this, "http://" + syncthingUrl); + registerOnWebGuiAvailableListener(mApi); + } new Thread(new NativeSyncthingRunnable()).start(); new PollWebGuiAvailableTask().execute(); @@ -295,6 +307,7 @@ public class SyncthingService extends Service { DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document d = db.parse(getConfigFile()); + // Hardcode default globalAnnounceServer ip. Element options = (Element) d.getDocumentElement().getElementsByTagName("options").item(0);