Initial commit
This commit is contained in:
parent
efe8e4a550
commit
31ea334898
6 changed files with 82 additions and 31 deletions
|
|
@ -4,7 +4,6 @@ import android.content.Context
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.view.View
|
|
||||||
import android.view.inputmethod.InputMethodManager
|
import android.view.inputmethod.InputMethodManager
|
||||||
import android.webkit.CookieManager
|
import android.webkit.CookieManager
|
||||||
import android.webkit.WebView
|
import android.webkit.WebView
|
||||||
|
|
@ -16,18 +15,29 @@ class MainActivity : AppCompatActivity() {
|
||||||
|
|
||||||
private lateinit var webView: WebView
|
private lateinit var webView: WebView
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
private const val BASE_URL = "https://duck.ai"
|
||||||
|
}
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
setContentView(R.layout.activity_main)
|
setContentView(R.layout.activity_main)
|
||||||
|
|
||||||
webView = findViewById(R.id.webView)
|
webView = findViewById(R.id.webView)
|
||||||
|
|
||||||
webView.settings.javaScriptEnabled = true
|
setupWebView()
|
||||||
webView.settings.domStorageEnabled = true
|
loadUrlFromIntent()
|
||||||
webView.settings.databaseEnabled = true
|
}
|
||||||
webView.settings.setSupportZoom(false)
|
|
||||||
webView.settings.loadWithOverviewMode = true
|
private fun setupWebView() {
|
||||||
webView.settings.useWideViewPort = true
|
webView.settings.apply {
|
||||||
|
javaScriptEnabled = true
|
||||||
|
domStorageEnabled = true
|
||||||
|
databaseEnabled = true
|
||||||
|
setSupportZoom(false)
|
||||||
|
loadWithOverviewMode = true
|
||||||
|
useWideViewPort = true
|
||||||
|
}
|
||||||
|
|
||||||
webView.isFocusable = true
|
webView.isFocusable = true
|
||||||
webView.isFocusableInTouchMode = true
|
webView.isFocusableInTouchMode = true
|
||||||
|
|
@ -35,13 +45,26 @@ class MainActivity : AppCompatActivity() {
|
||||||
CookieManager.getInstance().setAcceptCookie(true)
|
CookieManager.getInstance().setAcceptCookie(true)
|
||||||
CookieManager.getInstance().setAcceptThirdPartyCookies(webView, true)
|
CookieManager.getInstance().setAcceptThirdPartyCookies(webView, true)
|
||||||
|
|
||||||
webView.webViewClient = WebViewClientOverride()
|
webView.webViewClient = object : WebViewClient() {
|
||||||
|
override fun onPageFinished(view: WebView?, url: String?) {
|
||||||
|
view?.postDelayed({
|
||||||
|
view.evaluateJavascript(
|
||||||
|
"setTimeout(() => {" +
|
||||||
|
" const input = document.querySelector('input[type=\"text\"], textarea[id*=\"message\"], [role=\"combobox\"]');" +
|
||||||
|
" if(input) { input.focus(); input.click(); }" +
|
||||||
|
"}, 100);"
|
||||||
|
) { _ -> }
|
||||||
|
}, 800)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun loadUrlFromIntent() {
|
||||||
val query = intent?.data?.getQueryParameter("q")
|
val query = intent?.data?.getQueryParameter("q")
|
||||||
val url = if (query != null) {
|
val url = if (query != null) {
|
||||||
"https://duck.ai/?q=${Uri.encode(query)}"
|
"$BASE_URL/?q=${Uri.encode(query)}"
|
||||||
} else {
|
} else {
|
||||||
"https://duck.ai/"
|
BASE_URL
|
||||||
}
|
}
|
||||||
|
|
||||||
webView.loadUrl(url)
|
webView.loadUrl(url)
|
||||||
|
|
@ -52,32 +75,29 @@ class MainActivity : AppCompatActivity() {
|
||||||
}, 800)
|
}, 800)
|
||||||
}
|
}
|
||||||
|
|
||||||
private inner class WebViewClientOverride : WebViewClient() {
|
|
||||||
override fun onPageFinished(view: WebView?, url: String?) {
|
|
||||||
view?.postDelayed({
|
|
||||||
view.evaluateJavascript(
|
|
||||||
"setTimeout(() => {" +
|
|
||||||
" const input = document.querySelector('input[type=\"text\"], textarea[id*=\"message\"], [role=\"combobox\"]');" +
|
|
||||||
" if(input) { input.focus(); input.click(); }" +
|
|
||||||
"}, 100);"
|
|
||||||
) { _ -> }
|
|
||||||
}, 800)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun showKeyboard() {
|
private fun showKeyboard() {
|
||||||
val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
|
val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
|
||||||
webView.requestFocus()
|
webView.requestFocus()
|
||||||
imm.showSoftInput(webView, InputMethodManager.SHOW_IMPLICIT)
|
imm.showSoftInput(webView, InputMethodManager.SHOW_IMPLICIT)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun onPause() {
|
||||||
|
webView.onPause()
|
||||||
|
super.onPause()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onResume() {
|
||||||
|
webView.onResume()
|
||||||
|
super.onResume()
|
||||||
|
}
|
||||||
|
|
||||||
override fun onNewIntent(intent: Intent?) {
|
override fun onNewIntent(intent: Intent?) {
|
||||||
super.onNewIntent(intent)
|
super.onNewIntent(intent)
|
||||||
this.intent = intent
|
this.intent = intent
|
||||||
|
|
||||||
val query = intent?.data?.getQueryParameter("q")
|
val query = intent?.data?.getQueryParameter("q")
|
||||||
if (query != null) {
|
if (query != null) {
|
||||||
val url = "https://duck.ai/?q=${Uri.encode(query)}"
|
val url = "$BASE_URL/?q=${Uri.encode(query)}"
|
||||||
webView.loadUrl(url)
|
webView.loadUrl(url)
|
||||||
webView.postDelayed({
|
webView.postDelayed({
|
||||||
webView.requestFocus()
|
webView.requestFocus()
|
||||||
|
|
@ -85,4 +105,9 @@ class MainActivity : AppCompatActivity() {
|
||||||
}, 1000)
|
}, 1000)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun onDestroy() {
|
||||||
|
webView.destroy()
|
||||||
|
super.onDestroy()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1,12 +1,15 @@
|
||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
<androidx.coordinatorlayout.widget.CoordinatorLayout
|
||||||
|
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||||
android:id="@+id/webViewContainer"
|
android:id="@+id/webViewContainer"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="match_parent">
|
android:layout_height="match_parent"
|
||||||
|
android:background="?attr/colorSurface">
|
||||||
|
|
||||||
<WebView
|
<WebView
|
||||||
android:id="@+id/webView"
|
android:id="@+id/webView"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="match_parent" />
|
android:layout_height="match_parent" />
|
||||||
|
|
||||||
</LinearLayout>
|
</androidx.coordinatorlayout.widget.CoordinatorLayout>
|
||||||
|
|
@ -3,7 +3,7 @@
|
||||||
android:id="@+id/widget_container"
|
android:id="@+id/widget_container"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="48dp"
|
android:layout_height="48dp"
|
||||||
android:background="@drawable/widget_background"
|
android:background="?attr/colorPrimaryContainer"
|
||||||
android:gravity="center"
|
android:gravity="center"
|
||||||
android:orientation="horizontal"
|
android:orientation="horizontal"
|
||||||
android:paddingHorizontal="16dp">
|
android:paddingHorizontal="16dp">
|
||||||
|
|
@ -12,6 +12,7 @@
|
||||||
android:layout_width="24dp"
|
android:layout_width="24dp"
|
||||||
android:layout_height="24dp"
|
android:layout_height="24dp"
|
||||||
android:src="@drawable/ic_search"
|
android:src="@drawable/ic_search"
|
||||||
|
android:tint="?attr/colorOnPrimaryContainer"
|
||||||
android:contentDescription="@string/search" />
|
android:contentDescription="@string/search" />
|
||||||
|
|
||||||
<TextView
|
<TextView
|
||||||
|
|
@ -19,7 +20,7 @@
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:layout_marginStart="12dp"
|
android:layout_marginStart="12dp"
|
||||||
android:text="@string/ask_ai"
|
android:text="@string/ask_ai"
|
||||||
android:textColor="#1a1a1a"
|
android:textColor="?attr/colorOnPrimaryContainer"
|
||||||
android:textSize="16sp"
|
android:textSize="16sp"
|
||||||
android:textStyle="bold" />
|
android:textStyle="bold" />
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<resources>
|
<resources>
|
||||||
<color name="ic_launcher_background">#FF5722</color>
|
<color name="ic_launcher_background">#6750A4</color>
|
||||||
</resources>
|
</resources>
|
||||||
|
|
@ -4,4 +4,9 @@
|
||||||
<string name="search">Search</string>
|
<string name="search">Search</string>
|
||||||
<string name="search_duckai">Search DuckAI</string>
|
<string name="search_duckai">Search DuckAI</string>
|
||||||
<string name="ask_ai">Ask AI</string>
|
<string name="ask_ai">Ask AI</string>
|
||||||
|
<string name="clear_history">Clear Chat History</string>
|
||||||
|
<string name="clear_history_confirm">Clear all chat history?</string>
|
||||||
|
<string name="yes">Yes</string>
|
||||||
|
<string name="no">No</string>
|
||||||
|
<string name="history_cleared">Chat history cleared</string>
|
||||||
</resources>
|
</resources>
|
||||||
|
|
@ -1,4 +1,21 @@
|
||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<resources>
|
<resources>
|
||||||
<style name="Theme.DuckAI" parent="Theme.Material3.DayNight.NoActionBar" />
|
<style name="Theme.DuckAI" parent="Theme.Material3.DayNight.NoActionBar">
|
||||||
|
<item name="colorPrimary">#6750A4</item>
|
||||||
|
<item name="colorOnPrimary">#FFFFFF</item>
|
||||||
|
<item name="colorPrimaryContainer">#EADDFF</item>
|
||||||
|
<item name="colorOnPrimaryContainer">#21005D</item>
|
||||||
|
<item name="colorSecondary">#625B71</item>
|
||||||
|
<item name="colorOnSecondary">#FFFFFF</item>
|
||||||
|
<item name="colorSecondaryContainer">#E8DEF8</item>
|
||||||
|
<item name="colorOnSecondaryContainer">#1D192B</item>
|
||||||
|
<item name="colorTertiary">#7D5260</item>
|
||||||
|
<item name="colorOnTertiary">#FFFFFF</item>
|
||||||
|
<item name="colorTertiaryContainer">#FFD8E4</item>
|
||||||
|
<item name="colorOnTertiaryContainer">#31111D</item>
|
||||||
|
<item name="colorError">#B3261E</item>
|
||||||
|
<item name="colorOnError">#FFFFFF</item>
|
||||||
|
<item name="android:statusBarColor">@android:color/transparent</item>
|
||||||
|
<item name="android:windowLightStatusBar">true</item>
|
||||||
|
</style>
|
||||||
</resources>
|
</resources>
|
||||||
Loading…
Add table
Add a link
Reference in a new issue