From e9277b46f321c233f71814d100d550a11a3af454 Mon Sep 17 00:00:00 2001 From: Khairul <67180998+Khairul989@users.noreply.github.com> Date: Tue, 21 Apr 2026 10:52:34 +0800 Subject: [PATCH] [flutter_inappwebview_android] Fix easy Java deprecation warnings - Replace `new Long(String)` with `Long.parseLong(String)` in MyCookieManager. - Replace `new Handler()` with `new Handler(Looper.getMainLooper())` in InAppWebView. - Route previously-ungated `CookieManager.removeSessionCookie()` calls through a new `clearSessionCookies()` helper that uses `removeSessionCookies(null)` on API 21+ and falls back to the deprecated call only on pre-LOLLIPOP. - Annotate methods that retain pre-LOLLIPOP `CookieSyncManager` / `CookieManager.removeAllCookie` / `removeSessionCookie` fallbacks with `@SuppressWarnings("deprecation")` to silence javac warnings without changing behavior on minSdk 19/20. Refs #2641 --- flutter_inappwebview_android/CHANGELOG.md | 1 + .../MyCookieManager.java | 8 +++++++- .../webview/in_app_webview/InAppWebView.java | 17 ++++++++++++++--- .../in_app_webview/InAppWebViewClient.java | 1 + .../InAppWebViewClientCompat.java | 1 + 5 files changed, 24 insertions(+), 4 deletions(-) diff --git a/flutter_inappwebview_android/CHANGELOG.md b/flutter_inappwebview_android/CHANGELOG.md index b18e420171..576917e0ad 100644 --- a/flutter_inappwebview_android/CHANGELOG.md +++ b/flutter_inappwebview_android/CHANGELOG.md @@ -1,5 +1,6 @@ ## 1.2.0-beta.3 +- Fixed Java deprecation warnings in Android native code: replaced `new Long(String)` with `Long.parseLong`, `new Handler()` with `new Handler(Looper.getMainLooper())`, gated `CookieManager.removeSessionCookie()` behind pre-`LOLLIPOP` SDK check, and annotated methods retaining pre-`LOLLIPOP` `CookieSyncManager` / `CookieManager.removeAllCookie` / `removeSessionCookie` fallbacks with `@SuppressWarnings("deprecation")` [#2641](https://github.com/pichillilorenzo/flutter_inappwebview/issues/2641) - Updated flutter_inappwebview_platform_interface version to ^1.4.0-beta.3 - Updated native dependencies: - implementation from `'androidx.webkit:webkit:1.12.0'` to `'androidx.webkit:webkit:1.14.0'` diff --git a/flutter_inappwebview_android/android/src/main/java/com/pichillilorenzo/flutter_inappwebview_android/MyCookieManager.java b/flutter_inappwebview_android/android/src/main/java/com/pichillilorenzo/flutter_inappwebview_android/MyCookieManager.java index dccb7eabf3..279ef43b0b 100755 --- a/flutter_inappwebview_android/android/src/main/java/com/pichillilorenzo/flutter_inappwebview_android/MyCookieManager.java +++ b/flutter_inappwebview_android/android/src/main/java/com/pichillilorenzo/flutter_inappwebview_android/MyCookieManager.java @@ -59,7 +59,7 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result String domain = (String) call.argument("domain"); String path = (String) call.argument("path"); String expiresDateString = (String) call.argument("expiresDate"); - Long expiresDate = (expiresDateString != null ? new Long(expiresDateString) : null); + Long expiresDate = (expiresDateString != null ? Long.parseLong(expiresDateString) : null); Integer maxAge = (Integer) call.argument("maxAge"); Boolean isSecure = (Boolean) call.argument("isSecure"); Boolean isHttpOnly = (Boolean) call.argument("isHttpOnly"); @@ -145,6 +145,7 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result return cookieManager; } + @SuppressWarnings("deprecation") public void setCookie(String url, String name, String value, @@ -288,6 +289,7 @@ public List> getCookies(final String url) { } + @SuppressWarnings("deprecation") public void deleteCookie(String url, String name, String domain, String path, final MethodChannel.Result result) { cookieManager = getCookieManager(); if (cookieManager == null) { @@ -324,6 +326,7 @@ else if (plugin != null) { } } + @SuppressWarnings("deprecation") public void deleteCookies(String url, String domain, String path, final MethodChannel.Result result) { cookieManager = getCookieManager(); if (cookieManager == null) { @@ -368,6 +371,7 @@ public void deleteCookies(String url, String domain, String path, final MethodCh result.success(true); } + @SuppressWarnings("deprecation") public void deleteAllCookies(final MethodChannel.Result result) { cookieManager = getCookieManager(); if (cookieManager == null) { @@ -397,6 +401,7 @@ else if (plugin != null) { } } + @SuppressWarnings("deprecation") public void removeSessionCookies(final MethodChannel.Result result) { cookieManager = getCookieManager(); if (cookieManager == null) { @@ -426,6 +431,7 @@ else if (plugin != null) { } } + @SuppressWarnings("deprecation") public void flush(MethodChannel.Result result) { cookieManager = getCookieManager(); if (cookieManager == null) { diff --git a/flutter_inappwebview_android/android/src/main/java/com/pichillilorenzo/flutter_inappwebview_android/webview/in_app_webview/InAppWebView.java b/flutter_inappwebview_android/android/src/main/java/com/pichillilorenzo/flutter_inappwebview_android/webview/in_app_webview/InAppWebView.java index 7cf3f7f558..cd396b9c67 100755 --- a/flutter_inappwebview_android/android/src/main/java/com/pichillilorenzo/flutter_inappwebview_android/webview/in_app_webview/InAppWebView.java +++ b/flutter_inappwebview_android/android/src/main/java/com/pichillilorenzo/flutter_inappwebview_android/webview/in_app_webview/InAppWebView.java @@ -151,7 +151,7 @@ final public class InAppWebView extends InputAwareWebView implements InAppWebVie @Nullable public Map contextMenu = null; public Handler mainLooperHandler = new Handler(getWebViewLooper()); - static Handler mHandler = new Handler(); + static Handler mHandler = new Handler(Looper.getMainLooper()); public Runnable checkScrollStoppedTask; public int initialPositionScrollStoppedTask; @@ -337,7 +337,7 @@ else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) if (customSettings.clearCache) clearAllCache(); else if (customSettings.clearSessionCache) - CookieManager.getInstance().removeSessionCookie(); + clearSessionCookies(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) CookieManager.getInstance().setAcceptThirdPartyCookies(this, customSettings.thirdPartyCookiesEnabled); @@ -636,6 +636,7 @@ public void prepareAndAddUserScripts() { this.userContentController.addUserOnlyScripts(this.initialUserOnlyScripts); } + @SuppressWarnings("deprecation") public void setIncognito(boolean enabled) { WebSettings settings = getSettings(); if (enabled) { @@ -727,6 +728,7 @@ public boolean isLoading() { * @deprecated */ @Deprecated + @SuppressWarnings("deprecation") private void clearCookies() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { CookieManager.getInstance().removeAllCookies(new ValueCallback() { @@ -740,6 +742,15 @@ public void onReceiveValue(Boolean aBoolean) { } } + @SuppressWarnings("deprecation") + private void clearSessionCookies() { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + CookieManager.getInstance().removeSessionCookies(null); + } else { + CookieManager.getInstance().removeSessionCookie(); + } + } + /** * @deprecated */ @@ -919,7 +930,7 @@ else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) if (newSettingsMap.get("clearCache") != null && newCustomSettings.clearCache) clearAllCache(); else if (newSettingsMap.get("clearSessionCache") != null && newCustomSettings.clearSessionCache) - CookieManager.getInstance().removeSessionCookie(); + clearSessionCookies(); if (newSettingsMap.get("thirdPartyCookiesEnabled") != null && customSettings.thirdPartyCookiesEnabled != newCustomSettings.thirdPartyCookiesEnabled && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) CookieManager.getInstance().setAcceptThirdPartyCookies(this, newCustomSettings.thirdPartyCookiesEnabled); diff --git a/flutter_inappwebview_android/android/src/main/java/com/pichillilorenzo/flutter_inappwebview_android/webview/in_app_webview/InAppWebViewClient.java b/flutter_inappwebview_android/android/src/main/java/com/pichillilorenzo/flutter_inappwebview_android/webview/in_app_webview/InAppWebViewClient.java index aa59495267..1d436a2e8b 100755 --- a/flutter_inappwebview_android/android/src/main/java/com/pichillilorenzo/flutter_inappwebview_android/webview/in_app_webview/InAppWebViewClient.java +++ b/flutter_inappwebview_android/android/src/main/java/com/pichillilorenzo/flutter_inappwebview_android/webview/in_app_webview/InAppWebViewClient.java @@ -231,6 +231,7 @@ public void onPageStarted(WebView view, String url, Bitmap favicon) { } } + @SuppressWarnings("deprecation") public void onPageFinished(WebView view, String url) { final InAppWebView webView = (InAppWebView) view; webView.isLoading = false; diff --git a/flutter_inappwebview_android/android/src/main/java/com/pichillilorenzo/flutter_inappwebview_android/webview/in_app_webview/InAppWebViewClientCompat.java b/flutter_inappwebview_android/android/src/main/java/com/pichillilorenzo/flutter_inappwebview_android/webview/in_app_webview/InAppWebViewClientCompat.java index fed10340ce..c48015d85f 100755 --- a/flutter_inappwebview_android/android/src/main/java/com/pichillilorenzo/flutter_inappwebview_android/webview/in_app_webview/InAppWebViewClientCompat.java +++ b/flutter_inappwebview_android/android/src/main/java/com/pichillilorenzo/flutter_inappwebview_android/webview/in_app_webview/InAppWebViewClientCompat.java @@ -230,6 +230,7 @@ public void onPageStarted(WebView view, String url, Bitmap favicon) { } } + @SuppressWarnings("deprecation") public void onPageFinished(WebView view, String url) { final InAppWebView webView = (InAppWebView) view; webView.isLoading = false;