@@ -56,7 +56,9 @@ public class Http {
5656 private static final OkHttpClient httpClientWithCache ;
5757 private static final OkHttpClient httpClientWithCacheDoH ;
5858 private static final FallBackDNS fallbackDNS ;
59+ private static final CookieJar cookieJar ;
5960 private static final String androidacyUA ;
61+ private static final boolean hasWebView ;
6062 private static boolean doh ;
6163
6264 static {
@@ -102,7 +104,7 @@ public class Http {
102104 return Dns .SYSTEM .lookup (s );
103105 };
104106 httpclientBuilder .dns (dns );
105- httpclientBuilder .cookieJar (new CDNCookieJar (false ));
107+ httpclientBuilder .cookieJar (new CDNCookieJar ());
106108 dns = new DnsOverHttps .Builder ().client (httpclientBuilder .build ()).url (
107109 Objects .requireNonNull (HttpUrl .parse ("https://cloudflare-dns.com/dns-query" )))
108110 .bootstrapDnsHosts (cloudflareBootstrap ).resolvePrivateAddresses (true ).build ();
@@ -139,7 +141,16 @@ public class Http {
139141 "camo.githubusercontent.com" , "user-images.githubusercontent.com" ,
140142 "cdn.jsdelivr.net" , "img.shields.io" , "magisk-modules-repo.github.io" ,
141143 "www.androidacy.com" , "api.androidacy.com" );
142- httpclientBuilder .cookieJar (new CDNCookieJar (true ));
144+ CookieManager cookieManager ;
145+ try {
146+ cookieManager = CookieManager .getInstance ();
147+ cookieManager .flush (); // Make sure the instance work
148+ } catch (Throwable t ) {
149+ cookieManager = null ;
150+ Log .e (TAG , "No WebView support!" , t );
151+ }
152+ hasWebView = cookieManager != null ;
153+ httpclientBuilder .cookieJar (cookieJar = new CDNCookieJar (cookieManager ));
143154 httpclientBuilder .dns (Dns .SYSTEM );
144155 httpClient = httpclientBuilder .build ();
145156 httpclientBuilder .dns (fallbackDNS );
@@ -268,17 +279,29 @@ public static void setDoh(boolean doh) {
268279 private static class CDNCookieJar implements CookieJar {
269280 private final HashMap <String , Cookie > cookieMap = new HashMap <>();
270281 private final boolean androidacySupport ;
282+ private final CookieManager cookieManager ;
283+ private List <Cookie > androidacyCookies ;
271284
272- private CDNCookieJar (boolean androidacySupport ) {
273- this .androidacySupport = androidacySupport ;
285+ private CDNCookieJar () {
286+ this .androidacySupport = false ;
287+ this .cookieManager = null ;
288+ }
289+
290+ private CDNCookieJar (CookieManager cookieManager ) {
291+ this .androidacySupport = true ;
292+ this .cookieManager = cookieManager ;
293+ if (cookieManager == null ) {
294+ this .androidacyCookies = Collections .emptyList ();
295+ }
274296 }
275297
276298 @ NonNull
277299 @ Override
278300 public List <Cookie > loadForRequest (@ NonNull HttpUrl httpUrl ) {
279301 if (!httpUrl .isHttps ()) return Collections .emptyList ();
280302 if (this .androidacySupport && httpUrl .host ().endsWith (".androidacy.com" )) {
281- String cookies = CookieManager .getInstance ().getCookie (httpUrl .uri ().toString ());
303+ if (this .cookieManager == null ) return this .androidacyCookies ;
304+ String cookies = this .cookieManager .getCookie (httpUrl .uri ().toString ());
282305 if (cookies == null || cookies .isEmpty ()) return Collections .emptyList ();
283306 String [] splitCookies = cookies .split (";" );
284307 ArrayList <Cookie > cookieList = new ArrayList <>(splitCookies .length );
@@ -296,8 +319,13 @@ public List<Cookie> loadForRequest(@NonNull HttpUrl httpUrl) {
296319 public void saveFromResponse (@ NonNull HttpUrl httpUrl , @ NonNull List <Cookie > cookies ) {
297320 if (!httpUrl .isHttps ()) return ;
298321 if (this .androidacySupport && httpUrl .host ().endsWith (".androidacy.com" )) {
322+ if (this .cookieManager == null ) {
323+ if (httpUrl .host ().equals (".androidacy.com" ) || !cookies .isEmpty ())
324+ this .androidacyCookies = cookies ;
325+ return ;
326+ }
299327 for (Cookie cookie : cookies ) {
300- CookieManager . getInstance () .setCookie (
328+ this . cookieManager .setCookie (
301329 httpUrl .uri ().toString (), cookie .toString ());
302330 }
303331 return ;
@@ -454,6 +482,10 @@ public void writeTo(@NonNull BufferedSink bufferedSink) throws IOException {
454482 }
455483 }
456484
485+ public static boolean hasWebView () {
486+ return hasWebView ;
487+ }
488+
457489 /**
458490 * Change URL to appropriate url and force Magisk link to use latest version.
459491 */
@@ -495,4 +527,8 @@ public static String cdnIfyLink(String string) {
495527 }
496528 return string ;
497529 }
530+
531+ public static CookieJar getCookieJar () {
532+ return cookieJar ;
533+ }
498534}
0 commit comments