forked from PoomSmart/SBShortcutMenuSimulator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SBShortcutMenuListener.m
215 lines (174 loc) · 7.38 KB
/
SBShortcutMenuListener.m
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#import <UIKit/UIKit.h>
#import <objc/runtime.h>
#include <arpa/inet.h>
#define ANY_DEVICE
@interface SBUIForceTouchGestureRecognizer : UIGestureRecognizer
@end
@interface SBApplication : NSObject
@end
@interface SBIcon : NSObject
- (SBApplication *)application;
@end
@interface SBIconView : UIView
- (SBIcon *)icon;
- (SBUIForceTouchGestureRecognizer *)appIconForceTouchGestureRecognizer;
@end
@interface SBIconModel : NSObject
- (SBIcon *)applicationIconForBundleIdentifier:(NSString *)bundleIdentifier;
@end
@interface SBIconViewMap : NSObject
+ (instancetype)homescreenMap;
- (SBIconView *)mappedIconViewForIcon:(SBIcon *)icon;
- (SBIconView *)iconViewForIcon:(SBIcon *)icon;
- (SBIconView *)_iconViewForIcon:(SBIcon *)icon;
@end
@interface SBIconAccessoryViewMap : SBIconViewMap
@end
@interface SBUIAction : NSObject
@end
@interface SBSApplicationShortcutItem : NSObject
@end
@interface SBUIAppIconForceTouchShortcutViewController : UIViewController
- (SBUIAction *)_actionFromApplicationShortcutItem:(SBSApplicationShortcutItem *)item;
@end
@interface SBUIIconForceTouchController : NSObject
- (void)_setupWithGestureRecognizer:(SBUIForceTouchGestureRecognizer *)recognizer;
- (void)_presentAnimated:(BOOL)animated withCompletionHandler:(id)handler;
@end
@interface SBUIAppIconForceTouchController : NSObject {
SBUIIconForceTouchController *_iconForceTouchController;
}
- (void)_setupWithGestureRecognizer:(SBUIForceTouchGestureRecognizer *)recognizer;
- (void)_peekAnimated:(BOOL)animated withRelativeTouchForce:(double)force allowSmoothing:(BOOL)smooth;
- (void)presentAnimated:(BOOL)animated withCompletionHandler:(id)handler;
- (void)appIconForceTouchShortcutViewController:(SBUIAppIconForceTouchShortcutViewController *)controller activateApplicationShortcutItem:(SBSApplicationShortcutItem *)item;
@end
@interface SBApplicationShortcutMenu : NSObject
- (void)updateFromPressGestureRecognizer:(UIGestureRecognizer *)recognizer;
- (void)presentAnimated:(BOOL)animated;
@end
@interface SBIconController : UIViewController {
SBUIAppIconForceTouchController *_appIconForceTouchController;
}
@property (nonatomic, readonly, strong) SBIconModel *model;
+ (instancetype)sharedInstance;
- (SBIconViewMap *)homescreenIconViewMap;
- (SBApplicationShortcutMenu *)presentedShortcutMenu;
- (void)scrollToIconListContainingIcon:(SBIcon *)icon animate:(BOOL)animate;
- (void)_revealMenuForIconView:(SBIconView *)iconView;
- (void)_revealMenuForIconView:(SBIconView *)iconView presentImmediately:(BOOL)presentImmediately;
- (void)appIconForceTouchController:(SBUIAppIconForceTouchController *)controller willPresentForGestureRecognizer:(SBUIForceTouchGestureRecognizer *)recognizer;
- (void)_runAppIconForceTouchTest:(NSString *)test withOptions:(NSDictionary *)options;
@end
#ifdef ANY_DEVICE
@implementation UITraitCollection (Hack)
+ (void)load
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Class class = object_getClass((id)self);
SEL originalSelector = @selector(traitCollectionWithForceTouchCapability:);
SEL swizzledSelector = @selector(hax_traitCollectionWithForceTouchCapability:);
Method originalMethod = class_getClassMethod(class, originalSelector);
Method swizzledMethod = class_getClassMethod(class, swizzledSelector);
BOOL didAddMethod = class_addMethod(class, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod));
if (didAddMethod)
class_replaceMethod(class, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
else
method_exchangeImplementations(originalMethod, swizzledMethod);
});
}
- (int)forceTouchCapability
{
return 2;
}
+ (UITraitCollection *)hax_traitCollectionWithForceTouchCapability:(int)capability
{
return [self hax_traitCollectionWithForceTouchCapability:2];
}
@end
@implementation UIDevice (Override)
- (BOOL)_supportsForceTouch
{
return YES;
}
@end
@implementation UIScreen (Override)
- (int)_forceTouchCapability
{
return 2;
}
@end
#endif
static dispatch_source_t server = nil;
__attribute__((constructor))
static void SBShortcutMenuListenerInitialize() {
NSLog(@"SBShortcutMenuListener: Injecting into SpringBoard");
struct sockaddr_in server_addr = {};
server_addr.sin_len = sizeof(server_addr);
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(8000);
server_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
int sock;
if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1)
return;
if (bind(sock, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1)
return;
if (listen(sock, 1) == -1)
return;
server = dispatch_source_create(DISPATCH_SOURCE_TYPE_READ, sock, 0, dispatch_get_main_queue());
dispatch_source_set_event_handler(server, ^{
int client_sock;
struct sockaddr_in client_addr;
socklen_t size = sizeof(client_addr);
if ((client_sock = accept(sock, (struct sockaddr *)&client_addr, &size)) == -1)
return;
dispatch_io_t channel = dispatch_io_create(DISPATCH_IO_STREAM, client_sock, dispatch_get_main_queue(), ^(int error) {
close(client_sock);
});
dispatch_io_set_low_water(channel, 1);
dispatch_io_read(channel, 0, SIZE_MAX, dispatch_get_main_queue(), ^(bool done, dispatch_data_t data, int error) {
if (done && data == dispatch_data_empty)
return dispatch_io_close(channel, DISPATCH_IO_STOP);
if (data && dispatch_data_get_size(data) > 0) {
size_t length = 0;
const void *bytes = NULL;
dispatch_data_t mapped __attribute__((unused, objc_precise_lifetime)) = dispatch_data_create_map(data, &bytes, &length);
NSString *bundleIdentifier = [[NSString alloc] initWithBytes:bytes length:length encoding:NSUTF8StringEncoding];
bundleIdentifier = [bundleIdentifier stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
SBIconController *controller = [NSClassFromString(@"SBIconController") sharedInstance];
SBIcon *icon = [controller.model applicationIconForBundleIdentifier:bundleIdentifier];
if (icon == nil) {
NSLog(@"Application not found: %@", bundleIdentifier);
return;
}
[controller scrollToIconListContainingIcon:icon animate:NO];
if ([UIDevice.currentDevice.systemVersion floatValue] >= 9.2) {
SBIconView *iconView = [[controller homescreenIconViewMap] mappedIconViewForIcon:icon];
if ([UIDevice.currentDevice.systemVersion floatValue] >= 10.0) {
// iOS 10
SBUIForceTouchGestureRecognizer *recognizer = [iconView appIconForceTouchGestureRecognizer];
SBUIAppIconForceTouchController *forceTouch;
object_getInstanceVariable(controller, "_appIconForceTouchController", (void **)&forceTouch);
SBUIIconForceTouchController *ftController;
object_getInstanceVariable(forceTouch, "_iconForceTouchController", (void **)&ftController);
[forceTouch _setupWithGestureRecognizer:recognizer];
// or [ftController _setupWithGestureRecognizer:recognizer];
[ftController _presentAnimated:YES withCompletionHandler:nil];
} else {
// iOS 9.2/9.3
[controller _revealMenuForIconView:iconView];
[[controller presentedShortcutMenu] presentAnimated:YES];
}
} else {
SBIconView *iconView = [[NSClassFromString(@"SBIconViewMap") homescreenMap] iconViewForIcon:icon];
[controller _revealMenuForIconView:iconView presentImmediately:YES];
}
}
});
});
dispatch_source_set_cancel_handler(server, ^{
close(sock);
});
dispatch_resume(server);
}