User agent for all connections

This commit is contained in:
M66B
2021-05-14 11:47:14 +02:00
parent 54e0727c62
commit 78eec5ecb5
10 changed files with 49 additions and 31 deletions

View File

@@ -35,6 +35,7 @@ import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import androidx.annotation.NonNull;
import androidx.preference.PreferenceManager;
import androidx.webkit.WebSettingsCompat;
import androidx.webkit.WebViewFeature;
@@ -47,6 +48,8 @@ public class WebViewEx extends WebView implements DownloadListener, View.OnLongC
private IWebView intf;
private Runnable onPageLoaded;
private static String userAgent = null;
private static final long PAGE_LOADED_FALLBACK_DELAY = 1500L; // milliseconds
public WebViewEx(Context context) {
@@ -352,24 +355,37 @@ public class WebViewEx extends WebView implements DownloadListener, View.OnLongC
}
}
@NonNull
static String getUserAgent(Context context) {
return getUserAgent(context, null);
}
@NonNull
static String getUserAgent(Context context, WebView webView) {
// https://developer.chrome.com/docs/multidevice/user-agent/#chrome-for-android
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean generic_ua = prefs.getBoolean("generic_ua", false);
boolean generic_ua = prefs.getBoolean("generic_ua", true);
if (generic_ua)
return getGenericUserAgent(context);
if (generic_ua) {
boolean large = context.getResources().getConfiguration()
.isLayoutSizeAtLeast(Configuration.SCREENLAYOUT_SIZE_LARGE);
return (large ? "Mozilla/5.0" : "Mozilla/5.0 (Mobile)");
} else
try {
try {
if (userAgent == null) {
if (webView == null)
webView = new WebView(context);
return webView.getSettings().getUserAgentString();
} catch (Throwable ex) {
Log.e(ex);
return null;
userAgent = webView.getSettings().getUserAgentString();
}
return userAgent;
} catch (Throwable ex) {
Log.w(ex);
return getGenericUserAgent(context);
}
}
@NonNull
private static String getGenericUserAgent(Context context) {
boolean large = context.getResources().getConfiguration()
.isLayoutSizeAtLeast(Configuration.SCREENLAYOUT_SIZE_LARGE);
return (large ? "Mozilla/5.0" : "Mozilla/5.0 (Mobile)");
}
interface IWebView {