Skip to content
This repository has been archived by the owner on Dec 3, 2024. It is now read-only.

Commit

Permalink
Use Runnable instead of AsyncTask to poll for syncthing web gui (fixes
Browse files Browse the repository at this point in the history
…#41).

This works around a  problem with pre-Jellybean devices, where AsyncTask
must be created on the main thread,
  • Loading branch information
Nutomic committed Jun 18, 2014
1 parent 08d824a commit a215e30
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,12 @@ public void onCreate(Bundle savedInstanceState) {
*/
@Override
public void onWebGuiAvailable() {
mWebView.loadUrl(mSyncthingService.getApi().getUrl());
runOnUiThread(new Runnable() {
@Override
public void run() {
mWebView.loadUrl(mSyncthingService.getApi().getUrl());
}
});
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,9 @@ public void run() {
* Polls SYNCTHING_URL until it returns HTTP status OK, then calls all listeners
* in mOnWebGuiAvailableListeners and clears it.
*/
private class PollWebGuiAvailableTask extends AsyncTask<Void, Void, Void> {
private class PollWebGuiAvailableRunnable implements Runnable {
@Override
protected Void doInBackground(Void... voids) {
public void run() {
int status = 0;
HttpClient httpclient = new DefaultHttpClient();
HttpHead head = new HttpHead(mApi.getUrl());
Expand All @@ -225,11 +225,7 @@ protected Void doInBackground(Void... voids) {
Log.w(TAG, "Failed to poll for web interface", e);
}
} while(status != HttpStatus.SC_OK);
return null;
}

@Override
protected void onPostExecute(Void aVoid) {
Log.i(TAG, "Web GUI has come online at " + mApi.getUrl());
mIsWebGuiAvailable = true;
for (OnWebGuiAvailableListener listener : mOnWebGuiAvailableListeners) {
Expand Down Expand Up @@ -332,7 +328,7 @@ public void run() {
new PostTask().execute(mApi.getUrl(), PostTask.URI_SHUTDOWN, apiKey);
registerOnWebGuiAvailableListener(mApi);
}
new PollWebGuiAvailableTask().execute();
new Thread(new PollWebGuiAvailableRunnable()).start();
runNative();
}
}).start();
Expand Down Expand Up @@ -362,6 +358,8 @@ public boolean isWebGuiAvailable() {
*
* If the web gui is already available, listener will be called immediately.
* Listeners are unregistered automatically after being called.
*
* Note that the listener might be called on a background thread.
*/
public void registerOnWebGuiAvailableListener(OnWebGuiAvailableListener listener) {
if (mIsWebGuiAvailable) {
Expand Down

0 comments on commit a215e30

Please sign in to comment.