]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blobdiff - app/src/main/java/com/stoutner/privacybrowser/Webview.java
Remove app bar scrolling because it broke pinch to zoom and some web pages.
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / Webview.java
index 1fd1554828b983ada48c87c2f924fc93529d48b1..99a27344fd6e8651ceab2fb381f0f25950de7cc8 100644 (file)
@@ -1,19 +1,19 @@
 package com.stoutner.privacybrowser;
 
+import android.annotation.SuppressLint;
 import android.app.Activity;
 import android.content.Intent;
 import android.graphics.Bitmap;
 import android.net.Uri;
 import android.os.Bundle;
-import android.support.v4.widget.SwipeRefreshLayout;
 import android.support.v7.app.ActionBar;
 import android.support.v7.app.AppCompatActivity;
+import android.support.v7.app.AppCompatDelegate;
 import android.util.Patterns;
 import android.view.KeyEvent;
 import android.view.Menu;
 import android.view.MenuItem;
 import android.view.View;
-import android.view.ViewTreeObserver;
 import android.view.inputmethod.InputMethodManager;
 import android.webkit.WebChromeClient;
 import android.webkit.WebView;
@@ -31,49 +31,34 @@ public class Webview extends AppCompatActivity {
     static String formattedUrlString;
     static WebView mainWebView;
     static ProgressBar progressBar;
-    static SwipeRefreshLayout swipeToRefresh;
     static EditText urlTextBox;
     static ImageView favoriteIcon;
-    static final String homepage = "https://www.duckduckgo.com";
+    static final String homepage = "https://www.duckduckgo.com/";
+
+    // Remove Android Studio's warning about the dangers of using SetJavaScriptEnabled.
+    @SuppressLint("SetJavaScriptEnabled")
 
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_webview);
 
-        urlTextBox = (EditText) findViewById(R.id.urlTextBox);
-        swipeToRefresh = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayoutContainer);
         mainWebView = (WebView) findViewById(R.id.mainWebView);
-        progressBar = (ProgressBar) findViewById(R.id.progressBar);
-        favoriteIcon = (ImageView) findViewById(R.id.favoriteIcon);
 
-        // Remove the title from the action bar.
         final ActionBar actionBar = getSupportActionBar();
         if (actionBar != null) {
+            // Remove the title from the action bar.
             actionBar.setDisplayShowTitleEnabled(false);
-            // actionBar.setHideOnContentScrollEnabled(true);
-        }
 
-        // Implement swipe down to refresh.
-        swipeToRefresh.setColorSchemeColors(0xFF0097FF);
-        swipeToRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
-            @Override
-            public void onRefresh() {
-                mainWebView.loadUrl(formattedUrlString);
-            }
-        });
+            // Add the custom app_bar layout, which shows the favoriteIcon, urlTextBar, and progressBar.
+            actionBar.setCustomView(R.layout.app_bar);
+            actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
 
-        // Only enable swipeToRefresh if is mainWebView is scrolled to the top.
-        mainWebView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
-            @Override
-            public void onScrollChanged() {
-                if (mainWebView.getScrollY() == 0) {
-                    swipeToRefresh.setEnabled(true);
-                } else {
-                    swipeToRefresh.setEnabled(false);
-                }
-            }
-        });
+            // Initialize the variables for favoriteIcon, urlTextBox, and progressBar
+            favoriteIcon = (ImageView) actionBar.getCustomView().findViewById(R.id.favoriteIcon);
+            urlTextBox = (EditText) actionBar.getCustomView().findViewById(R.id.urlTextBox);
+            progressBar = (ProgressBar) actionBar.getCustomView().findViewById(R.id.progressBar);
+        }
 
         mainWebView.setWebViewClient(new WebViewClient() {
 
@@ -109,9 +94,6 @@ public class Webview extends AppCompatActivity {
                     progressBar.setVisibility(View.VISIBLE);
                 } else {
                     progressBar.setVisibility(View.GONE);
-
-                    // Stop the refreshing indicator if it is running.
-                    swipeToRefresh.setRefreshing(false);
                 }
             }
 
@@ -190,6 +172,10 @@ public class Webview extends AppCompatActivity {
                 mainWebView.loadUrl(homepage);
                 break;
 
+            case R.id.refresh:
+                mainWebView.loadUrl(formattedUrlString);
+                break;
+
             case R.id.back:
                 mainWebView.goBack();
                 break;