-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
120 lines (103 loc) · 2.78 KB
/
main.go
File metadata and controls
120 lines (103 loc) · 2.78 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Query audio state: volume levels, mute status, audio mode.
//
// Build:
//
// GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o build/audio_status ./examples/audio_status/
// adb push audio_status /data/local/tmp/ && adb shell /data/local/tmp/audio_status
package main
import (
"context"
"fmt"
"os"
"github.com/AndroidGoLab/binder/binder"
"github.com/AndroidGoLab/binder/binder/versionaware"
"github.com/AndroidGoLab/binder/android/media"
"github.com/AndroidGoLab/binder/kernelbinder"
"github.com/AndroidGoLab/binder/servicemanager"
)
type audioStream int32
const (
streamVoice audioStream = iota
streamSystem
streamRing
streamMusic
streamAlarm
streamNotification
)
type audioMode int32
const (
audioModeNormal audioMode = 0
audioModeRingtone audioMode = 1
audioModeInCall audioMode = 2
audioModeInCommunication audioMode = 3
)
var streamNames = map[audioStream]string{
streamVoice: "Voice",
streamSystem: "System",
streamRing: "Ring",
streamMusic: "Music",
streamAlarm: "Alarm",
streamNotification: "Notification",
}
func main() {
ctx := context.Background()
driver, err := kernelbinder.Open(ctx, binder.WithMapSize(128*1024))
if err != nil {
fmt.Fprintf(os.Stderr, "open binder: %v\n", err)
os.Exit(1)
}
defer driver.Close(ctx)
transport, err := versionaware.NewTransport(ctx, driver, 0)
if err != nil {
fmt.Fprintf(os.Stderr, "version-aware transport: %v\n", err)
os.Exit(1)
}
sm := servicemanager.New(transport)
svc, err := sm.GetService(ctx, servicemanager.AudioService)
if err != nil {
fmt.Fprintf(os.Stderr, "get audio service: %v\n", err)
os.Exit(1)
}
audio := media.NewAudioServiceProxy(svc)
mode, err := audio.GetMode(ctx)
if err != nil {
fmt.Fprintf(os.Stderr, "GetMode: %v\n", err)
} else {
modeName := "unknown"
switch audioMode(mode) {
case audioModeNormal:
modeName = "normal"
case audioModeRingtone:
modeName = "ringtone"
case audioModeInCall:
modeName = "in call"
case audioModeInCommunication:
modeName = "in communication"
}
fmt.Printf("Audio mode: %s (%d)\n", modeName, mode)
}
micMuted, err := audio.IsMicrophoneMuted(ctx)
if err != nil {
fmt.Fprintf(os.Stderr, "IsMicrophoneMuted: %v\n", err)
} else {
fmt.Printf("Microphone muted: %v\n", micMuted)
}
fmt.Println("\nVolume levels:")
for stream := streamVoice; stream <= streamNotification; stream++ {
name := streamNames[stream]
vol, err := audio.GetStreamVolume(ctx, int32(stream))
if err != nil {
continue
}
maxVol, err := audio.GetStreamMaxVolume(ctx, int32(stream))
if err != nil {
continue
}
muted, _ := audio.IsStreamMute(ctx, int32(stream))
muteStr := ""
if muted {
muteStr = " [MUTED]"
}
fmt.Printf(" %-12s %d / %d%s\n", name, vol, maxVol, muteStr)
}
}