Skip to content

Commit

Permalink
Merge pull request android#166 from android/lint-checks
Browse files Browse the repository at this point in the history
Lint checks
  • Loading branch information
nic0lette authored Aug 4, 2020
2 parents 378d1cd + 1bd6fca commit 1662a24
Showing 1 changed file with 30 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@

package com.android.samples.webviewdemo

import android.annotation.SuppressLint
import android.content.Intent
import android.content.pm.ApplicationInfo
import android.content.res.Configuration
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.os.Handler
import android.os.Looper
Expand All @@ -30,13 +29,12 @@ import android.webkit.WebResourceResponse
import android.webkit.WebView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat.startActivity
import androidx.webkit.JavaScriptReplyProxy
import androidx.webkit.WebMessageCompat
import androidx.webkit.WebSettingsCompat
import androidx.webkit.WebSettingsCompat.DARK_STRATEGY_PREFER_WEB_THEME_OVER_USER_AGENT_DARKENING
import androidx.webkit.WebViewAssetLoader
import androidx.webkit.WebViewClientCompat
import androidx.webkit.WebViewCompat
import androidx.webkit.WebViewCompat.WebMessageListener
import androidx.webkit.WebViewFeature
import com.android.samples.webviewdemo.databinding.ActivityMainBinding

Expand All @@ -58,10 +56,11 @@ class MainActivity : AppCompatActivity() {
/**
* Injects a JavaScript object which supports a {@code postMessage()} method.
* A feature check is used to determine if the preferred API, WebMessageListener, is supported.
* If it is, then WebMessageListener will be used to create a JavaScript object. The object will be
* injected into all of the frames that have an origin matching those in {@code allowedOriginRules}.
* If it is, then WebMessageListener will be used to create a JavaScript object. The object will
* be injected into all of the frames that have an origin matching those in
* `allowedOriginRules`.
* <p>
* If WebMessageListener is not supported then the method will defer to using JavascriptInterface
* If [WebMessageListener] is not supported then the method will defer to using JavascriptInterface
* to create the JavaScript object.
* <p>
* The {@code postMessage()} methods in the Javascript objects created by WebMessageListener and
Expand All @@ -82,7 +81,7 @@ class MainActivity : AppCompatActivity() {
* origin in this set then it will have the JS object injected into it
* @param onMessageReceived invoked on UI thread with message passed in from JavaScript postMessage() call
*/

@Suppress("SameParameterValue")
private fun createJsObject(
webview: WebView,
jsObjName: String,
Expand All @@ -91,24 +90,14 @@ class MainActivity : AppCompatActivity() {
) {
if (WebViewFeature.isFeatureSupported(WebViewFeature.WEB_MESSAGE_LISTENER)) {
WebViewCompat.addWebMessageListener(
webview, jsObjName, allowedOriginRules,
object : WebViewCompat.WebMessageListener {
override fun onPostMessage(
webview: WebView,
message: WebMessageCompat,
sourceOrigin: Uri,
isMainFrame: Boolean,
replyProxy: JavaScriptReplyProxy
) {
onMessageReceived(message.data!!)
}
})
webview, jsObjName, allowedOriginRules
) { _, message, _, _, _ -> onMessageReceived(message.data!!) }
} else {
webview.addJavascriptInterface(object {
@JavascriptInterface
fun postMessage(message: String) {
// Use the handler to invoke method on UI thread
handler.post(Runnable { onMessageReceived(message) })
handler.post { onMessageReceived(message) }
}
}, jsObjName)
}
Expand All @@ -125,12 +114,13 @@ class MainActivity : AppCompatActivity() {
startActivity(this@MainActivity, shareIntent, null)
}

@SuppressLint("SetJavaScriptEnabled")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
val jsObjName = "jsObject"
val allowedOriginRules = setOf<String>("https://raw.githubusercontent.com")
val allowedOriginRules = setOf("https://raw.githubusercontent.com")

// Configuring Dark Theme
// *NOTE* : The force dark setting is not persistent. You must call the static
Expand All @@ -153,11 +143,11 @@ class MainActivity : AppCompatActivity() {
/* Set how WebView content should be darkened. There are three options for how to darken
* a WebView.
* PREFER_WEB_THEME_OVER_USER_AGENT_DARKENING- checks for the "color-scheme" <meta> tag.
* If present, it uses media queries. If absent, it applies user-agent (automatic) darkening
* DARK_STRATEGY_WEB_THEME_DARKENING_ONLY - uses media queries always, even if there's
* no "color-scheme" <meta> tag present.
* DARK_STRATEGY_USER_AGENT_DARKENING_ONLY - it ignores web page theme and always applies
* user-agent (automatic) darkening.
* If present, it uses media queries. If absent, it applies user-agent (automatic)
* darkening DARK_STRATEGY_WEB_THEME_DARKENING_ONLY - uses media queries always, even
* if there's no "color-scheme" <meta> tag present.
* DARK_STRATEGY_USER_AGENT_DARKENING_ONLY - it ignores web page theme and always
* applies user-agent (automatic) darkening.
* More information about Force Dark Strategy can be found here:
* https://developer.android.com/reference/androidx/webkit/WebSettingsCompat#setForceDarkStrategy(android.webkit.WebSettings,%20int)
*/
Expand All @@ -171,12 +161,15 @@ class MainActivity : AppCompatActivity() {

// Configure asset loader with custom domain
// *NOTE* :
// The assets path handler is set with the sub path /views-widgets-samples/ here because we are tyring to ensure
// that the address loaded with loadUrl("https://raw.githubusercontent.com/views-widgets-samples/assets/index.html") does
// not conflict with a real web address. In this case, if the path were only /assests/ we would need to load
// "https://raw.githubusercontent.com/assets/index.html" in order to access our local index.html file.
// However we cannot guarantee "https://raw.githubusercontent.com/assets/index.html" is not a valid web address.
// Therefore we must let the AssetLoader know to expect the /views-widgets-samples/ sub path as well as the /assets/.
// The assets path handler is set with the sub path /views-widgets-samples/ here because we
// are tyring to ensure that the address loaded with
// loadUrl("https://raw.githubusercontent.com/views-widgets-samples/assets/index.html") does
// not conflict with a real web address. In this case, if the path were only /assests/ we
// would need to load "https://raw.githubusercontent.com/assets/index.html" in order to
// access our local index.html file.
// However we cannot guarantee "https://raw.githubusercontent.com/assets/index.html" is not
// a valid web address. Therefore we must let the AssetLoader know to expect the
// /views-widgets-samples/ sub path as well as the /assets/.
val assetLoader = WebViewAssetLoader.Builder()
.setDomain("raw.githubusercontent.com")
.addPathHandler(
Expand All @@ -196,10 +189,8 @@ class MainActivity : AppCompatActivity() {
title = getString(R.string.app_name)

// Setup debugging; See https://developers.google.com/web/tools/chrome-devtools/remote-debugging/webviews for reference
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
if (0 != applicationInfo.flags and ApplicationInfo.FLAG_DEBUGGABLE) {
WebView.setWebContentsDebuggingEnabled(true)
}
if (0 != applicationInfo.flags and ApplicationInfo.FLAG_DEBUGGABLE) {
WebView.setWebContentsDebuggingEnabled(true)
}

// Enable Javascript
Expand All @@ -214,5 +205,6 @@ class MainActivity : AppCompatActivity() {
) { message -> invokeShareIntent(message) }

// Load the content
binding.webview.loadUrl("https://raw.githubusercontent.com/views-widgets-samples/assets/index.html") }
binding.webview.loadUrl("https://raw.githubusercontent.com/views-widgets-samples/assets/index.html")
}
}

0 comments on commit 1662a24

Please sign in to comment.