Skip to content
This repository has been archived by the owner on Feb 15, 2024. It is now read-only.

Commit

Permalink
Update 1.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
D4rK7355608 committed Oct 7, 2023
1 parent 0a0e49b commit 1c09e42
Show file tree
Hide file tree
Showing 12 changed files with 133 additions and 123 deletions.
2 changes: 1 addition & 1 deletion .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
# Version 1.0_r1:
# Version 1.0.1:
- **Patch**: Fixed the error message that appeared when users had cookies issues.
- **Minor**: Migrated the build.gradle file to Kotlin DSL (build.gradle.kts).
- **Minor**: Updated dependencies to the latest versions.

# Version 1.0.0:
- Initial stable version.
59 changes: 0 additions & 59 deletions app/build.gradle

This file was deleted.

61 changes: 61 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import org.jetbrains.kotlin.gradle.plugin.mpp.pm20.util.archivesName
plugins {
id("com.android.application")
id("org.jetbrains.kotlin.android")
}
android {
compileSdk = 34
namespace = "com.google.android.apps.bard"
defaultConfig {
applicationId = "com.google.android.apps.bard"
minSdk = 26
targetSdk = 34
versionCode = 2
versionName = "1.0.1"
archivesName = "${applicationId}-v${versionName}"
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
multiDexEnabled = true
isMinifyEnabled = true
isShrinkResources = true
isDebuggable = false
proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
}
debug {
multiDexEnabled = true
isMinifyEnabled = true
isShrinkResources = true
isDebuggable = true
proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}
kotlinOptions {
jvmTarget = "17"
}
buildFeatures {
viewBinding = true
buildConfig = true
}
bundle {
storeArchive {
enable = true
}
}
}
dependencies {
implementation("androidx.core:core-ktx:1.12.0")
implementation("androidx.appcompat:appcompat:1.6.1")
implementation("androidx.constraintlayout:constraintlayout:2.1.4")
implementation("androidx.swiperefreshlayout:swiperefreshlayout:1.1.0")
implementation("com.google.android.material:material:1.9.0")
implementation("me.zhanghai.android.fastscroll:library:1.3.0")
testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
}
4 changes: 4 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" android:maxSdkVersion="32"/>
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<application
android:label="@string/app_name"
android:icon="@mipmap/ic_launcher"
Expand Down
76 changes: 39 additions & 37 deletions app/src/main/kotlin/com/google/android/apps/bard/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.app.Activity
import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.webkit.JavascriptInterface
Expand All @@ -15,12 +16,14 @@ import android.content.SharedPreferences
import android.webkit.CookieManager
import android.webkit.WebSettings
import com.google.android.apps.bard.databinding.ActivityMainBinding
import com.google.android.material.snackbar.Snackbar
import me.zhanghai.android.fastscroll.FastScrollerBuilder
class MainActivity : Activity() {
private val userAgent = "Mozilla/5.0 (Linux; Android 10) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.5615.135 Mobile Safari/537.36"
private val userAgent = "Mozilla/5.0 (Linux; Android 13) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.5938.60 Mobile Safari/537.36"
private val chatUrl = "https://bard.google.com/"
private lateinit var binding: ActivityMainBinding
private lateinit var sharedPreferences: SharedPreferences
private lateinit var cookieManager: CookieManager
@SuppressLint("SetJavaScriptEnabled")
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
Expand All @@ -32,6 +35,9 @@ class MainActivity : Activity() {
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
sharedPreferences = getSharedPreferences("MyPrefs", Context.MODE_PRIVATE)
cookieManager = CookieManager.getInstance()
cookieManager.setAcceptCookie(true)
cookieManager.setAcceptThirdPartyCookies(binding.webView, true)
initializeFastScroller()
registerBackButtonCallback()
val userAccount = getUserAccount()
Expand All @@ -48,17 +54,15 @@ class MainActivity : Activity() {
setupWebViewClient()
binding.swipeRefreshLayout.setOnRefreshListener {
binding.webView.reload()
binding.swipeRefreshLayout.isRefreshing = false
}
binding.webView.loadUrl(chatUrl)
}
private fun initializeFastScroller() {
FastScrollerBuilder(binding.webView).useMd2Style().build()
}
private fun registerBackButtonCallback() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
onBackInvokedDispatcher.registerOnBackInvokedCallback(
OnBackInvokedDispatcher.PRIORITY_DEFAULT
) {
onBackInvokedDispatcher.registerOnBackInvokedCallback(OnBackInvokedDispatcher.PRIORITY_DEFAULT) {
if (binding.webView.canGoBack()) {
binding.webView.goBack()
} else {
Expand All @@ -69,9 +73,6 @@ class MainActivity : Activity() {
}
@SuppressLint("SetJavaScriptEnabled")
private fun configureWebViewSettings() {
val cookieManager = CookieManager.getInstance()
cookieManager.setAcceptCookie(true)
cookieManager.setAcceptThirdPartyCookies(binding.webView, true)
binding.webView.settings.apply {
cacheMode = WebSettings.LOAD_DEFAULT
userAgentString = userAgent
Expand All @@ -84,49 +85,51 @@ class MainActivity : Activity() {
}
private fun setupWebViewClient() {
binding.webView.webViewClient = object : WebViewClient() {
override fun shouldOverrideUrlLoading(
view: WebView?,
request: WebResourceRequest?
): Boolean {
val url = request?.url ?: return false
if (url.toString().contains(chatUrl)) {
override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean {
val urlString = request?.url?.toString() ?: return false
if (urlString == "alert://alert") {
Snackbar.make(binding.root, "alert", Snackbar.LENGTH_LONG).show()
} else if (urlString == "choose://image") {
val intent = Intent(Intent.ACTION_GET_CONTENT)
intent.addCategory(Intent.CATEGORY_OPENABLE)
intent.type = "image/*"
startActivityForResult(intent, 1)
return true
}
if (urlString.contains(chatUrl)) {
return false
}
if (binding.webView.url.toString().contains(chatUrl) && !binding.webView.url.toString().contains("/auth")) {
view?.loadUrl(url.toString())
if (binding.webView.url?.contains(chatUrl) == true && !binding.webView.url.toString().contains("/auth")) {
view?.loadUrl(urlString)
return true
}
return false
}
override fun onPageFinished(view: WebView?, url: String?) {
super.onPageFinished(view, url)
if (binding.webView.url.toString().contains(chatUrl) && !binding.webView.url.toString().contains("/auth")) {
if (binding.webView.url?.contains(chatUrl) == true && !binding.webView.url.toString().contains("/auth")) {
val userAccount = extractUserAccountFromWebPage()
if (userAccount.isNotEmpty()) {
saveUserAccount(userAccount)
}
}
binding.swipeRefreshLayout.isRefreshing =
false
binding.swipeRefreshLayout.isEnabled =
!(binding.webView.url.toString().contains(chatUrl) && !binding.webView.url.toString().contains("/auth"))
binding.webView.evaluateJavascript(
"""
(() => {
navigator.clipboard.writeText = (text) => {
Android.copyToClipboard(text);
return Promise.resolve();
}
})();
""".trimIndent(), null
binding.swipeRefreshLayout.isRefreshing = false
binding.swipeRefreshLayout.isEnabled = !(binding.webView.url?.contains(chatUrl) == true && !binding.webView.url.toString().contains("/auth"))
binding.webView.evaluateJavascript("""
(() => {
navigator.clipboard.writeText = (text) => {
Android.copyToClipboard(text);
return Promise.resolve();
}
})();
""".trimIndent(), null
)
}
}
}
private fun extractUserAccountFromWebPage(): String {
var userAccount = ""
binding.webView.evaluateJavascript(
"""
binding.webView.evaluateJavascript("""
(function() {
// Replace this code with the appropriate JavaScript code to extract the account information
// Example: If the account is stored in a DOM element with id "account", you can use the following code:
Expand All @@ -136,8 +139,7 @@ class MainActivity : Activity() {
}
return '';
})();
"""
) { result ->
""") { result ->
userAccount = result?.trim('"') ?: ""
}
return userAccount
Expand All @@ -147,15 +149,15 @@ class MainActivity : Activity() {
}
override fun onPause() {
super.onPause()
val cookieManager = CookieManager.getInstance()
val cookies = cookieManager.getCookie(chatUrl)
sharedPreferences.edit().putString("cookies", cookies).apply()
}
override fun onResume() {
super.onResume()
val cookies = sharedPreferences.getString("cookies", "")
if (!cookies.isNullOrEmpty()) {
val cookieManager = CookieManager.getInstance()
cookieManager.removeAllCookies(null)
cookieManager.flush()
val cookieArray = cookies.split(";")
for (cookie in cookieArray) {
cookieManager.setCookie(chatUrl, cookie)
Expand All @@ -182,4 +184,4 @@ class MainActivity : Activity() {
private fun getUserAccount(): String {
return sharedPreferences.getString("userAccount", "") ?: ""
}
}
}
22 changes: 10 additions & 12 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/swipeRefreshLayout"
android:layout_height="match_parent">
<me.zhanghai.android.fastscroll.FastScrollWebView
android:id="@+id/web_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
<me.zhanghai.android.fastscroll.FastScrollWebView
android:id="@+id/web_view"
android:layout_height="match_parent"
android:focusable="true"
android:focusableInTouchMode="true">
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/swipe_refresh_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:focusableInTouchMode="true"/>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
android:layout_height="match_parent"/>
</me.zhanghai.android.fastscroll.FastScrollWebView>
</androidx.constraintlayout.widget.ConstraintLayout>
8 changes: 0 additions & 8 deletions build.gradle

This file was deleted.

5 changes: 5 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
plugins {
id("com.android.application") version "8.1.2" apply false
id("com.android.library") version "8.1.2" apply false
id("org.jetbrains.kotlin.android") version "1.9.0" apply false
}
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Sun Jul 16 12:27:34 EEST 2023
#Sat Oct 07 13:33:33 EEST 2023
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.2.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Loading

0 comments on commit 1c09e42

Please sign in to comment.