-
Notifications
You must be signed in to change notification settings - Fork 7
Add webview delegate #36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ikarenkov
wants to merge
1
commit into
master
Choose a base branch
from
feature/webview_delegate
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| /build |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| import org.jetbrains.kotlin.config.KotlinCompilerVersion | ||
|
|
||
| plugins { | ||
| id("com.android.library") | ||
| kotlin("android") | ||
| } | ||
|
|
||
| val versions: Map<String, *> by rootProject.extra | ||
| android { | ||
| compileSdkVersion(versions["compileSdk"] as Int) | ||
|
|
||
| defaultConfig { | ||
| minSdkVersion(16) | ||
| } | ||
|
|
||
| compileOptions { | ||
| sourceCompatibility = JavaVersion.VERSION_1_8 | ||
| targetCompatibility = JavaVersion.VERSION_1_8 | ||
| } | ||
| } | ||
|
|
||
| dependencies { | ||
| implementation(kotlin("stdlib", KotlinCompilerVersion.VERSION)) | ||
| implementation("androidx.core:core-ktx:${versions["coreKtx"]}") | ||
| implementation("androidx.annotation:annotation:${versions["androidx"]}") | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| <manifest package="ru.touchin.roboswag.webview_delegate" /> |
7 changes: 7 additions & 0 deletions
7
webview-delegate/src/main/java/ru/touchin/roboswag/webview_delegate/LoadingState.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package ru.touchin.roboswag.webview_delegate | ||
|
|
||
| enum class LoadingState { | ||
| LOADING, | ||
| ERROR, | ||
| LOADED | ||
| } |
62 changes: 62 additions & 0 deletions
62
webview-delegate/src/main/java/ru/touchin/roboswag/webview_delegate/SimpleWebViewClient.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| package ru.touchin.roboswag.webview_delegate | ||
|
|
||
| import android.annotation.TargetApi | ||
| import android.graphics.Bitmap | ||
| import android.net.http.SslError | ||
| import android.os.Build | ||
| import android.webkit.SslErrorHandler | ||
| import android.webkit.WebResourceError | ||
| import android.webkit.WebResourceRequest | ||
| import android.webkit.WebView | ||
| import android.webkit.WebViewClient | ||
|
|
||
| open class SimpleWebViewClient(private val callback: WebViewCallback) : WebViewClient() { | ||
|
|
||
| var isError = false | ||
|
|
||
| override fun onPageStarted(view: WebView, url: String, favicon: Bitmap?) { | ||
| super.onPageStarted(view, url, favicon) | ||
| isError = false | ||
| callback.onStateChanged(LoadingState.LOADING) | ||
| } | ||
|
|
||
| override fun onPageFinished(view: WebView, url: String) { | ||
| super.onPageFinished(view, url) | ||
| pageFinished(view) | ||
| } | ||
|
|
||
| override fun shouldOverrideUrlLoading(view: WebView, url: String?): Boolean { | ||
| val shouldOverride = callback.onOverrideUrlLoading(url) | ||
| if (!shouldOverride) { | ||
| view.loadUrl(url) | ||
| } | ||
| return shouldOverride | ||
| } | ||
|
|
||
| override fun shouldOverrideUrlLoading(view: WebView, request: WebResourceRequest): Boolean { | ||
| return shouldOverrideUrlLoading(view, request.url.toString()) | ||
| } | ||
|
|
||
| override fun onReceivedSslError(view: WebView, handler: SslErrorHandler, error: SslError) { | ||
| handler.proceed() | ||
| } | ||
|
|
||
| private fun pageFinished(view: WebView) { | ||
| callback.onStateChanged(if (isError) LoadingState.ERROR else LoadingState.LOADED) | ||
| callback.clearCacheAndData(view) | ||
| } | ||
|
|
||
| @TargetApi(Build.VERSION_CODES.M) | ||
| override fun onReceivedError(view: WebView, request: WebResourceRequest, error: WebResourceError) { | ||
| onReceivedError(view, error.errorCode, error.description.toString(), request.url.toString()) | ||
| } | ||
|
|
||
| override fun onReceivedError(view: WebView, errorCode: Int, description: String?, failingUrl: String) { | ||
| super.onReceivedError(view, errorCode, description, failingUrl) | ||
| if (!(errorCode == -10 && "about:blank" == failingUrl)) { | ||
| isError = true | ||
| } | ||
| pageFinished(view) | ||
| } | ||
|
|
||
| } | ||
75 changes: 75 additions & 0 deletions
75
webview-delegate/src/main/java/ru/touchin/roboswag/webview_delegate/SimpleWebViewDelegate.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| package ru.touchin.roboswag.webview_delegate | ||
|
|
||
| import android.content.pm.ApplicationInfo | ||
| import android.view.View | ||
| import android.webkit.WebView | ||
| import androidx.annotation.CallSuper | ||
| import androidx.core.view.isInvisible | ||
| import androidx.core.view.isVisible | ||
|
|
||
| open class SimpleWebViewDelegate( | ||
| protected val webView: WebView, | ||
| protected open val errorView: View, | ||
| protected open val loadingView: View | ||
| ) : WebViewCallback { | ||
|
|
||
| init { | ||
| if (0 != webView.context.applicationInfo.flags and ApplicationInfo.FLAG_DEBUGGABLE) { | ||
| WebView.setWebContentsDebuggingEnabled(true) | ||
| } | ||
| webView.webViewClient = getWebViewClient() | ||
|
|
||
| webView.scrollBarStyle = View.SCROLLBARS_INSIDE_OVERLAY | ||
| with(webView.settings) { | ||
| loadsImagesAutomatically = true | ||
| javaScriptEnabled = true | ||
| domStorageEnabled = true | ||
| useWideViewPort = true | ||
| loadWithOverviewMode = true | ||
| } | ||
| } | ||
|
|
||
| open fun getWebViewClient() = SimpleWebViewClient(this) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. это не |
||
|
|
||
| open override fun clearCacheAndData(view: WebView) { | ||
| with(view) { | ||
| clearMatches() | ||
| clearFormData() | ||
| clearCache(true) | ||
| } | ||
| } | ||
|
|
||
| @CallSuper | ||
| open override fun onStateChanged(newState: LoadingState) { | ||
| when (newState) { | ||
| LoadingState.LOADED -> { | ||
| webView.isVisible = true | ||
| errorView.isVisible = false | ||
| loadingView.isVisible = false | ||
| } | ||
| LoadingState.LOADING -> { | ||
| webView.isInvisible = true | ||
| errorView.isVisible = false | ||
| loadingView.isVisible = true | ||
| } | ||
| LoadingState.ERROR -> { | ||
| webView.isInvisible = true | ||
| errorView.isVisible = true | ||
| loadingView.isVisible = false | ||
| } | ||
| } | ||
| } | ||
|
|
||
| open fun loadUrl(url: String?) { | ||
| webView.loadUrl(url) | ||
| } | ||
|
|
||
| open fun onBackPressed(): Boolean { | ||
| if (webView.canGoBack()) { | ||
| webView.goBack() | ||
| return true | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| } | ||
17 changes: 17 additions & 0 deletions
17
webview-delegate/src/main/java/ru/touchin/roboswag/webview_delegate/WebViewCallback.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| package ru.touchin.roboswag.webview_delegate | ||
|
|
||
| import android.webkit.WebView | ||
|
|
||
| interface WebViewCallback { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| fun onOverrideUrlLoading(url: String?): Boolean = false | ||
|
|
||
| fun onInterceptRequest(url: String) {} | ||
|
|
||
| fun onStartPageLoading(url: String) {} | ||
|
|
||
| fun clearCacheAndData(view: WebView) | ||
|
|
||
| fun onStateChanged(newState: LoadingState) | ||
|
|
||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Magic number