Skip to content

Add camera / image upload capability to Duck.ai WebView #6156

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
May 30, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions duckchat/duckchat-impl/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ dependencies {
implementation AndroidX.core.ktx
implementation Google.android.material
implementation Google.dagger
implementation AndroidX.work.runtimeKtx

implementation "com.squareup.logcat:logcat:_"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import com.duckduckgo.common.utils.DispatcherProvider
import com.duckduckgo.di.scopes.AppScope
import com.duckduckgo.duckchat.api.DuckChat
import com.duckduckgo.duckchat.api.DuckChatSettingsNoParams
import com.duckduckgo.duckchat.impl.feature.AIChatImageUploadFeature
import com.duckduckgo.duckchat.impl.feature.DuckChatFeature
import com.duckduckgo.duckchat.impl.pixel.DuckChatPixelName
import com.duckduckgo.duckchat.impl.repository.DuckChatFeatureRepository
Expand Down Expand Up @@ -120,6 +121,11 @@ interface DuckChatInternal : DuckChat {
* Returns the current chat state.
*/
val chatState: StateFlow<ChatState>

/**
* Returns whether image upload is enabled or not.
*/
fun isImageUploadEnabled(): Boolean
}

enum class ChatState(val value: String) {
Expand Down Expand Up @@ -162,6 +168,7 @@ class RealDuckChat @Inject constructor(
@AppCoroutineScope private val appCoroutineScope: CoroutineScope,
private val pixel: Pixel,
private val experimentDataStore: VisualDesignExperimentDataStore,
private val imageUploadFeature: AIChatImageUploadFeature,
) : DuckChatInternal, PrivacyConfigCallbackPlugin {

private val closeChatFlow = MutableSharedFlow<Unit>(replay = 0)
Expand All @@ -178,6 +185,7 @@ class RealDuckChat @Inject constructor(
private var duckChatLink = DUCK_CHAT_WEB_LINK
private var bangRegex: Regex? = null
private var isAddressBarEntryPointEnabled: Boolean = false
private var isImageUploadEnabled: Boolean = false

init {
if (isMainProcess) {
Expand Down Expand Up @@ -276,6 +284,8 @@ class RealDuckChat @Inject constructor(

override val chatState: StateFlow<ChatState> get() = _chatState.asStateFlow()

override fun isImageUploadEnabled(): Boolean = isImageUploadEnabled

override fun openDuckChat(query: String?) {
val parameters = query?.let { originalQuery ->
val hasDuckChatBang = isDuckChatBang(originalQuery.toUri())
Expand Down Expand Up @@ -383,6 +393,7 @@ class RealDuckChat @Inject constructor(
bangRegex = settingsJson.aiChatBangRegex?.replace("{bangs}", bangAlternation)?.toRegex()
}
isAddressBarEntryPointEnabled = settingsJson?.addressBarEntryPoint ?: false
isImageUploadEnabled = imageUploadFeature.self().isEnabled()
cacheUserSettings()
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright (c) 2025 DuckDuckGo
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.duckduckgo.duckchat.impl.feature

import com.duckduckgo.anvil.annotations.ContributesRemoteFeature
import com.duckduckgo.di.scopes.AppScope
import com.duckduckgo.feature.toggles.api.Toggle
import com.duckduckgo.feature.toggles.api.Toggle.DefaultFeatureValue

@ContributesRemoteFeature(
scope = AppScope::class,
featureName = "aiChatImageUpload",
)

interface AIChatImageUploadFeature {

@Toggle.DefaultValue(DefaultFeatureValue.TRUE)
fun self(): Toggle
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ class RealDuckChatJSHelper @Inject constructor(
put(SUPPORTS_CLOSING_AI_CHAT, true)
put(SUPPORTS_OPENING_SETTINGS, true)
put(SUPPORTS_NATIVE_CHAT_INPUT, experimentDataStore.isExperimentEnabled.value && experimentDataStore.isDuckAIPoCEnabled.value)
put(SUPPORTS_IMAGE_UPLOAD, duckChat.isImageUploadEnabled())
}
return JsCallbackData(jsonPayload, featureName, method, id)
}
Expand All @@ -129,6 +130,7 @@ class RealDuckChatJSHelper @Inject constructor(
private const val SUPPORTS_CLOSING_AI_CHAT = "supportsClosingAIChat"
private const val SUPPORTS_OPENING_SETTINGS = "supportsOpeningSettings"
private const val SUPPORTS_NATIVE_CHAT_INPUT = "supportsNativeChatInput"
private const val SUPPORTS_IMAGE_UPLOAD = "supportsImageUpload"
private const val PLATFORM = "platform"
private const val ANDROID = "android"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,19 @@ package com.duckduckgo.duckchat.impl.ui

import android.Manifest
import android.annotation.SuppressLint
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Bundle
import android.os.Environment
import android.os.Message
import android.provider.MediaStore
import android.view.MenuItem
import android.view.ViewGroup
import android.webkit.MimeTypeMap
import android.webkit.ValueCallback
import android.webkit.WebChromeClient
import android.webkit.WebChromeClient.FileChooserParams
import android.webkit.WebSettings
import android.webkit.WebView
import androidx.annotation.AnyThread
Expand All @@ -37,6 +43,7 @@ import com.duckduckgo.app.di.AppCoroutineScope
import com.duckduckgo.app.tabs.BrowserNav
import com.duckduckgo.appbuildconfig.api.AppBuildConfig
import com.duckduckgo.common.ui.DuckDuckGoActivity
import com.duckduckgo.common.ui.view.dialog.ActionBottomSheetDialog
import com.duckduckgo.common.ui.view.makeSnackbarWithNoBottomInset
import com.duckduckgo.common.utils.ConflatedJob
import com.duckduckgo.common.utils.DispatcherProvider
Expand All @@ -54,10 +61,18 @@ import com.duckduckgo.duckchat.impl.R
import com.duckduckgo.duckchat.impl.feature.AIChatDownloadFeature
import com.duckduckgo.duckchat.impl.helper.DuckChatJSHelper
import com.duckduckgo.duckchat.impl.helper.RealDuckChatJSHelper.Companion.DUCK_CHAT_FEATURE_NAME
import com.duckduckgo.duckchat.impl.ui.filechooser.FileChooserIntentBuilder
import com.duckduckgo.duckchat.impl.ui.filechooser.capture.camera.CameraHardwareChecker
import com.duckduckgo.duckchat.impl.ui.filechooser.capture.launcher.UploadFromExternalMediaAppLauncher
import com.duckduckgo.duckchat.impl.ui.filechooser.capture.launcher.UploadFromExternalMediaAppLauncher.MediaCaptureResult.CouldNotCapturePermissionDenied
import com.duckduckgo.duckchat.impl.ui.filechooser.capture.launcher.UploadFromExternalMediaAppLauncher.MediaCaptureResult.ErrorAccessingMediaApp
import com.duckduckgo.duckchat.impl.ui.filechooser.capture.launcher.UploadFromExternalMediaAppLauncher.MediaCaptureResult.MediaCaptured
import com.duckduckgo.duckchat.impl.ui.filechooser.capture.launcher.UploadFromExternalMediaAppLauncher.MediaCaptureResult.NoMediaCaptured
import com.duckduckgo.js.messaging.api.JsMessageCallback
import com.duckduckgo.js.messaging.api.JsMessaging
import com.duckduckgo.navigation.api.GlobalActivityStarter
import com.duckduckgo.navigation.api.getActivityParams
import com.google.android.material.snackbar.BaseTransientBottomBar
import com.google.android.material.snackbar.Snackbar
import java.io.File
import javax.inject.Inject
Expand Down Expand Up @@ -114,9 +129,20 @@ open class DuckChatWebViewActivity : DuckDuckGoActivity(), DownloadConfirmationD
@Inject
lateinit var aiChatDownloadFeature: AIChatDownloadFeature

@Inject
lateinit var fileChooserIntentBuilder: FileChooserIntentBuilder

@Inject
lateinit var cameraHardwareChecker: CameraHardwareChecker

@Inject
lateinit var externalCameraLauncher: UploadFromExternalMediaAppLauncher

private var pendingFileDownload: PendingFileDownload? = null
private val downloadMessagesJob = ConflatedJob()

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

private val root: ViewGroup by lazy { findViewById(android.R.id.content) }
private val toolbar: Toolbar? by lazy { findViewById(com.duckduckgo.mobile.android.R.id.toolbar) }
internal val simpleWebview: WebView by lazy { findViewById(R.id.simpleWebview) }
Expand Down Expand Up @@ -156,6 +182,21 @@ open class DuckChatWebViewActivity : DuckDuckGoActivity(), DownloadConfirmationD
}
return false
}

override fun onShowFileChooser(
webView: WebView,
filePathCallback: ValueCallback<Array<Uri>>,
fileChooserParams: FileChooserParams,
): Boolean {
return try {
showFileChooser(filePathCallback, fileChooserParams)
true
} catch (e: Throwable) {
// cancel the request using the documented way
filePathCallback.onReceiveValue(null)
throw e
}
}
}

it.settings.apply {
Expand Down Expand Up @@ -209,6 +250,146 @@ open class DuckChatWebViewActivity : DuckDuckGoActivity(), DownloadConfirmationD
url?.let {
simpleWebview.loadUrl(it)
}

externalCameraLauncher.registerForResult(this) {
when (it) {
is MediaCaptured -> pendingUploadTask?.onReceiveValue(arrayOf(Uri.fromFile(it.file)))
is CouldNotCapturePermissionDenied -> {
pendingUploadTask?.onReceiveValue(null)
externalCameraLauncher.showPermissionRationaleDialog(this, it.inputAction)
}

is NoMediaCaptured -> pendingUploadTask?.onReceiveValue(null)
is ErrorAccessingMediaApp -> {
pendingUploadTask?.onReceiveValue(null)
Snackbar.make(root, it.messageId, BaseTransientBottomBar.LENGTH_SHORT).show()
}
}
pendingUploadTask = null
}
}

data class FileChooserRequestedParams(
val filePickingMode: Int,
val acceptMimeTypes: List<String>,
)

fun showFileChooser(
filePathCallback: ValueCallback<Array<Uri>>,
fileChooserParams: FileChooserParams,
) {
val mimeTypes = convertAcceptTypesToMimeTypes(fileChooserParams.acceptTypes)
val fileChooserRequestedParams = FileChooserRequestedParams(fileChooserParams.mode, mimeTypes)
val cameraHardwareAvailable = cameraHardwareChecker.hasCameraHardware()

when {
fileChooserParams.isCaptureEnabled -> {
when {
acceptsOnly("image/", fileChooserParams.acceptTypes) && cameraHardwareAvailable ->
launchCameraCapture(filePathCallback, fileChooserRequestedParams, MediaStore.ACTION_IMAGE_CAPTURE)

acceptsOnly("video/", fileChooserParams.acceptTypes) && cameraHardwareAvailable ->
launchCameraCapture(filePathCallback, fileChooserRequestedParams, MediaStore.ACTION_VIDEO_CAPTURE)

acceptsOnly("audio/", fileChooserParams.acceptTypes) ->
launchCameraCapture(filePathCallback, fileChooserRequestedParams, MediaStore.Audio.Media.RECORD_SOUND_ACTION)

else ->
launchFilePicker(filePathCallback, fileChooserRequestedParams)
}
}

fileChooserParams.acceptTypes.any { it.startsWith("image/") && cameraHardwareAvailable } ->
launchImageOrCameraChooser(filePathCallback, fileChooserRequestedParams, MediaStore.ACTION_IMAGE_CAPTURE)

fileChooserParams.acceptTypes.any { it.startsWith("video/") && cameraHardwareAvailable } ->
launchImageOrCameraChooser(filePathCallback, fileChooserRequestedParams, MediaStore.ACTION_VIDEO_CAPTURE)

else ->
launchFilePicker(filePathCallback, fileChooserRequestedParams)
}
}

private fun launchFilePicker(
filePathCallback: ValueCallback<Array<Uri>>,
fileChooserParams: FileChooserRequestedParams,
) {
pendingUploadTask = filePathCallback
val canChooseMultipleFiles = fileChooserParams.filePickingMode == FileChooserParams.MODE_OPEN_MULTIPLE
val intent = fileChooserIntentBuilder.intent(fileChooserParams.acceptMimeTypes.toTypedArray(), canChooseMultipleFiles)
startActivityForResult(intent, REQUEST_CODE_CHOOSE_FILE)
}

private fun launchCameraCapture(
filePathCallback: ValueCallback<Array<Uri>>,
fileChooserParams: FileChooserRequestedParams,
inputAction: String,
) {
if (Intent(inputAction).resolveActivity(packageManager) == null) {
launchFilePicker(filePathCallback, fileChooserParams)
return
}

pendingUploadTask = filePathCallback
externalCameraLauncher.launch(inputAction)
}

private fun launchImageOrCameraChooser(
filePathCallback: ValueCallback<Array<Uri>>,
fileChooserParams: FileChooserRequestedParams,
inputAction: String,
) {
val cameraString = getString(R.string.imageCaptureCameraGalleryDisambiguationCameraOption)
val cameraIcon = com.duckduckgo.mobile.android.R.drawable.ic_camera_24

val galleryString = getString(R.string.imageCaptureCameraGalleryDisambiguationGalleryOption)
val galleryIcon = com.duckduckgo.mobile.android.R.drawable.ic_image_24

ActionBottomSheetDialog.Builder(this)
.setTitle(getString(R.string.imageCaptureCameraGalleryDisambiguationTitle))
.setPrimaryItem(galleryString, galleryIcon)
.setSecondaryItem(cameraString, cameraIcon)
.addEventListener(
object : ActionBottomSheetDialog.EventListener() {
override fun onPrimaryItemClicked() {
launchFilePicker(filePathCallback, fileChooserParams)
}

override fun onSecondaryItemClicked() {
launchCameraCapture(filePathCallback, fileChooserParams, inputAction)
}

override fun onBottomSheetDismissed() {
filePathCallback.onReceiveValue(null)
pendingUploadTask = null
}
},
)
.show()
}

private fun acceptsOnly(
type: String,
acceptTypes: Array<String>,
): Boolean {
return acceptTypes.filter { it.startsWith(type) }.size == acceptTypes.size
}

private fun convertAcceptTypesToMimeTypes(acceptTypes: Array<String>): List<String> {
val mimeTypeMap = MimeTypeMap.getSingleton()
val mimeTypes = mutableSetOf<String>()
acceptTypes.forEach { type ->
// Attempt to convert any identified file extensions into corresponding MIME types.
val fileExtension = MimeTypeMap.getFileExtensionFromUrl(type)
if (fileExtension.isNotEmpty()) {
mimeTypeMap.getMimeTypeFromExtension(type.substring(1))?.let {
mimeTypes.add(it)
}
} else {
mimeTypes.add(type)
}
}
return mimeTypes.toList()
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
Expand Down Expand Up @@ -322,6 +503,30 @@ open class DuckChatWebViewActivity : DuckDuckGoActivity(), DownloadConfirmationD
requestPermissions(arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE), PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE)
}

override fun onActivityResult(
requestCode: Int,
resultCode: Int,
data: Intent?,
) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == REQUEST_CODE_CHOOSE_FILE) {
handleFileUploadResult(resultCode, data)
}
}

private fun handleFileUploadResult(
resultCode: Int,
intent: Intent?,
) {
if (resultCode != RESULT_OK || intent == null) {
pendingUploadTask?.onReceiveValue(null)
return
}

val uris = fileChooserIntentBuilder.extractSelectedFileUris(intent)
pendingUploadTask?.onReceiveValue(uris)
}

override fun onResume() {
launchDownloadMessagesJob()
super.onResume()
Expand All @@ -336,5 +541,6 @@ open class DuckChatWebViewActivity : DuckDuckGoActivity(), DownloadConfirmationD
private const val PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE = 200
private const val CUSTOM_UA =
"Mozilla/5.0 (Linux; Android 12) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/124.0.0.0 Mobile DuckDuckGo/5 Safari/537.36"
private const val REQUEST_CODE_CHOOSE_FILE = 100
}
}
Loading
Loading