From 8db1930d7623ba071123f5978153679da7161278 Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Sat, 25 Sep 2021 15:55:07 -0700 Subject: Create hilt web file chooser implementation --- .../frost/activities/BaseMainActivity.kt | 19 ++---- .../frost/activities/WebOverlayActivity.kt | 26 +++------ .../frost/contracts/ActivityContract.kt | 8 +-- .../com/pitchedapps/frost/contracts/FileChooser.kt | 67 ++++++++++++++++++++++ .../com/pitchedapps/frost/views/FrostWebView.kt | 6 +- .../pitchedapps/frost/web/FrostChromeClients.kt | 13 ++--- 6 files changed, 92 insertions(+), 47 deletions(-) (limited to 'app/src/main/kotlin/com') diff --git a/app/src/main/kotlin/com/pitchedapps/frost/activities/BaseMainActivity.kt b/app/src/main/kotlin/com/pitchedapps/frost/activities/BaseMainActivity.kt index 3766aef7..84352cb4 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/activities/BaseMainActivity.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/activities/BaseMainActivity.kt @@ -24,14 +24,11 @@ import android.content.res.ColorStateList import android.graphics.PointF import android.graphics.drawable.Drawable import android.graphics.drawable.RippleDrawable -import android.net.Uri import android.os.Bundle import android.view.Menu import android.view.MenuItem import android.view.View import android.view.ViewGroup -import android.webkit.ValueCallback -import android.webkit.WebChromeClient import android.webkit.WebView import android.widget.FrameLayout import android.widget.ImageView @@ -79,10 +76,9 @@ import com.mikepenz.iconics.typeface.IIcon import com.mikepenz.iconics.typeface.library.googlematerial.GoogleMaterial import com.pitchedapps.frost.BuildConfig import com.pitchedapps.frost.R -import com.pitchedapps.frost.contracts.FileChooserContract -import com.pitchedapps.frost.contracts.FileChooserDelegate import com.pitchedapps.frost.contracts.MainActivityContract import com.pitchedapps.frost.contracts.VideoViewHolder +import com.pitchedapps.frost.contracts.WebFileChooser import com.pitchedapps.frost.databinding.ActivityMainBinding import com.pitchedapps.frost.databinding.ActivityMainBottomTabsBinding import com.pitchedapps.frost.databinding.ActivityMainDrawerWrapperBinding @@ -149,7 +145,6 @@ import kotlin.math.abs abstract class BaseMainActivity : BaseActivity(), MainActivityContract, - FileChooserContract by FileChooserDelegate(), VideoViewHolder, SearchViewHolder { @@ -167,6 +162,9 @@ abstract class BaseMainActivity : @Inject lateinit var genericDao: GenericDao + @Inject + lateinit var webFileChooser: WebFileChooser + interface ActivityMainContentBinding { val root: View val toolbar: Toolbar @@ -715,16 +713,9 @@ abstract class BaseMainActivity : return true } - override fun openFileChooser( - filePathCallback: ValueCallback?>, - fileChooserParams: WebChromeClient.FileChooserParams - ) { - openMediaPicker(filePathCallback, fileChooserParams) - } - @SuppressLint("NewApi") override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { - if (onActivityResultWeb(requestCode, resultCode, data)) return + if (webFileChooser.onActivityResultWeb(requestCode, resultCode, data)) return super.onActivityResult(requestCode, resultCode, data) fun hasRequest(flag: Int) = resultCode and flag > 0 diff --git a/app/src/main/kotlin/com/pitchedapps/frost/activities/WebOverlayActivity.kt b/app/src/main/kotlin/com/pitchedapps/frost/activities/WebOverlayActivity.kt index 689d9a65..ef7579a8 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/activities/WebOverlayActivity.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/activities/WebOverlayActivity.kt @@ -18,12 +18,9 @@ package com.pitchedapps.frost.activities import android.content.Intent import android.graphics.PointF -import android.net.Uri import android.os.Bundle import android.view.Menu import android.view.MenuItem -import android.webkit.ValueCallback -import android.webkit.WebChromeClient import android.widget.FrameLayout import androidx.appcompat.widget.Toolbar import androidx.coordinatorlayout.widget.CoordinatorLayout @@ -49,11 +46,9 @@ import ca.allanwang.kau.utils.withMainContext import com.google.android.material.snackbar.BaseTransientBottomBar import com.mikepenz.iconics.typeface.library.googlematerial.GoogleMaterial import com.pitchedapps.frost.R -import com.pitchedapps.frost.contracts.ActivityContract -import com.pitchedapps.frost.contracts.FileChooserContract -import com.pitchedapps.frost.contracts.FileChooserDelegate import com.pitchedapps.frost.contracts.FrostContentContainer import com.pitchedapps.frost.contracts.VideoViewHolder +import com.pitchedapps.frost.contracts.WebFileChooser import com.pitchedapps.frost.enums.OverlayContext import com.pitchedapps.frost.facebook.FB_URL_BASE import com.pitchedapps.frost.facebook.FbItem @@ -70,10 +65,12 @@ import com.pitchedapps.frost.utils.frostSnackbar import com.pitchedapps.frost.views.FrostContentWeb import com.pitchedapps.frost.views.FrostVideoViewer import com.pitchedapps.frost.views.FrostWebView +import dagger.hilt.android.AndroidEntryPoint import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.launch import okhttp3.HttpUrl.Companion.toHttpUrlOrNull +import javax.inject.Inject /** * Created by Allan Wang on 2017-06-01. @@ -155,12 +152,11 @@ class WebOverlayDesktopActivity : WebOverlayActivityBase(USER_AGENT_DESKTOP_CONS class WebOverlayActivity : WebOverlayActivityBase() @UseExperimental(ExperimentalCoroutinesApi::class) +@AndroidEntryPoint abstract class WebOverlayActivityBase(private val userAgent: String = USER_AGENT) : BaseActivity(), - ActivityContract, FrostContentContainer, - VideoViewHolder, - FileChooserContract by FileChooserDelegate() { + VideoViewHolder { override val frameWrapper: FrameLayout by bindView(R.id.frame_wrapper) val toolbar: Toolbar by bindView(R.id.overlay_toolbar) @@ -169,6 +165,9 @@ abstract class WebOverlayActivityBase(private val userAgent: String = USER_AGENT get() = content.coreView private val coordinator: CoordinatorLayout by bindView(R.id.overlay_main_content) + @Inject + lateinit var webFileChooser: WebFileChooser + private inline val urlTest: String? get() = intent.getStringExtra(ARG_URL) ?: intent.dataString @@ -297,15 +296,8 @@ abstract class WebOverlayActivityBase(private val userAgent: String = USER_AGENT kauSwipeOnDestroy() } - override fun openFileChooser( - filePathCallback: ValueCallback?>, - fileChooserParams: WebChromeClient.FileChooserParams - ) { - openMediaPicker(filePathCallback, fileChooserParams) - } - override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { - if (onActivityResultWeb(requestCode, resultCode, data)) return + if (webFileChooser.onActivityResultWeb(requestCode, resultCode, data)) return super.onActivityResult(requestCode, resultCode, data) } diff --git a/app/src/main/kotlin/com/pitchedapps/frost/contracts/ActivityContract.kt b/app/src/main/kotlin/com/pitchedapps/frost/contracts/ActivityContract.kt index 84edfee9..756b1f3d 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/contracts/ActivityContract.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/contracts/ActivityContract.kt @@ -17,18 +17,12 @@ package com.pitchedapps.frost.contracts import com.mikepenz.iconics.typeface.IIcon -import com.pitchedapps.frost.activities.MainActivity import com.pitchedapps.frost.fragments.BaseFragment import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.channels.BroadcastChannel -/** - * All the contracts for [MainActivity] - */ -interface ActivityContract : FileChooserActivityContract - @UseExperimental(ExperimentalCoroutinesApi::class) -interface MainActivityContract : ActivityContract, MainFabContract { +interface MainActivityContract : MainFabContract { val fragmentChannel: BroadcastChannel val headerBadgeChannel: BroadcastChannel fun setTitle(res: Int) diff --git a/app/src/main/kotlin/com/pitchedapps/frost/contracts/FileChooser.kt b/app/src/main/kotlin/com/pitchedapps/frost/contracts/FileChooser.kt index 4853e7ff..21cb4948 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/contracts/FileChooser.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/contracts/FileChooser.kt @@ -26,12 +26,78 @@ import ca.allanwang.kau.permissions.kauRequestPermissions import ca.allanwang.kau.utils.string import com.pitchedapps.frost.R import com.pitchedapps.frost.utils.L +import dagger.Binds +import dagger.Module +import dagger.hilt.InstallIn +import dagger.hilt.android.components.ActivityComponent +import dagger.hilt.android.scopes.ActivityScoped +import javax.inject.Inject /** * Created by Allan Wang on 2017-07-04. */ const val MEDIA_CHOOSER_RESULT = 67 +interface WebFileChooser { + fun openMediaPicker( + filePathCallback: ValueCallback?>, + fileChooserParams: WebChromeClient.FileChooserParams + ) + + fun onActivityResultWeb( + requestCode: Int, + resultCode: Int, + intent: Intent? + ): Boolean +} + +class WebFileChooserImpl @Inject internal constructor(private val activity: Activity) : + WebFileChooser { + private var filePathCallback: ValueCallback?>? = null + + override fun openMediaPicker( + filePathCallback: ValueCallback?>, + fileChooserParams: WebChromeClient.FileChooserParams + ) { + activity.kauRequestPermissions(PERMISSION_WRITE_EXTERNAL_STORAGE) { granted, _ -> + if (!granted) { + L.d { "Failed to get write permissions" } + filePathCallback.onReceiveValue(null) + return@kauRequestPermissions + } + this.filePathCallback = filePathCallback + val intent = Intent() + intent.type = fileChooserParams.acceptTypes.firstOrNull() + intent.action = Intent.ACTION_GET_CONTENT + activity.startActivityForResult( + Intent.createChooser(intent, activity.string(R.string.pick_image)), + MEDIA_CHOOSER_RESULT + ) + } + } + + override fun onActivityResultWeb( + requestCode: Int, + resultCode: Int, + intent: Intent? + ): Boolean { + L.d { "FileChooser On activity results web $requestCode" } + if (requestCode != MEDIA_CHOOSER_RESULT) return false + val data = intent?.data + filePathCallback?.onReceiveValue(if (data != null) arrayOf(data) else null) + filePathCallback = null + return true + } +} + +@Module +@InstallIn(ActivityComponent::class) +interface WebFileChooserModule { + @Binds + @ActivityScoped + fun webFileChooser(to: WebFileChooserImpl): WebFileChooser +} + interface FileChooserActivityContract { fun openFileChooser( filePathCallback: ValueCallback?>, @@ -59,6 +125,7 @@ class FileChooserDelegate : FileChooserContract { ) { kauRequestPermissions(PERMISSION_WRITE_EXTERNAL_STORAGE) { granted, _ -> if (!granted) { + L.d { "Failed to get write permissions" } filePathCallback.onReceiveValue(null) return@kauRequestPermissions } diff --git a/app/src/main/kotlin/com/pitchedapps/frost/views/FrostWebView.kt b/app/src/main/kotlin/com/pitchedapps/frost/views/FrostWebView.kt index ec012ed5..695e1226 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/views/FrostWebView.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/views/FrostWebView.kt @@ -29,6 +29,7 @@ import ca.allanwang.kau.utils.launchMain import com.pitchedapps.frost.contracts.FrostContentContainer import com.pitchedapps.frost.contracts.FrostContentCore import com.pitchedapps.frost.contracts.FrostContentParent +import com.pitchedapps.frost.contracts.WebFileChooser import com.pitchedapps.frost.db.CookieDao import com.pitchedapps.frost.db.currentCookie import com.pitchedapps.frost.facebook.FB_HOME_URL @@ -69,6 +70,9 @@ class FrostWebView @JvmOverloads constructor( @Inject lateinit var themeProvider: ThemeProvider + @Inject + lateinit var webFileChooser: WebFileChooser + @Inject lateinit var cookieDao: CookieDao @@ -98,7 +102,7 @@ class FrostWebView @JvmOverloads constructor( // attempt to get custom client; otherwise fallback to original frostWebClient = (container as? WebFragment)?.client(this) ?: FrostWebViewClient(this) webViewClient = frostWebClient - webChromeClient = FrostChromeClient(this, themeProvider) + webChromeClient = FrostChromeClient(this, themeProvider, webFileChooser) addJavascriptInterface(FrostJSI(this), "Frost") setBackgroundColor(Color.TRANSPARENT) setDownloadListener { url, userAgent, contentDisposition, mimetype, contentLength -> diff --git a/app/src/main/kotlin/com/pitchedapps/frost/web/FrostChromeClients.kt b/app/src/main/kotlin/com/pitchedapps/frost/web/FrostChromeClients.kt index e687dd2d..61a76e70 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/web/FrostChromeClients.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/web/FrostChromeClients.kt @@ -30,11 +30,9 @@ import ca.allanwang.kau.permissions.kauRequestPermissions import ca.allanwang.kau.utils.materialDialog import com.afollestad.materialdialogs.callbacks.onDismiss import com.afollestad.materialdialogs.input.input -import com.pitchedapps.frost.R -import com.pitchedapps.frost.contracts.ActivityContract +import com.pitchedapps.frost.contracts.WebFileChooser import com.pitchedapps.frost.injectors.ThemeProvider import com.pitchedapps.frost.utils.L -import com.pitchedapps.frost.utils.frostSnackbar import com.pitchedapps.frost.views.FrostWebView import kotlinx.coroutines.channels.SendChannel @@ -49,13 +47,13 @@ import kotlinx.coroutines.channels.SendChannel */ class FrostChromeClient( web: FrostWebView, - private val themeProvider: ThemeProvider + private val themeProvider: ThemeProvider, + private val webFileChooser: WebFileChooser, ) : WebChromeClient() { private val refresh: SendChannel = web.parent.refreshChannel private val progress: SendChannel = web.parent.progressChannel private val title: SendChannel = web.parent.titleChannel - private val activity = (web.context as? ActivityContract) private val context = web.context!! override fun getDefaultVideoPoster(): Bitmap? = @@ -83,9 +81,8 @@ class FrostChromeClient( filePathCallback: ValueCallback?>, fileChooserParams: FileChooserParams ): Boolean { - activity?.openFileChooser(filePathCallback, fileChooserParams) - ?: webView.frostSnackbar(R.string.file_chooser_not_found, themeProvider) - return activity != null + webFileChooser.openMediaPicker(filePathCallback, fileChooserParams) + return true } private fun JsResult.frostCancel() { -- cgit v1.2.3 From 8be8b88247973592b2afe134f99c8102929e0b2d Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Sat, 25 Sep 2021 15:57:15 -0700 Subject: Delete old file chooser code --- .../com/pitchedapps/frost/contracts/FileChooser.kt | 67 +++------------------- 1 file changed, 8 insertions(+), 59 deletions(-) (limited to 'app/src/main/kotlin/com') diff --git a/app/src/main/kotlin/com/pitchedapps/frost/contracts/FileChooser.kt b/app/src/main/kotlin/com/pitchedapps/frost/contracts/FileChooser.kt index 21cb4948..521d0f97 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/contracts/FileChooser.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/contracts/FileChooser.kt @@ -25,7 +25,9 @@ import ca.allanwang.kau.permissions.PERMISSION_WRITE_EXTERNAL_STORAGE import ca.allanwang.kau.permissions.kauRequestPermissions import ca.allanwang.kau.utils.string import com.pitchedapps.frost.R +import com.pitchedapps.frost.injectors.ThemeProvider import com.pitchedapps.frost.utils.L +import com.pitchedapps.frost.utils.frostSnackbar import dagger.Binds import dagger.Module import dagger.hilt.InstallIn @@ -36,7 +38,7 @@ import javax.inject.Inject /** * Created by Allan Wang on 2017-07-04. */ -const val MEDIA_CHOOSER_RESULT = 67 +private const val MEDIA_CHOOSER_RESULT = 67 interface WebFileChooser { fun openMediaPicker( @@ -51,8 +53,10 @@ interface WebFileChooser { ): Boolean } -class WebFileChooserImpl @Inject internal constructor(private val activity: Activity) : - WebFileChooser { +class WebFileChooserImpl @Inject internal constructor( + private val activity: Activity, + private val themeProvider: ThemeProvider +) : WebFileChooser { private var filePathCallback: ValueCallback?>? = null override fun openMediaPicker( @@ -62,6 +66,7 @@ class WebFileChooserImpl @Inject internal constructor(private val activity: Acti activity.kauRequestPermissions(PERMISSION_WRITE_EXTERNAL_STORAGE) { granted, _ -> if (!granted) { L.d { "Failed to get write permissions" } + activity.frostSnackbar(R.string.file_chooser_not_found, themeProvider) filePathCallback.onReceiveValue(null) return@kauRequestPermissions } @@ -97,59 +102,3 @@ interface WebFileChooserModule { @ActivityScoped fun webFileChooser(to: WebFileChooserImpl): WebFileChooser } - -interface FileChooserActivityContract { - fun openFileChooser( - filePathCallback: ValueCallback?>, - fileChooserParams: WebChromeClient.FileChooserParams - ) -} - -interface FileChooserContract { - var filePathCallback: ValueCallback?>? - fun Activity.openMediaPicker( - filePathCallback: ValueCallback?>, - fileChooserParams: WebChromeClient.FileChooserParams - ) - - fun Activity.onActivityResultWeb(requestCode: Int, resultCode: Int, intent: Intent?): Boolean -} - -class FileChooserDelegate : FileChooserContract { - - override var filePathCallback: ValueCallback?>? = null - - override fun Activity.openMediaPicker( - filePathCallback: ValueCallback?>, - fileChooserParams: WebChromeClient.FileChooserParams - ) { - kauRequestPermissions(PERMISSION_WRITE_EXTERNAL_STORAGE) { granted, _ -> - if (!granted) { - L.d { "Failed to get write permissions" } - filePathCallback.onReceiveValue(null) - return@kauRequestPermissions - } - this@FileChooserDelegate.filePathCallback = filePathCallback - val intent = Intent() - intent.type = fileChooserParams.acceptTypes.firstOrNull() - intent.action = Intent.ACTION_GET_CONTENT - startActivityForResult( - Intent.createChooser(intent, string(R.string.pick_image)), - MEDIA_CHOOSER_RESULT - ) - } - } - - override fun Activity.onActivityResultWeb( - requestCode: Int, - resultCode: Int, - intent: Intent? - ): Boolean { - L.d { "FileChooser On activity results web $requestCode" } - if (requestCode != MEDIA_CHOOSER_RESULT) return false - val data = intent?.data - filePathCallback?.onReceiveValue(if (data != null) arrayOf(data) else null) - filePathCallback = null - return true - } -} -- cgit v1.2.3 From 3b4b164f524575c9dc10955d329710ba0706c9ed Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Sat, 25 Sep 2021 16:54:09 -0700 Subject: Create frost web component for jsi --- .../com/pitchedapps/frost/views/FrostWebView.kt | 44 +++++++++++++++++++++- .../kotlin/com/pitchedapps/frost/web/FrostJSI.kt | 41 ++++++++++---------- 2 files changed, 65 insertions(+), 20 deletions(-) (limited to 'app/src/main/kotlin/com') diff --git a/app/src/main/kotlin/com/pitchedapps/frost/views/FrostWebView.kt b/app/src/main/kotlin/com/pitchedapps/frost/views/FrostWebView.kt index 695e1226..f384d134 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/views/FrostWebView.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/views/FrostWebView.kt @@ -18,6 +18,7 @@ package com.pitchedapps.frost.views import android.animation.ValueAnimator import android.annotation.SuppressLint +import android.app.Activity import android.content.Context import android.graphics.Color import android.util.AttributeSet @@ -44,8 +45,15 @@ import com.pitchedapps.frost.web.FrostChromeClient import com.pitchedapps.frost.web.FrostJSI import com.pitchedapps.frost.web.FrostWebViewClient import com.pitchedapps.frost.web.NestedWebView +import dagger.BindsInstance +import dagger.hilt.DefineComponent +import dagger.hilt.EntryPoint +import dagger.hilt.EntryPoints +import dagger.hilt.InstallIn import dagger.hilt.android.AndroidEntryPoint +import dagger.hilt.android.components.ViewComponent import javax.inject.Inject +import javax.inject.Scope import kotlin.math.abs import kotlin.math.max import kotlin.math.min @@ -61,6 +69,9 @@ class FrostWebView @JvmOverloads constructor( defStyleAttr: Int = 0 ) : NestedWebView(context, attrs, defStyleAttr), FrostContentCore { + @Inject + lateinit var activity: Activity + @Inject lateinit var fbCookie: FbCookie @@ -76,6 +87,9 @@ class FrostWebView @JvmOverloads constructor( @Inject lateinit var cookieDao: CookieDao + @Inject + lateinit var frostWebComponentBuilder: FrostWebComponentBuilder + override fun reload(animate: Boolean) { if (parent.registerTransition(false, animate)) super.reload() @@ -90,6 +104,8 @@ class FrostWebView @JvmOverloads constructor( @SuppressLint("SetJavaScriptEnabled") override fun bind(container: FrostContentContainer): View { + val component = frostWebComponentBuilder.frostWebView(this).build() + val entryPoint = EntryPoints.get(component, FrostWebEntryPoint::class.java) userAgentString = USER_AGENT with(settings) { javaScriptEnabled = true @@ -103,7 +119,7 @@ class FrostWebView @JvmOverloads constructor( frostWebClient = (container as? WebFragment)?.client(this) ?: FrostWebViewClient(this) webViewClient = frostWebClient webChromeClient = FrostChromeClient(this, themeProvider, webFileChooser) - addJavascriptInterface(FrostJSI(this), "Frost") + addJavascriptInterface(entryPoint.frostJsi(), "Frost") setBackgroundColor(Color.TRANSPARENT) setDownloadListener { url, userAgent, contentDisposition, mimetype, contentLength -> context.ctxCoroutine.launchMain { @@ -235,3 +251,29 @@ class FrostWebView @JvmOverloads constructor( super.destroy() } } + +@Scope +@Retention(AnnotationRetention.BINARY) +@Target( + AnnotationTarget.FUNCTION, + AnnotationTarget.TYPE, + AnnotationTarget.CLASS +) +annotation class FrostWebScoped + +@FrostWebScoped +@DefineComponent(parent = ViewComponent::class) +interface FrostWebComponent + +@DefineComponent.Builder +interface FrostWebComponentBuilder { + fun frostWebView(@BindsInstance web: FrostWebView): FrostWebComponentBuilder + fun build(): FrostWebComponent +} + +@EntryPoint +@InstallIn(FrostWebComponent::class) +interface FrostWebEntryPoint { + @FrostWebScoped + fun frostJsi(): FrostJSI +} diff --git a/app/src/main/kotlin/com/pitchedapps/frost/web/FrostJSI.kt b/app/src/main/kotlin/com/pitchedapps/frost/web/FrostJSI.kt index 9a853dae..3ead78f4 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/web/FrostJSI.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/web/FrostJSI.kt @@ -16,12 +16,11 @@ */ package com.pitchedapps.frost.web -import android.content.Context +import android.app.Activity import android.webkit.JavascriptInterface import ca.allanwang.kau.utils.ctxCoroutine import com.pitchedapps.frost.activities.MainActivity import com.pitchedapps.frost.activities.WebOverlayActivityBase -import com.pitchedapps.frost.contracts.MainActivityContract import com.pitchedapps.frost.contracts.VideoViewHolder import com.pitchedapps.frost.db.CookieEntity import com.pitchedapps.frost.facebook.FbCookie @@ -35,19 +34,23 @@ import com.pitchedapps.frost.utils.showWebContextMenu import com.pitchedapps.frost.views.FrostWebView import kotlinx.coroutines.channels.SendChannel import kotlinx.coroutines.launch +import javax.inject.Inject /** * Created by Allan Wang on 2017-06-01. */ -class FrostJSI(val web: FrostWebView) { - - private val fbCookie: FbCookie get() = web.fbCookie - private val prefs: Prefs get() = web.prefs - private val context: Context = web.context - private val activity: MainActivity? = context as? MainActivity - private val header: SendChannel? = activity?.headerBadgeChannel +class FrostJSI @Inject internal constructor( + val web: FrostWebView, + private val activity: Activity, + private val fbCookie: FbCookie, + private val prefs: Prefs +) { + + private val mainActivity: MainActivity? = activity as? MainActivity + private val webActivity: WebOverlayActivityBase? = activity as? WebOverlayActivityBase + private val header: SendChannel? = mainActivity?.headerBadgeChannel private val refresh: SendChannel = web.parent.refreshChannel - private val cookies: List = activity?.cookies() ?: arrayListOf() + private val cookies: List = activity.cookies() /** * Attempts to load the url in an overlay @@ -61,7 +64,7 @@ class FrostJSI(val web: FrostWebView) { fun loadVideo(url: String?, isGif: Boolean): Boolean = if (url != null && prefs.enablePip) { web.post { - (context as? VideoViewHolder)?.showVideo(url, isGif) + (activity as? VideoViewHolder)?.showVideo(url, isGif) ?: L.e { "Could not load video; contract not implemented" } } true @@ -82,7 +85,7 @@ class FrostJSI(val web: FrostWebView) { fun contextMenu(url: String?, text: String?) { // url will be formatted through webcontext web.post { - context.showWebContextMenu( + activity.showWebContextMenu( WebContext(url.takeIf { it.isIndependent }, text), fbCookie, prefs @@ -96,7 +99,7 @@ class FrostJSI(val web: FrostWebView) { */ @JavascriptInterface fun longClick(start: Boolean) { - activity?.contentBinding?.viewpager?.enableSwipe = !start + mainActivity?.contentBinding?.viewpager?.enableSwipe = !start if (web.frostWebClient.urlSupportsRefresh) { web.parent.swipeDisabledByAction = start } @@ -113,15 +116,15 @@ class FrostJSI(val web: FrostWebView) { web.parent.swipeDisabledByAction = disable if (disable) { // locked onto an input field; ensure content is visible - (context as? MainActivityContract)?.collapseAppBar() + mainActivity?.collapseAppBar() } } @JavascriptInterface fun loadLogin() { L.d { "Sign up button found; load login" } - context.ctxCoroutine.launch { - fbCookie.logout(context, deleteCookie = false) + activity.ctxCoroutine.launch { + fbCookie.logout(activity, deleteCookie = false) } } @@ -130,7 +133,7 @@ class FrostJSI(val web: FrostWebView) { */ @JavascriptInterface fun loadImage(imageUrl: String, text: String?) { - context.launchImageActivity(imageUrl, text) + activity.launchImageActivity(imageUrl, text) } @JavascriptInterface @@ -159,8 +162,8 @@ class FrostJSI(val web: FrostWebView) { @JavascriptInterface fun allowHorizontalScrolling(enable: Boolean) { - activity?.contentBinding?.viewpager?.enableSwipe = enable - (context as? WebOverlayActivityBase)?.swipeBack?.disallowIntercept = !enable + mainActivity?.contentBinding?.viewpager?.enableSwipe = enable + webActivity?.swipeBack?.disallowIntercept = !enable } private var isScrolling = false -- cgit v1.2.3