Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

Commit

Permalink
[camera] Expanded platform interface to support setting flash mode (#…
Browse files Browse the repository at this point in the history
…3313)

* Expanded platform interface so support setting flash mode

* Formatted dart code

* Manually serialize flash mode enum rather than relying on stringification.

* Add default to flash mode serialization
  • Loading branch information
BeMacized authored Dec 10, 2020
1 parent f9d5525 commit a7c4929
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 1 deletion.
4 changes: 4 additions & 0 deletions packages/camera/camera_platform_interface/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.0.1

- Added interface methods for setting flash mode.

## 1.0.0

- Initial open-source release
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,35 @@ class MethodChannelCamera extends CameraPlatform {
<String, dynamic>{'cameraId': cameraId},
);

@override
Future<void> setFlashMode(int cameraId, FlashMode mode) =>
_channel.invokeMethod<void>(
'setFlashMode',
<String, dynamic>{
'cameraId': cameraId,
'mode': _serializeFlashMode(mode),
},
);

@override
Widget buildPreview(int cameraId) {
return Texture(textureId: cameraId);
}

/// Returns the flash mode as a String.
String _serializeFlashMode(FlashMode flashMode) {
switch (flashMode) {
case FlashMode.off:
return 'off';
case FlashMode.auto:
return 'auto';
case FlashMode.always:
return 'always';
default:
throw ArgumentError('Unknown FlashMode value');
}
}

/// Returns the resolution preset as a String.
String _serializeResolutionPreset(ResolutionPreset resolutionPreset) {
switch (resolutionPreset) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ abstract class CameraPlatform extends PlatformInterface {
throw UnimplementedError('resumeVideoRecording() is not implemented.');
}

/// Sets the flash mode for taking pictures.
Future<void> setFlashMode(int cameraId, FlashMode mode) {
throw UnimplementedError('setFlashMode() is not implemented.');
}

/// Returns a widget showing a live camera preview.
Widget buildPreview(int cameraId) {
throw UnimplementedError('buildView() has not been implemented.');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

/// The possible flash modes that can be set for a camera
enum FlashMode {
/// Do not use the flash when taking a picture.
off,

/// Let the device decide whether to flash the camera when taking a picture.
auto,

/// Always use the flash when taking a picture.
always,
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
export 'camera_description.dart';
export 'resolution_preset.dart';
export 'camera_exception.dart';
export 'flash_mode.dart';
2 changes: 1 addition & 1 deletion packages/camera/camera_platform_interface/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description: A common platform interface for the camera plugin.
homepage: https://github.com/flutter/plugins/tree/master/packages/camera/camera_platform_interface
# NOTE: We strongly prefer non-breaking changes, even at the expense of a
# less-clean API. See https://flutter.dev/go/platform-interface-breaking-changes
version: 1.0.0
version: 1.0.1

dependencies:
flutter:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,19 @@ void main() {
);
});

test(
'Default implementation of setFlashMode() should throw unimplemented error',
() {
// Arrange
final cameraPlatform = ExtendsCameraPlatform();

// Act & Assert
expect(
() => cameraPlatform.setFlashMode(1, null),
throwsUnimplementedError,
);
});

test(
'Default implementation of startVideoRecording() should throw unimplemented error',
() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,29 @@ void main() {
]);
});

test('Should set the flash mode', () async {
// Arrange
MethodChannelMock channel = MethodChannelMock(
channelName: 'plugins.flutter.io/camera',
methods: {'setFlashMode': null},
);

// Act
await camera.setFlashMode(cameraId, FlashMode.always);
await camera.setFlashMode(cameraId, FlashMode.auto);
await camera.setFlashMode(cameraId, FlashMode.off);

// Assert
expect(channel.log, <Matcher>[
isMethodCall('setFlashMode',
arguments: {'cameraId': cameraId, 'mode': 'always'}),
isMethodCall('setFlashMode',
arguments: {'cameraId': cameraId, 'mode': 'auto'}),
isMethodCall('setFlashMode',
arguments: {'cameraId': cameraId, 'mode': 'off'}),
]);
});

test('Should build a texture widget as preview widget', () async {
// Act
Widget widget = camera.buildPreview(cameraId);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:camera_platform_interface/camera_platform_interface.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
test('FlashMode should contain 3 options', () {
final values = FlashMode.values;

expect(values.length, 3);
});

test("FlashMode enum should have items in correct index", () {
final values = FlashMode.values;

expect(values[0], FlashMode.off);
expect(values[1], FlashMode.auto);
expect(values[2], FlashMode.always);
});
}

0 comments on commit a7c4929

Please sign in to comment.