Skip to content

Commit

Permalink
Allow custom user agent
Browse files Browse the repository at this point in the history
  • Loading branch information
xb2016 committed Aug 28, 2024
1 parent b990a0c commit 5ceb0d8
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 3 deletions.
8 changes: 8 additions & 0 deletions app/src/main/java/com/hippo/ehviewer/Settings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import com.hippo.ehviewer.EhApplication.Companion.application
import com.hippo.ehviewer.client.data.FavListUrlBuilder
import com.hippo.ehviewer.ui.scene.GalleryListScene
import com.hippo.glgallery.GalleryView
import com.hippo.okhttp.ChromeRequestBuilder.Companion.CHROME_USER_AGENT
import com.hippo.unifile.UniFile
import com.hippo.yorozuya.NumberUtils
import java.util.Locale
Expand Down Expand Up @@ -177,6 +178,7 @@ object Settings {
private const val DEFAULT_PROXY_PORT = -1
private const val KEY_BUILT_IN_HOSTS = "built_in_hosts_2"
private const val DEFAULT_BUILT_IN_HOSTS = false
private const val KEY_USER_AGENT = "user_agent"
private const val KEY_APP_LINK_VERIFY_TIP = "app_link_verify_tip"
private const val DEFAULT_APP_LINK_VERIFY_TIP = false

Expand Down Expand Up @@ -664,6 +666,12 @@ object Settings {
val builtInHosts: Boolean
get() = getBoolean(KEY_BUILT_IN_HOSTS, DEFAULT_BUILT_IN_HOSTS)

val userAgent: String?
get() = getString(KEY_USER_AGENT, CHROME_USER_AGENT)
fun putUserAgent(value: String?) {
putString(KEY_USER_AGENT, value)
}

val appLinkVerifyTip: Boolean
get() = getBoolean(KEY_APP_LINK_VERIFY_TIP, DEFAULT_APP_LINK_VERIFY_TIP)
fun putAppLinkVerifyTip(value: Boolean) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Copyright 2024 Moedog
*
* This file is part of EhViewer
*
* EhViewer is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* EhViewer is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with EhViewer.
* If not, see <https://www.gnu.org/licenses/>.
*/
package com.hippo.ehviewer.preference

import android.annotation.SuppressLint
import android.content.Context
import android.content.DialogInterface
import android.util.AttributeSet
import android.view.KeyEvent
import android.view.LayoutInflater
import android.view.View
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import android.widget.TextView.OnEditorActionListener
import androidx.appcompat.app.AlertDialog
import com.hippo.ehviewer.R
import com.hippo.ehviewer.Settings
import com.hippo.okhttp.ChromeRequestBuilder.Companion.CHROME_USER_AGENT
import com.hippo.preference.DialogPreference

@SuppressLint("InflateParams")
class UserAgentPreference(
context: Context,
attrs: AttributeSet? = null,
) : DialogPreference(context, attrs),
View.OnClickListener,
OnEditorActionListener {
private lateinit var mDialog: AlertDialog
private lateinit var mButton: Button
private var mUserAgent: String? = Settings.userAgent
private val view: View =
LayoutInflater.from(context).inflate(R.layout.dialog_edittext_builder, null)
private val editText: EditText = view.findViewById(R.id.edit_text)

init {
updateSummary()
}

private fun updateSummary() {
summary = mUserAgent
}

override fun onCreateDialogView(): View {
return view
}

override fun onDialogCreated(dialog: AlertDialog) {
super.onDialogCreated(dialog)
mDialog = dialog
mButton = dialog.getButton(DialogInterface.BUTTON_POSITIVE)
mButton.setOnClickListener(this)
editText.isSingleLine = false
editText.setText(mUserAgent)
editText.setSelection(editText.text.length)
editText.setOnEditorActionListener(this)
}

override fun onClick(v: View) {
mUserAgent = editText.text.toString().trim().ifBlank { null } ?: CHROME_USER_AGENT
Settings.putUserAgent(mUserAgent)
updateSummary()
mDialog.dismiss()
}

override fun onEditorAction(v: TextView?, p1: Int, event: KeyEvent?): Boolean {
mButton.performClick()
return true
}
}
7 changes: 4 additions & 3 deletions app/src/main/java/com/hippo/okhttp/ChromeRequestBuilder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,21 @@
*/
package com.hippo.okhttp

import com.hippo.ehviewer.Settings
import okhttp3.Request

open class ChromeRequestBuilder(url: String) : Request.Builder() {
companion object {
const val CHROME_USER_AGENT =
"Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Mobile Safari/537.36"
"Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Mobile Safari/537.36"
private const val CHROME_ACCEPT =
"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"
"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"
private const val CHROME_ACCEPT_LANGUAGE = "en-US,en;q=0.9"
}

init {
this.url(url)
this.addHeader("User-Agent", CHROME_USER_AGENT)
this.addHeader("User-Agent", Settings.userAgent!!)
this.addHeader("Accept", CHROME_ACCEPT)
this.addHeader("Accept-Language", CHROME_ACCEPT_LANGUAGE)
}
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,7 @@
<string name="settings_advanced_built_in_hosts_summary">Apply the built-in host-to-IP mapping provided by the app.\nIt can be overridden by custom hosts.txt</string>
<string name="settings_advanced_custom_hosts_title">Custom hosts.txt</string>
<string name="settings_advanced_custom_hosts_summary">Map hostnames to IP addresses.\nIt can override built-in hosts.txt</string>
<string name="settings_advanced_user_agent_title" translatable="false">User Agent</string>
<string name="settings_advanced_backup_favorite">Backup favorite list</string>
<string name="settings_advanced_backup_favorite_summary">Backup remote favorite list to local</string>
<string name="settings_advanced_backup_favorite_start">Backing up favorite list %s</string>
Expand Down
7 changes: 7 additions & 0 deletions app/src/main/res/xml/advanced_settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,13 @@
android:title="@string/settings_advanced_custom_hosts_title"
app:iconSpaceReserved="false" />

<com.hippo.ehviewer.preference.UserAgentPreference
android:key="user_agent"
android:title="@string/settings_advanced_user_agent_title"
app:iconSpaceReserved="false"
app:negativeButtonText="@android:string/cancel"
app:positiveButtonText="@android:string/ok" />

<Preference
android:key="backup_favorite"
android:summary="@string/settings_advanced_backup_favorite_summary"
Expand Down

0 comments on commit 5ceb0d8

Please sign in to comment.