├── .gitignore ├── .travis.yml ├── CONDUCT.md ├── INSTRUCTIONS.md ├── LICENSE ├── README.md ├── build.gradle ├── demoapp ├── .gitignore ├── build.gradle ├── proguard-rules.pro ├── server │ ├── Gemfile │ ├── Gemfile.lock │ ├── config.ru │ ├── lib │ │ ├── turbolinks_demo.rb │ │ └── turbolinks_demo │ │ │ ├── app.rb │ │ │ └── views │ │ │ ├── error.erb │ │ │ ├── index.erb │ │ │ ├── layout.erb │ │ │ ├── one.erb │ │ │ ├── slow.erb │ │ │ └── two.erb │ └── tmp │ │ └── restart.txt └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── basecamp │ │ └── turbolinks │ │ └── demo │ │ └── ApplicationTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── basecamp │ │ │ └── turbolinks │ │ │ └── demo │ │ │ └── MainActivity.java │ └── res │ │ ├── layout │ │ └── activity_main.xml │ │ ├── mipmap-hdpi │ │ └── ic_launcher.png │ │ ├── mipmap-mdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxxhdpi │ │ └── ic_launcher.png │ │ ├── values-v21 │ │ └── styles.xml │ │ ├── values-w820dp │ │ └── dimens.xml │ │ └── values │ │ ├── colors.xml │ │ ├── dimens.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── basecamp │ └── turbolinks │ └── demo │ └── ExampleUnitTest.java ├── docs ├── allclasses-frame.html ├── allclasses-noframe.html ├── com │ └── basecamp │ │ └── turbolinks │ │ ├── TurbolinksAdapter.html │ │ ├── TurbolinksHelper.html │ │ ├── TurbolinksLog.html │ │ ├── TurbolinksScrollUpCallback.html │ │ ├── TurbolinksSession.html │ │ ├── TurbolinksSwipeRefreshLayout.html │ │ ├── TurbolinksTestActivity.html │ │ ├── TurbolinksView.html │ │ ├── package-frame.html │ │ ├── package-summary.html │ │ └── package-tree.html ├── constant-values.html ├── deprecated-list.html ├── help-doc.html ├── index-files │ ├── index-1.html │ ├── index-10.html │ ├── index-11.html │ ├── index-12.html │ ├── index-13.html │ ├── index-14.html │ ├── index-15.html │ ├── index-16.html │ ├── index-17.html │ ├── index-2.html │ ├── index-3.html │ ├── index-4.html │ ├── index-5.html │ ├── index-6.html │ ├── index-7.html │ ├── index-8.html │ └── index-9.html ├── index.html ├── overview-tree.html ├── package-list ├── script.js └── stylesheet.css ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── settings.gradle └── turbolinks ├── .gitignore ├── build.gradle ├── proguard-consumer-rules.pro ├── proguard-rules.pro └── src ├── androidTest └── java │ └── com │ └── basecamp │ └── turbolinks │ └── ApplicationTest.java ├── main ├── AndroidManifest.xml ├── assets │ └── js │ │ └── turbolinks_bridge.js ├── java │ └── com │ │ └── basecamp │ │ └── turbolinks │ │ ├── TurbolinksAdapter.java │ │ ├── TurbolinksHelper.java │ │ ├── TurbolinksLog.java │ │ ├── TurbolinksScrollUpCallback.java │ │ ├── TurbolinksSession.java │ │ ├── TurbolinksSwipeRefreshLayout.java │ │ ├── TurbolinksTestActivity.java │ │ └── TurbolinksView.java └── res │ ├── layout │ └── turbolinks_progress.xml │ └── values │ └── strings.xml └── test └── java └── com └── basecamp └── turbolinks ├── BaseTest.java ├── TestBuildConfig.java ├── TurbolinksHelperTest.java ├── TurbolinksSessionTest.java └── TurbolinksViewTest.java /.gitignore: -------------------------------------------------------------------------------- 1 | .gradle 2 | /local.properties 3 | .DS_Store 4 | /build 5 | 6 | .idea/* 7 | *.iml 8 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: android 2 | jdk: oraclejdk8 3 | android: 4 | components: 5 | - tools 6 | - tools # See https://github.com/travis-ci/travis-ci/issues/6040#issuecomment-219367943 7 | - platform-tools 8 | - build-tools-25.0.2 9 | - android-24 10 | - android-25 11 | - sys-img-armeabi-v7a-android-24 12 | - extra-android-support 13 | - extra-android-m2repository 14 | before_script: 15 | # Create and start emulator 16 | - echo no | android create avd --force -n test -t android-24 --abi armeabi-v7a 17 | - emulator -avd test -no-skin -no-window & 18 | - android-wait-for-emulator 19 | - adb shell input keyevent 82 & 20 | before_cache: 21 | - rm -f $HOME/.gradle/caches/modules-2/modules-2.lock 22 | cache: 23 | directories: 24 | - $HOME/.gradle/caches/ 25 | - $HOME/.gradle/wrapper/ 26 | branches: 27 | except: 28 | - gh-pages 29 | script: 30 | ./gradlew clean testRelease -p turbolinks 31 | -------------------------------------------------------------------------------- /CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Code of Conduct 2 | 3 | As contributors and maintainers of this project, and in the interest of fostering an open and welcoming community, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities. 4 | 5 | We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, religion, or nationality. 6 | 7 | Examples of unacceptable behavior by participants include: 8 | 9 | * The use of sexualized language or imagery 10 | * Personal attacks 11 | * Trolling or insulting/derogatory comments 12 | * Public or private harassment 13 | * Publishing other's private information, such as physical or electronic addresses, without explicit permission 14 | * Other unethical or unprofessional conduct. 15 | 16 | Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. By adopting this Code of Conduct, project maintainers commit themselves to fairly and consistently applying these principles to every aspect of managing this project. Project maintainers who do not follow or enforce the Code of Conduct may be permanently removed from the project team. 17 | 18 | This code of conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. 19 | 20 | Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one of the project maintainers, [Dan Kim](mailto:dan@basecamp.com) or [Jay Ohms](mailto:jay@basecamp.com). 21 | 22 | This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.2.0, available at [http://contributor-covenant.org/version/1/2/0/](http://contributor-covenant.org/version/1/2/0/) 23 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2016 Basecamp, LLC 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining 4 | a copy of this software and associated documentation files (the 5 | "Software"), to deal in the Software without restriction, including 6 | without limitation the rights to use, copy, modify, merge, publish, 7 | distribute, sublicense, and/or sell copies of the Software, and to 8 | permit persons to whom the Software is furnished to do so, subject to 9 | the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be 12 | included in all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 18 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 19 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 20 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Deprecated 2 | Turbolinks Android 1.x is deprecated. We will not be maintaining this adapter any further. We'd encourage you to read the [detailed explanation for why we deprecated this library](https://github.com/turbolinks/turbolinks-android/wiki/Turbolinks-Android-Adapter-1.x-Deprecation). The previous instructions are [still available](INSTRUCTIONS.md). 3 | 4 | # New Version 5 | The new Turbo Android framework is available at https://github.com/hotwired/turbo-android and is now a part of [Hotwire](https://hotwire.dev). 6 | 7 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | 3 | buildscript { 4 | repositories { 5 | jcenter() 6 | google() 7 | } 8 | dependencies { 9 | classpath 'com.android.tools.build:gradle:3.2.1' 10 | 11 | // NOTE: Do not place your application dependencies here; they belong 12 | // in the individual module build.gradle files 13 | } 14 | } 15 | 16 | allprojects { 17 | repositories { 18 | jcenter() 19 | google() 20 | } 21 | } 22 | 23 | task clean(type: Delete) { 24 | delete rootProject.buildDir 25 | } 26 | -------------------------------------------------------------------------------- /demoapp/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /demoapp/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 28 5 | buildToolsVersion '28.0.3' 6 | 7 | defaultConfig { 8 | applicationId "com.basecamp.turbolinks.demo" 9 | minSdkVersion 19 10 | targetSdkVersion 28 11 | versionCode 1 12 | versionName "1.0" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | debug { 20 | debuggable true 21 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 22 | } 23 | } 24 | } 25 | 26 | dependencies { 27 | implementation fileTree(include: ['*.jar'], dir: 'libs') 28 | testImplementation 'junit:junit:4.12' 29 | implementation 'androidx.appcompat:appcompat:1.0.2' 30 | implementation 'com.google.android.material:material:1.1.0-alpha01' 31 | implementation project(':turbolinks') 32 | } 33 | -------------------------------------------------------------------------------- /demoapp/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /Users/dankim/android/sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /demoapp/server/Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | 3 | gem 'sinatra' 4 | gem 'turbolinks-source' 5 | -------------------------------------------------------------------------------- /demoapp/server/Gemfile.lock: -------------------------------------------------------------------------------- 1 | GEM 2 | remote: https://rubygems.org/ 3 | specs: 4 | rack (1.6.4) 5 | rack-protection (1.5.3) 6 | rack 7 | sinatra (1.4.7) 8 | rack (~> 1.5) 9 | rack-protection (~> 1.4) 10 | tilt (>= 1.3, < 3) 11 | tilt (2.0.2) 12 | turbolinks-source (5.0.0.beta1.1) 13 | 14 | PLATFORMS 15 | ruby 16 | 17 | DEPENDENCIES 18 | sinatra 19 | turbolinks-source 20 | 21 | BUNDLED WITH 22 | 1.10.6 23 | -------------------------------------------------------------------------------- /demoapp/server/config.ru: -------------------------------------------------------------------------------- 1 | $:.unshift File.dirname(__FILE__)+'/lib' 2 | 3 | require 'rubygems' 4 | require 'turbolinks_demo' 5 | 6 | run TurbolinksDemo::App 7 | -------------------------------------------------------------------------------- /demoapp/server/lib/turbolinks_demo.rb: -------------------------------------------------------------------------------- 1 | require_relative 'turbolinks_demo/app' 2 | -------------------------------------------------------------------------------- /demoapp/server/lib/turbolinks_demo/app.rb: -------------------------------------------------------------------------------- 1 | require 'sinatra' 2 | require 'turbolinks/source' 3 | 4 | module TurbolinksDemo 5 | class App < Sinatra::Base 6 | get '/' do 7 | @title = 'Demo' 8 | erb :index, layout: :layout 9 | end 10 | 11 | get '/one' do 12 | @title = 'Page One' 13 | erb :one, layout: :layout 14 | end 15 | 16 | get '/two' do 17 | @title = 'Page Two' 18 | erb :two, layout: :layout 19 | end 20 | 21 | get '/slow' do 22 | sleep 2 23 | @title = 'Slow' 24 | erb :slow, layout: :layout 25 | end 26 | 27 | get '/error' do 28 | @title = 'Error' 29 | erb :error, layout: :layout 30 | end 31 | 32 | get '/turbolinks.js' do 33 | send_file(Turbolinks::Source.asset_path + '/turbolinks.js') 34 | end 35 | end 36 | end 37 | -------------------------------------------------------------------------------- /demoapp/server/lib/turbolinks_demo/views/error.erb: -------------------------------------------------------------------------------- 1 |
Hello from Turbolinks! This demo app will help you get acquainted with the framework.
2 | 3 |interface TurbolinksScrollUpCallback
107 | Defines a callback for determining whether or not a child view can scroll up.
Modifier and Type | 124 |Method and Description | 125 |
---|---|
boolean |
128 | canChildScrollUp()
129 | Used to determine whether or not a child view can scroll up.
130 | |
131 |
boolean canChildScrollUp()154 |
Used to determine whether or not a child view can scroll up.
Interface | 80 |Description | 81 |
---|---|
TurbolinksAdapter | 85 |
86 | Defines callbacks that Turbolinks makes available to your app.
87 | |
88 |
Class | 97 |Description | 98 |
---|---|
TurbolinksSession | 102 |
103 | The main concrete class to use Turbolinks 5 in your app.
104 | |
105 |
TurbolinksView | 108 |
109 | The custom view to add to your activity layout.
110 | |
111 |
Each package has a page that contains a list of its classes and interfaces, with a summary for each. This page can contain six categories:
79 |Each class, interface, nested class and nested interface has its own separate page. Each of these pages has three sections consisting of a class/interface description, summary tables, and detailed member descriptions:
91 |Each summary entry contains the first sentence from the detailed description for that item. The summary entries are alphabetical, while the detailed descriptions are in the order they appear in the source code. This preserves the logical groupings established by the programmer.
111 |Each annotation type has its own separate page with the following sections:
115 |Each enum has its own separate page with the following sections:
126 |There is a Class Hierarchy page for all packages, plus a hierarchy for each package. Each hierarchy page contains a list of classes and a list of interfaces. The classes are organized by inheritance structure starting with java.lang.Object
. The interfaces do not inherit from java.lang.Object
.
The Deprecated API page lists all of the API that have been deprecated. A deprecated API is not recommended for use, generally due to improvements, and a replacement API is usually given. Deprecated APIs may be removed in future implementations.
144 |The Index contains an alphabetic list of all classes, interfaces, constructors, methods, and fields.
148 |These links take you to the next or previous class, interface, package, or related page.
152 |These links show and hide the HTML frames. All pages are available with or without frames.
156 |The All Classes link shows all classes and interfaces except non-static nested types.
160 |Each serializable or externalizable class has a description of its serialization fields and methods. This information is of interest to re-implementors, not to developers using the API. While there is no link in the navigation bar, you can get to this information by going to any serialized class and clicking "Serialized Form" in the "See also" section of the class description.
164 |The Constant Field Values page lists the static final fields and their values.
168 |TurbolinksAdapter
implementation is required so that callbacks
83 | during the Turbolinks event lifecycle can be passed back to your app.Defines callbacks that Turbolinks makes available to your app. This interface is required, and 5 | * should be implemented in an activity (or similar class).
6 | * 7 | *Often these callbacks handle error conditions, but there are also some convenient timing events 8 | * where you can do things like routing, inject custom Javascript, etc.
9 | */ 10 | public interface TurbolinksAdapter { 11 | /** 12 | *Called after the Turbolinks Javascript bridge has been injected into the webView, during the 13 | * Android WebViewClient's standard onPageFinished callback. 14 | */ 15 | void onPageFinished(); 16 | 17 | /** 18 | *
Called when the Android WebViewClient's standard onReceivedError callback is fired.
19 | * 20 | * @param errorCode Passed through error code returned by the Android WebViewClient. 21 | */ 22 | void onReceivedError(int errorCode); 23 | 24 | /** 25 | *Called when Turbolinks detects that the page being visited has been invalidated, typically 26 | * by new resources in the the page HEAD.
27 | */ 28 | void pageInvalidated(); 29 | 30 | /** 31 | *Called when Turbolinks receives an HTTP error from a Turbolinks request.
32 | * 33 | * @param statusCode HTTP status code returned by the request. 34 | */ 35 | void requestFailedWithStatusCode(int statusCode); 36 | 37 | /** 38 | *Called when Turbolinks considers the visit fully completed -- the request fulfilled 39 | * successfully and page rendered.
40 | */ 41 | void visitCompleted(); 42 | 43 | /** 44 | *Called when Turbolinks first starts a visit, typically from a link inside a webView.
45 | * 46 | * @param location URL to be visited. 47 | * @param action Whether to treat the request as an advance (navigating forward) or a replace (back). 48 | */ 49 | void visitProposedToLocationWithAction(String location, String action); 50 | } 51 | -------------------------------------------------------------------------------- /turbolinks/src/main/java/com/basecamp/turbolinks/TurbolinksHelper.java: -------------------------------------------------------------------------------- 1 | package com.basecamp.turbolinks; 2 | 3 | import android.annotation.SuppressLint; 4 | import android.content.Context; 5 | import android.content.MutableContextWrapper; 6 | import android.os.Handler; 7 | import android.util.Base64; 8 | import android.view.ViewGroup; 9 | import android.webkit.WebChromeClient; 10 | import android.webkit.WebSettings; 11 | import android.webkit.WebView; 12 | 13 | import android.widget.FrameLayout; 14 | import com.google.gson.Gson; 15 | import com.google.gson.GsonBuilder; 16 | 17 | import org.apache.commons.lang3.StringUtils; 18 | 19 | import java.io.IOException; 20 | import java.io.InputStream; 21 | import java.net.URI; 22 | import java.net.URL; 23 | 24 | class TurbolinksHelper { 25 | private static String scriptInjectionFormat = "(function(){var parent = document.getElementsByTagName('head').item(0);var script = document.createElement('script');script.type = 'text/javascript';script.innerHTML = window.atob('%s');parent.appendChild(script);return true;})()"; 26 | 27 | // --------------------------------------------------- 28 | // Package public 29 | // --------------------------------------------------- 30 | 31 | /** 32 | *Creates the shared webView used throughout the lifetime of the TurbolinksSession.
33 | * 34 | * @param applicationContext An application context. 35 | * @return The shared WebView. 36 | */ 37 | static WebView createWebView(Context applicationContext) { 38 | MutableContextWrapper contextWrapper = new MutableContextWrapper(applicationContext); 39 | WebView webView = new WebView(contextWrapper); 40 | configureWebViewDefaults(webView); 41 | setWebViewLayoutParams(webView); 42 | 43 | return webView; 44 | } 45 | 46 | /** 47 | *Encodes URLs so they are properly escaped, specifically for Javascript calls. Liberally 48 | * borrowed from: http://stackoverflow.com/questions/3286067/url-encoding-in-android/8962879#8962879
49 | * 50 | * @param originalUrl Original URL string. 51 | * @return Encoded, escaped URL string. 52 | */ 53 | static String encodeUrl(String originalUrl) { 54 | try { 55 | URL newUrl = new URL(originalUrl); 56 | URI uri = new URI(newUrl.getProtocol(), newUrl.getUserInfo(), newUrl.getHost(), newUrl.getPort(), newUrl.getPath(), newUrl.getQuery(), newUrl.getRef()); 57 | return uri.toURL().toString(); 58 | } catch (Exception e) { 59 | return null; 60 | } 61 | } 62 | 63 | /** 64 | *Gets the base64-encoded string of a local asset file (typically a Javascript or HTML file)
65 | * 66 | * @param context An activity context. 67 | * @param filePath Local file path relative to the main/src directory. 68 | * @return A base-64 encoded string of the file contents. 69 | * @throws IOException Typically if a file cannot be found or read in. 70 | */ 71 | static String getContentFromAssetFile(Context context, String filePath) throws IOException { 72 | InputStream inputStream = context.getAssets().open(filePath); 73 | byte[] buffer = new byte[inputStream.available()]; 74 | inputStream.read(buffer); 75 | inputStream.close(); 76 | return Base64.encodeToString(buffer, Base64.NO_WRAP); 77 | } 78 | 79 | /** 80 | *Injects Javascript into the webView.
81 | * 82 | * @param turbolinksSession The TurbolinksSession. 83 | * @param context Any Android context. 84 | * @param webView The shared webView. 85 | */ 86 | static void injectTurbolinksBridge(final TurbolinksSession turbolinksSession, Context context, WebView webView) { 87 | try { 88 | String jsCall = String.format(scriptInjectionFormat, TurbolinksHelper.getContentFromAssetFile(context, "js/turbolinks_bridge.js")); 89 | runJavascriptRaw(context, webView, jsCall); 90 | } catch (IOException e) { 91 | TurbolinksLog.e("Error injecting script file into webview: " + e.toString()); 92 | } 93 | } 94 | 95 | /** 96 | *JSONifies any arbitrary number of params and runs the the Javascript function in the 97 | * webView.
98 | * 99 | * @param context An activity context. 100 | * @param webView The shared webView. 101 | * @param functionName The Javascript function name only (no parenthesis or parameters). 102 | * @param params A comma delimited list of parameter values. 103 | */ 104 | static void runJavascript(Context context, final WebView webView, String functionName, Object... params) { 105 | final String fullJs; 106 | 107 | if (params != null) { 108 | Gson gson = new GsonBuilder().disableHtmlEscaping().create(); 109 | for (int i = 0; i < params.length; i++) { 110 | params[i] = gson.toJson(params[i]); 111 | } 112 | 113 | fullJs = String.format("javascript: %s(%s);", functionName, StringUtils.join(params, ",")); 114 | } else { 115 | fullJs = String.format("javascript: %s();", functionName); 116 | } 117 | 118 | runOnMainThread(context, new Runnable() { 119 | @Override 120 | public void run() { 121 | webView.loadUrl(fullJs); 122 | } 123 | }); 124 | } 125 | 126 | /** 127 | *Runs raw Javascript that's passed in. You are responsible for encoding/escaping the 128 | * function call.
129 | * 130 | * @param context An activity context. 131 | * @param webView The shared webView. 132 | * @param javascript The raw Javascript to be executed, fully escaped/encoded in advance. 133 | */ 134 | static void runJavascriptRaw(Context context, final WebView webView, final String javascript) { 135 | runOnMainThread(context, new Runnable() { 136 | @Override 137 | public void run() { 138 | webView.loadUrl("javascript:" + javascript); 139 | } 140 | }); 141 | } 142 | 143 | /** 144 | *Executes a given runnable on the main thread.
145 | * 146 | * @param context An activity context. 147 | * @param runnable A runnable to execute on the main thread. 148 | */ 149 | static void runOnMainThread(Context context, Runnable runnable) { 150 | Handler handler = new Handler(context.getMainLooper()); 151 | handler.post(runnable); 152 | } 153 | 154 | // --------------------------------------------------- 155 | // Private 156 | // --------------------------------------------------- 157 | 158 | /** 159 | *Configures basic settings of the webView (Javascript enabled, DOM storage enabled, 160 | * database enabled).
161 | * 162 | * @param webView The shared webView. 163 | */ 164 | @SuppressLint("SetJavaScriptEnabled") 165 | private static void configureWebViewDefaults(WebView webView) { 166 | WebSettings settings = webView.getSettings(); 167 | settings.setJavaScriptEnabled(true); 168 | settings.setDomStorageEnabled(true); 169 | settings.setDatabaseEnabled(true); 170 | 171 | webView.setWebChromeClient(new WebChromeClient()); 172 | } 173 | 174 | /** 175 | * Sets the WebView's width/height layout params to MATCH_PARENT 176 | * 177 | * @param webView The shared webView. 178 | */ 179 | private static void setWebViewLayoutParams(WebView webView) { 180 | FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); 181 | webView.setLayoutParams(params); 182 | } 183 | } 184 | -------------------------------------------------------------------------------- /turbolinks/src/main/java/com/basecamp/turbolinks/TurbolinksLog.java: -------------------------------------------------------------------------------- 1 | package com.basecamp.turbolinks; 2 | 3 | import android.util.Log; 4 | 5 | class TurbolinksLog { 6 | private static final String DEFAULT_TAG = "TurbolinksLog"; 7 | private static boolean debugLoggingEnabled = false; 8 | 9 | /** 10 | *Enables/disables debug logging.
11 | * 12 | * @param enabled True, to enable. 13 | */ 14 | static void setDebugLoggingEnabled(boolean enabled) { 15 | debugLoggingEnabled = enabled; 16 | } 17 | 18 | /** 19 | *Send a DEBUG level log statement with the default tag
20 | * 21 | * @param msg Debug message. 22 | */ 23 | static void d(String msg) { 24 | log(Log.DEBUG, DEFAULT_TAG, msg); 25 | } 26 | 27 | /** 28 | *Send a ERROR level log statement with the default tag
29 | * 30 | * @param msg Error message. 31 | */ 32 | static void e(String msg) { 33 | log(Log.ERROR, DEFAULT_TAG, msg); 34 | } 35 | 36 | /** 37 | *Default log statement called by other convenience methods.
38 | * 39 | * @param logLevel Log level of the statement. 40 | * @param tag Tag to identify the logging statement. 41 | * @param msg Message to log. 42 | */ 43 | private static void log(int logLevel, String tag, String msg) { 44 | switch (logLevel) { 45 | case Log.DEBUG: 46 | if (debugLoggingEnabled) { 47 | Log.d(tag, msg); 48 | } 49 | break; 50 | case Log.ERROR: 51 | Log.e(tag, msg); 52 | break; 53 | default: 54 | break; 55 | } 56 | } 57 | } -------------------------------------------------------------------------------- /turbolinks/src/main/java/com/basecamp/turbolinks/TurbolinksScrollUpCallback.java: -------------------------------------------------------------------------------- 1 | package com.basecamp.turbolinks; 2 | 3 | /** 4 | *Defines a callback for determining whether or not a child view can scroll up.
5 | */ 6 | interface TurbolinksScrollUpCallback { 7 | /** 8 | *Used to determine whether or not a child view can scroll up.
9 | * 10 | * @return True if the child can scroll up. False otherwise. 11 | */ 12 | boolean canChildScrollUp(); 13 | } 14 | -------------------------------------------------------------------------------- /turbolinks/src/main/java/com/basecamp/turbolinks/TurbolinksSwipeRefreshLayout.java: -------------------------------------------------------------------------------- 1 | package com.basecamp.turbolinks; 2 | 3 | import android.content.Context; 4 | import android.util.AttributeSet; 5 | 6 | import androidx.swiperefreshlayout.widget.SwipeRefreshLayout; 7 | 8 | /** 9 | *Custom SwipeRefreshLayout for Turbolinks.
10 | */ 11 | class TurbolinksSwipeRefreshLayout extends SwipeRefreshLayout { 12 | private TurbolinksScrollUpCallback callback; 13 | 14 | /** 15 | *Constructor to match SwipeRefreshLayout
16 | * 17 | * @param context Refer to SwipeRefreshLayout 18 | */ 19 | TurbolinksSwipeRefreshLayout(Context context) { 20 | super(context); 21 | } 22 | 23 | /** 24 | *Constructor to match SwipeRefreshLayout
25 | * 26 | * @param context Refer to SwipeRefreshLayout 27 | * @param attrs Refer to SwipeRefreshLayout 28 | */ 29 | TurbolinksSwipeRefreshLayout(Context context, AttributeSet attrs) { 30 | super(context, attrs); 31 | } 32 | 33 | /** 34 | *Overridden from SwipeRefreshLayout. Uses a custom callback.
35 | *If the custom callback is null, it falls back to the parent canChildScrollUp()
36 | * 37 | * @return True if the child view can scroll up. False otherwise. 38 | */ 39 | @Override 40 | public boolean canChildScrollUp() { 41 | if (callback != null) { return callback.canChildScrollUp(); } 42 | return super.canChildScrollUp(); 43 | } 44 | 45 | /** 46 | *Sets the callback to be used in canChildScrollUp().
47 | *See canChildScrollUp() to see how it's used.
48 | * 49 | * @param callback The custom callback to be set 50 | */ 51 | void setCallback(TurbolinksScrollUpCallback callback) { this.callback = callback; } 52 | } 53 | -------------------------------------------------------------------------------- /turbolinks/src/main/java/com/basecamp/turbolinks/TurbolinksTestActivity.java: -------------------------------------------------------------------------------- 1 | package com.basecamp.turbolinks; 2 | 3 | import android.annotation.SuppressLint; 4 | import android.app.Activity; 5 | 6 | /** 7 | * For testing purposes only. Robolectric sometimes struggles with mocks. 8 | */ 9 | @SuppressLint("Instantiatable") 10 | class TurbolinksTestActivity extends Activity {} 11 | -------------------------------------------------------------------------------- /turbolinks/src/main/res/layout/turbolinks_progress.xml: -------------------------------------------------------------------------------- 1 | 2 |