forked from kurenai7968/volume_controller
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathandroid_volume_button_handler.dart
More file actions
54 lines (43 loc) · 1.47 KB
/
android_volume_button_handler.dart
File metadata and controls
54 lines (43 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
class AndroidVolumeButtonHandler {
static AndroidVolumeButtonHandler? _instance;
final _platform = const MethodChannel("com.codeSomethingDaily/method");
final _event = const EventChannel("com.codeSomethingDaily/event");
Stream<String>? stream;
AndroidVolumeButtonHandler._();
/// return a singletion instance for invoking methods
factory AndroidVolumeButtonHandler() {
_instance ??= AndroidVolumeButtonHandler._();
return _instance!;
}
/// Blocks System change to volume and the volume UI
void startBlocking() {
_platform.invokeMethod('startBlockingVolumeKey');
}
/// Unblocks System change to volume and the volume UI
void stopBlocking() {
_platform.invokeMethod('stopBlockingVolumeKey');
}
/// Add a listener to volume up and down
/// return either "volumeUp" or "volumeDown" as String
StreamSubscription<String> listen(void Function(String) onData) {
stream ??= _event.receiveBroadcastStream().map((event) => event as String);
return stream!.listen(onData);
}
/// removes the listener
void cancel(StreamSubscription<String> subscription) {
subscription.cancel();
}
/// For testing only
@visibleForTesting
MethodChannel getMethodChannel() {
return _platform;
}
/// For testing only
@visibleForTesting
Future<bool> testMethod() async {
return await _platform.invokeMethod('stopBlockingVolumeKey');
}
}