Allow to customize the user agent

This commit is contained in:
tom79 2019-11-29 18:02:49 +01:00
parent dacfa6ea11
commit 6c9ebb3035
9 changed files with 103 additions and 25 deletions

View File

@ -293,6 +293,10 @@ public class PeertubeActivity extends BaseActivity implements OnRetrievePeertube
}
}
});
String user_agent = sharedpreferences.getString(Helper.SET_CUSTOM_USER_AGENT, null);
if( user_agent != null) {
webview_video.getSettings().setUserAgentString(user_agent);
}
webview_video.getSettings().setAllowFileAccess(true);
webview_video.setWebChromeClient(mastalabWebChromeClient);
webview_video.getSettings().setDomStorageEnabled(true);

View File

@ -133,6 +133,10 @@ public class WebviewConnectActivity extends BaseActivity {
webView = findViewById(R.id.webviewConnect);
clearCookies(getApplicationContext());
webView.getSettings().setJavaScriptEnabled(true);
String user_agent = sharedpreferences.getString(Helper.SET_CUSTOM_USER_AGENT, null);
if( user_agent != null) {
webView.getSettings().setUserAgentString(user_agent);
}
if (android.os.Build.VERSION.SDK_INT >= 21) {
CookieManager.getInstance().setAcceptThirdPartyCookies(webView, true);
} else {

View File

@ -94,6 +94,7 @@ public class HttpsConnection {
private SharedPreferences sharedpreferences;
private Proxy proxy;
private String instance;
private String USER_AGENT;
public HttpsConnection(Context context, String instance) {
this.instance = instance;
@ -102,6 +103,8 @@ public class HttpsConnection {
boolean proxyEnabled = sharedpreferences.getBoolean(Helper.SET_PROXY_ENABLED, false);
int type = sharedpreferences.getInt(Helper.SET_PROXY_TYPE, 0);
proxy = null;
USER_AGENT = sharedpreferences.getString(Helper.SET_CUSTOM_USER_AGENT, Helper.USER_AGENT);
if (proxyEnabled) {
try {
String host = sharedpreferences.getString(Helper.SET_PROXY_HOST, "127.0.0.1");
@ -211,7 +214,7 @@ public class HttpsConnection {
httpsURLConnection = (HttpsURLConnection) url.openConnection();
httpsURLConnection.setConnectTimeout(timeout * 1000);
httpsURLConnection.setRequestProperty("http.keepAlive", "false");
httpsURLConnection.setRequestProperty("User-Agent", Helper.USER_AGENT);
httpsURLConnection.setRequestProperty("User-Agent", USER_AGENT);
httpsURLConnection.setRequestProperty("Content-Type", "application/json");
httpsURLConnection.setRequestProperty("Accept", "application/json");
httpsURLConnection.setSSLSocketFactory(new TLSSocketFactory(this.instance));
@ -389,7 +392,7 @@ public class HttpsConnection {
httpsURLConnection = (HttpsURLConnection) url.openConnection(proxy);
else
httpsURLConnection = (HttpsURLConnection) url.openConnection();
httpsURLConnection.setRequestProperty("User-Agent", Helper.USER_AGENT);
httpsURLConnection.setRequestProperty("User-Agent", USER_AGENT);
httpsURLConnection.setConnectTimeout(timeout * 1000);
httpsURLConnection.setDoOutput(true);
httpsURLConnection.setSSLSocketFactory(new TLSSocketFactory(this.instance));
@ -456,7 +459,7 @@ public class HttpsConnection {
httpURLConnection = (HttpURLConnection) url.openConnection(proxy);
else
httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestProperty("User-Agent", Helper.USER_AGENT);
httpURLConnection.setRequestProperty("User-Agent", USER_AGENT);
httpURLConnection.setConnectTimeout(timeout * 1000);
httpURLConnection.setDoOutput(true);
httpURLConnection.setRequestMethod("POST");
@ -511,7 +514,7 @@ public class HttpsConnection {
httpsURLConnection = (HttpsURLConnection) url.openConnection(proxy);
else
httpsURLConnection = (HttpsURLConnection) url.openConnection();
httpsURLConnection.setRequestProperty("User-Agent", Helper.USER_AGENT);
httpsURLConnection.setRequestProperty("User-Agent", USER_AGENT);
httpsURLConnection.setConnectTimeout(timeout * 1000);
httpsURLConnection.setDoOutput(true);
httpsURLConnection.setSSLSocketFactory(new TLSSocketFactory(this.instance));
@ -564,7 +567,7 @@ public class HttpsConnection {
httpURLConnection = (HttpURLConnection) url.openConnection(proxy);
else
httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestProperty("User-Agent", Helper.USER_AGENT);
httpURLConnection.setRequestProperty("User-Agent", USER_AGENT);
httpURLConnection.setConnectTimeout(timeout * 1000);
httpURLConnection.setDoOutput(true);
httpURLConnection.setRequestMethod("POST");
@ -617,7 +620,7 @@ public class HttpsConnection {
httpsURLConnection = (HttpsURLConnection) url.openConnection(proxy);
else
httpsURLConnection = (HttpsURLConnection) url.openConnection();
httpsURLConnection.setRequestProperty("User-Agent", Helper.USER_AGENT);
httpsURLConnection.setRequestProperty("User-Agent", USER_AGENT);
httpsURLConnection.setConnectTimeout(timeout * 1000);
httpsURLConnection.setDoOutput(true);
httpsURLConnection.setSSLSocketFactory(new TLSSocketFactory(this.instance));
@ -682,7 +685,7 @@ public class HttpsConnection {
httpsURLConnection = (HttpsURLConnection) url.openConnection(proxy);
else
httpsURLConnection = (HttpsURLConnection) url.openConnection();
httpsURLConnection.setRequestProperty("User-Agent", Helper.USER_AGENT);
httpsURLConnection.setRequestProperty("User-Agent", USER_AGENT);
int responseCode = httpsURLConnection.getResponseCode();
// always check HTTP response code first
@ -770,7 +773,7 @@ public class HttpsConnection {
httpURLConnection = (HttpURLConnection) url.openConnection(proxy);
else
httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestProperty("User-Agent", Helper.USER_AGENT);
httpURLConnection.setRequestProperty("User-Agent", USER_AGENT);
int responseCode = httpURLConnection.getResponseCode();
// always check HTTP response code first
@ -866,7 +869,7 @@ public class HttpsConnection {
else
httpsURLConnection = (HttpsURLConnection) url.openConnection();
httpsURLConnection.setSSLSocketFactory(new TLSSocketFactory(this.instance));
httpsURLConnection.setRequestProperty("User-Agent", Helper.USER_AGENT);
httpsURLConnection.setRequestProperty("User-Agent", USER_AGENT);
int responseCode = httpsURLConnection.getResponseCode();
// always check HTTP response code first
if (responseCode == HttpURLConnection.HTTP_OK) {
@ -889,7 +892,7 @@ public class HttpsConnection {
httpURLConnection = (HttpURLConnection) url.openConnection(proxy);
else
httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestProperty("User-Agent", Helper.USER_AGENT);
httpURLConnection.setRequestProperty("User-Agent", USER_AGENT);
int responseCode = httpURLConnection.getResponseCode();
// always check HTTP response code first
if (responseCode == HttpURLConnection.HTTP_OK) {
@ -996,7 +999,7 @@ public class HttpsConnection {
httpsURLConnection = (HttpsURLConnection) url.openConnection(proxy);
else
httpsURLConnection = (HttpsURLConnection) url.openConnection();
httpsURLConnection.setRequestProperty("User-Agent", Helper.USER_AGENT);
httpsURLConnection.setRequestProperty("User-Agent", USER_AGENT);
httpsURLConnection.setConnectTimeout(timeout * 1000);
httpsURLConnection.setSSLSocketFactory(new TLSSocketFactory(this.instance));
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) {
@ -1077,7 +1080,7 @@ public class HttpsConnection {
httpURLConnection = (HttpsURLConnection) url.openConnection(proxy);
else
httpURLConnection = (HttpsURLConnection) url.openConnection();
httpURLConnection.setRequestProperty("User-Agent", Helper.USER_AGENT);
httpURLConnection.setRequestProperty("User-Agent", USER_AGENT);
httpURLConnection.setConnectTimeout(timeout * 1000);
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) {
httpURLConnection.setRequestMethod("PATCH");
@ -1160,7 +1163,7 @@ public class HttpsConnection {
httpsURLConnection = (HttpsURLConnection) url.openConnection(proxy);
else
httpsURLConnection = (HttpsURLConnection) url.openConnection();
httpsURLConnection.setRequestProperty("User-Agent", Helper.USER_AGENT);
httpsURLConnection.setRequestProperty("User-Agent", USER_AGENT);
httpsURLConnection.setConnectTimeout(timeout * 1000);
httpsURLConnection.setSSLSocketFactory(new TLSSocketFactory(this.instance));
if (token != null && !token.startsWith("Basic "))
@ -1228,7 +1231,7 @@ public class HttpsConnection {
httpURLConnection = (HttpURLConnection) url.openConnection(proxy);
else
httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestProperty("User-Agent", Helper.USER_AGENT);
httpURLConnection.setRequestProperty("User-Agent", USER_AGENT);
httpURLConnection.setConnectTimeout(timeout * 1000);
if (token != null && !token.startsWith("Basic "))
httpURLConnection.setRequestProperty("Authorization", "Bearer " + token);
@ -1300,7 +1303,7 @@ public class HttpsConnection {
httpsURLConnection = (HttpsURLConnection) url.openConnection(proxy);
else
httpsURLConnection = (HttpsURLConnection) url.openConnection();
httpsURLConnection.setRequestProperty("User-Agent", Helper.USER_AGENT);
httpsURLConnection.setRequestProperty("User-Agent", USER_AGENT);
httpsURLConnection.setSSLSocketFactory(new TLSSocketFactory(this.instance));
if (token != null && !token.startsWith("Basic "))
httpsURLConnection.setRequestProperty("Authorization", "Bearer " + token);
@ -1363,7 +1366,7 @@ public class HttpsConnection {
httpURLConnection = (HttpURLConnection) url.openConnection(proxy);
else
httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestProperty("User-Agent", Helper.USER_AGENT);
httpURLConnection.setRequestProperty("User-Agent", USER_AGENT);
if (token != null && !token.startsWith("Basic "))
httpsURLConnection.setRequestProperty("Authorization", "Bearer " + token);
else if (token != null && token.startsWith("Basic "))

View File

@ -2316,6 +2316,10 @@ public class StatusListAdapter extends RecyclerView.Adapter implements OnPostAct
.into(holder.webview_preview_card);
holder.status_cardview.setVisibility(View.GONE);
holder.status_cardview_video.setVisibility(View.VISIBLE);
String user_agent = sharedpreferences.getString(Helper.SET_CUSTOM_USER_AGENT, null);
if( user_agent != null) {
holder.status_cardview_webview.getSettings().setUserAgentString(user_agent);
}
holder.status_cardview_webview.getSettings().setJavaScriptEnabled(true);
String html = card.getHtml();
String src = card.getUrl();

View File

@ -919,6 +919,32 @@ public class ContentSettingsFragment extends Fragment implements OnRetrieveRemot
});
TextView set_user_agent = rootView.findViewById(R.id.set_user_agent);
String user_agent = sharedpreferences.getString(Helper.SET_CUSTOM_USER_AGENT, null);
if (user_agent != null) {
set_user_agent.setText(user_agent);
}
set_user_agent.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
SharedPreferences.Editor editor = sharedpreferences.edit();
if (s.toString().trim().length() > 0) {
editor.putString(Helper.SET_CUSTOM_USER_AGENT, s.toString().toLowerCase().trim());
} else {
editor.putString(Helper.SET_CUSTOM_USER_AGENT, null);
}
editor.apply();
}
});
TextView set_nitter_host = rootView.findViewById(R.id.set_nitter_host);
boolean nitter = sharedpreferences.getBoolean(Helper.SET_NITTER, false);
final SwitchCompat set_nitter = rootView.findViewById(R.id.set_nitter);

View File

@ -294,6 +294,10 @@ public class MediaSliderFragment extends Fragment implements MediaPlayer.OnCompl
webview_video.setWebChromeClient(mastalabWebChromeClient);
webview_video.getSettings().setDomStorageEnabled(true);
webview_video.getSettings().setAppCacheEnabled(true);
String user_agent = sharedpreferences.getString(Helper.SET_CUSTOM_USER_AGENT, null);
if( user_agent != null) {
webview_video.getSettings().setUserAgentString(user_agent);
}
webview_video.getSettings().setMediaPlaybackRequiresUserGesture(false);
webview_video.setWebViewClient(new MastalabWebViewClient((Activity) context));
webview_video.loadUrl(attachment.getUrl());

View File

@ -476,6 +476,7 @@ public class Helper {
public static final String SET_ART_WITH_NSFW = "set_art_with_nsfw";
public static final String SET_SECURITY_PROVIDER = "set_security_provider";
public static final String SET_ALLOW_STREAM = "set_allow_stream";
public static final String SET_CUSTOM_USER_AGENT = "set_custom_user_agent";
//End points
public static final String EP_AUTHORIZE = "/oauth/authorize";
@ -2460,15 +2461,10 @@ public class Helper {
webView.getSettings().setAllowContentAccess(true);
webView.getSettings().setLoadsImagesAutomatically(true);
webView.getSettings().setSupportMultipleWindows(false);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
//noinspection deprecation
webView.getSettings().setPluginState(WebSettings.PluginState.ON);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
webView.getSettings().setMediaPlaybackRequiresUserGesture(true);
}
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
webView.getSettings().setMediaPlaybackRequiresUserGesture(true);
String user_agent = sharedpreferences.getString(Helper.SET_CUSTOM_USER_AGENT, null);
if( user_agent != null) {
webView.getSettings().setUserAgentString(user_agent);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
boolean cookies = sharedpreferences.getBoolean(Helper.SET_COOKIES, false);

View File

@ -1818,6 +1818,8 @@
<!-- Featured tags -->
<LinearLayout
android:layout_marginTop="@dimen/settings_option_margin"
android:layout_marginBottom="@dimen/settings_option_margin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
@ -1849,6 +1851,38 @@
android:textColor="@color/mastodonC2" />
</LinearLayout>
<!-- user agent -->
<LinearLayout
android:layout_marginTop="@dimen/settings_option_margin"
android:layout_marginBottom="@dimen/settings_option_margin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:labelFor="@+id/set_user_agent"
android:text="@string/set_user_agent"
android:textSize="16sp" />
<EditText
android:id="@+id/set_user_agent"
android:ellipsize="end"
android:hint="@string/set_user_agent_hint"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:lines="2"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/set_user_agent_indication"
android:textColor="@color/mastodonC2" />
</LinearLayout>
<!-- TRUNCATE LONG TOOTS -->
<TextView
android:layout_width="wrap_content"

View File

@ -1197,4 +1197,7 @@
<string name="check_redirect">Check redirect</string>
<string name="no_redirect">This URL does not redirect</string>
<string name="redirect_detected">%1$s \n\nredirects to\n\n %2$s</string>
<string name="set_user_agent">Change the user agent</string>
<string name="set_user_agent_hint">Set a custom user agent or leave blank</string>
<string name="set_user_agent_indication">Allows to customize the user agent used for api calls or with the built-in browser.</string>
</resources>