Add WebView support, cookies, focus and keyboard on launch
This commit is contained in:
parent
abed3258f8
commit
efe8e4a550
10 changed files with 274 additions and 69 deletions
|
|
@ -29,17 +29,6 @@
|
|||
</intent-filter>
|
||||
</activity>
|
||||
|
||||
<receiver
|
||||
android:name=".widget.SearchWidgetProvider"
|
||||
android:exported="true">
|
||||
<intent-filter>
|
||||
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
|
||||
</intent-filter>
|
||||
<meta-data
|
||||
android:name="android.appwidget.provider"
|
||||
android:resource="@xml/search_widget_info" />
|
||||
</receiver>
|
||||
|
||||
</application>
|
||||
|
||||
</manifest>
|
||||
|
|
@ -4,7 +4,9 @@ import android.content.Context
|
|||
import android.content.Intent
|
||||
import android.net.Uri
|
||||
import android.os.Bundle
|
||||
import android.view.View
|
||||
import android.view.inputmethod.InputMethodManager
|
||||
import android.webkit.CookieManager
|
||||
import android.webkit.WebView
|
||||
import android.webkit.WebViewClient
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
|
|
@ -19,7 +21,20 @@ class MainActivity : AppCompatActivity() {
|
|||
setContentView(R.layout.activity_main)
|
||||
|
||||
webView = findViewById(R.id.webView)
|
||||
|
||||
webView.settings.javaScriptEnabled = true
|
||||
webView.settings.domStorageEnabled = true
|
||||
webView.settings.databaseEnabled = true
|
||||
webView.settings.setSupportZoom(false)
|
||||
webView.settings.loadWithOverviewMode = true
|
||||
webView.settings.useWideViewPort = true
|
||||
|
||||
webView.isFocusable = true
|
||||
webView.isFocusableInTouchMode = true
|
||||
|
||||
CookieManager.getInstance().setAcceptCookie(true)
|
||||
CookieManager.getInstance().setAcceptThirdPartyCookies(webView, true)
|
||||
|
||||
webView.webViewClient = WebViewClientOverride()
|
||||
|
||||
val query = intent?.data?.getQueryParameter("q")
|
||||
|
|
@ -30,22 +45,30 @@ class MainActivity : AppCompatActivity() {
|
|||
}
|
||||
|
||||
webView.loadUrl(url)
|
||||
|
||||
window.decorView.postDelayed({
|
||||
webView.requestFocus()
|
||||
showKeyboard()
|
||||
}, 800)
|
||||
}
|
||||
|
||||
private inner class WebViewClientOverride : WebViewClient() {
|
||||
override fun onPageFinished(view: WebView?, url: String?) {
|
||||
view?.postDelayed({
|
||||
view.evaluateJavascript(
|
||||
"document.querySelector('input[type=\"text\"], input[placeholder*=\"задайте\"], input[placeholder*=\"ask\"], [role=\"combobox\"]')?.focus();"
|
||||
) { }
|
||||
showKeyboard()
|
||||
}, 500)
|
||||
"setTimeout(() => {" +
|
||||
" const input = document.querySelector('input[type=\"text\"], textarea[id*=\"message\"], [role=\"combobox\"]');" +
|
||||
" if(input) { input.focus(); input.click(); }" +
|
||||
"}, 100);"
|
||||
) { _ -> }
|
||||
}, 800)
|
||||
}
|
||||
}
|
||||
|
||||
private fun showKeyboard() {
|
||||
val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
|
||||
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0)
|
||||
webView.requestFocus()
|
||||
imm.showSoftInput(webView, InputMethodManager.SHOW_IMPLICIT)
|
||||
}
|
||||
|
||||
override fun onNewIntent(intent: Intent?) {
|
||||
|
|
@ -56,7 +79,10 @@ class MainActivity : AppCompatActivity() {
|
|||
if (query != null) {
|
||||
val url = "https://duck.ai/?q=${Uri.encode(query)}"
|
||||
webView.loadUrl(url)
|
||||
webView.postDelayed({ showKeyboard() }, 1000)
|
||||
webView.postDelayed({
|
||||
webView.requestFocus()
|
||||
showKeyboard()
|
||||
}, 1000)
|
||||
}
|
||||
}
|
||||
}
|
||||
17
app/src/main/res/drawable/ic_launcher_foreground.xml
Normal file
17
app/src/main/res/drawable/ic_launcher_foreground.xml
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:width="108dp"
|
||||
android:height="108dp"
|
||||
android:viewportWidth="108"
|
||||
android:viewportHeight="108">
|
||||
<path
|
||||
android:fillColor="#FFFFFF"
|
||||
android:pathData="M54,30 L54,78 M30,54 L78,54"
|
||||
android:strokeWidth="8"
|
||||
android:strokeColor="#FFFFFF"/>
|
||||
<path
|
||||
android:fillColor="#FFFFFF"
|
||||
android:pathData="M54,40 A14,14 0 1,1 54,68 A14,14 0 1,1 54,40"
|
||||
android:strokeWidth="4"
|
||||
android:strokeColor="#FFFFFF"/>
|
||||
</vector>
|
||||
5
app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml
Normal file
5
app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<background android:drawable="@color/ic_launcher_background"/>
|
||||
<foreground android:drawable="@drawable/ic_launcher_foreground"/>
|
||||
</adaptive-icon>
|
||||
6
app/src/main/res/mipmap-hdpi/ic_launcher.xml
Normal file
6
app/src/main/res/mipmap-hdpi/ic_launcher.xml
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<shape xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:shape="oval">
|
||||
<solid android:color="#FF5722"/>
|
||||
<size android:width="48dp" android:height="48dp"/>
|
||||
</shape>
|
||||
4
app/src/main/res/values/colors.xml
Normal file
4
app/src/main/res/values/colors.xml
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<color name="ic_launcher_background">#FF5722</color>
|
||||
</resources>
|
||||
Loading…
Add table
Add a link
Reference in a new issue