Skip to content

[webview_flutter_android] Fix Android lint warnings #3675

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
Apr 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 3.5.1

* Updates pigeon dev dependency to `9.2.4`.
* Fixes Android lint warnings.

## 3.5.0

* Adds support for `PlatformNavigationDelegate.onUrlChange`.
Expand Down
4 changes: 2 additions & 2 deletions packages/webview_flutter/webview_flutter_android/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ import io.flutter.plugins.webviewflutter.WebViewFlutterAndroidExternalApi;
This package uses [pigeon][3] to generate the communication layer between Flutter and the host
platform (Android). The communication interface is defined in the `pigeons/android_webview.dart`
file. After editing the communication interface regenerate the communication layer by running
`flutter pub run pigeon --input pigeons/android_webview.dart`.
`dart run pigeon --input pigeons/android_webview.dart`.

Besides [pigeon][3] this package also uses [mockito][4] to generate mock objects for testing
purposes. To generate the mock objects run the following command:
```bash
flutter pub run build_runner build --delete-conflicting-outputs
dart run build_runner build --delete-conflicting-outputs
```

If you would like to contribute to the plugin, check out our [contribution guide][5].
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ android {
checkAllWarnings true
warningsAsErrors true
disable 'AndroidGradlePluginVersion', 'InvalidPackage', 'GradleDependency'
baseline file("lint-baseline.xml")
}

dependencies {
Expand Down
4,107 changes: 0 additions & 4,107 deletions packages/webview_flutter/webview_flutter_android/android/lint-baseline.xml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@

import android.os.Build;
import android.webkit.CookieManager;
import androidx.annotation.NonNull;
import io.flutter.plugins.webviewflutter.GeneratedAndroidWebView.CookieManagerHostApi;

class CookieManagerHostApiImpl implements GeneratedAndroidWebView.CookieManagerHostApi {
class CookieManagerHostApiImpl implements CookieManagerHostApi {
@Override
public void clearCookies(GeneratedAndroidWebView.Result<Boolean> result) {
public void clearCookies(@NonNull GeneratedAndroidWebView.Result<Boolean> result) {
CookieManager cookieManager = CookieManager.getInstance();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
cookieManager.removeAllCookies(result::success);
Expand All @@ -19,7 +21,7 @@ public void clearCookies(GeneratedAndroidWebView.Result<Boolean> result) {
}

@Override
public void setCookie(String url, String value) {
public void setCookie(@NonNull String url, @NonNull String value) {
CookieManager.getInstance().setCookie(url, value);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import static android.hardware.display.DisplayManager.DisplayListener;

import android.annotation.TargetApi;
import android.hardware.display.DisplayManager;
import android.os.Build;
import android.util.Log;
Expand Down Expand Up @@ -39,7 +38,6 @@
* first initialization of a webview within the process the difference between the lists is the
* webview's display listener.
*/
@TargetApi(Build.VERSION_CODES.KITKAT)
class DisplayListenerProxy {
private static final String TAG = "DisplayListenerProxy";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package io.flutter.plugins.webviewflutter;

import android.webkit.DownloadListener;
import androidx.annotation.NonNull;
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugins.webviewflutter.GeneratedAndroidWebView.DownloadListenerFlutterApi;

Expand All @@ -23,20 +24,20 @@ public class DownloadListenerFlutterApiImpl extends DownloadListenerFlutterApi {
* @param instanceManager maintains instances stored to communicate with Dart objects
*/
public DownloadListenerFlutterApiImpl(
BinaryMessenger binaryMessenger, InstanceManager instanceManager) {
@NonNull BinaryMessenger binaryMessenger, @NonNull InstanceManager instanceManager) {
super(binaryMessenger);
this.instanceManager = instanceManager;
}

/** Passes arguments from {@link DownloadListener#onDownloadStart} to Dart. */
public void onDownloadStart(
DownloadListener downloadListener,
String url,
String userAgent,
String contentDisposition,
String mimetype,
@NonNull DownloadListener downloadListener,
@NonNull String url,
@NonNull String userAgent,
@NonNull String contentDisposition,
@NonNull String mimetype,
long contentLength,
Reply<Void> callback) {
@NonNull Reply<Void> callback) {
onDownloadStart(
getIdentifierForListener(downloadListener),
url,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ public DownloadListenerImpl(@NonNull DownloadListenerFlutterApiImpl flutterApi)

@Override
public void onDownloadStart(
String url,
String userAgent,
String contentDisposition,
String mimetype,
@NonNull String url,
@NonNull String userAgent,
@NonNull String contentDisposition,
@NonNull String mimetype,
long contentLength) {
flutterApi.onDownloadStart(
this, url, userAgent, contentDisposition, mimetype, contentLength, reply -> {});
Expand All @@ -53,7 +53,9 @@ public static class DownloadListenerCreator {
* @param flutterApi handles sending messages to Dart
* @return the created {@link DownloadListenerImpl}
*/
public DownloadListenerImpl createDownloadListener(DownloadListenerFlutterApiImpl flutterApi) {
@NonNull
public DownloadListenerImpl createDownloadListener(
@NonNull DownloadListenerFlutterApiImpl flutterApi) {
return new DownloadListenerImpl(flutterApi);
}
}
Expand All @@ -66,16 +68,16 @@ public DownloadListenerImpl createDownloadListener(DownloadListenerFlutterApiImp
* @param flutterApi handles sending messages to Dart
*/
public DownloadListenerHostApiImpl(
InstanceManager instanceManager,
DownloadListenerCreator downloadListenerCreator,
DownloadListenerFlutterApiImpl flutterApi) {
@NonNull InstanceManager instanceManager,
@NonNull DownloadListenerCreator downloadListenerCreator,
@NonNull DownloadListenerFlutterApiImpl flutterApi) {
this.instanceManager = instanceManager;
this.downloadListenerCreator = downloadListenerCreator;
this.flutterApi = flutterApi;
}

@Override
public void create(Long instanceId) {
public void create(@NonNull Long instanceId) {
final DownloadListener downloadListener =
downloadListenerCreator.createDownloadListener(flutterApi);
instanceManager.addDartCreatedInstance(downloadListener, instanceId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import android.os.Build;
import android.webkit.WebChromeClient;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import io.flutter.plugin.common.BinaryMessenger;
import java.util.Arrays;
Expand All @@ -28,7 +29,7 @@ public class FileChooserParamsFlutterApiImpl
* @param instanceManager maintains instances stored to communicate with Dart objects
*/
public FileChooserParamsFlutterApiImpl(
BinaryMessenger binaryMessenger, InstanceManager instanceManager) {
@NonNull BinaryMessenger binaryMessenger, @NonNull InstanceManager instanceManager) {
super(binaryMessenger);
this.instanceManager = instanceManager;
}
Expand Down Expand Up @@ -58,7 +59,8 @@ private static GeneratedAndroidWebView.FileChooserModeEnumData toFileChooserEnum
* Stores the FileChooserParams instance and notifies Dart to create a new FileChooserParams
* instance that is attached to this one.
*/
public void create(WebChromeClient.FileChooserParams instance, Reply<Void> callback) {
public void create(
@NonNull WebChromeClient.FileChooserParams instance, @NonNull Reply<Void> callback) {
if (!instanceManager.containsInstance(instance)) {
create(
instanceManager.addHostCreatedInstance(instance),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.io.IOException;

/** Provides access to the assets registered as part of the App bundle. */
@SuppressWarnings({"deprecation", "DeprecatedIsStillUsed"})
abstract class FlutterAssetManager {
final AssetManager assetManager;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package io.flutter.plugins.webviewflutter;

import android.webkit.WebView;
import androidx.annotation.NonNull;
import io.flutter.plugins.webviewflutter.GeneratedAndroidWebView.FlutterAssetManagerHostApi;
import java.io.IOException;
import java.util.ArrayList;
Expand All @@ -20,12 +21,13 @@ public class FlutterAssetManagerHostApiImpl implements FlutterAssetManagerHostAp
final FlutterAssetManager flutterAssetManager;

/** Constructs a new instance of {@link FlutterAssetManagerHostApiImpl}. */
public FlutterAssetManagerHostApiImpl(FlutterAssetManager flutterAssetManager) {
public FlutterAssetManagerHostApiImpl(@NonNull FlutterAssetManager flutterAssetManager) {
this.flutterAssetManager = flutterAssetManager;
}

@NonNull
@Override
public List<String> list(String path) {
public List<String> list(@NonNull String path) {
try {
String[] paths = flutterAssetManager.list(path);

Expand All @@ -39,8 +41,9 @@ public List<String> list(String path) {
}
}

@NonNull
@Override
public String getAssetFilePathByName(String name) {
public String getAssetFilePathByName(@NonNull String name) {
return flutterAssetManager.getAssetFilePathByName(name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
package io.flutter.plugins.webviewflutter;

import android.content.Context;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import io.flutter.plugin.common.StandardMessageCodec;
import io.flutter.plugin.platform.PlatformView;
import io.flutter.plugin.platform.PlatformViewFactory;
Expand All @@ -17,9 +19,15 @@ class FlutterWebViewFactory extends PlatformViewFactory {
this.instanceManager = instanceManager;
}

@NonNull
@Override
public PlatformView create(Context context, int id, Object args) {
final PlatformView view = (PlatformView) instanceManager.getInstance((Integer) args);
public PlatformView create(Context context, int viewId, @Nullable Object args) {
final Integer identifier = (Integer) args;
if (identifier == null) {
throw new IllegalStateException("An identifier is required to retrieve WebView instance.");
}

final PlatformView view = instanceManager.getInstance(identifier);
if (view == null) {
throw new IllegalStateException("Unable to find WebView instance: " + args);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Autogenerated from Pigeon (v9.2.3), do not edit directly.
// Autogenerated from Pigeon (v9.2.4), do not edit directly.
// See also: https://pub.dev/packages/pigeon

package io.flutter.plugins.webviewflutter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;
Expand Down Expand Up @@ -69,7 +70,8 @@ public interface FinalizationListener {
* @param finalizationListener the listener for garbage collected weak references.
* @return a new `InstanceManager`.
*/
public static InstanceManager open(FinalizationListener finalizationListener) {
@NonNull
public static InstanceManager open(@NonNull FinalizationListener finalizationListener) {
return new InstanceManager(finalizationListener);
}

Expand Down Expand Up @@ -113,7 +115,7 @@ public <T> T remove(long identifier) {
* null if the manager doesn't contain the value or the manager is closed.
*/
@Nullable
public Long getIdentifierForStrongReference(Object instance) {
public Long getIdentifierForStrongReference(@Nullable Object instance) {
if (assertNotClosed()) {
return null;
}
Expand All @@ -137,7 +139,7 @@ public Long getIdentifierForStrongReference(Object instance) {
* @param identifier the identifier to be paired with instance. This value must be >= 0 and
* unique.
*/
public void addDartCreatedInstance(Object instance, long identifier) {
public void addDartCreatedInstance(@NonNull Object instance, long identifier) {
if (assertNotClosed()) {
return;
}
Expand All @@ -151,7 +153,7 @@ public void addDartCreatedInstance(Object instance, long identifier) {
* @return the unique identifier stored with instance. If the manager is closed, returns -1.
* Otherwise, returns a value >= 0.
*/
public long addHostCreatedInstance(Object instance) {
public long addHostCreatedInstance(@NonNull Object instance) {
if (assertNotClosed()) {
return INSTANCE_CLOSED;
}
Expand Down Expand Up @@ -193,7 +195,7 @@ public <T> T getInstance(long identifier) {
* @return whether this manager contains the given `instance`. If the manager is closed, returns
* `false`.
*/
public boolean containsInstance(Object instance) {
public boolean containsInstance(@Nullable Object instance) {
if (assertNotClosed()) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class JavaObjectHostApiImpl implements GeneratedAndroidWebView.JavaObject
*
* @param instanceManager maintains instances stored to communicate with Dart objects
*/
public JavaObjectHostApiImpl(InstanceManager instanceManager) {
public JavaObjectHostApiImpl(@NonNull InstanceManager instanceManager) {
this.instanceManager = instanceManager;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public class JavaScriptChannel {
*/
public JavaScriptChannel(
@NonNull JavaScriptChannelFlutterApiImpl flutterApi,
String channelName,
Handler platformThreadHandler) {
@NonNull String channelName,
@NonNull Handler platformThreadHandler) {
this.flutterApi = flutterApi;
this.javaScriptChannelName = channelName;
this.platformThreadHandler = platformThreadHandler;
Expand All @@ -40,11 +40,9 @@ public JavaScriptChannel(
// Suppressing unused warning as this is invoked from JavaScript.
@SuppressWarnings("unused")
@JavascriptInterface
public void postMessage(final String message) {
public void postMessage(@NonNull final String message) {
final Runnable postMessageRunnable =
() -> {
flutterApi.postMessage(JavaScriptChannel.this, message, reply -> {});
};
() -> flutterApi.postMessage(JavaScriptChannel.this, message, reply -> {});

if (platformThreadHandler.getLooper() == Looper.myLooper()) {
postMessageRunnable.run();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

package io.flutter.plugins.webviewflutter;

import androidx.annotation.NonNull;
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugins.webviewflutter.GeneratedAndroidWebView.JavaScriptChannelFlutterApi;

Expand All @@ -22,14 +23,16 @@ public class JavaScriptChannelFlutterApiImpl extends JavaScriptChannelFlutterApi
* @param instanceManager Maintains instances stored to communicate with Dart objects.
*/
public JavaScriptChannelFlutterApiImpl(
BinaryMessenger binaryMessenger, InstanceManager instanceManager) {
@NonNull BinaryMessenger binaryMessenger, @NonNull InstanceManager instanceManager) {
super(binaryMessenger);
this.instanceManager = instanceManager;
}

/** Passes arguments from {@link JavaScriptChannel#postMessage} to Dart. */
public void postMessage(
JavaScriptChannel javaScriptChannel, String messageArg, Reply<Void> callback) {
@NonNull JavaScriptChannel javaScriptChannel,
@NonNull String messageArg,
@NonNull Reply<Void> callback) {
super.postMessage(getIdentifierForJavaScriptChannel(javaScriptChannel), messageArg, callback);
}

Expand Down
Loading