From ff7f6a811c42fcd7fde5b133b00cf246ce9a3de9 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Wed, 14 May 2014 11:29:31 +0200 Subject: [PATCH] Get version name from Rest API instead of hardcoding it. --- src/main/AndroidManifest.xml | 2 +- .../syncthingandroid/SettingsActivity.java | 15 ++++- .../syncthingandroid/WebGuiActivity.java | 3 + .../syncthingandroid/service/GetTask.java | 56 +++++++++++++++++++ .../{RestTask.java => service/PostTask.java} | 21 +++---- .../{ => service}/SyncthingService.java | 15 ++++- .../{ => service}/SyncthingServiceBinder.java | 4 +- src/main/res/values/strings.xml | 7 ++- src/main/res/xml/settings.xml | 1 + 9 files changed, 105 insertions(+), 19 deletions(-) create mode 100644 src/main/java/com/nutomic/syncthingandroid/service/GetTask.java rename src/main/java/com/nutomic/syncthingandroid/{RestTask.java => service/PostTask.java} (52%) rename src/main/java/com/nutomic/syncthingandroid/{ => service}/SyncthingService.java (90%) rename src/main/java/com/nutomic/syncthingandroid/{ => service}/SyncthingServiceBinder.java (70%) diff --git a/src/main/AndroidManifest.xml b/src/main/AndroidManifest.xml index 2f230306..f488b347 100644 --- a/src/main/AndroidManifest.xml +++ b/src/main/AndroidManifest.xml @@ -28,7 +28,7 @@ android:name=".SettingsActivity" android:label="@string/settings_title" /> - + diff --git a/src/main/java/com/nutomic/syncthingandroid/SettingsActivity.java b/src/main/java/com/nutomic/syncthingandroid/SettingsActivity.java index 67fa4c61..ec37c275 100644 --- a/src/main/java/com/nutomic/syncthingandroid/SettingsActivity.java +++ b/src/main/java/com/nutomic/syncthingandroid/SettingsActivity.java @@ -7,17 +7,30 @@ import android.preference.Preference; import android.preference.PreferenceActivity; import android.preference.PreferenceScreen; -import com.nutomic.syncthingandroid.R; +import com.nutomic.syncthingandroid.service.GetTask; +import com.nutomic.syncthingandroid.service.SyncthingService; public class SettingsActivity extends PreferenceActivity { private static final String REPORT_ISSUE_KEY = "report_issue"; + private static final String SYNCTHING_VERSION_KEY = "syncthing_version"; + @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); 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(SyncthingService.SYNCTHING_URL + SyncthingService.PATH_VERSION); } diff --git a/src/main/java/com/nutomic/syncthingandroid/WebGuiActivity.java b/src/main/java/com/nutomic/syncthingandroid/WebGuiActivity.java index d53b2f77..8e59b49a 100644 --- a/src/main/java/com/nutomic/syncthingandroid/WebGuiActivity.java +++ b/src/main/java/com/nutomic/syncthingandroid/WebGuiActivity.java @@ -18,6 +18,9 @@ import android.webkit.WebViewClient; import android.widget.ProgressBar; import android.widget.TextView; +import com.nutomic.syncthingandroid.service.SyncthingService; +import com.nutomic.syncthingandroid.service.SyncthingServiceBinder; + import java.io.File; import java.io.FileOutputStream; import java.io.IOException; diff --git a/src/main/java/com/nutomic/syncthingandroid/service/GetTask.java b/src/main/java/com/nutomic/syncthingandroid/service/GetTask.java new file mode 100644 index 00000000..302fa899 --- /dev/null +++ b/src/main/java/com/nutomic/syncthingandroid/service/GetTask.java @@ -0,0 +1,56 @@ +package com.nutomic.syncthingandroid.service; + + +import android.os.AsyncTask; +import android.util.Log; + +import org.apache.http.HttpEntity; +import org.apache.http.HttpResponse; +import org.apache.http.client.HttpClient; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.impl.client.DefaultHttpClient; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; + +/** + * Performs a GET request with no parameters to the URL in uri[0] and + * returns the result as a String. + */ +public class GetTask extends AsyncTask { + + private static final String TAG = "GetTask"; + + @Override + protected String doInBackground(String... uri) { + HttpClient httpclient = new DefaultHttpClient(); + HttpGet get = new HttpGet(uri[0]); + String responseString = null; + try { + HttpResponse response = httpclient.execute(get); + HttpEntity entity = response.getEntity(); + + if (entity != null) { + + // A Simple JSON Response Read + InputStream is = entity.getContent(); + + BufferedReader br = new BufferedReader(new InputStreamReader(is)); + String line; + String result = ""; + while((line = br.readLine()) != null) { + result += line; + } + br.close(); + return result; + } + } + catch (IOException e) { + Log.w(TAG, "Failed to call Rest API at " + uri[0], e); + } + return null; + } +} diff --git a/src/main/java/com/nutomic/syncthingandroid/RestTask.java b/src/main/java/com/nutomic/syncthingandroid/service/PostTask.java similarity index 52% rename from src/main/java/com/nutomic/syncthingandroid/RestTask.java rename to src/main/java/com/nutomic/syncthingandroid/service/PostTask.java index 0e4c4a2a..1d563cfc 100644 --- a/src/main/java/com/nutomic/syncthingandroid/RestTask.java +++ b/src/main/java/com/nutomic/syncthingandroid/service/PostTask.java @@ -5,34 +5,31 @@ import android.os.AsyncTask; import android.util.Log; import org.apache.http.HttpResponse; -import org.apache.http.HttpStatus; -import org.apache.http.StatusLine; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; -import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.DefaultHttpClient; -import java.io.ByteArrayOutputStream; import java.io.IOException; /** * Performs a POST request with no parameters to the URL in uri[0]. */ -class RestTask extends AsyncTask { +public class PostTask extends AsyncTask { - private static final String TAG = "RequestTask"; + private static final String TAG = "PostTask"; - @Override - protected Void doInBackground(String... uri) { + @Override + protected Void doInBackground(String... uri) { HttpClient httpclient = new DefaultHttpClient(); - HttpPost httppost = new HttpPost(uri[0]); + HttpPost post = new HttpPost(uri[0]); String responseString = null; try { - HttpResponse response = httpclient.execute(httppost); + HttpResponse response = httpclient.execute(post); } catch (IOException e) { - Log.w(TAG, "Failed to call Rest API", e); + Log.w(TAG, "Failed to call Rest API at " + uri[0], e); } return null; - } + } + } diff --git a/src/main/java/com/nutomic/syncthingandroid/SyncthingService.java b/src/main/java/com/nutomic/syncthingandroid/service/SyncthingService.java similarity index 90% rename from src/main/java/com/nutomic/syncthingandroid/SyncthingService.java rename to src/main/java/com/nutomic/syncthingandroid/service/SyncthingService.java index 263489f5..4b31aa34 100644 --- a/src/main/java/com/nutomic/syncthingandroid/SyncthingService.java +++ b/src/main/java/com/nutomic/syncthingandroid/service/SyncthingService.java @@ -1,4 +1,4 @@ -package com.nutomic.syncthingandroid; +package com.nutomic.syncthingandroid.service; import android.app.Notification; import android.app.PendingIntent; @@ -8,11 +8,16 @@ import android.os.IBinder; import android.support.v4.app.NotificationCompat; import android.util.Log; +import com.nutomic.syncthingandroid.R; +import com.nutomic.syncthingandroid.WebGuiActivity; + import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStreamReader; +import com.nutomic.syncthingandroid.PostTask; + /** * Holds the native syncthing instance and provides an API to access it. */ @@ -37,6 +42,11 @@ public class SyncthingService extends Service { */ private static final String PATH_SHUTDOWN = "/rest/shutdown"; + /** + * Path to call to get version name (with GET). + */ + public static final String PATH_VERSION = "/rest/version"; + private final SyncthingServiceBinder mBinder = new SyncthingServiceBinder(this); @Override @@ -123,6 +133,7 @@ public class SyncthingService extends Service { @Override public void onDestroy() { super.onDestroy(); - new RestTask().execute(SYNCTHING_URL + PATH_SHUTDOWN); + new PostTask().execute(SYNCTHING_URL + PATH_SHUTDOWN); } + } diff --git a/src/main/java/com/nutomic/syncthingandroid/SyncthingServiceBinder.java b/src/main/java/com/nutomic/syncthingandroid/service/SyncthingServiceBinder.java similarity index 70% rename from src/main/java/com/nutomic/syncthingandroid/SyncthingServiceBinder.java rename to src/main/java/com/nutomic/syncthingandroid/service/SyncthingServiceBinder.java index 7d91d834..995c2e62 100644 --- a/src/main/java/com/nutomic/syncthingandroid/SyncthingServiceBinder.java +++ b/src/main/java/com/nutomic/syncthingandroid/service/SyncthingServiceBinder.java @@ -1,7 +1,9 @@ -package com.nutomic.syncthingandroid; +package com.nutomic.syncthingandroid.service; import android.os.Binder; +import com.nutomic.syncthingandroid.service.SyncthingService; + public class SyncthingServiceBinder extends Binder { SyncthingService mService; diff --git a/src/main/res/values/strings.xml b/src/main/res/values/strings.xml index 4095b0c9..d740e9b1 100644 --- a/src/main/res/values/strings.xml +++ b/src/main/res/values/strings.xml @@ -3,8 +3,11 @@ Syncthing - - Syncthing Version: HEAD + + Syncthing Version + + + Could not read version First Start diff --git a/src/main/res/xml/settings.xml b/src/main/res/xml/settings.xml index e4d1f46d..dcc4e79c 100644 --- a/src/main/res/xml/settings.xml +++ b/src/main/res/xml/settings.xml @@ -6,6 +6,7 @@ android:title="@string/report_issue_title" />