From 76d7429a5873792897b52d540eae89993fc882b5 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Tue, 6 May 2014 12:03:09 +0200 Subject: [PATCH] Changed Activity to show web UI. --- AndroidManifest.xml | 4 +- res/layout/main.xml | 21 ++++++ .../syncthingandroid/MainActivity.java | 17 ----- .../syncthingandroid/WebGuiActivity.java | 72 +++++++++++++++++++ 4 files changed, 95 insertions(+), 19 deletions(-) create mode 100644 res/layout/main.xml delete mode 100644 src/com/nutomic/syncthingandroid/MainActivity.java create mode 100644 src/com/nutomic/syncthingandroid/WebGuiActivity.java diff --git a/AndroidManifest.xml b/AndroidManifest.xml index ea829eb4..9f821cec 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -14,8 +14,8 @@ android:label="@string/app_name" android:icon="@drawable/ic_launcher" > - diff --git a/res/layout/main.xml b/res/layout/main.xml new file mode 100644 index 00000000..98a3b1c2 --- /dev/null +++ b/res/layout/main.xml @@ -0,0 +1,21 @@ + + + + + + + + + diff --git a/src/com/nutomic/syncthingandroid/MainActivity.java b/src/com/nutomic/syncthingandroid/MainActivity.java deleted file mode 100644 index 91e5c8c0..00000000 --- a/src/com/nutomic/syncthingandroid/MainActivity.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.nutomic.syncthingandroid; - -import android.app.Activity; -import android.content.Intent; -import android.os.Bundle; - -public class MainActivity extends Activity { - - private static final String TAG = "MainActivity"; - - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - startService(new Intent(this, SyncthingService.class)); - } - -} diff --git a/src/com/nutomic/syncthingandroid/WebGuiActivity.java b/src/com/nutomic/syncthingandroid/WebGuiActivity.java new file mode 100644 index 00000000..1e46ce06 --- /dev/null +++ b/src/com/nutomic/syncthingandroid/WebGuiActivity.java @@ -0,0 +1,72 @@ +package com.nutomic.syncthingandroid; + +import android.app.Activity; +import android.content.Intent; +import android.os.Bundle; +import android.os.Handler; +import android.view.View; +import android.webkit.WebView; +import android.webkit.WebViewClient; +import android.widget.ProgressBar; + +public class WebGuiActivity extends Activity { + + private static final String TAG = "WebGuiActivity"; + + /** + * URL of the local syncthing web UI. + * + * TODO: read this out from native code. + */ + private static final String SYNCTHING_URL = "http://127.0.0.1:8080"; + + private WebView mWebView; + private ProgressBar mLoadingView; + + /** + * Retries loading every second until the web UI becomes available. + */ + private WebViewClient mWebViewClient = new WebViewClient() { + + private int mError = 0; + + @Override + public void onReceivedError(WebView view, int errorCode, String description, + String failingUrl) { + mError = errorCode; + new Handler().postDelayed(new Runnable() { + @Override + public void run() { + mError = 0; + mWebView.loadUrl(SYNCTHING_URL); + } + }, 1000); + + } + + @Override + public void onPageFinished(WebView view, String url) { + if (mError == 0) { + mWebView.setVisibility(View.VISIBLE); + mLoadingView.setVisibility(View.GONE); + } + } + }; + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + startService(new Intent(this, SyncthingService.class)); + + setContentView(R.layout.main); + + mLoadingView = (ProgressBar) findViewById(R.id.loading); + mLoadingView.setIndeterminate(true); + mWebView = (WebView) findViewById(R.id.webview); + mWebView.getSettings().setJavaScriptEnabled(true); + mWebView.setWebViewClient(new WebViewClient()); + mWebView.setWebViewClient(mWebViewClient); + mWebView.loadUrl(SYNCTHING_URL); + } + +}