aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/kotlin/com/pitchedapps/frost/contracts/FileChooser.kt
blob: 21cb4948b0e38bcd0bcbb618382f28966efcccd8 (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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
 * Copyright 2018 Allan Wang
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
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.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.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<Array<Uri>?>,
        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<Array<Uri>?>? = null

    override fun openMediaPicker(
        filePathCallback: ValueCallback<Array<Uri>?>,
        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<Array<Uri>?>,
        fileChooserParams: WebChromeClient.FileChooserParams
    )
}

interface FileChooserContract {
    var filePathCallback: ValueCallback<Array<Uri>?>?
    fun Activity.openMediaPicker(
        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.openMediaPicker(
        filePathCallback: ValueCallback<Array<Uri>?>,
        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
    }
}