aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/kotlin/com/pitchedapps/frost/contracts/FileChooser.kt
blob: bd31d6ce2930c6edff0461b1e62d07b454a9ecf2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.pitchedapps.frost.contracts

import android.app.Activity
import android.content.Intent
import android.net.Uri
import android.webkit.ValueCallback
import android.webkit.WebChromeClient
import ca.allanwang.kau.imagepicker.kauLaunchImagePicker
import ca.allanwang.kau.imagepicker.kauOnImagePickerResult
import com.pitchedapps.frost.activities.ImagePickerActivity
import com.pitchedapps.frost.utils.L
import java.io.File

/**
 * Created by Allan Wang on 2017-07-04.
 */
const val IMAGE_CHOOSER_REQUEST = 67

interface FileChooserActivityContract {
    fun openFileChooser(filePathCallback: ValueCallback<Array<Uri>>, fileChooserParams: WebChromeClient.FileChooserParams)
}

interface FileChooserContract {
    var filePathCallback: ValueCallback<Array<Uri>>?
    fun Activity.openImagePicker(filePathCallback: ValueCallback<Array<Uri>>, fileChooserParams: WebChromeClient.FileChooserParams)
    fun Activity.onActivityResultWeb(requestCode: Int, resultCode: Int, intent: Intent?): Boolean
}

class FileChooserDelegate : FileChooserContract {

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

    override fun Activity.openImagePicker(filePathCallback: ValueCallback<Array<Uri>>, fileChooserParams: WebChromeClient.FileChooserParams) {
        this@FileChooserDelegate.filePathCallback = filePathCallback
        kauLaunchImagePicker(ImagePickerActivity::class.java, IMAGE_CHOOSER_REQUEST)
    }

    override fun Activity.onActivityResultWeb(requestCode: Int, resultCode: Int, intent: Intent?): Boolean {
        L.d("FileChooser On activity results web $requestCode")
        if (requestCode != IMAGE_CHOOSER_REQUEST) return false
        val results = kauOnImagePickerResult(resultCode, intent).map { it.uri }.toTypedArray()
        L.d("FileChooser result ${results.contentToString()}")
        //proper path content://com.android.providers.media.documents/document/image%3A36341
        L.d("FileChooser Callback received; ${filePathCallback != null}")
        filePathCallback?.onReceiveValue(results)
        filePathCallback = null
        return true
    }

}