mirror of
https://github.com/syncthing/syncthing-android.git
synced 2024-12-23 03:11:30 +00:00
Get version name from Rest API instead of hardcoding it.
This commit is contained in:
parent
6da9b7b8f9
commit
ff7f6a811c
9 changed files with 105 additions and 19 deletions
|
@ -28,7 +28,7 @@
|
||||||
android:name=".SettingsActivity"
|
android:name=".SettingsActivity"
|
||||||
android:label="@string/settings_title" />
|
android:label="@string/settings_title" />
|
||||||
|
|
||||||
<service android:name=".SyncthingService" />
|
<service android:name=".service.SyncthingService" />
|
||||||
|
|
||||||
</application>
|
</application>
|
||||||
</manifest>
|
</manifest>
|
||||||
|
|
|
@ -7,17 +7,30 @@ import android.preference.Preference;
|
||||||
import android.preference.PreferenceActivity;
|
import android.preference.PreferenceActivity;
|
||||||
import android.preference.PreferenceScreen;
|
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 {
|
public class SettingsActivity extends PreferenceActivity {
|
||||||
|
|
||||||
private static final String REPORT_ISSUE_KEY = "report_issue";
|
private static final String REPORT_ISSUE_KEY = "report_issue";
|
||||||
|
|
||||||
|
private static final String SYNCTHING_VERSION_KEY = "syncthing_version";
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreate(Bundle savedInstanceState) {
|
public void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
|
|
||||||
addPreferencesFromResource(R.xml.settings);
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -18,6 +18,9 @@ import android.webkit.WebViewClient;
|
||||||
import android.widget.ProgressBar;
|
import android.widget.ProgressBar;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
|
|
||||||
|
import com.nutomic.syncthingandroid.service.SyncthingService;
|
||||||
|
import com.nutomic.syncthingandroid.service.SyncthingServiceBinder;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
|
@ -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<String, Void, String> {
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -5,34 +5,31 @@ import android.os.AsyncTask;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
import org.apache.http.HttpResponse;
|
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.HttpClient;
|
||||||
import org.apache.http.client.methods.HttpPost;
|
import org.apache.http.client.methods.HttpPost;
|
||||||
import org.apache.http.entity.StringEntity;
|
|
||||||
import org.apache.http.impl.client.DefaultHttpClient;
|
import org.apache.http.impl.client.DefaultHttpClient;
|
||||||
|
|
||||||
import java.io.ByteArrayOutputStream;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Performs a POST request with no parameters to the URL in uri[0].
|
* Performs a POST request with no parameters to the URL in uri[0].
|
||||||
*/
|
*/
|
||||||
class RestTask extends AsyncTask<String, Void, Void> {
|
public class PostTask extends AsyncTask<String, Void, Void> {
|
||||||
|
|
||||||
private static final String TAG = "RequestTask";
|
private static final String TAG = "PostTask";
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Void doInBackground(String... uri) {
|
protected Void doInBackground(String... uri) {
|
||||||
HttpClient httpclient = new DefaultHttpClient();
|
HttpClient httpclient = new DefaultHttpClient();
|
||||||
HttpPost httppost = new HttpPost(uri[0]);
|
HttpPost post = new HttpPost(uri[0]);
|
||||||
String responseString = null;
|
String responseString = null;
|
||||||
try {
|
try {
|
||||||
HttpResponse response = httpclient.execute(httppost);
|
HttpResponse response = httpclient.execute(post);
|
||||||
}
|
}
|
||||||
catch (IOException e) {
|
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;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
package com.nutomic.syncthingandroid;
|
package com.nutomic.syncthingandroid.service;
|
||||||
|
|
||||||
import android.app.Notification;
|
import android.app.Notification;
|
||||||
import android.app.PendingIntent;
|
import android.app.PendingIntent;
|
||||||
|
@ -8,11 +8,16 @@ import android.os.IBinder;
|
||||||
import android.support.v4.app.NotificationCompat;
|
import android.support.v4.app.NotificationCompat;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
|
import com.nutomic.syncthingandroid.R;
|
||||||
|
import com.nutomic.syncthingandroid.WebGuiActivity;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.DataOutputStream;
|
import java.io.DataOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
|
|
||||||
|
import com.nutomic.syncthingandroid.PostTask;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Holds the native syncthing instance and provides an API to access it.
|
* 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";
|
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);
|
private final SyncthingServiceBinder mBinder = new SyncthingServiceBinder(this);
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -123,6 +133,7 @@ public class SyncthingService extends Service {
|
||||||
@Override
|
@Override
|
||||||
public void onDestroy() {
|
public void onDestroy() {
|
||||||
super.onDestroy();
|
super.onDestroy();
|
||||||
new RestTask().execute(SYNCTHING_URL + PATH_SHUTDOWN);
|
new PostTask().execute(SYNCTHING_URL + PATH_SHUTDOWN);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -1,7 +1,9 @@
|
||||||
package com.nutomic.syncthingandroid;
|
package com.nutomic.syncthingandroid.service;
|
||||||
|
|
||||||
import android.os.Binder;
|
import android.os.Binder;
|
||||||
|
|
||||||
|
import com.nutomic.syncthingandroid.service.SyncthingService;
|
||||||
|
|
||||||
public class SyncthingServiceBinder extends Binder {
|
public class SyncthingServiceBinder extends Binder {
|
||||||
|
|
||||||
SyncthingService mService;
|
SyncthingService mService;
|
|
@ -3,8 +3,11 @@
|
||||||
|
|
||||||
<string name="app_name">Syncthing</string>
|
<string name="app_name">Syncthing</string>
|
||||||
|
|
||||||
<!-- Upstream version name of the included syncthing binary -->
|
<!-- Title of the preference showing upstream version name -->
|
||||||
<string name="syncthing_version_title">Syncthing Version: HEAD</string>
|
<string name="syncthing_version_title">Syncthing Version</string>
|
||||||
|
|
||||||
|
<!-- Displayed instead of the version string if it could not be read -->
|
||||||
|
<string name="syncthing_version_error">Could not read version</string>
|
||||||
|
|
||||||
<!-- Title for dialog displayed on first start -->
|
<!-- Title for dialog displayed on first start -->
|
||||||
<string name="welcome_title">First Start</string>
|
<string name="welcome_title">First Start</string>
|
||||||
|
|
|
@ -6,6 +6,7 @@
|
||||||
android:title="@string/report_issue_title" />
|
android:title="@string/report_issue_title" />
|
||||||
|
|
||||||
<Preference
|
<Preference
|
||||||
|
android:key="syncthing_version"
|
||||||
android:title="@string/syncthing_version_title"
|
android:title="@string/syncthing_version_title"
|
||||||
android:enabled="false" />
|
android:enabled="false" />
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue