|
| 1 | +# Contributing to camera\_android\_camerax |
| 2 | + |
| 3 | +## Plugin structure |
| 4 | + |
| 5 | +The `camera_platform_interface` implementation is located at |
| 6 | +`lib/src/android_camera_camerax.dart`, and it is implemented using Dart classes |
| 7 | +that are wrapped versions of native Android Java classes. |
| 8 | + |
| 9 | +In this approach, each native Android library used in the plugin implementation |
| 10 | +is represented by an equivalent Dart class. Instances of these classes are |
| 11 | +considered paired and represent each other in Java and Dart, respectively. An |
| 12 | +`InstanceManager`, which is essentially a map between `long` identifiers and |
| 13 | +objects that also provides notifications when an object has become unreachable |
| 14 | +by memory. There is both a Dart and Java `InstanceManager` implementation, so |
| 15 | +when a Dart instance is created that represens an Android native instance, |
| 16 | +both are stored in the `InstanceManager` of their respective language with a |
| 17 | +shared `long` identifier. These `InstanceManager`s take callbacks that run |
| 18 | +when objects become unrechable or removed, allowing the Dart library to easily |
| 19 | +handle removing references to native resources automatically. To ensure all |
| 20 | +created instances are properly managed and to more easily allow for testing, |
| 21 | +each wrapped Android native class in Dart takes an `InstanceManager` and has |
| 22 | +a detached constructor, a constructor that allows for the creation of instances |
| 23 | +not attached to the `InstanceManager` and unlinked to a paired Android native |
| 24 | +instance. |
| 25 | + |
| 26 | +In `lib/src/`, you will find all of the Dart-wrapped native Android classes that |
| 27 | +the plugin currently uses in its implementation. As aforementioned, each of |
| 28 | +these classes uses an `InstanceManager` (implementation in `instance_manager.dart`) |
| 29 | +to manage objects that are created by the plugin implementation that map to objects |
| 30 | +of the same type created in the native Android code. This plugin uses [`pigeon`][1] |
| 31 | +to generate the communication layer between Flutter and native Android code, so each |
| 32 | +of these Dart-wrapped classes may also have Host API and Flutter API implementations |
| 33 | +that handle communication to the host native Android platform and from the host |
| 34 | +native Android platform, respectively. The communication interface is defined in |
| 35 | +the `pigeons/camerax_library.dart` file. After editing the communication interface, |
| 36 | +regenerate the communication layer by running |
| 37 | +`dart run pigeon --input pigeons/camerax_library.dart` from the plugin root. |
| 38 | + |
| 39 | +In the native Java Android code in `android/src/main/java/io/flutter/plugins/camerax/`, |
| 40 | +you'll find the Host API and Flutter API implementations of the same classes |
| 41 | +wrapped with Dart in `lib/src/` that handle communication from that Dart code |
| 42 | +and to that Dart code, respectively. The Host API implementations should directly |
| 43 | +delegate calls to the CameraX or other wrapped Android libraries and should not |
| 44 | +have any additional logic or abstraction; any exceptions should be thoroughly |
| 45 | +documented in the code. As aforementioned, the objects created in the native |
| 46 | +Android code map to objects created on the Dart side and are also managed by |
| 47 | +an `InstanceManager` (implementation in `InstanceManager.java`). |
| 48 | + |
| 49 | +If CameraX or other Android classes that you need to access do not have a |
| 50 | +duplicately named implementation in `lib/src/`, then follow the same structure |
| 51 | +described above to add them. |
| 52 | + |
| 53 | +For more information, please see the [design document][2] or feel free |
| 54 | +to ask any questions on the #hackers-ecosystem channel on [Discord][6]. For |
| 55 | +more information on contributing packages in general, check out our |
| 56 | +[contribution guide][3]. |
| 57 | + |
| 58 | +## Testing |
| 59 | + |
| 60 | +While none of the generated `pigeon` files are tested, all plugin impelementation and |
| 61 | +wrapped native Android classes (Java & Dart) are tested. You can find the Java tests under |
| 62 | +`android/src/test/java/io/flutter/plugins/camerax/` and the Dart tests under `test/`. To |
| 63 | +run these tests, please see the instructions in the [running plugin tests guide][5]. |
| 64 | + |
| 65 | +Besides [`pigeon`][1], this plugin also uses [`mockito`][4] to generate mock objects for |
| 66 | +testing purposes. To generate the mock objects, run |
| 67 | +`dart run build_runner build --delete-conflicting-outputs`. |
| 68 | + |
| 69 | + |
| 70 | +[1]: https://pub.dev/packages/pigeon |
| 71 | +[2]: https://docs.google.com/document/d/1wXB1zNzYhd2SxCu1_BK3qmNWRhonTB6qdv4erdtBQqo/edit?usp=sharing&resourcekey=0-WOBqqOKiO9SARnziBg28pg |
| 72 | +[3]: https://github.com/flutter/packages/blob/main/CONTRIBUTING.md |
| 73 | +[4]: https://pub.dev/packages/mockito |
| 74 | +[5]: https://github.com/flutter/flutter/wiki/Plugin-Tests#running-tests |
| 75 | +[6]: https://github.com/flutter/flutter/wiki/Chat |
0 commit comments