Added option to use generic user agent string

This commit is contained in:
M66B 2021-05-13 18:46:43 +02:00
parent 3a1382aa11
commit f4a52222ce
6 changed files with 65 additions and 2 deletions

View File

@ -6824,6 +6824,7 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
WebView wv = view.findViewById(R.id.wv);
WebSettings settings = wv.getSettings();
settings.setUserAgentString(WebViewEx.getUserAgent(getContext(), wv));
settings.setUseWideViewPort(true);
settings.setLoadWithOverviewMode(overview_mode);

View File

@ -7888,6 +7888,7 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences.
printWebView = new WebView(context);
WebSettings settings = printWebView.getSettings();
settings.setUserAgentString(WebViewEx.getUserAgent(context, printWebView));
settings.setLoadsImagesAutomatically(print_html_images);
settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
settings.setAllowFileAccess(true);

View File

@ -73,6 +73,8 @@ public class FragmentOptionsPrivacy extends FragmentBase implements SharedPrefer
private SwitchCompat swIncognitoKeyboard;
private ImageButton ibIncognitoKeyboard;
private SwitchCompat swSecure;
private SwitchCompat swGenericUserAgent;
private TextView tvGenericUserAgent;
private SwitchCompat swSafeBrowsing;
private ImageButton ibSafeBrowsing;
private ImageButton ibDisconnectBlacklist;
@ -88,7 +90,8 @@ public class FragmentOptionsPrivacy extends FragmentBase implements SharedPrefer
"confirm_links", "browse_links", "confirm_images", "confirm_html",
"disable_tracking", "hide_timezone",
"pin", "biometrics", "biometrics_timeout",
"display_hidden", "incognito_keyboard", "secure", "safe_browsing",
"display_hidden", "incognito_keyboard", "secure",
"generic_ua", "safe_browsing",
"disconnect_auto_update", "disconnect_links", "disconnect_images"
};
@ -115,6 +118,8 @@ public class FragmentOptionsPrivacy extends FragmentBase implements SharedPrefer
swIncognitoKeyboard = view.findViewById(R.id.swIncognitoKeyboard);
ibIncognitoKeyboard = view.findViewById(R.id.ibIncognitoKeyboard);
swSecure = view.findViewById(R.id.swSecure);
swGenericUserAgent = view.findViewById(R.id.swGenericUserAgent);
tvGenericUserAgent = view.findViewById(R.id.tvGenericUserAgent);
swSafeBrowsing = view.findViewById(R.id.swSafeBrowsing);
ibSafeBrowsing = view.findViewById(R.id.ibSafeBrowsing);
ibDisconnectBlacklist = view.findViewById(R.id.ibDisconnectBlacklist);
@ -256,6 +261,13 @@ public class FragmentOptionsPrivacy extends FragmentBase implements SharedPrefer
}
});
swGenericUserAgent.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("generic_ua", checked).apply();
}
});
swSafeBrowsing.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
@ -406,6 +418,9 @@ public class FragmentOptionsPrivacy extends FragmentBase implements SharedPrefer
swDisplayHidden.setChecked(prefs.getBoolean("display_hidden", false));
swIncognitoKeyboard.setChecked(prefs.getBoolean("incognito_keyboard", false));
swSecure.setChecked(prefs.getBoolean("secure", false));
tvGenericUserAgent.setText(WebViewEx.getUserAgent(getContext(), null));
swGenericUserAgent.setChecked(prefs.getBoolean("generic_ua", false));
swSafeBrowsing.setChecked(prefs.getBoolean("safe_browsing", false));
long time = prefs.getLong("disconnect_last", -1);

View File

@ -21,6 +21,7 @@ package eu.faircode.email;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
@ -65,6 +66,7 @@ public class WebViewEx extends WebView implements DownloadListener, View.OnLongC
setOnLongClickListener(this);
WebSettings settings = getSettings();
settings.setUserAgentString(getUserAgent(context, this));
settings.setUseWideViewPort(true);
settings.setLoadWithOverviewMode(overview_mode);
@ -350,6 +352,26 @@ public class WebViewEx extends WebView implements DownloadListener, View.OnLongC
}
}
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);
if (generic_ua) {
boolean large = context.getResources().getConfiguration()
.isLayoutSizeAtLeast(Configuration.SCREENLAYOUT_SIZE_LARGE);
return (large ? "Mozilla/5.0" : "Mozilla/5.0 (Mobile)");
} else
try {
if (webView == null)
webView = new WebView(context);
return webView.getSettings().getUserAgentString();
} catch (Throwable ex) {
Log.e(ex);
return null;
}
}
interface IWebView {
void onSizeChanged(int w, int h, int ow, int oh);

View File

@ -339,6 +339,29 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/swSecure" />
<androidx.appcompat.widget.SwitchCompat
android:id="@+id/swGenericUserAgent"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:text="@string/title_advanced_generic_user_agent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvSecureHint"
app:switchPadding="12dp" />
<eu.faircode.email.FixedTextView
android:id="@+id/tvGenericUserAgent"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="48dp"
android:text="Mozilla/5.0 (Mobile)"
android:textAppearance="@style/TextAppearance.AppCompat.Small"
android:textStyle="italic"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/swGenericUserAgent" />
<androidx.appcompat.widget.SwitchCompat
android:id="@+id/swSafeBrowsing"
android:layout_width="0dp"
@ -347,7 +370,7 @@
android:text="@string/title_advanced_safe_browsing"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvSecureHint"
app:layout_constraintTop_toBottomOf="@id/tvGenericUserAgent"
app:switchPadding="12dp" />
<eu.faircode.email.FixedTextView

View File

@ -509,6 +509,7 @@
<string name="title_advanced_secure">Hide from recent apps screen and prevent taking screenshots</string>
<string name="title_advanced_pin">PIN</string>
<string name="title_advanced_biometrics_timeout">Biometric authentication timeout</string>
<string name="title_advanced_generic_user_agent">Use generic browser user agent</string>
<string name="title_advanced_safe_browsing" translatable="false">Google Safe browsing (Android 8+)</string>
<string name="title_advanced_disconnect_blacklist" translatable="false">Disconnect\'s tracker protection lists</string>
<string name="title_advanced_disconnect_auto_update">Automatically update lists weekly</string>