-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathBTManager.x
executable file
·188 lines (156 loc) · 5.99 KB
/
BTManager.x
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#import <BTManager.h>
BOOL enabled = NO;
BOOL appswitchingEnabled = NO;
NSDictionary* deviceOrder;
NSDictionary* deviceSettings;
SBMediaController* mediaController;
MPAVRoutingController* routeController;
NSMutableDictionary* routes;
MPAVRoute* activeRoute;
MPAVRoute* previousRoute;
UIView* routingSuperview;
SBApplication* nowPlayingApplication;
void updateAvailableRoutes() {
mediaController = [%c(SBMediaController) sharedInstance];
routeController = [mediaController valueForKey:@"_routingController"];
NSArray* availableRoutes = [routeController availableRoutes];
[routes removeAllObjects];
for (MPAVOutputDeviceRoute* route in availableRoutes) {
if ([route isAppleTVRoute]) {
continue;
}
NSString* uid = route.routeUID;
if (!uid) {
continue;
}
[routes setObject:route forKey:uid];
}
activeRoute = [routeController pickedRoute];
}
void updateActiveRoute() {
if (!appswitchingEnabled) {
return;
}
mediaController = [%c(SBMediaController) sharedInstance];
routeController = [mediaController valueForKey:@"_routingController"];
for (NSString* routeAddress in deviceOrder[nowPlayingApplication.bundleIdentifier] ?: deviceOrder[@"SYSTEM"]) {
for (NSString* routeUID in routes) {
if ([routeUID containsString:routeAddress]) {
activeRoute = routes[routeUID];
[routeController pickRoute:routes[routeUID]];
return;
}
}
}
for (NSString* routeUID in routes) {
if ([routeUID isEqual:@"Speaker"]) {
[routeController pickRoute:routes[routeUID]];
return;
}
}
}
static void loadPrefs() {
NSMutableDictionary *settings = [[NSMutableDictionary alloc] initWithContentsOfFile:@"/var/mobile/Library/Preferences/com.beckettobrien.btmanagerprefs.plist"];
enabled = [[settings objectForKey:@"enabled"] boolValue];
appswitchingEnabled = [[settings objectForKey:@"appswitchingenabled"] boolValue];
deviceOrder = [settings objectForKey:@"deviceOrder"] ?: [[NSDictionary alloc] init];
deviceSettings = [settings objectForKey:@"deviceSettings"] ?: [[NSDictionary alloc] init];
}
%hook SBBluetoothController
-(id)deviceForAudioRoute:(id)arg1 {
id out = %orig;
if (enabled == 0) {
return out;
}
if ([arg1[@"RouteUID"] isEqual:activeRoute.routeUID]) {
return out;
}
previousRoute = activeRoute;
updateAvailableRoutes();
NSString* mac;
for (NSString* routeAddress in deviceSettings) {
if ([arg1[@"RouteUID"] containsString:routeAddress]) {
mac = routeAddress;
}
}
if (!mac) {
return out;
}
if (!(deviceSettings[mac][@"switchConfirm"] ? [deviceSettings[mac][@"switchConfirm"] boolValue] : NO)) {
return out;
}
UIWindow* foundWindow = nil;
for (UIWindow* window in [[UIApplication sharedApplication] windows]) {
if (window.isKeyWindow) {
foundWindow = window;
break;
}
}
if (routingSuperview) {
if (routingSuperview.hidden == NO | [foundWindow class] == NSClassFromString(@"SBTransientOverlayWindow")) {
return out;
}
}
if ([[%c(SBMediaController) sharedInstance] isPlaying]) {
[[%c(SBMediaController) sharedInstance] togglePlayPauseForEventSource:0];
}
UIAlertController *alert = [UIAlertController alertControllerWithTitle:[NSString stringWithFormat:@"Switch to %@", arg1[@"RouteName"]] message:
[NSString stringWithFormat:@"Confirm to switch to this device, cancel to return to %@ if available or switch to speakers and stop.", previousRoute.routeName]
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *ok = [UIAlertAction actionWithTitle:@"Confirm" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
if ([[%c(SBMediaController) sharedInstance] isPaused]) {
[[%c(SBMediaController) sharedInstance] togglePlayPauseForEventSource:0];
}
}];
UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
updateAvailableRoutes();
MPAVOutputDeviceRoute* route = routes[previousRoute.routeUID] ?: routes[@"Speaker"];
[[[%c(SBMediaController) sharedInstance] valueForKey:@"_routingController"] pickRoute:route];
if (![route.routeUID isEqual:@"Speaker"]) {
[[%c(SBMediaController) sharedInstance] togglePlayPauseForEventSource:0];
}
}];
[alert addAction:cancel];
[alert addAction:ok];
[foundWindow.rootViewController presentViewController:alert animated:YES completion:nil];
return out;
}
%end
%hook SBMediaController
-(void)routingControllerAvailableRoutesDidChange:(id)arg1 {
%orig;
if (enabled == 0) {
return;
}
updateAvailableRoutes();
if (!routes[activeRoute.routeUID]) {
updateActiveRoute();
}
}
-(void)_mediaRemoteNowPlayingApplicationDidChange:(id)arg1 {
%orig;
if (enabled == 0) {
return;
}
nowPlayingApplication = [self nowPlayingApplication];
updateAvailableRoutes();
updateActiveRoute();
}
%end
%hook MPAVRoutingController
-(bool)pickRoute:(id)arg1 {
updateAvailableRoutes();
return %orig;
}
%end
%hook MPAVClippingTableView
-(void)didMoveToSuperview {
routingSuperview = self.superview;
%orig;
}
%end
%ctor {
routes = [NSMutableDictionary new];
loadPrefs();
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), NULL, (CFNotificationCallback)loadPrefs, CFSTR("com.beckettobrien.btmanagerprefs.settingschanged"), NULL, CFNotificationSuspensionBehaviorCoalesce);
}