Skip to content

Commit

Permalink
merge(#34): fix: file upload
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubsta authored Oct 6, 2023
2 parents 54ae1d7 + 0051937 commit 90ec95a
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 16 deletions.
6 changes: 3 additions & 3 deletions rampsdk/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ android {
defaultConfig {
minSdkVersion 21
targetSdkVersion 32
versionCode 17
versionName "3.0.3"
versionCode 18
versionName "3.0.4"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles 'consumer-rules.pro'
buildConfigField 'String', 'VERSION', "\"${defaultConfig.versionName}\""
Expand Down Expand Up @@ -84,7 +84,7 @@ afterEvaluate {
from components.release
groupId = 'com.github.RampNetwork'
artifactId = 'ramp-sdk-android'
version = '3.0.3'
version = '3.0.4'
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package network.ramp.sdk.ui.activity


import android.app.Activity
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Bundle
import android.webkit.ValueCallback
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
Expand All @@ -24,6 +29,18 @@ internal class RampWidgetActivity : AppCompatActivity(), Contract.View {

private val scope = CoroutineScope(Dispatchers.Main + SupervisorJob())

private var filePathCallback: ValueCallback<Array<Uri>>? = null

private val fileChooserLauncher: ActivityResultLauncher<Intent> = registerForActivityResult(
ActivityResultContracts.StartActivityForResult()
) { result ->
val uris = result.data?.data?.let {
arrayOf(it)
} ?: arrayOf()
filePathCallback?.onReceiveValue(uris)
filePathCallback = null
}

lateinit var rampPresenter: RampPresenter

private lateinit var binding: WidgetActivityBinding
Expand All @@ -43,10 +60,11 @@ internal class RampWidgetActivity : AppCompatActivity(), Contract.View {

rampPresenter = RampPresenter(this)
binding.webView.setupWebView(
this,
RampWidgetWebViewClient(binding.progressBar),
jsInterface
)
activity = this,
wvClient = RampWidgetWebViewClient(binding.progressBar),
jsInterface = jsInterface,
fileChooserLauncher = fileChooserLauncher
) { filePathCallback = it }
intent.extras?.getParcelable<Config>(CONFIG_EXTRA)?.let {
config = it
} ?: returnOnError("Config object cannot be null")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ package network.ramp.sdk.ui.webview

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.util.AttributeSet
import android.webkit.ValueCallback
import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.activity.result.ActivityResultLauncher
import network.ramp.sdk.events.RampSdkJsInterface
import network.ramp.sdk.events.RampSdkJsInterface.Companion.RampSdkInterfaceName
import network.ramp.sdk.ui.activity.RampWidgetActivity
Expand All @@ -15,8 +19,14 @@ internal class RampWidgetWebView @JvmOverloads constructor(
) : WebView(context, attrs, defStyleAttr) {

@SuppressLint("SetJavaScriptEnabled")
fun setupWebView(activity: RampWidgetActivity, wvClient: WebViewClient, jsInterface: RampSdkJsInterface?) {
webChromeClient = RampWidgetWebViewChromeClient(activity)
fun setupWebView(
activity: RampWidgetActivity,
wvClient: WebViewClient,
jsInterface: RampSdkJsInterface?,
fileChooserLauncher: ActivityResultLauncher<Intent>,
onFilePathCallback: (ValueCallback<Array<Uri>>?) -> Unit
) {
webChromeClient = RampWidgetWebViewChromeClient(activity, fileChooserLauncher, onFilePathCallback)
webViewClient = wvClient
settings.javaScriptEnabled = true
settings.javaScriptCanOpenWindowsAutomatically = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,22 @@ import android.content.Intent
import android.content.pm.PackageManager
import android.graphics.Bitmap
import android.net.Uri
import android.os.Build
import android.os.Message
import android.webkit.PermissionRequest
import android.webkit.ValueCallback
import android.webkit.WebChromeClient
import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.activity.result.ActivityResultLauncher
import androidx.core.app.ActivityCompat
import network.ramp.sdk.ui.activity.RampWidgetActivity.Companion.ACTION_VIEW_INTENT
import timber.log.Timber


internal class RampWidgetWebViewChromeClient(val activity: Activity) : WebChromeClient() {
internal class RampWidgetWebViewChromeClient(
val activity: Activity,
private val fileChooserLauncher: ActivityResultLauncher<Intent>,
private var onFilePathCallback: (ValueCallback<Array<Uri>>?) -> Unit
) : WebChromeClient() {

override fun onCreateWindow(
view: WebView?,
Expand Down Expand Up @@ -52,13 +56,28 @@ internal class RampWidgetWebViewChromeClient(val activity: Activity) : WebChrome
if (!hasCameraPermission()) {
requestPermissions()
request.deny()
}
else
} else
request.grant(request.resources)
}
}
}

override fun onShowFileChooser(
webView: WebView?,
filePath: ValueCallback<Array<Uri>>,
fileChooserParams: FileChooserParams
): Boolean {
onFilePathCallback(filePath)

val intent = fileChooserParams.createIntent()
try {
fileChooserLauncher.launch(intent)
} catch (e: Exception) {
return false
}
return true
}

private fun hasCameraPermission() =
ActivityCompat.checkSelfPermission(
activity,
Expand All @@ -73,7 +92,6 @@ internal class RampWidgetWebViewChromeClient(val activity: Activity) : WebChrome
)
}


companion object {
const val CAMERA_PERMISSION_REQUEST = 1
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ object UrlSafeChecker {
"https://app.ramp.network",
"https://app.dev.ramp-network.org",
"https://app.demo.ramp.network",
"https://app.ramp.network"
"https://app.ramp.network",
"https://app.pepe.ramp-network.org"
)
private val listOfSafeRegex = listOf("^https://ri-widget-dev-(\\d+)\\.firebaseapp\\.com$", "^https://app.(\\d+)\\.dev\\.ramp-network\\.org")

Expand Down

0 comments on commit 90ec95a

Please sign in to comment.