From 5b8c49dd4b910280e463458b574ef11f3dd9a59d Mon Sep 17 00:00:00 2001 From: PGMacDesign Date: Tue, 19 Mar 2019 17:01:06 -0700 Subject: [PATCH] Adding in support for custom headers --- .../turbolinks/TurbolinksSession.java | 170 ++++++++++++++---- 1 file changed, 135 insertions(+), 35 deletions(-) diff --git a/turbolinks/src/main/java/com/basecamp/turbolinks/TurbolinksSession.java b/turbolinks/src/main/java/com/basecamp/turbolinks/TurbolinksSession.java index 5cdec6d..34427c7 100644 --- a/turbolinks/src/main/java/com/basecamp/turbolinks/TurbolinksSession.java +++ b/turbolinks/src/main/java/com/basecamp/turbolinks/TurbolinksSession.java @@ -9,6 +9,7 @@ import android.os.Build; import android.text.TextUtils; import android.util.Log; +import android.view.View; import android.view.ViewTreeObserver; import android.webkit.CookieManager; import android.webkit.ValueCallback; @@ -28,11 +29,14 @@ */ public class TurbolinksSession implements TurbolinksScrollUpCallback { + //region Final Static Vars private static final String JAVASCRIPT_GET_WEB_PAGE_HEIGHT = "document.body.scrollHeight"; + //endregion + // --------------------------------------------------- - // Package public vars (allows for greater flexibility and access for testing) + //region Package public vars (allows for greater flexibility and access for testing) // --------------------------------------------------- boolean bridgeInjectionInProgress; // Ensures the bridge is only injected once @@ -46,6 +50,7 @@ public class TurbolinksSession implements TurbolinksScrollUpCallback { int xPosition, yPosition, heightOfPage; long previousOverrideTime; Activity activity; + HashMap customHeaders = new HashMap<>(); HashMap javascriptInterfaces = new HashMap<>(); HashMap restorationIdentifierMap = new HashMap<>(); String location; @@ -57,8 +62,10 @@ public class TurbolinksSession implements TurbolinksScrollUpCallback { static volatile TurbolinksSession defaultInstance; + //endregion + // --------------------------------------------------- - // Final vars + //region Final vars // --------------------------------------------------- static final String ACTION_ADVANCE = "advance"; @@ -69,10 +76,13 @@ public class TurbolinksSession implements TurbolinksScrollUpCallback { static final int PROGRESS_INDICATOR_DELAY = 500; final Context applicationContext; - WebView webView; //Removed final prefix on 20192-27 + + WebView webView; //Removed final prefix on 2019-02-27 + //endregion + // --------------------------------------------------- - // Constructor + //region Constructor // --------------------------------------------------- /** @@ -96,10 +106,18 @@ private TurbolinksSession(@NonNull final Context context) { this.webView.addJavascriptInterface(this, JAVASCRIPT_INTERFACE_NAME); this.webView.setWebViewClient(new MyWebViewClient()); this.setWebviewScrollListener(); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { + this.webView.setLayerType(View.LAYER_TYPE_HARDWARE, null); + } else if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB && + Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { + this.webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null); + } } + //endregion + // --------------------------------------------------- - // Initialization + //region Initialization // --------------------------------------------------- @@ -152,6 +170,8 @@ public static void setDebugLoggingEnabled(boolean enabled) { TurbolinksLog.setDebugLoggingEnabled(enabled); } + //endregion + //region Custom Setters /** @@ -205,7 +225,7 @@ public boolean adjustUserAgentString(String newUserAgentString, boolean appendTo //endregion // --------------------------------------------------- - // Required chained methods + //region Required chained methods // --------------------------------------------------- /** @@ -290,7 +310,8 @@ public void visit(String location) { if (!turbolinksIsReady && !coldBootInProgress) { TurbolinksLog.d("Cold booting: " + location); - webView.loadUrl(location); + TurbolinksSession.this.initCustomHeaders(); + webView.loadUrl(location, TurbolinksSession.this.customHeaders); } // Reset so that cached snapshot is not the default for the next visit @@ -305,27 +326,10 @@ always have the last desired location. And when setTurbolinksIsReady(true) is ca */ } - /** - * Public accessor to stop the refresh layout indicator - */ - public void stopRefreshingManual(){ - this.stopRefreshing(); - } - - - /** - * Manual setter for refreshing - * {@link androidx.swiperefreshlayout.widget.SwipeRefreshLayout.OnRefreshListener} manually - * @param isRefreshing - */ - public void setRefreshingManual(boolean isRefreshing){ - try { - this.turbolinksView.getRefreshLayout().setRefreshing(isRefreshing); - } catch (Exception e){} - } + //endregion // --------------------------------------------------- - // Optional chained methods + //region Optional chained methods // --------------------------------------------------- /** @@ -342,8 +346,10 @@ public TurbolinksSession restoreWithCachedSnapshot(boolean restoreWithCachedSnap return this; } + //endregion + // --------------------------------------------------- - // TurbolinksNative adapter methods + //region TurbolinksNative adapter methods // --------------------------------------------------- /** @@ -481,7 +487,6 @@ public void run() { }); } } - /** *

JavascriptInterface only Called when Turbolinks detects that the page being visited @@ -507,8 +512,10 @@ public void run() { // route through normal chain so progress view is shown, reg }); } + //endregion + // --------------------------------------------------- - // TurbolinksNative helper methods + //region TurbolinksNative helper methods // --------------------------------------------------- /** @@ -596,10 +603,86 @@ public void run() { }); } + //endregion + // ----------------------------------------------------------------------- - // Public + //region Public // ----------------------------------------------------------------------- + //region Custom Headers + /** + * Override the existing custom headers with the ones passed + * @param headers + */ + public void setHeaders(HashMap headers){ + if(headers == null){ + return; + } + if(headers.size() <= 0){ + return; + } + this.customHeaders = headers; + } + + /** + * Add a single header to the {@link TurbolinksSession#customHeaders} map + * @param key Key The key to set. If attempting to send "user-agent", will forward instead to + * {@link TurbolinksSession#adjustUserAgentString(String, boolean)} with second param as true + * @param value Value Value to set + */ + public void addHeader(String key, String value){ + if(key == null || value == null){ + return; + } + if(key.isEmpty() || value.isEmpty()){ + return; + } + if(key.equalsIgnoreCase("user-agent")){ + this.adjustUserAgentString(value, true); + return; + } + this.initCustomHeaders(); + this.customHeaders.put(key, value); + } + + /** + * Remove a header from the existing {@link TurbolinksSession#customHeaders} + * @param key Key to remove + */ + public void removeHeader(String key){ + this.initCustomHeaders(); + if(this.customHeaders.containsKey(key)){ + this.customHeaders.remove(key); + } + } + + /** + * Clear all headers from the existing {@link TurbolinksSession#customHeaders} + */ + public void clearHeaders(){ + this.customHeaders = new HashMap<>(); + } + + /** + * Public accessor to stop the refresh layout indicator + */ + public void stopRefreshingManual(){ + this.stopRefreshing(); + } + + //endregion + + /** + * Manual setter for refreshing + * {@link androidx.swiperefreshlayout.widget.SwipeRefreshLayout.OnRefreshListener} manually + * @param isRefreshing + */ + public void setRefreshingManual(boolean isRefreshing){ + try { + this.turbolinksView.getRefreshLayout().setRefreshing(isRefreshing); + } catch (Exception e){} + } + /** *

Provides the ability to add an arbitrary number of custom Javascript Interfaces to the built-in * Turbolinks webView.

@@ -710,10 +793,22 @@ public void visitLocationWithAction(String location, String action) { TurbolinksHelper.encodeUrl(location), action, getRestorationIdentifierFromMap()); } + //endregion + // --------------------------------------------------- - // Private + + //region Private // --------------------------------------------------- - + + /** + * Simple init for the custom headers map + */ + private void initCustomHeaders(){ + if(this.customHeaders == null){ + this.customHeaders = new HashMap<>(); + } + } + /** *

Adds the restoration (cached scroll position) identifier to the local Hashmap.

* @@ -768,6 +863,8 @@ private void validateRequiredParams() { } } + //endregion + //region Getters public int getWebviewXPosition(){ return this.xPosition; @@ -783,7 +880,7 @@ public int getWebviewPageHeight(){ //endregion // --------------------------------------------------- - // Interfaces + //region Interfaces // --------------------------------------------------- /** @@ -797,7 +894,9 @@ public boolean canChildScrollUp() { // return this.webView.getScrollY() > 0; } - //region CustoWebview scroll changedm Web View Client + //endregion + + //region CustomWebview scroll changed Web View Client /** * Custom Webview Client @@ -860,7 +959,8 @@ public boolean shouldOverrideUrlLoading(WebView view, String location) { // TurbolinksLog.d("Changing normal behavior, passing back up the adapter callback as reload"); // turbolinksAdapter.visitProposedToLocationWithAction(location, ACTION_RELOAD); // return true; - view.loadUrl(location); + TurbolinksSession.this.initCustomHeaders(); + view.loadUrl(location, TurbolinksSession.this.customHeaders); TurbolinksSession.this.turbolinksAdapter.visitCompletedByWebview(location); return false; } catch (Exception e){