mirror of
https://github.com/syncthing/syncthing-android.git
synced 2024-12-23 11:21:29 +00:00
Use Runnable instead of AsyncTask to poll for syncthing web gui (fixes #41).
This works around a problem with pre-Jellybean devices, where AsyncTask must be created on the main thread,
This commit is contained in:
parent
08d824a46e
commit
a215e30098
2 changed files with 11 additions and 8 deletions
|
@ -99,8 +99,13 @@ public class WebGuiActivity extends Activity implements SyncthingService.OnWebGu
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void onWebGuiAvailable() {
|
public void onWebGuiAvailable() {
|
||||||
|
runOnUiThread(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
mWebView.loadUrl(mSyncthingService.getApi().getUrl());
|
mWebView.loadUrl(mSyncthingService.getApi().getUrl());
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onDestroy() {
|
public void onDestroy() {
|
||||||
|
|
|
@ -200,9 +200,9 @@ public class SyncthingService extends Service {
|
||||||
* Polls SYNCTHING_URL until it returns HTTP status OK, then calls all listeners
|
* Polls SYNCTHING_URL until it returns HTTP status OK, then calls all listeners
|
||||||
* in mOnWebGuiAvailableListeners and clears it.
|
* in mOnWebGuiAvailableListeners and clears it.
|
||||||
*/
|
*/
|
||||||
private class PollWebGuiAvailableTask extends AsyncTask<Void, Void, Void> {
|
private class PollWebGuiAvailableRunnable implements Runnable {
|
||||||
@Override
|
@Override
|
||||||
protected Void doInBackground(Void... voids) {
|
public void run() {
|
||||||
int status = 0;
|
int status = 0;
|
||||||
HttpClient httpclient = new DefaultHttpClient();
|
HttpClient httpclient = new DefaultHttpClient();
|
||||||
HttpHead head = new HttpHead(mApi.getUrl());
|
HttpHead head = new HttpHead(mApi.getUrl());
|
||||||
|
@ -225,11 +225,7 @@ public class SyncthingService extends Service {
|
||||||
Log.w(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;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void onPostExecute(Void aVoid) {
|
|
||||||
Log.i(TAG, "Web GUI has come online at " + mApi.getUrl());
|
Log.i(TAG, "Web GUI has come online at " + mApi.getUrl());
|
||||||
mIsWebGuiAvailable = true;
|
mIsWebGuiAvailable = true;
|
||||||
for (OnWebGuiAvailableListener listener : mOnWebGuiAvailableListeners) {
|
for (OnWebGuiAvailableListener listener : mOnWebGuiAvailableListeners) {
|
||||||
|
@ -332,7 +328,7 @@ public class SyncthingService extends Service {
|
||||||
new PostTask().execute(mApi.getUrl(), PostTask.URI_SHUTDOWN, apiKey);
|
new PostTask().execute(mApi.getUrl(), PostTask.URI_SHUTDOWN, apiKey);
|
||||||
registerOnWebGuiAvailableListener(mApi);
|
registerOnWebGuiAvailableListener(mApi);
|
||||||
}
|
}
|
||||||
new PollWebGuiAvailableTask().execute();
|
new Thread(new PollWebGuiAvailableRunnable()).start();
|
||||||
runNative();
|
runNative();
|
||||||
}
|
}
|
||||||
}).start();
|
}).start();
|
||||||
|
@ -362,6 +358,8 @@ public class SyncthingService extends Service {
|
||||||
*
|
*
|
||||||
* If the web gui is already available, listener will be called immediately.
|
* If the web gui is already available, listener will be called immediately.
|
||||||
* Listeners are unregistered automatically after being called.
|
* Listeners are unregistered automatically after being called.
|
||||||
|
*
|
||||||
|
* Note that the listener might be called on a background thread.
|
||||||
*/
|
*/
|
||||||
public void registerOnWebGuiAvailableListener(OnWebGuiAvailableListener listener) {
|
public void registerOnWebGuiAvailableListener(OnWebGuiAvailableListener listener) {
|
||||||
if (mIsWebGuiAvailable) {
|
if (mIsWebGuiAvailable) {
|
||||||
|
|
Loading…
Reference in a new issue