From 95a3932f8b418fea88bc3aea4cc0914f4ec9f43b Mon Sep 17 00:00:00 2001 From: M66B Date: Sat, 4 Jun 2022 12:23:38 +0200 Subject: [PATCH] Added support for User-Agent Client Hints (2) --- .../eu/faircode/email/ConnectionHelper.java | 35 +++++++++++++++---- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/ConnectionHelper.java b/app/src/main/java/eu/faircode/email/ConnectionHelper.java index 5b88847f6e..0f26f81b75 100644 --- a/app/src/main/java/eu/faircode/email/ConnectionHelper.java +++ b/app/src/main/java/eu/faircode/email/ConnectionHelper.java @@ -628,13 +628,34 @@ public class ConnectionHelper { static void setUserAgent(Context context, HttpURLConnection connection) { connection.setRequestProperty("User-Agent", WebViewEx.getUserAgent(context)); - // https://web.dev/migrate-to-ua-ch/ + if (BuildConfig.DEBUG) { + // https://web.dev/migrate-to-ua-ch/ + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); + boolean generic_ua = prefs.getBoolean("generic_ua", false); - // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Sec-CH-UA - connection.setRequestProperty("Sec-CH-UA", "\"Chromium\""); - // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Sec-CH-UA-Mobile - connection.setRequestProperty("Sec-CH-UA-Mobile", "?1"); - // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Sec-CH-UA-Platform - connection.setRequestProperty("Sec-CH-UA-Platform", "Android"); + // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Sec-CH-UA + connection.setRequestProperty("Sec-CH-UA", "\"Chromium\""); // No WebView API yet + // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Sec-CH-UA-Mobile + connection.setRequestProperty("Sec-CH-UA-Mobile", "?1"); + // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Sec-CH-UA-Platform + connection.setRequestProperty("Sec-CH-UA-Platform", "\"Android\""); + + if (!generic_ua) { + String release = Build.VERSION.RELEASE; + if (release == null) + release = ""; + release = release.replace("\"", "'"); + + String model = Build.MODEL; + if (model == null) + model = ""; + model = model.replace("\"", "'"); + + // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Sec-CH-UA-Platform-Version + connection.setRequestProperty("Sec-CH-UA-Platform-Version", "\"" + release + "\""); + // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Sec-CH-UA-Model + connection.setRequestProperty("Sec-CH-UA-Model", "\"" + model + "\""); + } + } } }