2023-07-03 08:47:01 +00:00
|
|
|
import 'package:flutter/foundation.dart';
|
|
|
|
import 'package:flutter/services.dart';
|
2023-07-05 14:29:33 +00:00
|
|
|
import 'package:platform/platform.dart';
|
2023-07-03 08:47:01 +00:00
|
|
|
|
|
|
|
class VolumeEventsService {
|
2023-07-05 14:29:33 +00:00
|
|
|
final LocalPlatform localPlatform;
|
|
|
|
|
2023-07-03 08:47:01 +00:00
|
|
|
@visibleForTesting
|
|
|
|
static const volumeHandlingChannel = MethodChannel("com.vodemn.lightmeter/volumeHandling");
|
|
|
|
|
|
|
|
@visibleForTesting
|
|
|
|
static const volumeEventsChannel = EventChannel("com.vodemn.lightmeter/volumeEvents");
|
|
|
|
|
2023-07-05 14:29:33 +00:00
|
|
|
const VolumeEventsService(this.localPlatform);
|
2023-07-03 08:47:01 +00:00
|
|
|
|
2023-07-05 13:45:36 +00:00
|
|
|
/// If set to `false` we allow system to handle key events.
|
|
|
|
/// Returns current status of volume handling.
|
2023-07-03 08:47:01 +00:00
|
|
|
Future<bool> setVolumeHandling(bool enableHandling) async {
|
2023-07-05 14:29:33 +00:00
|
|
|
if (!localPlatform.isAndroid) {
|
2023-07-05 13:45:36 +00:00
|
|
|
return false;
|
|
|
|
}
|
2023-07-03 08:47:01 +00:00
|
|
|
return volumeHandlingChannel
|
|
|
|
.invokeMethod<bool>("setVolumeHandling", enableHandling)
|
|
|
|
.then((value) => value!);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Emits new events on
|
|
|
|
/// KEYCODE_VOLUME_UP = 24;
|
|
|
|
/// KEYCODE_VOLUME_DOWN = 25;
|
|
|
|
/// pressed
|
|
|
|
Stream<int> volumeButtonsEventStream() {
|
2023-07-05 14:29:33 +00:00
|
|
|
if (!localPlatform.isAndroid) {
|
2023-07-05 13:45:36 +00:00
|
|
|
return const Stream.empty();
|
|
|
|
}
|
|
|
|
return volumeEventsChannel
|
|
|
|
.receiveBroadcastStream()
|
|
|
|
.cast<int>()
|
|
|
|
.where((event) => event == 24 || event == 25);
|
2023-07-03 08:47:01 +00:00
|
|
|
}
|
|
|
|
}
|