@@ -9,11 +9,11 @@ import android.net.Uri
9
9
import android.os.Bundle
10
10
import android.os.Environment
11
11
import android.provider.MediaStore
12
- import android.util.Log
13
12
import android.view.View.VISIBLE
14
13
import android.webkit.PermissionRequest
15
14
import android.webkit.ValueCallback
16
15
import android.webkit.WebChromeClient
16
+ import android.webkit.WebChromeClient.FileChooserParams
17
17
import android.webkit.WebView
18
18
import android.webkit.WebViewClient
19
19
import androidx.appcompat.app.AlertDialog
@@ -63,8 +63,7 @@ import java.util.Locale
63
63
* - Use a NoActionBar theme
64
64
* - Manage back navigation: users can press back hardware button and exit from the flow
65
65
*/
66
- private const val CAMERA_REQUEST_CODE = 1112
67
- private const val FILE_PICKER_REQUEST_CODE = 1113
66
+ private const val CAPTURE_REQUEST_CODE = 1112
68
67
private const val PERMISSIONS_REQUEST_CODE = 1114
69
68
70
69
private const val TAG = " YdsWebSample"
@@ -84,6 +83,12 @@ class MainActivity : AppCompatActivity(), SessionConfigurationListener {
84
83
}
85
84
})
86
85
86
+ private val mimeTypeMap = mapOf (
87
+ " .pdf" to " application/pdf" ,
88
+ " .png" to " image/png" ,
89
+ " .jpg" to " image/jpeg"
90
+ )
91
+
87
92
override fun onCreate (savedInstanceState : Bundle ? ) {
88
93
super .onCreate(savedInstanceState)
89
94
setContentView(R .layout.activity_main)
@@ -98,20 +103,15 @@ class MainActivity : AppCompatActivity(), SessionConfigurationListener {
98
103
override fun onActivityResult (requestCode : Int , resultCode : Int , data : Intent ? ) {
99
104
super .onActivityResult(requestCode, resultCode, data)
100
105
101
- if (! isViewRecreated && resultCode == Activity .RESULT_OK ) {
102
- when (requestCode) {
103
- FILE_PICKER_REQUEST_CODE -> {
104
- data?.data?.let {
105
- Log .d(TAG , " Receive file result $it " )
106
- filePathCallback?.onReceiveValue(arrayOf(it))
107
- }
108
- }
109
- CAMERA_REQUEST_CODE -> {
110
- Log .d(TAG , " Receive camera result $cameraCaptureFileUri " )
111
- filePathCallback?.onReceiveValue(arrayOf(cameraCaptureFileUri))
112
- }
106
+ if (requestCode == CAPTURE_REQUEST_CODE ) {
107
+ if (! isViewRecreated && resultCode == Activity .RESULT_OK ) {
108
+ val resultUri = data?.data ? : cameraCaptureFileUri
109
+ filePathCallback?.onReceiveValue(arrayOf(resultUri))
110
+ } else {
111
+ filePathCallback?.onReceiveValue(null )
113
112
}
114
113
}
114
+
115
115
}
116
116
117
117
override fun onResume () {
@@ -192,33 +192,41 @@ class MainActivity : AppCompatActivity(), SessionConfigurationListener {
192
192
javaScriptCanOpenWindowsAutomatically = true
193
193
mediaPlaybackRequiresUserGesture = false
194
194
setAppCacheEnabled(true )
195
+
195
196
}
196
197
this .webViewClient = YdsWebClient ()
197
198
this .webChromeClient = YdsWebChromeClient ()
198
199
}
199
200
200
- private fun launchCamera () {
201
- Log .d(TAG , " Launch Camera intent" )
202
- cameraCaptureFileUri = createFileUri()
201
+ private fun showCameraAndFilePickerChooser (fileChooserParams : FileChooserParams ) {
203
202
204
- val cameraIntent = Intent (MediaStore .ACTION_IMAGE_CAPTURE )
205
- .setFlags(Intent .FLAG_GRANT_READ_URI_PERMISSION )
206
- .putExtra(MediaStore .EXTRA_OUTPUT , cameraCaptureFileUri)
203
+ cameraCaptureFileUri = createFileUri()
207
204
208
- startActivityForResult(Intent .createChooser(cameraIntent, " Select a picture" ), CAMERA_REQUEST_CODE )
205
+ Intent .createChooser(createFilePickerIntent(fileChooserParams), fileChooserParams.title)
206
+ .run {
207
+ putExtra(
208
+ Intent .EXTRA_INITIAL_INTENTS ,
209
+ listOf (createCameraIntent(cameraCaptureFileUri)).toTypedArray()
210
+ )
211
+ startActivityForResult(this , CAPTURE_REQUEST_CODE )
212
+ }
209
213
}
210
214
211
- private fun launchImagePicker () {
212
- Log .d(TAG , " Launch file picker intent" )
213
- val filePickerIntent = Intent ()
214
- .setType(" image/*" )
215
- .addCategory(Intent .CATEGORY_OPENABLE )
216
- .setAction(Intent .ACTION_GET_CONTENT )
215
+ private fun createFilePickerIntent (params : FileChooserParams ): Intent ? {
216
+ return params.createIntent()?.apply {
217
+ type = " */*"
218
+ putExtra(
219
+ Intent .EXTRA_MIME_TYPES ,
220
+ params.acceptTypes?.map { mimeTypeMap[it] }!! .filterNotNull().toTypedArray()
221
+ )
222
+ }
223
+ }
217
224
218
- startActivityForResult(
219
- Intent .createChooser(filePickerIntent, " Select a file" ),
220
- FILE_PICKER_REQUEST_CODE
221
- )
225
+ private fun createCameraIntent (outputFile : Uri ): Intent {
226
+ return Intent (MediaStore .ACTION_IMAGE_CAPTURE ).apply {
227
+ flags = Intent .FLAG_GRANT_READ_URI_PERMISSION
228
+ putExtra(MediaStore .EXTRA_OUTPUT , outputFile)
229
+ }
222
230
}
223
231
224
232
/* *
@@ -248,10 +256,12 @@ class MainActivity : AppCompatActivity(), SessionConfigurationListener {
248
256
): Boolean {
249
257
this @MainActivity.filePathCallback = filePathCallback
250
258
251
- if (fileChooserParams?.isCaptureEnabled == true ) launchCamera()
252
- else launchImagePicker()
253
-
254
- return true
259
+ return if (fileChooserParams?.mode == FileChooserParams .MODE_OPEN ) {
260
+ showCameraAndFilePickerChooser(fileChooserParams)
261
+ true
262
+ } else {
263
+ false
264
+ }
255
265
}
256
266
257
267
// Grant permission requested
0 commit comments