Skip to content

Commit

Permalink
Add device name to user agent and ensure user agent is used in all co…
Browse files Browse the repository at this point in the history
…mmunication (#1776)

* Add user agent header to webview and whenever it was replaced in a call

* Add device name to user agent string and during auth

* Switch to WebSettings for User Agent in WebView

* Add header to more locations

* Review comments

* Switch from device name to device model and clean up
  • Loading branch information
dshokouhi authored Oct 16, 2021
1 parent fc8f10a commit 602b6b9
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.Fragment
import io.homeassistant.companion.android.BuildConfig
import io.homeassistant.companion.android.DaggerPresenterComponent
import io.homeassistant.companion.android.PresenterModule
import io.homeassistant.companion.android.R
Expand All @@ -26,6 +27,7 @@ class AuthenticationFragment : Fragment(), AuthenticationView {

companion object {
private const val TAG = "AuthenticationFragment"
private const val USER_AGENT_STRING = "HomeAssistant/Android"

fun newInstance(): AuthenticationFragment {
return AuthenticationFragment()
Expand Down Expand Up @@ -63,6 +65,7 @@ class AuthenticationFragment : Fragment(), AuthenticationView {
webView.apply {
settings.javaScriptEnabled = true
settings.domStorageEnabled = true
settings.userAgentString = USER_AGENT_STRING + " ${Build.MODEL} ${BuildConfig.VERSION_NAME}"
webViewClient = object : WebViewClient() {
override fun shouldOverrideUrlLoading(view: WebView?, url: String): Boolean {
return presenter.onRedirectUrl(url)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ class WebViewActivity : BaseActivity(), io.homeassistant.companion.android.webvi
private const val APP_PREFIX = "app://"
private const val INTENT_PREFIX = "intent://"
private const val MARKET_PREFIX = "https://play.google.com/store/apps/details?id="
private const val USER_AGENT_STRING = "HomeAssistant/Android"

fun newInstance(context: Context, path: String? = null): Intent {
return Intent(context, WebViewActivity::class.java).apply {
Expand Down Expand Up @@ -215,6 +216,7 @@ class WebViewActivity : BaseActivity(), io.homeassistant.companion.android.webvi

settings.javaScriptEnabled = true
settings.domStorageEnabled = true
settings.userAgentString = USER_AGENT_STRING + " ${Build.MODEL} ${BuildConfig.VERSION_NAME}"
webViewClient = object : WebViewClient() {
override fun onReceivedError(
view: WebView?,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.homeassistant.companion.android.common.data

import android.os.Build
import com.fasterxml.jackson.databind.DeserializationFeature
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.PropertyNamingStrategy
Expand Down Expand Up @@ -45,7 +46,7 @@ class HomeAssistantRetrofit @Inject constructor(urlRepository: UrlRepository) {
val newRequest = runBlocking {
it.request().newBuilder()
.url(it.request().url.toString().replace(LOCAL_HOST, urlRepository.getUrl().toString()))
.header(USER_AGENT, "$USER_AGENT_STRING ${BuildConfig.VERSION_NAME}")
.header(USER_AGENT, "$USER_AGENT_STRING ${Build.MODEL} ${BuildConfig.VERSION_NAME}")
.build()
}
it.proceed(newRequest)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ class AuthenticationRepositoryImpl @Inject constructor(
private const val PREF_EXPIRED_DATE = "expires_date"
private const val PREF_REFRESH_TOKEN = "refresh_token"
private const val PREF_TOKEN_TYPE = "token_type"

private const val PREF_BIOMETRIC_ENABLED = "biometric_enabled"
}

Expand Down

0 comments on commit 602b6b9

Please sign in to comment.