Skip to content

Commit

Permalink
[camerax] Marks all wrapped classes as immutable (#4451)
Browse files Browse the repository at this point in the history
Marks all Dart-wrapped Android native classes as `@immutable`. They are all immutable anyway because they extend `JavaObject` which is immutable, but marking them as immutable explicitly avoids lint warnings caused by [`must_be_immutable`](https://dart.dev/tools/diagnostic-messages#must_be_immutable) when generated mocks of these classes are used (see dart-lang/mockito#200).

Also, updates `CONTRIBUTING.md` to include marking wrapped classes as immutable and extending `JavaObject` as requirements for adding new wrapped classes.

cc @ChristianEdwardPadilla: this should fix the internal lint errors caused by `JavaObject` being immutable.
  • Loading branch information
camsim99 authored Jul 14, 2023
1 parent c18c646 commit 6181f65
Show file tree
Hide file tree
Showing 46 changed files with 138 additions and 27 deletions.
5 changes: 5 additions & 0 deletions packages/camera/camera_android_camerax/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.5.0+9

* Marks all Dart-wrapped Android native classes as `@immutable`.
* Updates `CONTRIBUTING.md` to note requirements of Dart-wrapped Android native classes.

## 0.5.0+8

* Fixes unawaited_futures violations.
Expand Down
5 changes: 4 additions & 1 deletion packages/camera/camera_android_camerax/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ an `InstanceManager` (implementation in `InstanceManager.java`).

If CameraX or other Android classes that you need to access do not have a
duplicately named implementation in `lib/src/`, then follow the same structure
described above to add them.
described above to add them. Please note that any Dart-wrapped native Android
classes that you add should extend `JavaObject`. Additionally, they should be
annotated as `@immutable` to avoid lint errors with mock objects that are
generated for them that you may use for testing.

For more information, please see the [design document][2] or feel free
to ask any questions on the #hackers-ecosystem channel on [Discord][6]. For
Expand Down
3 changes: 2 additions & 1 deletion packages/camera/camera_android_camerax/lib/src/analyzer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import 'dart:async';

import 'package:flutter/services.dart' show BinaryMessenger;
import 'package:meta/meta.dart' show protected;
import 'package:meta/meta.dart' show immutable, protected;

import 'android_camera_camerax_flutter_api_impls.dart';
import 'camerax_library.g.dart';
Expand All @@ -16,6 +16,7 @@ import 'java_object.dart';
/// Wrapper of callback for analyzing images.
///
/// See https://developer.android.com/reference/androidx/camera/core/ImageAnalysis.Analyzer.
@immutable
class Analyzer extends JavaObject {
/// Creates an [Analyzer].
Analyzer(
Expand Down
2 changes: 2 additions & 0 deletions packages/camera/camera_android_camerax/lib/src/camera.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// found in the LICENSE file.

import 'package:flutter/services.dart' show BinaryMessenger;
import 'package:meta/meta.dart' show immutable;

import 'android_camera_camerax_flutter_api_impls.dart';
import 'camera_info.dart';
Expand All @@ -14,6 +15,7 @@ import 'java_object.dart';
/// camera, and publich the state of the camera.
///
/// See https://developer.android.com/reference/androidx/camera/core/Camera.
@immutable
class Camera extends JavaObject {
/// Constructs a [Camera] that is not automatically attached to a native object.
Camera.detached(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import 'dart:async';

import 'package:flutter/services.dart' show BinaryMessenger;
import 'package:meta/meta.dart' show immutable;

import 'android_camera_camerax_flutter_api_impls.dart';
import 'camera_state.dart';
Expand All @@ -18,6 +19,7 @@ import 'zoom_state.dart';
/// The metadata of a camera.
///
/// See https://developer.android.com/reference/androidx/camera/core/CameraInfo.
@immutable
class CameraInfo extends JavaObject {
/// Constructs a [CameraInfo] that is not automatically attached to a native object.
CameraInfo.detached(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// found in the LICENSE file.

import 'package:flutter/services.dart';
import 'package:meta/meta.dart' show immutable;

import 'android_camera_camerax_flutter_api_impls.dart';
import 'camera_info.dart';
Expand All @@ -13,6 +14,7 @@ import 'java_object.dart';
/// Selects a camera for use.
///
/// See https://developer.android.com/reference/androidx/camera/core/CameraSelector.
@immutable
class CameraSelector extends JavaObject {
/// Creates a [CameraSelector].
CameraSelector(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import 'java_object.dart';
/// A snapshot of the camera state.
///
/// See https://developer.android.com/reference/androidx/camera/core/CameraState.
@immutable
class CameraState extends JavaObject {
/// Constructs a [CameraState] that is not automatically attached to a native object.
CameraState.detached(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import 'java_object.dart';
/// The error that a camera has encountered.
///
/// See https://developer.android.com/reference/androidx/camera/core/CameraState.StateError.
@immutable
class CameraStateError extends JavaObject {
/// Constructs a [CameraStateError] that is not automatically attached to a native object.
CameraStateError.detached(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// found in the LICENSE file.

import 'package:flutter/services.dart' show BinaryMessenger;
import 'package:meta/meta.dart' show immutable;

import 'android_camera_camerax_flutter_api_impls.dart';
import 'camerax_library.g.dart';
Expand All @@ -12,6 +13,7 @@ import 'java_object.dart';
/// Represents exposure related information of a camera.
///
/// See https://developer.android.com/reference/androidx/camera/core/ExposureState.
@immutable
class ExposureState extends JavaObject {
/// Constructs a [ExposureState] that is not automatically attached to a native object.
ExposureState.detached(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import 'dart:async';

import 'package:flutter/services.dart' show BinaryMessenger;
import 'package:meta/meta.dart' show immutable;

import 'analyzer.dart';
import 'android_camera_camerax_flutter_api_impls.dart';
Expand All @@ -16,6 +17,7 @@ import 'use_case.dart';
/// Use case for providing CPU accessible images for performing image analysis.
///
/// See https://developer.android.com/reference/androidx/camera/core/ImageAnalysis.
@immutable
class ImageAnalysis extends UseCase {
/// Creates an [ImageAnalysis].
ImageAnalysis(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// found in the LICENSE file.

import 'package:flutter/services.dart' show BinaryMessenger;
import 'package:meta/meta.dart' show immutable;

import 'camerax_library.g.dart';
import 'instance_manager.dart';
Expand All @@ -12,6 +13,7 @@ import 'use_case.dart';
/// Use case for picture taking.
///
/// See https://developer.android.com/reference/androidx/camera/core/ImageCapture.
@immutable
class ImageCapture extends UseCase {
/// Creates an [ImageCapture].
ImageCapture({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import 'dart:async';

import 'package:flutter/services.dart' show BinaryMessenger;
import 'package:meta/meta.dart' show protected;
import 'package:meta/meta.dart' show immutable, protected;

import 'android_camera_camerax_flutter_api_impls.dart';
import 'camerax_library.g.dart';
Expand All @@ -16,6 +16,7 @@ import 'plane_proxy.dart';
/// Representation of a single complete image buffer.
///
/// See https://developer.android.com/reference/androidx/camera/core/ImageProxy.
@immutable
class ImageProxy extends JavaObject {
/// Constructs a [ImageProxy] that is not automatically attached to a native object.
ImageProxy.detached(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import 'zoom_state.dart';
/// Android Activity to which this plugin is attached.
///
/// See https://developer.android.com/reference/androidx/lifecycle/LiveData.
@immutable
class LiveData<T extends Object> extends JavaObject {
/// Constructs a [LiveData] that is not automatically attached to a native object.
LiveData.detached({this.binaryMessenger, this.instanceManager})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import 'live_data.dart';
/// Callback that can receive from [LiveData].
///
/// See https://developer.android.com/reference/androidx/lifecycle/Observer.
@immutable
class Observer<T> extends JavaObject {
/// Constructor for [Observer].
Observer(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// found in the LICENSE file.

import 'package:flutter/services.dart' show BinaryMessenger;
import 'package:meta/meta.dart' show immutable;

import 'android_camera_camerax_flutter_api_impls.dart';
import 'camerax_library.g.dart';
Expand All @@ -13,6 +14,7 @@ import 'recording.dart';
/// Dart wrapping of PendingRecording CameraX class.
///
/// See https://developer.android.com/reference/androidx/camera/video/PendingRecording
@immutable
class PendingRecording extends JavaObject {
/// Creates a [PendingRecording] that is not automatically attached to
/// a native object.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import 'dart:typed_data';

import 'package:flutter/services.dart' show BinaryMessenger;
import 'package:meta/meta.dart' show protected;
import 'package:meta/meta.dart' show immutable, protected;

import 'android_camera_camerax_flutter_api_impls.dart';
import 'camerax_library.g.dart';
Expand All @@ -15,6 +15,7 @@ import 'java_object.dart';
/// A single color plane of image data.
///
/// See https://developer.android.com/reference/androidx/camera/core/ImageProxy.PlaneProxy.
@immutable
class PlaneProxy extends JavaObject {
/// Constructs a [PlaneProxy] that is not automatically attached to a native object.
PlaneProxy.detached(
Expand Down
2 changes: 2 additions & 0 deletions packages/camera/camera_android_camerax/lib/src/preview.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// found in the LICENSE file.

import 'package:flutter/services.dart' show BinaryMessenger;
import 'package:meta/meta.dart' show immutable;

import 'camerax_library.g.dart';
import 'instance_manager.dart';
Expand All @@ -12,6 +13,7 @@ import 'use_case.dart';
/// Use case that provides a camera preview stream for display.
///
/// See https://developer.android.com/reference/androidx/camera/core/Preview.
@immutable
class Preview extends UseCase {
/// Creates a [Preview].
Preview(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// found in the LICENSE file.

import 'package:flutter/services.dart';
import 'package:meta/meta.dart' show immutable;

import 'android_camera_camerax_flutter_api_impls.dart';
import 'camera.dart';
Expand All @@ -16,6 +17,7 @@ import 'use_case.dart';
/// Provides an object to manage the camera.
///
/// See https://developer.android.com/reference/androidx/camera/lifecycle/ProcessCameraProvider.
@immutable
class ProcessCameraProvider extends JavaObject {
/// Creates a detached [ProcessCameraProvider].
ProcessCameraProvider.detached(
Expand Down
2 changes: 2 additions & 0 deletions packages/camera/camera_android_camerax/lib/src/recorder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// found in the LICENSE file.

import 'package:flutter/services.dart';
import 'package:meta/meta.dart' show immutable;

import 'android_camera_camerax_flutter_api_impls.dart';
import 'camerax_library.g.dart';
Expand All @@ -13,6 +14,7 @@ import 'pending_recording.dart';
/// A dart wrapping of the CameraX Recorder class.
///
/// See https://developer.android.com/reference/androidx/camera/video/Recorder.
@immutable
class Recorder extends JavaObject {
/// Creates a [Recorder].
Recorder(
Expand Down
2 changes: 2 additions & 0 deletions packages/camera/camera_android_camerax/lib/src/recording.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// found in the LICENSE file.

import 'package:flutter/services.dart' show BinaryMessenger;
import 'package:meta/meta.dart' show immutable;

import 'android_camera_camerax_flutter_api_impls.dart';
import 'camerax_library.g.dart';
Expand All @@ -12,6 +13,7 @@ import 'java_object.dart';
/// Wraps a CameraX recording class.
///
/// See https://developer.android.com/reference/androidx/camera/video/Recording.
@immutable
class Recording extends JavaObject {
/// Constructs a detached [Recording]
Recording.detached(
Expand Down
3 changes: 3 additions & 0 deletions packages/camera/camera_android_camerax/lib/src/surface.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:meta/meta.dart' show immutable;

import 'java_object.dart';

/// Handle onto the raw buffer managed by screen compositor.
///
/// See https://developer.android.com/reference/android/view/Surface.html.
@immutable
class Surface extends JavaObject {
/// Creates a detached [Surface].
Surface.detached({super.binaryMessenger, super.instanceManager})
Expand Down
3 changes: 3 additions & 0 deletions packages/camera/camera_android_camerax/lib/src/use_case.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:meta/meta.dart' show immutable;

import 'java_object.dart';

/// An object representing the different functionalities of the camera.
///
/// See https://developer.android.com/reference/androidx/camera/core/UseCase.
@immutable
class UseCase extends JavaObject {
/// Creates a detached [UseCase].
UseCase.detached({super.binaryMessenger, super.instanceManager})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// found in the LICENSE file.

import 'package:flutter/services.dart';
import 'package:meta/meta.dart' show immutable;

import 'android_camera_camerax_flutter_api_impls.dart';
import 'camerax_library.g.dart';
Expand All @@ -14,6 +15,7 @@ import 'use_case.dart';
/// Dart wrapping of CameraX VideoCapture class.
///
/// See https://developer.android.com/reference/androidx/camera/video/VideoCapture.
@immutable
class VideoCapture extends UseCase {
/// Creates a VideoCapture that is not automatically attached to a native object.
VideoCapture.detached(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// found in the LICENSE file.

import 'package:flutter/services.dart' show BinaryMessenger;
import 'package:meta/meta.dart' show immutable;

import 'android_camera_camerax_flutter_api_impls.dart';
import 'camerax_library.g.dart';
Expand All @@ -12,6 +13,7 @@ import 'java_object.dart';
/// Represents zoom related information of a camera.
///
/// See https://developer.android.com/reference/androidx/camera/core/ZoomState.
@immutable
class ZoomState extends JavaObject {
/// Constructs a [CameraInfo] that is not automatically attached to a native object.
ZoomState.detached(
Expand Down
2 changes: 1 addition & 1 deletion packages/camera/camera_android_camerax/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: camera_android_camerax
description: Android implementation of the camera plugin using the CameraX library.
repository: https://github.com/flutter/packages/tree/main/packages/camera/camera_android_camerax
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+camera%22
version: 0.5.0+8
version: 0.5.0+9

environment:
sdk: ">=2.19.0 <4.0.0"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Mocks generated by Mockito 5.4.0 from annotations
// Mocks generated by Mockito 5.4.1 from annotations
// in camera_android_camerax/test/analyzer_test.dart.
// Do not manually edit this file.

// @dart=2.19

// ignore_for_file: no_leading_underscores_for_library_prefixes
import 'package:mockito/mockito.dart' as _i1;

Expand Down
Loading

0 comments on commit 6181f65

Please sign in to comment.