|
| 1 | +// Copyright 2019 The Chromium Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +/// Generic Event coming from the native side of Camera. |
| 6 | +/// |
| 7 | +/// All [CameraEvent]s contain the `cameraId` that originated the event. This |
| 8 | +/// should never be `null`. |
| 9 | +/// |
| 10 | +/// This class is used as a base class for all the events that might be |
| 11 | +/// triggered from a Camera, but it is never used directly as an event type. |
| 12 | +/// |
| 13 | +/// Do NOT instantiate new events like `CameraEvent(cameraId)` directly, |
| 14 | +/// use a specific class instead: |
| 15 | +/// |
| 16 | +/// Do `class NewEvent extend CameraEvent` when creating your own events. |
| 17 | +/// See below for examples: `CameraClosingEvent`, `CameraErrorEvent`... |
| 18 | +/// These events are more semantic and more pleasant to use than raw generics. |
| 19 | +/// They can be (and in fact, are) filtered by the `instanceof`-operator. |
| 20 | +abstract class CameraEvent { |
| 21 | + /// The ID of the Camera this event is associated to. |
| 22 | + final int cameraId; |
| 23 | + |
| 24 | + /// Build a Camera Event, that relates a `cameraId`. |
| 25 | + /// |
| 26 | + /// The `cameraId` is the ID of the camera that triggered the event. |
| 27 | + CameraEvent(this.cameraId) : assert(cameraId != null); |
| 28 | + |
| 29 | + @override |
| 30 | + bool operator ==(Object other) => |
| 31 | + identical(this, other) || |
| 32 | + other is CameraEvent && |
| 33 | + runtimeType == other.runtimeType && |
| 34 | + cameraId == other.cameraId; |
| 35 | + |
| 36 | + @override |
| 37 | + int get hashCode => cameraId.hashCode; |
| 38 | +} |
| 39 | + |
| 40 | +/// An event fired when the camera has finished initializing. |
| 41 | +class CameraInitializedEvent extends CameraEvent { |
| 42 | + /// The width of the preview in pixels. |
| 43 | + final double previewWidth; |
| 44 | + |
| 45 | + /// The height of the preview in pixels. |
| 46 | + final double previewHeight; |
| 47 | + |
| 48 | + /// Build a CameraInitialized event triggered from the camera represented by |
| 49 | + /// `cameraId`. |
| 50 | + /// |
| 51 | + /// The `previewWidth` represents the width of the generated preview in pixels. |
| 52 | + /// The `previewHeight` represents the height of the generated preview in pixels. |
| 53 | + CameraInitializedEvent( |
| 54 | + int cameraId, |
| 55 | + this.previewWidth, |
| 56 | + this.previewHeight, |
| 57 | + ) : super(cameraId); |
| 58 | + |
| 59 | + /// Converts the supplied [Map] to an instance of the [CameraInitializedEvent] |
| 60 | + /// class. |
| 61 | + CameraInitializedEvent.fromJson(Map<String, dynamic> json) |
| 62 | + : previewWidth = json['previewWidth'], |
| 63 | + previewHeight = json['previewHeight'], |
| 64 | + super(json['cameraId']); |
| 65 | + |
| 66 | + /// Converts the [CameraInitializedEvent] instance into a [Map] instance that |
| 67 | + /// can be serialized to JSON. |
| 68 | + Map<String, dynamic> toJson() => { |
| 69 | + 'cameraId': cameraId, |
| 70 | + 'previewWidth': previewWidth, |
| 71 | + 'previewHeight': previewHeight, |
| 72 | + }; |
| 73 | + |
| 74 | + @override |
| 75 | + bool operator ==(Object other) => |
| 76 | + identical(this, other) || |
| 77 | + super == other && |
| 78 | + other is CameraInitializedEvent && |
| 79 | + runtimeType == other.runtimeType && |
| 80 | + previewWidth == other.previewWidth && |
| 81 | + previewHeight == other.previewHeight; |
| 82 | + |
| 83 | + @override |
| 84 | + int get hashCode => |
| 85 | + super.hashCode ^ previewWidth.hashCode ^ previewHeight.hashCode; |
| 86 | +} |
| 87 | + |
| 88 | +/// An event fired when the resolution preset of the camera has changed. |
| 89 | +class CameraResolutionChangedEvent extends CameraEvent { |
| 90 | + /// The capture width in pixels. |
| 91 | + final double captureWidth; |
| 92 | + |
| 93 | + /// The capture height in pixels. |
| 94 | + final double captureHeight; |
| 95 | + |
| 96 | + /// Build a CameraResolutionChanged event triggered from the camera |
| 97 | + /// represented by `cameraId`. |
| 98 | + /// |
| 99 | + /// The `captureWidth` represents the width of the resulting image in pixels. |
| 100 | + /// The `captureHeight` represents the height of the resulting image in pixels. |
| 101 | + CameraResolutionChangedEvent( |
| 102 | + int cameraId, |
| 103 | + this.captureWidth, |
| 104 | + this.captureHeight, |
| 105 | + ) : super(cameraId); |
| 106 | + |
| 107 | + /// Converts the supplied [Map] to an instance of the |
| 108 | + /// [CameraResolutionChangedEvent] class. |
| 109 | + CameraResolutionChangedEvent.fromJson(Map<String, dynamic> json) |
| 110 | + : captureWidth = json['captureWidth'], |
| 111 | + captureHeight = json['captureHeight'], |
| 112 | + super(json['cameraId']); |
| 113 | + |
| 114 | + /// Converts the [CameraResolutionChangedEvent] instance into a [Map] instance |
| 115 | + /// that can be serialized to JSON. |
| 116 | + Map<String, dynamic> toJson() => { |
| 117 | + 'cameraId': cameraId, |
| 118 | + 'captureWidth': captureWidth, |
| 119 | + 'captureHeight': captureHeight, |
| 120 | + }; |
| 121 | + |
| 122 | + @override |
| 123 | + bool operator ==(Object other) => |
| 124 | + identical(this, other) || |
| 125 | + other is CameraResolutionChangedEvent && |
| 126 | + super == (other) && |
| 127 | + runtimeType == other.runtimeType && |
| 128 | + captureWidth == other.captureWidth && |
| 129 | + captureHeight == other.captureHeight; |
| 130 | + |
| 131 | + @override |
| 132 | + int get hashCode => |
| 133 | + super.hashCode ^ captureWidth.hashCode ^ captureHeight.hashCode; |
| 134 | +} |
| 135 | + |
| 136 | +/// An event fired when the camera is going to close. |
| 137 | +class CameraClosingEvent extends CameraEvent { |
| 138 | + /// Build a CameraClosing event triggered from the camera represented by |
| 139 | + /// `cameraId`. |
| 140 | + CameraClosingEvent(int cameraId) : super(cameraId); |
| 141 | + |
| 142 | + /// Converts the supplied [Map] to an instance of the [CameraClosingEvent] |
| 143 | + /// class. |
| 144 | + CameraClosingEvent.fromJson(Map<String, dynamic> json) |
| 145 | + : super(json['cameraId']); |
| 146 | + |
| 147 | + /// Converts the [CameraClosingEvent] instance into a [Map] instance that can |
| 148 | + /// be serialized to JSON. |
| 149 | + Map<String, dynamic> toJson() => { |
| 150 | + 'cameraId': cameraId, |
| 151 | + }; |
| 152 | + |
| 153 | + @override |
| 154 | + bool operator ==(Object other) => |
| 155 | + identical(this, other) || |
| 156 | + super == (other) && |
| 157 | + other is CameraClosingEvent && |
| 158 | + runtimeType == other.runtimeType; |
| 159 | + |
| 160 | + @override |
| 161 | + int get hashCode => super.hashCode; |
| 162 | +} |
| 163 | + |
| 164 | +/// An event fired when an error occured while operating the camera. |
| 165 | +class CameraErrorEvent extends CameraEvent { |
| 166 | + /// Description of the error. |
| 167 | + final String description; |
| 168 | + |
| 169 | + /// Build a CameraError event triggered from the camera represented by |
| 170 | + /// `cameraId`. |
| 171 | + /// |
| 172 | + /// The `description` represents the error occured on the camera. |
| 173 | + CameraErrorEvent(int cameraId, this.description) : super(cameraId); |
| 174 | + |
| 175 | + /// Converts the supplied [Map] to an instance of the [CameraErrorEvent] |
| 176 | + /// class. |
| 177 | + CameraErrorEvent.fromJson(Map<String, dynamic> json) |
| 178 | + : description = json['description'], |
| 179 | + super(json['cameraId']); |
| 180 | + |
| 181 | + /// Converts the [CameraErrorEvent] instance into a [Map] instance that can be |
| 182 | + /// serialized to JSON. |
| 183 | + Map<String, dynamic> toJson() => { |
| 184 | + 'cameraId': cameraId, |
| 185 | + 'description': description, |
| 186 | + }; |
| 187 | + |
| 188 | + @override |
| 189 | + bool operator ==(Object other) => |
| 190 | + identical(this, other) || |
| 191 | + super == (other) && |
| 192 | + other is CameraErrorEvent && |
| 193 | + runtimeType == other.runtimeType && |
| 194 | + description == other.description; |
| 195 | + |
| 196 | + @override |
| 197 | + int get hashCode => super.hashCode ^ description.hashCode; |
| 198 | +} |
0 commit comments