X-Git-Url: https://gitweb.stoutner.com/?a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fcom%2Fstoutner%2Fprivacybrowser%2FWebview.java;h=ff718ff961bd5a43f83b752bc35f00cec61b1c72;hb=828458a749772bd036f9e2747bd9f6bf9c468691;hp=02613821fa21e4b2e3f4601e91e68828b93135fe;hpb=f5eaa704a5cc7e2240e10d40517980c7152af958;p=PrivacyBrowserAndroid.git diff --git a/app/src/main/java/com/stoutner/privacybrowser/Webview.java b/app/src/main/java/com/stoutner/privacybrowser/Webview.java index 02613821..ff718ff9 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/Webview.java +++ b/app/src/main/java/com/stoutner/privacybrowser/Webview.java @@ -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,41 @@ 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.google.com/"; + + // Remove Android Studio's warning about the dangers of using SetJavaScriptEnabled. + @SuppressLint("SetJavaScriptEnabled") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + + // FEATURE_ACTION_BAR_OVERLAY is required to scroll the actionBar. + supportRequestWindowFeature(AppCompatDelegate.FEATURE_SUPPORT_ACTION_BAR_OVERLAY); + 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); + + // Scroll the actionBar. + actionBar.setHideOnContentScrollEnabled(true); + } mainWebView.setWebViewClient(new WebViewClient() { @@ -109,9 +101,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 +179,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; @@ -217,7 +210,6 @@ public class Webview extends AppCompatActivity { String unformattedUrlString = urlTextBox.getText().toString(); URL unformattedUrl = null; Uri.Builder formattedUri = new Uri.Builder(); - String scheme; // Check to see if unformattedUrlString is a valid URL. Otherwise, convert it into a Duck Duck Go search. if (Patterns.WEB_URL.matcher(unformattedUrlString).matches()) { @@ -234,16 +226,12 @@ public class Webview extends AppCompatActivity { e.printStackTrace(); } - if (unformattedUrl.getProtocol() != null) { - scheme = unformattedUrl.getProtocol(); - } else { - scheme = "http"; - } - - final String authority = unformattedUrl.getAuthority(); - final String path = unformattedUrl.getPath(); - final String query = unformattedUrl.getQuery(); - final String fragment = unformattedUrl.getRef(); + // The ternary operator (? :) makes sure that unformattedUrl.get() does not cause a null pointer exception. + final String scheme = unformattedUrl != null ? unformattedUrl.getProtocol() : null; + final String authority = unformattedUrl != null ? unformattedUrl.getAuthority() : null; + final String path = unformattedUrl != null ? unformattedUrl.getPath() : null; + final String query = unformattedUrl != null ? unformattedUrl.getQuery() : null; + final String fragment = unformattedUrl != null ? unformattedUrl.getRef() : null; formattedUri.scheme(scheme).authority(authority).path(path).query(query).fragment(fragment); formattedUrlString = formattedUri.build().toString(); @@ -254,7 +242,6 @@ public class Webview extends AppCompatActivity { formattedUrlString = "https://duckduckgo.com/?q=" + encodedUrlString; } - // Place formattedUrlString back in the address bar and load the website. mainWebView.loadUrl(formattedUrlString); // Hides the keyboard so we can see the webpage.