This repository has been archived by the owner on May 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathRAHostedAppView.xm
457 lines (388 loc) · 16.3 KB
/
RAHostedAppView.xm
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
#import "RAHostedAppView.h"
#import "BioLockdown.h"
#import "RAHostManager.h"
#import "RAMessagingServer.h"
#import "RASnapshotProvider.h"
#import "RASpringBoardKeyboardActivation.h"
#import "Asphaleia2.h"
#import "dispatch_after_cancel.h"
NSMutableDictionary *appsBeingHosted = [NSMutableDictionary dictionary];
@interface RAHostedAppView () {
//NSTimer *verifyTimer;
BOOL isPreloading;
FBWindowContextHostManager *contextHostManager;
UIActivityIndicatorView *activityView;
UIImageView *splashScreenImageView;
UILabel *isForemostAppLabel;
UILabel *authenticationDidFailLabel;
UITapGestureRecognizer *authenticationFailedRetryTapGesture;
int startTries;
BOOL disablePreload;
NSTimer *loadedTimer;
}
@end
@implementation RAHostedAppView
-(id) initWithBundleIdentifier:(NSString*)bundleIdentifier
{
if (self = [super init])
{
self.bundleIdentifier = bundleIdentifier;
self.autosizesApp = NO;
self.allowHidingStatusBar = YES;
self.showSplashscreenInsteadOfSpinner = NO;
startTries = 0;
disablePreload = NO;
self.renderWallpaper = NO;
self.backgroundColor = [UIColor clearColor];
}
return self;
}
-(void) _preloadOrAttemptToUpdateReachabilityCounterpart
{
if (app)
{
if ([app mainScene])
{
isPreloading = NO;
if (((SBReachabilityManager*)[%c(SBReachabilityManager) sharedInstance]).reachabilityModeActive && [GET_SBWORKSPACE respondsToSelector:@selector(RA_updateViewSizes)])
[GET_SBWORKSPACE performSelector:@selector(RA_updateViewSizes) withObject:nil afterDelay:0.5]; // App is launched using ReachApp - animations commence. We have to wait for those animations to finish or this won't work.
}
else if (![app mainScene])
{
if (disablePreload)
disablePreload = NO;
else
[self preloadApp];
}
}
}
-(void) setBundleIdentifier:(NSString*)value
{
_orientation = UIInterfaceOrientationPortrait;
_bundleIdentifier = value;
app = [[%c(SBApplicationController) sharedInstance] RA_applicationWithBundleIdentifier:value];
}
-(void) setShouldUseExternalKeyboard:(BOOL)value
{
_shouldUseExternalKeyboard = value;
[RAMessagingServer.sharedInstance setShouldUseExternalKeyboard:value forApp:self.bundleIdentifier completion:nil];
}
-(void) preloadApp
{
startTries++;
if (startTries > 5)
{
isPreloading = NO;
NSLog(@"[ReachApp] maxed out preload attempts for app %@", app.bundleIdentifier);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:LOCALIZE(@"MULTIPLEXER") message:[NSString stringWithFormat:@"Unable to start app %@", app.displayName] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
return;
}
if (app == nil)
return;
if (_isCurrentlyHosting)
return;
isPreloading = YES;
FBScene *scene = [app mainScene];
if (![app pid] || scene == nil)
{
[UIApplication.sharedApplication launchApplicationWithIdentifier:self.bundleIdentifier suspended:YES];
[[%c(FBProcessManager) sharedInstance] createApplicationProcessForBundleID:self.bundleIdentifier]; // ummm...?
}
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.5 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{ [self _preloadOrAttemptToUpdateReachabilityCounterpart]; });
// this ^ runs either way. when _preloadOrAttemptToUpdateReachabilityCounterpart runs, if the app is "loaded" it will not call preloadApp again, otherwise
// it will call it again.
}
-(void) _actualLoadApp
{
if (isPreloading)
{
[self performSelector:@selector(_actualLoadApp) withObject:nil afterDelay:0.3];
return;
}
if (_isCurrentlyHosting)
return;
_isCurrentlyHosting = YES;
appsBeingHosted[app.bundleIdentifier] = [appsBeingHosted objectForKey:app.bundleIdentifier] ? @([appsBeingHosted[app.bundleIdentifier] intValue] + 1) : @1;
view = (FBWindowContextHostWrapperView*)[RAHostManager enabledHostViewForApplication:app];
contextHostManager = (FBWindowContextHostManager*)[RAHostManager hostManagerForApp:app];
view.backgroundColorWhileNotHosting = [UIColor clearColor];
view.backgroundColorWhileHosting = [UIColor clearColor];
view.frame = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height);
//view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self addSubview:view];
[RAMessagingServer.sharedInstance setHosted:YES forIdentifier:app.bundleIdentifier completion:nil];
//if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
[RAHostedAppView iPad_iOS83_fixHosting];
[RARunningAppsProvider.sharedInstance addTarget:self];
loadedTimer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(verifyHostingAndRehostIfNecessary) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:loadedTimer forMode:NSRunLoopCommonModes];
}
-(void) loadApp
{
startTries = 0;
disablePreload = NO;
[self preloadApp];
if (!app)
return;
if (_isCurrentlyHosting)
return;
if ([UIApplication.sharedApplication._accessibilityFrontMostApplication isEqual:app])
{
isForemostAppLabel = [[UILabel alloc] initWithFrame:self.bounds];
isForemostAppLabel.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.8];
isForemostAppLabel.textColor = [UIColor whiteColor];
isForemostAppLabel.textAlignment = NSTextAlignmentCenter;
isForemostAppLabel.font = [UIFont systemFontOfSize:36];
isForemostAppLabel.numberOfLines = 0;
isForemostAppLabel.lineBreakMode = NSLineBreakByWordWrapping;
isForemostAppLabel.text = [NSString stringWithFormat:LOCALIZE(@"ACTIVE_APP_WARNING"),self.app.displayName];
[self addSubview:isForemostAppLabel];
return;
}
IF_BIOLOCKDOWN {
id failedBlock = ^{
[self removeLoadingIndicator];
if (!authenticationDidFailLabel)
{
authenticationDidFailLabel = [[UILabel alloc] initWithFrame:self.bounds];
authenticationDidFailLabel.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.8];
authenticationDidFailLabel.textColor = [UIColor whiteColor];
authenticationDidFailLabel.textAlignment = NSTextAlignmentCenter;
authenticationDidFailLabel.font = [UIFont systemFontOfSize:36];
authenticationDidFailLabel.numberOfLines = 0;
authenticationDidFailLabel.lineBreakMode = NSLineBreakByWordWrapping;
authenticationDidFailLabel.text = [NSString stringWithFormat:LOCALIZE(@"BIOLOCKDOWN_AUTH_FAILED"),self.app.displayName];
[self addSubview:authenticationDidFailLabel];
authenticationFailedRetryTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(loadApp)];
[self addGestureRecognizer:authenticationFailedRetryTapGesture];
self.userInteractionEnabled = YES;
}
};
BIOLOCKDOWN_AUTHENTICATE_APP(app.bundleIdentifier, ^{
[self _actualLoadApp];
}, failedBlock /* stupid commas */);
}
else
{
IF_ASPHALEIA2 {
void (^failedBlock)() = ^{
[self removeLoadingIndicator];
if (!authenticationDidFailLabel)
{
authenticationDidFailLabel = [[UILabel alloc] initWithFrame:self.bounds];
authenticationDidFailLabel.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.8];
authenticationDidFailLabel.textColor = [UIColor whiteColor];
authenticationDidFailLabel.textAlignment = NSTextAlignmentCenter;
authenticationDidFailLabel.font = [UIFont systemFontOfSize:36];
authenticationDidFailLabel.numberOfLines = 0;
authenticationDidFailLabel.lineBreakMode = NSLineBreakByWordWrapping;
authenticationDidFailLabel.text = [NSString stringWithFormat:LOCALIZE(@"ASPHALEIA2_AUTH_FAILED"),self.app.displayName];
[self addSubview:authenticationDidFailLabel];
authenticationFailedRetryTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(loadApp)];
[self addGestureRecognizer:authenticationFailedRetryTapGesture];
self.userInteractionEnabled = YES;
}
};
ASPHALEIA2_AUTHENTICATE_APP(app.bundleIdentifier, ^{
[self _actualLoadApp];
}, failedBlock);
}
else
[self _actualLoadApp];
}
if (self.showSplashscreenInsteadOfSpinner)
{
if (splashScreenImageView)
{
[splashScreenImageView removeFromSuperview];
splashScreenImageView = nil;
}
UIImage *img = [RASnapshotProvider.sharedInstance snapshotForIdentifier:self.bundleIdentifier];
splashScreenImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)];
splashScreenImageView.image = img;
[self insertSubview:splashScreenImageView atIndex:0];
}
else
{
if (!activityView)
{
activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
[self addSubview:activityView];
}
CGFloat size = 50;
activityView.frame = CGRectMake((self.bounds.size.width - size) / 2, (self.bounds.size.height - size) / 2, size, size);
[activityView startAnimating];
}
}
-(void) verifyHostingAndRehostIfNecessary
{
if (!isPreloading && _isCurrentlyHosting && (app.isRunning == NO || view.contextHosted == NO)) // && (app.pid == 0 || view == nil || view.manager == nil)) // || view._isReallyHosting == NO))
{
//[activityView startAnimating];
[self unloadApp];
[self loadApp];
}
else
{
[self removeLoadingIndicator];
[loadedTimer invalidate];
loadedTimer = nil;
}
}
-(void) appDidDie:(SBApplication*)app_
{
if (app_ == self.app)
{
[self verifyHostingAndRehostIfNecessary];
}
}
-(void) removeLoadingIndicator
{
if (self.showSplashscreenInsteadOfSpinner)
{
[splashScreenImageView removeFromSuperview];
splashScreenImageView = nil;
}
else if (activityView)
[activityView stopAnimating];
}
-(void) drawRect:(CGRect)rect
{
if (_renderWallpaper)
[[RASnapshotProvider.sharedInstance wallpaperImage] drawInRect:rect];
}
-(void) setFrame:(CGRect)frame
{
[super setFrame:frame];
[view setFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
if (self.autosizesApp)
{
RAMessageAppData data = [RAMessagingServer.sharedInstance getDataForIdentifier:self.bundleIdentifier];
data.canHideStatusBarIfWanted = self.allowHidingStatusBar;
[RAMessagingServer.sharedInstance setData:data forIdentifier:self.bundleIdentifier];
[RAMessagingServer.sharedInstance resizeApp:self.bundleIdentifier toSize:CGSizeMake(frame.size.width, frame.size.height) completion:nil];
}
else if (self.bundleIdentifier)
{
[RAMessagingServer.sharedInstance endResizingApp:self.bundleIdentifier completion:nil];
}
}
-(void) setHideStatusBar:(BOOL)value
{
_hideStatusBar = value;
if (!self.bundleIdentifier)
return;
if (value)
[RAMessagingServer.sharedInstance forceStatusBarVisibility:!value forApp:self.bundleIdentifier completion:nil];
else
[RAMessagingServer.sharedInstance unforceStatusBarVisibilityForApp:self.bundleIdentifier completion:nil];
}
-(void) unloadApp
{
[self unloadApp:NO];
}
-(void) unloadApp:(BOOL)forceImmediate
{
//if (activityView)
// [activityView stopAnimating];
[self removeLoadingIndicator];
[loadedTimer invalidate];
loadedTimer = nil;
[RARunningAppsProvider.sharedInstance removeTarget:self];
disablePreload = YES;
if (_isCurrentlyHosting == NO)
return;
_isCurrentlyHosting = NO;
FBScene *scene = [app mainScene];
if (authenticationDidFailLabel)
{
[authenticationDidFailLabel removeFromSuperview];
authenticationDidFailLabel = nil;
[self removeGestureRecognizer:authenticationFailedRetryTapGesture];
self.userInteractionEnabled = NO;
}
if (isForemostAppLabel)
{
[isForemostAppLabel removeFromSuperview];
isForemostAppLabel = nil;
}
if ([RASpringBoardKeyboardActivation.sharedInstance.currentIdentifier isEqual:self.bundleIdentifier])
[RASpringBoardKeyboardActivation.sharedInstance hideKeyboard];
if (contextHostManager)
{
[contextHostManager disableHostingForRequester:@"reachapp"];
contextHostManager = nil;
}
//if ([UIApplication.sharedApplication._accessibilityFrontMostApplication isEqual:app])
// return;
__weak RAHostedAppView *weakSelf = self;
__block BOOL didRun = NO;
RAMessageCompletionCallback block = ^(BOOL success) {
if (didRun || (weakSelf && [UIApplication.sharedApplication._accessibilityFrontMostApplication isEqual:weakSelf.app]))
return;
if (!scene)
return;
appsBeingHosted[app.bundleIdentifier] = [appsBeingHosted objectForKey:app.bundleIdentifier] ? @([appsBeingHosted[app.bundleIdentifier] intValue] - 1) : @0;
if ([appsBeingHosted[app.bundleIdentifier] intValue] > 0)
return;
FBSMutableSceneSettings *settings = [[scene mutableSettings] mutableCopy];
SET_BACKGROUNDED(settings, YES);
[scene _applyMutableSettings:settings withTransitionContext:nil completion:nil];
//FBWindowContextHostManager *contextHostManager = [scene contextHostManager];
didRun = YES;
};
[RAMessagingServer.sharedInstance setHosted:NO forIdentifier:app.bundleIdentifier completion:nil];
[RAMessagingServer.sharedInstance unforceStatusBarVisibilityForApp:self.bundleIdentifier completion:nil];
[RAMessagingServer.sharedInstance unRotateApp:self.bundleIdentifier completion:nil];
if (forceImmediate)
{
[RAMessagingServer.sharedInstance endResizingApp:self.bundleIdentifier completion:nil];
block(YES);
}
else
{
// >Somewhere in the messaging server, the block is being removed from the waitingCompletions dictionary without being called.
// >This is a large issue (probably to do with asynchronous code) TODO: FIXME
// lol im retarded, it's the default empty callback the messaging server made
//[RAMessagingServer.sharedInstance unforceStatusBarVisibilityForApp:self.bundleIdentifier completion:block];
//[RAMessagingServer.sharedInstance unRotateApp:self.bundleIdentifier completion:block];
[RAMessagingServer.sharedInstance endResizingApp:self.bundleIdentifier completion:block];
}
}
-(void) rotateToOrientation:(UIInterfaceOrientation)o
{
_orientation = o;
[RAMessagingServer.sharedInstance rotateApp:self.bundleIdentifier toOrientation:o completion:nil];
}
+(void) iPad_iOS83_fixHosting
{
for (NSString *bundleIdentifier in appsBeingHosted.allKeys)
{
NSNumber *num = appsBeingHosted[bundleIdentifier];
if (num.intValue > 0)
{
SBApplication *app_ = [[%c(SBApplicationController) sharedInstance] RA_applicationWithBundleIdentifier:bundleIdentifier];
FBWindowContextHostManager *manager = (FBWindowContextHostManager*)[RAHostManager hostManagerForApp:app_];
if (manager)
{
NSLog(@"[ReachApp] rehosting for iPad: %@", bundleIdentifier);
[manager enableHostingForRequester:@"reachapp" priority:1];
}
}
}
}
// This allows for any subviews (with gestures) (e.g. the SwipeOver bar with a negative y origin) to recieve touch events.
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
BOOL isContained = NO;
for (UIView *subview in self.subviews)
{
if (CGRectContainsPoint(subview.frame, point)) // [self convertPoint:point toView:view]))
isContained = YES;
}
return isContained;
}
-(SBApplication*) app { return app; }
-(NSString*) displayName { return app.displayName; }
@end