import 'dart:io'; import 'package:flutter/foundation.dart'; import 'package:flutter/services.dart'; class VolumeEventsService { @visibleForTesting static const volumeHandlingChannel = MethodChannel("com.vodemn.lightmeter/volumeHandling"); @visibleForTesting static const volumeEventsChannel = EventChannel("com.vodemn.lightmeter/volumeEvents"); const VolumeEventsService(); /// If set to `false` we allow system to handle key events. /// Returns current status of volume handling. Future setVolumeHandling(bool enableHandling) async { if (!Platform.isAndroid) { return false; } return volumeHandlingChannel .invokeMethod("setVolumeHandling", enableHandling) .then((value) => value!); } /// Emits new events on /// KEYCODE_VOLUME_UP = 24; /// KEYCODE_VOLUME_DOWN = 25; /// pressed Stream volumeButtonsEventStream() { if (!Platform.isAndroid) { return const Stream.empty(); } return volumeEventsChannel .receiveBroadcastStream() .cast() .where((event) => event == 24 || event == 25); } }