Implement SwipeToRefresh for real this time.
authorSoren Stoutner <soren@stoutner.com>
Thu, 18 Feb 2016 05:08:48 +0000 (22:08 -0700)
committerSoren Stoutner <soren@stoutner.com>
Thu, 18 Feb 2016 05:08:48 +0000 (22:08 -0700)
12 files changed:
.idea/dictionaries/soren.xml
app/app.iml
app/build.gradle
app/src/main/AndroidManifest.xml
app/src/main/java/com/stoutner/privacybrowser/MainWebViewActivity.java
app/src/main/res/layout/activity_webview.xml
app/src/main/res/layout/url_bar.xml
app/src/main/res/menu/menu_webview.xml
app/src/main/res/values-v11/styles.xml [new file with mode: 0644]
app/src/main/res/values/colors.xml
app/src/main/res/values/styles.xml
app/src/main/res/xml/preferences.xml

index 03413ea..db1251c 100644 (file)
@@ -2,6 +2,7 @@
   <dictionary name="soren">
     <words>
       <w>duckduckgo</w>
+      <w>privacybrowser</w>
       <w>rehide</w>
       <w>rehides</w>
       <w>soren</w>
index c752c86..41cbfa7 100644 (file)
@@ -71,6 +71,8 @@
       <excludeFolder url="file://$MODULE_DIR$/build/intermediates/dependency-cache" />
       <excludeFolder url="file://$MODULE_DIR$/build/intermediates/dex" />
       <excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.android.support/appcompat-v7/23.1.1/jars" />
+      <excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.android.support/design/23.1.1/jars" />
+      <excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.android.support/recyclerview-v7/23.1.1/jars" />
       <excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.android.support/support-v4/23.1.1/jars" />
       <excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental" />
       <excludeFolder url="file://$MODULE_DIR$/build/intermediates/jniLibs" />
     </content>
     <orderEntry type="jdk" jdkName="Android API 23 Platform" jdkType="Android SDK" />
     <orderEntry type="sourceFolder" forTests="false" />
+    <orderEntry type="library" exported="" name="recyclerview-v7-23.1.1" level="project" />
     <orderEntry type="library" exported="" name="support-annotations-23.1.1" level="project" />
     <orderEntry type="library" exported="" name="support-v4-23.1.1" level="project" />
+    <orderEntry type="library" exported="" name="design-23.1.1" level="project" />
     <orderEntry type="library" exported="" name="appcompat-v7-23.1.1" level="project" />
   </component>
 </module>
\ No newline at end of file
index b5963c2..f741400 100644 (file)
@@ -23,4 +23,6 @@ android {
 dependencies {
     compile fileTree(dir: 'libs', include: ['*.jar'])
     compile 'com.android.support:appcompat-v7:23.1.1'
+    compile 'com.android.support:design:23.1.1'
+    compile 'com.android.support:support-v4:23.1.1'
 }
index bcf0270..febfeaf 100644 (file)
@@ -26,6 +26,7 @@
     <!-- Required to create homescreen shortcuts. -->
     <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
 
+    <!-- on API >= 23, app data is automatically backed up to Google cloud servers unless android:allowBackup="false" and android:fullBackupContent="false" is set. -->
     <application
         android:allowBackup="false"
         android:fullBackupContent="false"
@@ -40,7 +41,8 @@
             android:name=".MainWebViewActivity"
             android:configChanges="orientation|screenSize"
             android:label="@string/privacy_browser"
-            android:launchMode="singleTask">
+            android:launchMode="singleTask"
+            android:theme="@style/AppTheme.NoActionBar">
 
             <intent-filter>
                 <action android:name="android.intent.action.MAIN" />
index 5d23073..7f49a6c 100644 (file)
@@ -31,6 +31,7 @@ import android.os.Build;
 import android.os.Bundle;
 import android.preference.PreferenceManager;
 import android.support.v4.app.DialogFragment;
+import android.support.v4.widget.SwipeRefreshLayout;
 import android.support.v7.app.ActionBar;
 import android.support.v7.app.AppCompatActivity;
 import android.support.v7.app.AppCompatDialogFragment;
@@ -40,7 +41,6 @@ import android.view.KeyEvent;
 import android.view.Menu;
 import android.view.MenuItem;
 import android.view.View;
-import android.view.Window;
 import android.view.inputmethod.InputMethodManager;
 import android.webkit.CookieManager;
 import android.webkit.DownloadListener;
@@ -90,14 +90,9 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateHome
     private EditText urlTextBox;
 
     @Override
-    // Remove Android Studio's warning about the dangers of using SetJavaScriptEnabled.
+    // Remove Android Studio's warning about the dangers of using SetJavaScriptEnabled.  The whole premise of Privacy Browser is built around an understanding of these dangers.
     @SuppressLint("SetJavaScriptEnabled")
     protected void onCreate(Bundle savedInstanceState) {
-        // Window.FEATURE_ACTION_BAR_OVERLAY must be enabled to set the app bar to HideOnContentScroll.  It must be set before any content is added to the activity.
-        if (Build.VERSION.SDK_INT >= 11) {
-            requestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
-        }
-
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_webview);
 
@@ -110,19 +105,19 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateHome
         // We need to use the SupportActionBar from android.support.v7.app.ActionBar until the minimum API is >= 21.
         final ActionBar appBar = getSupportActionBar();
 
-        mainWebView = (WebView) findViewById(R.id.mainWebView);
-
-        if (appBar != null) {
-            /* TODO Enable app bar scrolling.
-            // Scroll the app bar, but only if the API supports overlay mode (>= 11).
-            if (Build.VERSION.SDK_INT >= 11) {
-                appBar.setHideOnContentScrollEnabled(true);
+        // Implement swipe to refresh
+        final SwipeRefreshLayout swipeToRefresh = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);
+        swipeToRefresh.setColorSchemeResources(R.color.blue);
+        swipeToRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
+            @Override
+            public void onRefresh() {
+                mainWebView.reload();
             }
-            */
+        });
 
-            // Remove the title from the app bar.
-            appBar.setDisplayShowTitleEnabled(false);
+        mainWebView = (WebView) findViewById(R.id.mainWebView);
 
+        if (appBar != null) {
             // Add the custom url_bar layout, which shows the favoriteIcon, urlTextBar, and progressBar.
             appBar.setCustomView(R.layout.url_bar);
             appBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
@@ -189,6 +184,9 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateHome
                         progressBar.setVisibility(View.VISIBLE);
                     } else {
                         progressBar.setVisibility(View.GONE);
+
+                        //Stop the SwipeToRefresh indicator if it is running
+                        swipeToRefresh.setRefreshing(false);
                     }
                 }
             }
@@ -340,6 +338,9 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateHome
 
         // Load the website.
         mainWebView.loadUrl(formattedUrlString);
+
+        // Clear the keyboard if displayed and remove the focus on the urlTextBar if it has it.
+        mainWebView.requestFocus();
     }
 
     @Override
@@ -563,10 +564,6 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateHome
                 mainWebView.loadUrl(homepage);
                 return true;
 
-            case R.id.refresh:
-                mainWebView.reload();
-                return true;
-
             case R.id.back:
                 mainWebView.goBack();
                 return true;
index f50c399..d90adc7 100644 (file)
   along with Privacy Browser.  If not, see <http://www.gnu.org/licenses/>. -->
 
 <!-- Setting the layout root to be focusableInTouchMode prevents urlTextBox from stealing focus on launch and opening the keyboard. -->
-<RelativeLayout
-    android:id="@+id/rootRelativeLayout"
+<android.support.design.widget.CoordinatorLayout
+    android:id="@+id/rootCoordinatorLayout"
     xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto"
+    xmlns:tools="http://schemas.android.com/tools"
+    tools:context="com.stoutner.privacybrowser.MainWebViewActivity"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
+    android:focusable="true"
     android:focusableInTouchMode="true" >
 
-    <android.support.v7.widget.Toolbar
-        android:id="@+id/appBar"
+    <android.support.design.widget.AppBarLayout
+        android:id="@+id/appBarLayout"
         android:layout_width="match_parent"
-        android:layout_height="?attr/actionBarSize"
-        android:background="?attr/colorPrimary"
-        android:elevation="4dp"
-        android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
-        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
-
-    <WebView
-        android:id="@+id/mainWebView"
-        android:layout_below="@id/appBar"
+        android:layout_height="wrap_content"
+        android:theme="@style/AppTheme.AppBarOverlay" >
+
+        <android.support.v7.widget.Toolbar
+            android:id="@+id/appBar"
+            android:layout_width="match_parent"
+            android:layout_height="?attr/actionBarSize"
+            android:background="?attr/colorPrimary"
+            app:popupTheme="@style/AppTheme.PopupOverlay" />
+    </android.support.design.widget.AppBarLayout>
+
+    <!-- android:layout_martinTop="?attr/actionBarSize" moves the SwipeRefreshLayout below the AppBarLayout, which otherwise would cover the top of the WebView. -->
+    <android.support.v4.widget.SwipeRefreshLayout
+        android:id="@+id/swipeRefreshLayout"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
-        android:focusable="true"
-        android:focusableInTouchMode="true" />
+        android:layout_marginTop="?attr/actionBarSize">
+
+        <!-- Google does not currently want to support hiding the AppBar on scroll for a WebView child with the Support Toolbar.  https://code.google.com/p/android/issues/detail?id=200394 -->
+        <WebView
+            android:id="@+id/mainWebView"
+            android:layout_width="match_parent"
+            android:layout_height="match_parent"
+            android:focusable="true"
+            android:focusableInTouchMode="true"/>
+
+    </android.support.v4.widget.SwipeRefreshLayout>
 
     <!-- fullScreenVideoFrameLayout is used to display full screen videos.  It is initially android:visibility="gone" to hide it from view. -->
     <FrameLayout
@@ -51,4 +68,4 @@
         android:layout_height="match_parent"
         android:visibility="gone"
         android:background="@color/black" />
-</RelativeLayout>
+</android.support.design.widget.CoordinatorLayout>
\ No newline at end of file
index 2363375..b5fd028 100644 (file)
     android:layout_width="match_parent"
     android:layout_height="wrap_content">
 
+    <!-- RelativeLayout is used instead of a LinearLayout because supportAppBar does not let android:layout_weight="1" to cause urlTextBox to fill all the available space. -->
     <RelativeLayout
         android:id="@+id/addressBarRelativeLayout"
         android:layout_width="match_parent"
         android:layout_height="wrap_content">
 
         <!-- Set the program icon as the initial favoriteIcon. -->
-        <!-- layout_width and layout_height of 28dp matches the appBar icons. -->
+        <!-- layout_width and layout_height of 26dp matches the AppBar icons. -->
         <ImageView
             android:id="@+id/favoriteIcon"
             android:src="@drawable/ic_language_black_24dp"
-            android:layout_width="28dp"
-            android:layout_height="28dp"
+            android:layout_width="26dp"
+            android:layout_height="26dp"
             android:layout_centerVertical="true"
             android:contentDescription="@string/favorite_icon"/>
 
@@ -62,7 +63,8 @@
         android:layout_height="3dp"
         android:layout_gravity="bottom"
         android:max="100"
-        android:progressTint="@color/light_blue"
+        android:progressTint="@color/blue"
         android:progressBackgroundTint="@color/white"
         android:visibility="gone" />
+
 </FrameLayout>
\ No newline at end of file
index eb964bf..7ff39ec 100644 (file)
                 android:orderInCategory="91"
                 app:showAsAction="never" />
 
-            <item
-                android:id="@+id/refresh"
-                android:title="@string/refresh"
-                android:orderInCategory="92"
-                app:showAsAction="never" />
-
             <item
                 android:id="@+id/back"
                 android:title="@string/back"
-                android:orderInCategory="93"
+                android:orderInCategory="92"
                 app:showAsAction="never" />
 
             <item
                 android:id="@+id/forward"
                 android:title="@string/forward"
-                android:orderInCategory="94"
+                android:orderInCategory="93"
                 app:showAsAction="never" />
         </menu>
     </item>
diff --git a/app/src/main/res/values-v11/styles.xml b/app/src/main/res/values-v11/styles.xml
new file mode 100644 (file)
index 0000000..716ca04
--- /dev/null
@@ -0,0 +1,30 @@
+<?xml version="1.0" encoding="utf-8"?>
+
+<!--
+  Copyright 2016 Soren Stoutner <soren@stoutner.com>.
+
+  This file is part of Privacy Browser <https://privacybrowser.stoutner.com/>.
+
+  Privacy Browser is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 3 of the License, or
+  (at your option) any later version.
+
+  Privacy Browser is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  GNU General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with Privacy Browser.  If not, see <http://www.gnu.org/licenses/>. -->
+
+<resources>
+
+    <!-- android:actionBarSize is supported by API >= 11.  actionBarSize is for older versions. -->
+    <!-- The default actionBarSize is 56dp.  53dp is the smallest that doesn't cause the urlTextBox to scroll vertically with a font size of Huge. -->
+    <style name="AppTheme" parent="Theme.AppCompat.Light">
+        <item name="colorAccent">@color/blue</item>
+        <item name="android:actionBarSize">53dp</item>
+    </style>
+
+</resources>
\ No newline at end of file
index 8fe826f..34a8707 100644 (file)
@@ -21,7 +21,7 @@
 <!-- These color resources are hardcoded for vector drawables.  Once the minimum API is >= 22 we can remove the hardcoded colors and reference these entries. -->
 <resources>
     <color name="black">#FF000000</color>
-    <color name="light_blue">#FF0091EA</color>
+    <color name="blue">#1e88e5</color>
     <color name="green">#FF64DD17</color>
     <color name="red">#FFD50000</color>
     <color name="white">#FFFFFFFF</color>
index a68cd81..3553083 100644 (file)
 
 <resources>
 
-    <!-- We want to use .NoActionBar so that we can set a SupportActionBar. -->
     <!-- android:actionBarSize is supported by API >= 11.  actionBarSize is for older versions. -->
     <!-- The default actionBarSize is 56dp.  53dp is the smallest that doesn't cause the urlTextBox to scroll vertically with a font size of Huge. -->
-    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
-        <item name="colorAccent">@color/light_blue</item>
-        <item name="android:actionBarSize">53dp</item>
+    <style name="AppTheme" parent="Theme.AppCompat.Light">
+        <item name="colorAccent">@color/blue</item>
         <item name="actionBarSize">53dp</item>
     </style>
 
+    <style name="AppTheme.NoActionBar">
+        <item name="windowActionBar">false</item>
+        <item name="windowNoTitle">true</item>
+    </style>
+
+    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.ActionBar" />
+
+    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
+
 </resources>
index 97965a5..b56f932 100644 (file)
         android:key="privacy_settings"
         android:title="@string/privacy_settings" >
 
-        <CheckBoxPreference
+        <SwitchPreference
             android:key="javascript_enabled"
             android:title="@string/javascript_preference"
             android:summary="@string/javascript_preference_summary"
             android:defaultValue="false" />
 
-        <CheckBoxPreference
+        <SwitchPreference
             android:key="dom_storage_enabled"
             android:title="@string/dom_storage_preference"
             android:summary="@string/dom_storage_preference_summary"
             android:defaultValue="false" />
 
-        <CheckBoxPreference
+        <SwitchPreference
             android:key="cookies_enabled"
             android:title="@string/cookies_preference"
             android:summary="@string/cookies_preference_summary"
@@ -50,6 +50,7 @@
             android:key="homepage"
             android:title="@string/homepage_preference"
             android:summary="@string/homepage_preference_summary"
-            android:defaultValue="https://www.duckduckgo.com" />
+            android:defaultValue="https://www.duckduckgo.com"
+            android:singleLine="true"/>
     </PreferenceCategory>
 </PreferenceScreen>
\ No newline at end of file