-
Notifications
You must be signed in to change notification settings - Fork 935
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
593 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
app/src/main/java/com/duckduckgo/app/browser/duckchat/DuckChatJSHelper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
* 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.app.browser.duckchat | ||
|
||
import com.duckduckgo.app.browser.commands.Command | ||
import com.duckduckgo.app.browser.commands.Command.SendResponseToJs | ||
import com.duckduckgo.di.scopes.AppScope | ||
import com.duckduckgo.duckchat.api.DuckChat | ||
import com.duckduckgo.js.messaging.api.JsCallbackData | ||
import com.squareup.anvil.annotations.ContributesBinding | ||
import javax.inject.Inject | ||
import org.json.JSONObject | ||
|
||
interface DuckChatJSHelper { | ||
suspend fun processJsCallbackMessage( | ||
featureName: String, | ||
method: String, | ||
id: String?, | ||
data: JSONObject?, | ||
): Command? | ||
} | ||
|
||
@ContributesBinding(AppScope::class) | ||
class RealDuckChatJSHelper @Inject constructor( | ||
private val duckChat: DuckChat, | ||
private val preferencesStore: DuckChatPreferencesStore, | ||
) : DuckChatJSHelper { | ||
private fun getUserValues(featureName: String, method: String, id: String): JsCallbackData { | ||
val jsonPayload = JSONObject().apply { | ||
put(PLATFORM, ANDROID) | ||
put(IS_HANDOFF_ENABLED, duckChat.isEnabled()) | ||
put(PAYLOAD, preferencesStore.fetchAndClearUserPreferences()) | ||
} | ||
return JsCallbackData(jsonPayload, featureName, method, id) | ||
} | ||
|
||
override suspend fun processJsCallbackMessage( | ||
featureName: String, | ||
method: String, | ||
id: String?, | ||
data: JSONObject?, | ||
): Command? = when (method) { | ||
METHOD_GET_USER_VALUES -> id?.let { | ||
SendResponseToJs(getUserValues(featureName, method, it)) | ||
} | ||
METHOD_OPEN_AI_CHAT -> { | ||
data?.optString(PAYLOAD).let { payload -> | ||
preferencesStore.updateUserPreferences(payload) | ||
} | ||
duckChat.openDuckChat() | ||
null | ||
} | ||
else -> null | ||
} | ||
|
||
companion object { | ||
const val DUCK_CHAT_FEATURE_NAME = "aiChat" | ||
private const val METHOD_GET_USER_VALUES = "getUserValues" | ||
private const val METHOD_OPEN_AI_CHAT = "openAIChat" | ||
private const val PAYLOAD = "aiChatPayload" | ||
private const val IS_HANDOFF_ENABLED = "isAIChatHandoffEnabled" | ||
private const val PLATFORM = "platform" | ||
private const val ANDROID = "android" | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
app/src/main/java/com/duckduckgo/app/browser/duckchat/DuckChatPreferencesStore.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* 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.app.browser.duckchat | ||
|
||
import androidx.core.content.edit | ||
import com.duckduckgo.data.store.api.SharedPreferencesProvider | ||
import com.duckduckgo.di.scopes.AppScope | ||
import com.squareup.anvil.annotations.ContributesBinding | ||
import javax.inject.Inject | ||
|
||
interface DuckChatPreferencesStore { | ||
fun fetchAndClearUserPreferences(): String? | ||
fun updateUserPreferences(userPreferences: String?) | ||
} | ||
|
||
@ContributesBinding(AppScope::class) | ||
class RealDuckChatPreferencesStore @Inject constructor( | ||
private val sharedPreferencesProvider: SharedPreferencesProvider, | ||
) : DuckChatPreferencesStore { | ||
|
||
private val preferences by lazy { | ||
sharedPreferencesProvider.getSharedPreferences(FILENAME) | ||
} | ||
|
||
override fun fetchAndClearUserPreferences(): String? = | ||
preferences.getString(USER_PREFERENCES, null).also { | ||
preferences.edit { remove(USER_PREFERENCES) } | ||
} | ||
|
||
override fun updateUserPreferences(userPreferences: String?) { | ||
preferences.edit { | ||
putString(USER_PREFERENCES, userPreferences) | ||
} | ||
} | ||
|
||
companion object { | ||
const val FILENAME = "com.duckduckgo.app.duckchat" | ||
const val USER_PREFERENCES = "DUCK_CHAT_USER_PREFERENCES" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.