From b1574aaee1dc8cf55fc9f5f494e95b734a8d4e24 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Mon, 26 May 2014 13:37:17 +0200 Subject: [PATCH] Moved functions accessing syncthing API to new class RestApi. --- .../syncthingandroid/SettingsActivity.java | 42 ++++++++++++++----- .../syncthingandroid/WebGuiActivity.java | 5 +-- .../syncthingandroid/syncthing/RestApi.java | 40 ++++++++++++++++++ .../syncthing/SyncthingService.java | 23 +++++++--- 4 files changed, 90 insertions(+), 20 deletions(-) create mode 100644 src/main/java/com/nutomic/syncthingandroid/syncthing/RestApi.java diff --git a/src/main/java/com/nutomic/syncthingandroid/SettingsActivity.java b/src/main/java/com/nutomic/syncthingandroid/SettingsActivity.java index 93d7bd8f..160aa7cd 100644 --- a/src/main/java/com/nutomic/syncthingandroid/SettingsActivity.java +++ b/src/main/java/com/nutomic/syncthingandroid/SettingsActivity.java @@ -1,17 +1,22 @@ package com.nutomic.syncthingandroid; import android.annotation.TargetApi; +import android.content.ComponentName; +import android.content.Context; import android.content.Intent; +import android.content.ServiceConnection; import android.net.Uri; import android.os.Build; import android.os.Bundle; +import android.os.IBinder; import android.preference.Preference; import android.preference.PreferenceActivity; import android.preference.PreferenceScreen; import android.support.v4.app.NavUtils; import android.view.MenuItem; -import com.nutomic.syncthingandroid.syncthing.GetTask; +import com.nutomic.syncthingandroid.syncthing.SyncthingService; +import com.nutomic.syncthingandroid.syncthing.SyncthingServiceBinder; public class SettingsActivity extends PreferenceActivity { @@ -19,6 +24,23 @@ public class SettingsActivity extends PreferenceActivity { private static final String SYNCTHING_VERSION_KEY = "syncthing_version"; + private SyncthingService mSyncthingService; + + 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); + version.setSummary(mSyncthingService.getApi().getVersion()); + } + + public void onServiceDisconnected(ComponentName className) { + mSyncthingService = null; + } + }; + /** * Loads layout, sets version from Rest API. * @@ -35,19 +57,17 @@ public class SettingsActivity extends PreferenceActivity { getActionBar().setDisplayHomeAsUpEnabled(true); } + bindService(new Intent(this, SyncthingService.class), + mSyncthingServiceConnection, Context.BIND_AUTO_CREATE); + addPreferencesFromResource(R.xml.settings); - final PreferenceScreen screen = getPreferenceScreen(); - final Preference version = screen.findPreference(SYNCTHING_VERSION_KEY); - new GetTask() { - @Override - protected void onPostExecute(String versionName) { - version.setSummary((versionName != null) - ? versionName - : getString(R.string.syncthing_version_error)); - } - }.execute(GetTask.URI_VERSION); } + @Override + public void onDestroy() { + super.onDestroy(); + unbindService(mSyncthingServiceConnection); + } /** * Opens issue tracker when that preference is clicked. diff --git a/src/main/java/com/nutomic/syncthingandroid/WebGuiActivity.java b/src/main/java/com/nutomic/syncthingandroid/WebGuiActivity.java index ae36bdf3..66fd4025 100644 --- a/src/main/java/com/nutomic/syncthingandroid/WebGuiActivity.java +++ b/src/main/java/com/nutomic/syncthingandroid/WebGuiActivity.java @@ -89,7 +89,7 @@ public class WebGuiActivity extends Activity implements SyncthingService.OnWebGu getApplicationContext().startService( new Intent(this, SyncthingService.class)); - getApplicationContext().bindService( + bindService( new Intent(this, SyncthingService.class), mSyncthingServiceConnection, Context.BIND_AUTO_CREATE); } @@ -102,11 +102,10 @@ public class WebGuiActivity extends Activity implements SyncthingService.OnWebGu mWebView.loadUrl(SyncthingService.SYNCTHING_URL); } - @Override public void onDestroy() { super.onDestroy(); - getApplicationContext().unbindService(mSyncthingServiceConnection); + unbindService(mSyncthingServiceConnection); } @Override diff --git a/src/main/java/com/nutomic/syncthingandroid/syncthing/RestApi.java b/src/main/java/com/nutomic/syncthingandroid/syncthing/RestApi.java new file mode 100644 index 00000000..bbd5a124 --- /dev/null +++ b/src/main/java/com/nutomic/syncthingandroid/syncthing/RestApi.java @@ -0,0 +1,40 @@ +package com.nutomic.syncthingandroid.syncthing; + +import android.content.Context; + +import com.nutomic.syncthingandroid.R; + +/** + * Provides functions to interact with the syncthing REST API. + */ +public class RestApi implements SyncthingService.OnWebGuiAvailableListener { + + private Context mContext; + + private String mVersion; + + public RestApi(Context context) { + mContext = context; + } + + @Override + public void onWebGuiAvailable() { + new GetTask() { + @Override + protected void onPostExecute(String versionName) { + mVersion = (versionName != null) + ? versionName + : mContext.getString(R.string.syncthing_version_error); + } + }.execute(GetTask.URI_VERSION); + } + + public String getVersion() { + return mVersion; + } + + public void shutdown() { + new PostTask().execute(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 1d9a1129..1ff3527b 100644 --- a/src/main/java/com/nutomic/syncthingandroid/syncthing/SyncthingService.java +++ b/src/main/java/com/nutomic/syncthingandroid/syncthing/SyncthingService.java @@ -57,11 +57,6 @@ public class SyncthingService extends Service { */ private static final String BINARY_NAME = "lib/libsyncthing.so"; - /** - * URL of the local syncthing web UI. - */ - public static final String SYNCTHING_URL = "http://127.0.0.1:8080"; - /** * Interval in ms, at which connections to the web gui are performed on first start * to find out if it's online. @@ -73,6 +68,15 @@ 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); /** @@ -237,6 +241,9 @@ public class SyncthingService extends Service { n.flags |= Notification.FLAG_ONGOING_EVENT; startForeground(NOTIFICATION_ID, n); + mApi = new RestApi(this); + registerOnWebGuiAvailableListener(mApi); + new Thread(new NativeSyncthingRunnable()).start(); new PollWebGuiAvailableTask().execute(); } @@ -252,7 +259,7 @@ public class SyncthingService extends Service { @Override public void onDestroy() { super.onDestroy(); - new PostTask().execute(PostTask.URI_SHUTDOWN); + mApi.shutdown(); } /** @@ -370,4 +377,8 @@ public class SyncthingService extends Service { } } + public RestApi getApi() { + return mApi; + } + }