AssistantPlus/assistant+/AssistantHooks.xm

225 lines
6.6 KiB
Plaintext
Raw Normal View History

2015-03-14 18:24:48 +00:00
#import <Foundation/Foundation.h>
2015-03-14 21:47:16 +00:00
#import "assistantpluspluginmanager/AssistantPlusHeaders.h"
#import "assistantpluspluginmanager/AssistantHeaders.h"
2015-03-30 02:07:27 +00:00
#import "assistantpluspluginmanager/APPluginSystem.h"
2015-03-14 21:47:16 +00:00
#import "assistantpluspluginmanager/APSession.h"
2015-03-21 06:07:04 +00:00
#import "assistantpluspluginmanager/APSpringboardUtils.h"
2015-03-30 02:07:27 +00:00
#import <substrate.h>
2015-03-14 21:47:16 +00:00
2015-03-14 18:24:48 +00:00
@protocol SAAceSerializable <NSObject>
@end
@interface SBUIPluginController : NSObject
@end
@interface SAUIAppPunchOut : NSObject
@end
@interface SAUIConfirmationOptions : NSObject
@end
static BOOL defaultHandling = YES;
2015-03-15 20:51:49 +00:00
static AFConnection *currConnection;
2015-03-30 02:07:27 +00:00
static APPluginSystem *pluginManager;
static BOOL hasLoadedSnippets = NO;
2015-03-15 20:51:49 +00:00
BOOL shouldHandleRequest(NSString *text, APSession *currSession) {
2015-03-30 02:07:27 +00:00
pluginManager = [[%c(APSpringboardUtils) sharedAPUtils] getPluginManager];
2015-03-21 06:07:04 +00:00
NSLog(@"Manager: %@", pluginManager);
NSSet *tokens = [NSSet setWithArray:[text componentsSeparatedByString: @" "]];
return [pluginManager handleCommand:text withTokens:tokens withSession:currSession];
2015-03-15 20:51:49 +00:00
}
2015-03-14 18:24:48 +00:00
%hook BasicAceContext
2015-03-15 20:51:49 +00:00
- (Class)classWithClassName:(NSString*)name group:(NSString*)group {
id r;
2015-03-15 20:51:49 +00:00
if ([name isEqualToString:@"SnippetObject"] && [group isEqualToString:@"zaid.assistantplus.plugin"]) {
r = NSClassFromString(@"SAUISnippet");
} else {
r = %orig;
2015-03-15 20:51:49 +00:00
}
return r;
}
%end
2015-03-14 18:24:48 +00:00
2015-03-21 06:07:04 +00:00
%hook SiriUISnippetViewController
-(void)setSnippet:(id)arg1 {
%log;
%orig;
}
%end
2015-03-15 20:51:49 +00:00
%hook SiriUIPluginManager
2015-03-14 18:24:48 +00:00
2015-03-15 20:51:49 +00:00
- (id)transcriptItemForObject:(AceObject*)arg1 {
2015-03-30 02:07:27 +00:00
if (!hasLoadedSnippets) {
[self loadAssistantPlusSnippets];
}
2015-03-15 20:51:49 +00:00
NSDictionary *properties = [arg1 properties];
if (properties) {
NSString *className = properties[@"snippetClass"];
if (className) {
NSLog(@"AP: Looking for custom snippet: %@", className);
2015-03-30 02:07:27 +00:00
NSLog(@"Attempt to get: %@", NSClassFromString(className));
2015-03-21 06:07:04 +00:00
id<APPluginSnippet> customClass = [[NSClassFromString(className) alloc] initWithProperties:properties[@"snippetProps"]];
if ([customClass respondsToSelector:@selector(view)]) {
2015-03-30 02:07:27 +00:00
NSLog(@"Custom class: %@", customClass);
2015-03-21 06:07:04 +00:00
UIViewController *customVC = (UIViewController*)customClass;
2015-03-15 20:51:49 +00:00
SiriUISnippetViewController *vc = [[%c(SiriUISnippetViewController) alloc] init];
object_setClass(vc, [%c(APPluginSnippetViewController) class]);
[(APPluginSnippetViewController*)vc setCustomView:customVC];
SiriUITranscriptItem *item = [%c(SiriUITranscriptItem) transcriptItemWithAceObject:arg1];
item.viewController = vc;
return item;
} else {
NSLog(@"AP ERROR: %@ did not respond to customView", className);
2015-03-15 20:51:49 +00:00
}
} else {
NSLog(@"AP: No custom class for snippet, going to default!");
2015-03-15 20:51:49 +00:00
}
} else {
NSLog(@"AP ERROR: No properties for snippet, this shouldn't hapepn...");
2015-03-15 20:51:49 +00:00
}
2015-03-30 02:07:27 +00:00
id r = %orig(arg1);
2015-03-15 20:51:49 +00:00
return r;
}
2015-03-14 18:24:48 +00:00
2015-03-30 02:07:27 +00:00
%new
- (void)loadAssistantPlusSnippets {
hasLoadedSnippets = YES;
NSURL *directoryPath = [NSURL URLWithString:@PLUGIN_PATH];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *contents = [fileManager contentsOfDirectoryAtURL:directoryPath
includingPropertiesForKeys:@[]
options:NSDirectoryEnumerationSkipsHiddenFiles
error:nil];
NSLog(@"SiriUIPluginManager: Plugins:");
for (NSURL *fileURL in contents) {
NSString *name = [[[fileURL absoluteString] lastPathComponent] stringByDeletingPathExtension];
NSLog(@"Loading %@ at %@", name, fileURL);
NSBundle *bundle = [NSBundle bundleWithURL:fileURL];
if (!bundle) {
NSLog(@"Failed to open extension bundle %@ (%@)!", fileURL, fileURL);
continue;
}
if (![bundle load]) {
NSLog(@"Failed to load extension bundle %@ (wrong CFBundleExecutable? Missing? Not signed?)!", name);
continue;
} else {
NSLog(@"Loaded bundle!");
}
}
}
2015-03-14 18:24:48 +00:00
2015-03-15 20:51:49 +00:00
%end
2015-03-14 18:24:48 +00:00
%hook AFConnection
- (void)_doCommand:(SAUIAddViews*)arg1 reply:(id)arg2 {
2015-03-30 02:07:27 +00:00
NSLog(@"Doing: %@ with reply: %@", arg1, arg2);
2015-03-15 20:51:49 +00:00
if ([arg1 respondsToSelector:@selector(views)]) {
NSLog(@"Views: %@", arg1.views);
}
2015-03-14 18:24:48 +00:00
%log;
%orig;
}
- (void)startRequestWithCorrectedText:(NSString*)text forSpeechIdentifier:(id)arg2 {
NSLog(@"AP: Starting request with corrected text: %@", text);
2015-03-30 02:07:27 +00:00
APSession *currSession = [APSession sessionWithConnection:self];
if (shouldHandleRequest(text, currSession)) {
NSLog(@"Handling!");
} else {
%orig;
}
}
2015-03-14 18:24:48 +00:00
- (void)startAcousticIDRequestWithOptions:(id)arg1 { %log; %orig; }
- (void)startSpeechPronunciationRequestWithOptions:(id)arg1 pronunciationContext:(id)arg2 { %log; %orig; }
- (void)startSpeechRequestWithOptions:(id)arg1 {
NSLog(@"%@", arg1);
%log;
%orig;
}
- (void)startContinuationRequestWithUserInfo:(id)arg1 { %log; %orig; }
- (void)startDirectActionRequestWithString:(id)arg1 { %log; %orig; }
- (void)startRequestWithText:(NSString*)text {
NSLog(@"AP: Starting request with text: %@", text);
2015-03-30 02:07:27 +00:00
APSession *currSession = [APSession sessionWithConnection:self];
if (shouldHandleRequest(text, currSession)) {
2015-03-14 18:24:48 +00:00
NSLog(@"Handling!");
} else {
NSLog(@"Default!");
2015-03-14 21:47:16 +00:00
%orig;
2015-03-14 18:24:48 +00:00
}
}
%end
2015-03-21 06:07:04 +00:00
%hook AFUISiriSession
- (void)_requestContextWithCompletion:(id)arg1 {
NSLog(@"%@", arg1);
%log;
%orig;
}
- (void)_requestDidFinishWithError:(id)arg1 {
NSLog(@"%@", arg1);
%log;
%orig;
}
- (void)assistantConnectionRequestFinished:(id)arg1 {
NSLog(@"%@", arg1);
%log;
%orig;
}
- (void)end {
%log;
%orig;
}
%end
2015-03-14 18:24:48 +00:00
%hook AFConnectionClientServiceDelegate
- (void)speechRecognized:(SASSpeechRecognized*)arg1 {
NSMutableString *phraseBuilder = [NSMutableString string];
for (AFSpeechPhrase *currPhrase in arg1.recognition.phrases) {
if (currPhrase.interpretations.count > 0) {
SASInterpretation *currInterpretation = currPhrase.interpretations[0];
if (currInterpretation.tokens.count > 0) {
AFSpeechToken *currToken = currInterpretation.tokens[0];
[phraseBuilder appendString:[NSString stringWithFormat:@"%@ ", currToken.text]];
}
}
}
NSLog(@"AP Starting Speech Query: %@", phraseBuilder);
2015-03-14 18:24:48 +00:00
2015-03-21 06:07:04 +00:00
AFConnection *connection = MSHookIvar<AFConnection*>(self, "_connection");
2015-03-15 20:51:49 +00:00
2015-03-30 02:07:27 +00:00
APSession *currSession = [APSession sessionWithConnection:connection];
if (shouldHandleRequest(phraseBuilder, currSession)) {
2015-03-15 20:51:49 +00:00
defaultHandling = NO;
NSLog(@"Handling with plugin!");
} else {
defaultHandling = YES;
2015-03-15 20:51:49 +00:00
NSLog(@"Going to default!");
%orig;
2015-03-15 20:51:49 +00:00
}
2015-03-14 18:24:48 +00:00
}
- (void)requestDidFinish{ %log; %orig; }
- (void)requestDidReceiveCommand:(id)arg1 reply:(CDUnknownBlockType*)arg2 {
%log;
if (defaultHandling) {
%orig;
}
}
%end