77import android .system .Os ;
88import android .util .Log ;
99import android .webkit .CookieManager ;
10+ import android .webkit .WebSettings ;
1011
1112import androidx .annotation .NonNull ;
1213import androidx .annotation .Nullable ;
@@ -77,6 +78,16 @@ public class Http {
7778 }
7879 throw error ;
7980 }
81+ CookieManager cookieManager ;
82+ try {
83+ cookieManager = CookieManager .getInstance ();
84+ cookieManager .setAcceptCookie (true );
85+ cookieManager .flush (); // Make sure the instance work
86+ } catch (Throwable t ) {
87+ cookieManager = null ;
88+ Log .e (TAG , "No WebView support!" , t );
89+ }
90+ hasWebView = cookieManager != null ;
8091 OkHttpClient .Builder httpclientBuilder = new OkHttpClient .Builder ();
8192 // Default is 10, extend it a bit for slow mobile connections.
8293 httpclientBuilder .connectTimeout (15 , TimeUnit .SECONDS );
@@ -112,10 +123,15 @@ public class Http {
112123 Log .e (TAG , "Failed to init DoH" , e );
113124 }
114125 httpclientBuilder .cookieJar (CookieJar .NO_COOKIES );
115- androidacyUA = // User-Agent format was agreed on telegram
116- "Mozilla/5.0 (Linux; Android " + Build .VERSION .RELEASE + "; " + Build .DEVICE +")" +
117- " AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Mobile Safari/537.36"
118- + " FoxMmm/" + BuildConfig .VERSION_CODE ;
126+ // User-Agent format was agreed on telegram
127+ if (hasWebView ) {
128+ androidacyUA = WebSettings .getDefaultUserAgent (mainApplication ).replace ("; wv" , "" )
129+ + " FoxMmm/" + BuildConfig .VERSION_CODE ;
130+ } else {
131+ androidacyUA = "Mozilla/5.0 (Linux; Android " + Build .VERSION .RELEASE + "; " + Build .DEVICE +")" +
132+ " AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Mobile Safari/537.36"
133+ + " FoxMmm/" + BuildConfig .VERSION_CODE ;
134+ }
119135 httpclientBuilder .addInterceptor (chain -> {
120136 Request .Builder request = chain .request ().newBuilder ();
121137 request .header ("Upgrade-Insecure-Requests" , "1" );
@@ -141,16 +157,6 @@ public class Http {
141157 "camo.githubusercontent.com" , "user-images.githubusercontent.com" ,
142158 "cdn.jsdelivr.net" , "img.shields.io" , "magisk-modules-repo.github.io" ,
143159 "www.androidacy.com" , "api.androidacy.com" );
144- CookieManager cookieManager ;
145- try {
146- cookieManager = CookieManager .getInstance ();
147- cookieManager .setAcceptCookie (true );
148- cookieManager .flush (); // Make sure the instance work
149- } catch (Throwable t ) {
150- cookieManager = null ;
151- Log .e (TAG , "No WebView support!" , t );
152- }
153- hasWebView = cookieManager != null ;
154160 httpclientBuilder .cookieJar (cookieJar = new CDNCookieJar (cookieManager ));
155161 httpclientBuilder .dns (Dns .SYSTEM );
156162 httpClient = followRedirects (httpclientBuilder , true ).build ();
0 commit comments