blob: a2513e0376624b7ed1d71f59b601008974de5b11 [file] [log] [blame]
// 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.
import 'dart:async';
import 'package:camera_platform_interface/camera_platform_interface.dart'
show CameraException, DeviceOrientationChangedEvent;
import 'package:flutter/services.dart';
import 'android_camera_camerax_flutter_api_impls.dart';
import 'camerax_library.g.dart';
// Ignoring lint indicating this class only contains static members
// as this class is a wrapper for various Android system services.
// ignore_for_file: avoid_classes_with_only_static_members
/// Utility class that offers access to Android system services needed for
/// camera usage and other informational streams.
class SystemServices {
/// Stream that emits the device orientation whenever it is changed.
/// Values may start being added to the stream once
/// `startListeningForDeviceOrientationChange(...)` is called.
static final StreamController<DeviceOrientationChangedEvent>
deviceOrientationChangedStreamController =
/// Stream that emits the errors caused by camera usage on the native side.
static final StreamController<String> cameraErrorStreamController =
/// Requests permission to access the camera and audio if specified.
static Future<void> requestCameraPermissions(bool enableAudio,
{BinaryMessenger? binaryMessenger}) {
final SystemServicesHostApiImpl api =
SystemServicesHostApiImpl(binaryMessenger: binaryMessenger);
return api.sendCameraPermissionsRequest(enableAudio);
/// Requests that [deviceOrientationChangedStreamController] start
/// emitting values for any change in device orientation.
static void startListeningForDeviceOrientationChange(
bool isFrontFacing, int sensorOrientation,
{BinaryMessenger? binaryMessenger}) {
final SystemServicesHostApi api =
SystemServicesHostApi(binaryMessenger: binaryMessenger);
isFrontFacing, sensorOrientation);
/// Stops the [deviceOrientationChangedStreamController] from emitting values
/// for changes in device orientation.
static void stopListeningForDeviceOrientationChange(
{BinaryMessenger? binaryMessenger}) {
final SystemServicesHostApi api =
SystemServicesHostApi(binaryMessenger: binaryMessenger);
/// Returns a file path which was used to create a temporary file.
/// Prefix is a part of the file name, and suffix is the file extension.
/// The file and path constraints are determined by the implementation of
/// File.createTempFile(prefix, suffix, cacheDir), on the android side, where
/// where cacheDir is the cache directory identified by the current application
/// context using context.getCacheDir().
/// Ex: getTempFilePath('prefix', 'suffix') would return a string of the form
/// '<cachePath>/prefix3213453.suffix', where the numbers after prefix and
/// before suffix are determined by the call to File.createTempFile and
/// therefore random.
static Future<String> getTempFilePath(String prefix, String suffix,
{BinaryMessenger? binaryMessenger}) {
final SystemServicesHostApi api =
SystemServicesHostApi(binaryMessenger: binaryMessenger);
return api.getTempFilePath(prefix, suffix);
/// Host API implementation of [SystemServices].
class SystemServicesHostApiImpl extends SystemServicesHostApi {
/// Constructs an [SystemServicesHostApiImpl].
/// If [binaryMessenger] is null, the default [BinaryMessenger] will be used,
/// which routes to the host platform.
: super(binaryMessenger: binaryMessenger);
/// Receives binary data across the Flutter platform barrier.
final BinaryMessenger? binaryMessenger;
/// Requests permission to access the camera and audio if specified.
/// Will complete normally if permissions are successfully granted; otherwise,
/// will throw a [CameraException].
Future<void> sendCameraPermissionsRequest(bool enableAudio) async {
final CameraPermissionsErrorData? error =
await requestCameraPermissions(enableAudio);
if (error != null) {
throw CameraException(
/// Flutter API implementation of [SystemServices].
class SystemServicesFlutterApiImpl implements SystemServicesFlutterApi {
/// Constructs an [SystemServicesFlutterApiImpl].
/// Callback method for any changes in device orientation.
/// Will only be called if
/// `SystemServices.startListeningForDeviceOrientationChange(...)` was called
/// to start listening for device orientation updates.
void onDeviceOrientationChanged(String orientation) {
final DeviceOrientation deviceOrientation =
/// Deserializes device orientation in [String] format into a
/// [DeviceOrientation].
DeviceOrientation deserializeDeviceOrientation(String orientation) {
switch (orientation) {
return DeviceOrientation.landscapeLeft;
return DeviceOrientation.landscapeRight;
return DeviceOrientation.portraitDown;
return DeviceOrientation.portraitUp;
throw ArgumentError(
'"$orientation" is not a valid DeviceOrientation value');
/// Callback method for any errors caused by camera usage on the Java side.
void onCameraError(String errorDescription) {