mirror of
https://github.com/syncthing/syncthing-android.git
synced 2024-12-23 03:11:30 +00:00
Read web gui url from config instead of hardcoding it.
This commit is contained in:
parent
b1574aaee1
commit
77b7f3a299
6 changed files with 54 additions and 22 deletions
|
@ -26,13 +26,16 @@ public class SettingsActivity extends PreferenceActivity {
|
||||||
|
|
||||||
private SyncthingService mSyncthingService;
|
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() {
|
private ServiceConnection mSyncthingServiceConnection = new ServiceConnection() {
|
||||||
|
|
||||||
public void onServiceConnected(ComponentName className, IBinder service) {
|
public void onServiceConnected(ComponentName className, IBinder service) {
|
||||||
SyncthingServiceBinder binder = (SyncthingServiceBinder) service;
|
SyncthingServiceBinder binder = (SyncthingServiceBinder) service;
|
||||||
mSyncthingService = binder.getService();
|
mSyncthingService = binder.getService();
|
||||||
final PreferenceScreen screen = getPreferenceScreen();
|
Preference version = getPreferenceScreen().findPreference(SYNCTHING_VERSION_KEY);
|
||||||
final Preference version = screen.findPreference(SYNCTHING_VERSION_KEY);
|
|
||||||
version.setSummary(mSyncthingService.getApi().getVersion());
|
version.setSummary(mSyncthingService.getApi().getVersion());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -99,7 +99,7 @@ public class WebGuiActivity extends Activity implements SyncthingService.OnWebGu
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void onWebGuiAvailable() {
|
public void onWebGuiAvailable() {
|
||||||
mWebView.loadUrl(SyncthingService.SYNCTHING_URL);
|
mWebView.loadUrl(mSyncthingService.getApi().getUrl());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -25,12 +25,12 @@ public class GetTask extends AsyncTask<String, Void, String> {
|
||||||
/**
|
/**
|
||||||
* URI to call to get version name.
|
* 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
|
@Override
|
||||||
protected String doInBackground(String... uri) {
|
protected String doInBackground(String... uri) {
|
||||||
HttpClient httpclient = new DefaultHttpClient();
|
HttpClient httpclient = new DefaultHttpClient();
|
||||||
HttpGet get = new HttpGet(uri[0]);
|
HttpGet get = new HttpGet(uri[0] + uri[1]);
|
||||||
String responseString = null;
|
String responseString = null;
|
||||||
try {
|
try {
|
||||||
HttpResponse response = httpclient.execute(get);
|
HttpResponse response = httpclient.execute(get);
|
||||||
|
|
|
@ -20,12 +20,12 @@ public class PostTask extends AsyncTask<String, Void, Void> {
|
||||||
/**
|
/**
|
||||||
* URI to call for shutdown.
|
* 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
|
@Override
|
||||||
protected Void doInBackground(String... uri) {
|
protected Void doInBackground(String... uri) {
|
||||||
HttpClient httpclient = new DefaultHttpClient();
|
HttpClient httpclient = new DefaultHttpClient();
|
||||||
HttpPost post = new HttpPost(uri[0]);
|
HttpPost post = new HttpPost(uri[0] + uri[1]);
|
||||||
String responseString = null;
|
String responseString = null;
|
||||||
try {
|
try {
|
||||||
HttpResponse response = httpclient.execute(post);
|
HttpResponse response = httpclient.execute(post);
|
||||||
|
|
|
@ -13,8 +13,18 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener {
|
||||||
|
|
||||||
private String mVersion;
|
private String mVersion;
|
||||||
|
|
||||||
public RestApi(Context context) {
|
private String mUrl;
|
||||||
|
|
||||||
|
public RestApi(Context context, String url) {
|
||||||
mContext = context;
|
mContext = context;
|
||||||
|
mUrl = url;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the full URL of the web gui.
|
||||||
|
*/
|
||||||
|
public String getUrl() {
|
||||||
|
return mUrl;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -26,15 +36,21 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener {
|
||||||
? versionName
|
? versionName
|
||||||
: mContext.getString(R.string.syncthing_version_error);
|
: 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() {
|
public String getVersion() {
|
||||||
return mVersion;
|
return mVersion;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stops syncthing. You should probably use SyncthingService.stopService() instead.
|
||||||
|
*/
|
||||||
public void shutdown() {
|
public void shutdown() {
|
||||||
new PostTask().execute(PostTask.URI_SHUTDOWN);
|
new PostTask().execute(mUrl, PostTask.URI_SHUTDOWN);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -68,13 +68,6 @@ public class SyncthingService extends Service {
|
||||||
*/
|
*/
|
||||||
private static final String CONFIG_FILE = "config.xml";
|
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 RestApi mApi;
|
||||||
|
|
||||||
private final SyncthingServiceBinder mBinder = new SyncthingServiceBinder(this);
|
private final SyncthingServiceBinder mBinder = new SyncthingServiceBinder(this);
|
||||||
|
@ -192,7 +185,7 @@ public class SyncthingService extends Service {
|
||||||
protected Void doInBackground(Void... voids) {
|
protected Void doInBackground(Void... voids) {
|
||||||
int status = 0;
|
int status = 0;
|
||||||
HttpClient httpclient = new DefaultHttpClient();
|
HttpClient httpclient = new DefaultHttpClient();
|
||||||
HttpHead head = new HttpHead(SYNCTHING_URL);
|
HttpHead head = new HttpHead(mApi.getUrl());
|
||||||
do {
|
do {
|
||||||
try {
|
try {
|
||||||
Thread.sleep(WEB_GUI_POLL_INTERVAL);
|
Thread.sleep(WEB_GUI_POLL_INTERVAL);
|
||||||
|
@ -206,10 +199,10 @@ public class SyncthingService extends Service {
|
||||||
// so we ignore and continue.
|
// so we ignore and continue.
|
||||||
}
|
}
|
||||||
catch (IOException e) {
|
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) {
|
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);
|
} while(status != HttpStatus.SC_OK);
|
||||||
return null;
|
return null;
|
||||||
|
@ -241,8 +234,27 @@ public class SyncthingService extends Service {
|
||||||
n.flags |= Notification.FLAG_ONGOING_EVENT;
|
n.flags |= Notification.FLAG_ONGOING_EVENT;
|
||||||
startForeground(NOTIFICATION_ID, n);
|
startForeground(NOTIFICATION_ID, n);
|
||||||
|
|
||||||
mApi = new RestApi(this);
|
String syncthingUrl = null;
|
||||||
registerOnWebGuiAvailableListener(mApi);
|
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 Thread(new NativeSyncthingRunnable()).start();
|
||||||
new PollWebGuiAvailableTask().execute();
|
new PollWebGuiAvailableTask().execute();
|
||||||
|
@ -295,6 +307,7 @@ public class SyncthingService extends Service {
|
||||||
DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
|
DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
|
||||||
Document d = db.parse(getConfigFile());
|
Document d = db.parse(getConfigFile());
|
||||||
|
|
||||||
|
|
||||||
// Hardcode default globalAnnounceServer ip.
|
// Hardcode default globalAnnounceServer ip.
|
||||||
Element options = (Element)
|
Element options = (Element)
|
||||||
d.getDocumentElement().getElementsByTagName("options").item(0);
|
d.getDocumentElement().getElementsByTagName("options").item(0);
|
||||||
|
|
Loading…
Reference in a new issue