]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blobdiff - app/src/main/java/com/stoutner/privacybrowser/Webview.java
Move favoriteIcon, urlTextBox, and progressBar to a custom view in the app bar.
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / Webview.java
index affbc10ffc3c75f970d79c9f796ca386d80c5bf7..7e87fdfdde415fd4de5765fd526c4988b3cd0fa5 100644 (file)
@@ -1,10 +1,9 @@
 package com.stoutner.privacybrowser;
 
+import android.annotation.SuppressLint;
 import android.app.Activity;
 import android.content.Intent;
 import android.graphics.Bitmap;
-import android.graphics.drawable.BitmapDrawable;
-import android.graphics.drawable.Drawable;
 import android.net.Uri;
 import android.os.Bundle;
 import android.support.v4.widget.SwipeRefreshLayout;
@@ -16,8 +15,8 @@ import android.view.Menu;
 import android.view.MenuItem;
 import android.view.View;
 import android.view.ViewTreeObserver;
+import android.view.Window;
 import android.view.inputmethod.InputMethodManager;
-import android.webkit.ClientCertRequest;
 import android.webkit.WebChromeClient;
 import android.webkit.WebView;
 import android.webkit.WebViewClient;
@@ -39,22 +38,32 @@ public class Webview extends AppCompatActivity {
     static ImageView favoriteIcon;
     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);
+
+            // 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);
+
+            // 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);
+
+            //actionBar.setHideOnContentScrollEnabled(true);
         }
 
         // Implement swipe down to refresh.
@@ -121,7 +130,7 @@ public class Webview extends AppCompatActivity {
             // Set the favorite icon when it changes.
             @Override
             public void onReceivedIcon(WebView view, Bitmap icon) {
-                favoriteIcon.setImageBitmap(icon);
+                favoriteIcon.setImageBitmap(Bitmap.createScaledBitmap(icon, 64, 64, true));
             }
         });
 
@@ -220,7 +229,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()) {
@@ -237,16 +245,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();
@@ -257,7 +261,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.