From ae8cadde959baf423c4f8c8793ebce8253993603 Mon Sep 17 00:00:00 2001
From: zzz <zzz@mail.i2p>
Date: Mon, 20 Jun 2011 20:23:46 +0000
Subject: [PATCH] - Support eepsites in web viewer - Async task for web/eep
 loads - Progress dialog for web/eep loads - Cancel loads with back button

---
 src/net/i2p/android/apps/EepGetFetcher.java   |  10 +-
 .../router/activity/I2PWebViewClient.java     | 125 +++++++++++++++++-
 .../android/router/activity/NewsActivity.java |   9 +-
 .../android/router/activity/WebActivity.java  |   9 +-
 4 files changed, 137 insertions(+), 16 deletions(-)

diff --git a/src/net/i2p/android/apps/EepGetFetcher.java b/src/net/i2p/android/apps/EepGetFetcher.java
index a3849f8c0..9290f50a9 100644
--- a/src/net/i2p/android/apps/EepGetFetcher.java
+++ b/src/net/i2p/android/apps/EepGetFetcher.java
@@ -33,9 +33,13 @@ public class EepGetFetcher implements EepGet.StatusListener {
         _eepget = new EepGet(_context, true, "localhost", 4444, 0, -1, MAX_LEN,
                              _file.getAbsolutePath(), null, url,
                              true, null, null, null);
-        _eepget.addStatusListener(this);
+        //_eepget.addStatusListener(this);
     }
     
+    public void addStatusListener(EepGet.StatusListener l) {
+        _eepget.addStatusListener(l);
+    }
+
     public boolean fetch() {
         _success = _eepget.fetch();
         return _success;
@@ -48,8 +52,8 @@ public class EepGetFetcher implements EepGet.StatusListener {
         if (!_success)
             return "text/plain";
         String rv = _eepget.getContentType();
-        if (rv == null)
-            return "text/html";
+        if (rv == null || rv.equals("text/html"))
+            return "text/html; charset=utf-8";
         return rv;
     }
 
diff --git a/src/net/i2p/android/router/activity/I2PWebViewClient.java b/src/net/i2p/android/router/activity/I2PWebViewClient.java
index e9f075524..f6033557c 100644
--- a/src/net/i2p/android/router/activity/I2PWebViewClient.java
+++ b/src/net/i2p/android/router/activity/I2PWebViewClient.java
@@ -1,5 +1,8 @@
 package net.i2p.android.router.activity;
 
+import android.app.Dialog;
+import android.app.ProgressDialog;
+import android.os.AsyncTask;
 import android.webkit.WebView;
 import android.webkit.WebViewClient;
 
@@ -7,6 +10,7 @@ import java.net.URI;
 import java.net.URISyntaxException;
 
 import net.i2p.android.apps.EepGetFetcher;
+import net.i2p.util.EepGet;
 
 class I2PWebViewClient extends WebViewClient {
 
@@ -29,6 +33,8 @@ class I2PWebViewClient extends WebViewClient {
             String h = uri.getHost();
             if (h == null)
                 return false;
+
+            view.stopLoading();
             view.getSettings().setBuiltInZoomControls(true);
             h = h.toLowerCase();
             if (h.endsWith(".i2p")) {
@@ -36,19 +42,124 @@ class I2PWebViewClient extends WebViewClient {
                 //    return false;
                 view.getSettings().setLoadsImagesAutomatically(false);
                 //view.loadData(ERROR_EEPSITE, "text/html", "UTF-8");
-                EepGetFetcher fetcher = new EepGetFetcher(url);
-                // todo spinner
-                boolean success = fetcher.fetch();
-                if (!success)
-                    System.err.println("Fetch failed for " + url);
-                view.loadData(fetcher.getData(), fetcher.getContentType(), fetcher.getEncoding());
+                (new BackgroundEepLoad(view, h)).execute(url);
             } else {
                 view.getSettings().setLoadsImagesAutomatically(true);
-                view.loadUrl(url);
+                //view.loadUrl(url);
+                (new BackgroundLoad(view)).execute(url);
             }
             return true;
         } catch (URISyntaxException use) {
             return false;
         }
     }
+
+    private static class BackgroundLoad extends AsyncTask<String, Integer, Integer> {
+        private final WebView _view;
+        private ProgressDialog _dialog;
+
+        public BackgroundLoad(WebView view) {
+            _view = view;
+        }
+
+        protected Integer doInBackground(String... urls) {
+            publishProgress(Integer.valueOf(-1));
+            _view.loadUrl(urls[0]);
+            return Integer.valueOf(0);
+        }
+
+        protected void onProgressUpdate(Integer... progress) {
+            if (progress[0].intValue() < 0) {
+                _dialog = ProgressDialog.show(_view.getContext(), "Loading", "some url");
+                _dialog.setCancelable(true);
+            }
+        }
+
+        protected void onPostExecute(Integer result) {
+            if (_dialog != null)
+                _dialog.dismiss();
+        }
+    }
+
+    private static class BackgroundEepLoad extends AsyncTask<String, Integer, Integer> implements EepGet.StatusListener {
+        private final WebView _view;
+        private final String _host;
+        private ProgressDialog _dialog;
+        private int _total;
+        private String _data;
+
+        public BackgroundEepLoad(WebView view, String host) {
+            _view = view;
+            _host = host;
+        }
+
+        protected Integer doInBackground(String... urls) {
+            String url = urls[0];
+            publishProgress(Integer.valueOf(-1));
+            EepGetFetcher fetcher = new EepGetFetcher(url);
+            fetcher.addStatusListener(this);
+            boolean success = fetcher.fetch();
+            if (!success)
+                System.err.println("Fetch failed for " + url);
+            String d = fetcher.getData();
+            String t = fetcher.getContentType();
+            String e = fetcher.getEncoding();
+            System.err.println("Len: " + d.length() + " type: \"" + t + "\" encoding: \"" + e + '"');
+            _view.loadData(d, t, e);
+            return Integer.valueOf(0);
+        }
+
+        protected void onProgressUpdate(Integer... progress) {
+            int prog = progress[0].intValue();
+            if (prog < 0) {
+                // Can't change style on the fly later, results in an NPE in setMax()
+                //_dialog = ProgressDialog.show(_view.getContext(), "Fetching...", "from " + _host);
+                ProgressDialog d = new ProgressDialog(_view.getContext());
+                d.setCancelable(true);
+                d.setTitle("Fetching...");
+                d.setMessage("from " + _host);
+                d.setIndeterminate(true);
+                d.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
+                d.show();
+                _dialog = d;
+            } else if (prog == 0 && _total > 0) {
+                _dialog.setTitle("Downloading");
+                _dialog.setIndeterminate(false);
+                _dialog.setMax(_total);
+                _dialog.setProgress(0);
+            } else if (_total > 0) {
+                _dialog.setProgress(prog);
+            } else {
+                // nothing
+            }
+        }
+
+        protected void onPostExecute(Integer result) {
+            if (_dialog != null)
+                _dialog.dismiss();
+        }
+
+        // EepGet callbacks
+
+        public void attemptFailed(String url, long bytesTransferred, long bytesRemaining, int currentAttempt, int numRetries, Exception cause) {}
+
+        public void bytesTransferred(long alreadyTransferred, int currentWrite, long bytesTransferred, long bytesRemaining, String url) {
+            publishProgress(Integer.valueOf(Math.max(0, (int) (alreadyTransferred + bytesTransferred))));
+        }
+
+        public void transferComplete(long alreadyTransferred, long bytesTransferred, long bytesRemaining, String url, String outputFile, boolean notModified) {}
+
+        public void transferFailed(String url, long bytesTransferred, long bytesRemaining, int currentAttempt) {}
+
+        public void headerReceived(String url, int attemptNum, String key, String val) {
+            if (key.equalsIgnoreCase("Content-Length")) {
+                try {
+                    _total = Integer.parseInt(val.trim());
+                    publishProgress(Integer.valueOf(0));
+                } catch (NumberFormatException nfe) {}
+            }
+        }
+
+        public void attempting(String url) {}
+    }
 }
diff --git a/src/net/i2p/android/router/activity/NewsActivity.java b/src/net/i2p/android/router/activity/NewsActivity.java
index 354bdc839..564926d8b 100644
--- a/src/net/i2p/android/router/activity/NewsActivity.java
+++ b/src/net/i2p/android/router/activity/NewsActivity.java
@@ -93,9 +93,12 @@ public class NewsActivity extends I2PActivityBase {
     @Override
     public boolean onKeyDown(int keyCode, KeyEvent event) {
         WebView wv = (WebView) findViewById(R.id.news_webview);
-        if ((keyCode == KeyEvent.KEYCODE_BACK) && wv.canGoBack()) {
-            wv.goBack();
-            return true;
+        if ((keyCode == KeyEvent.KEYCODE_BACK)) {
+            wv.stopLoading();
+            if (wv.canGoBack()) {
+                wv.goBack();
+                return true;
+            }
         }
         return super.onKeyDown(keyCode, event);
     }
diff --git a/src/net/i2p/android/router/activity/WebActivity.java b/src/net/i2p/android/router/activity/WebActivity.java
index cb80c5c98..32c5abb7d 100644
--- a/src/net/i2p/android/router/activity/WebActivity.java
+++ b/src/net/i2p/android/router/activity/WebActivity.java
@@ -73,9 +73,12 @@ public class WebActivity extends I2PActivityBase {
     @Override
     public boolean onKeyDown(int keyCode, KeyEvent event) {
         WebView wv = (WebView) findViewById(R.id.browser_webview);
-        if ((keyCode == KeyEvent.KEYCODE_BACK) && wv.canGoBack()) {
-            wv.goBack();
-            return true;
+        if ((keyCode == KeyEvent.KEYCODE_BACK)) {
+            wv.stopLoading();
+            if (wv.canGoBack()) {
+                wv.goBack();
+                return true;
+            }
         }
         return super.onKeyDown(keyCode, event);
     }
-- 
GitLab