Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a background serial queue and run RPC notifications on it #813

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions SmartDeviceLink/SDLLockScreenPresenter.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,36 @@ - (void)present {
return;
}

[[self.class sdl_getCurrentViewController] presentViewController:self.viewController animated:YES completion:nil];
dispatch_async(dispatch_get_main_queue(), ^{
[[self.class sdl_getCurrentViewController] presentViewController:self.viewController animated:YES completion:nil];
});
}

- (void)dismiss {
if (!self.viewController) {
return;
}

[self.viewController.presentingViewController dismissViewControllerAnimated:YES completion:nil];
dispatch_async(dispatch_get_main_queue(), ^{
[self.viewController.presentingViewController dismissViewControllerAnimated:YES completion:nil];
});
}

- (BOOL)presented {
if (!self.viewController) {
return NO;
}

return (self.viewController.isViewLoaded && (self.viewController.view.window || self.viewController.isBeingPresented));
__block BOOL presented = NO;
if (![NSThread isMainThread]) {
dispatch_sync(dispatch_get_main_queue(), ^{
presented = (self.viewController.isViewLoaded && (self.viewController.view.window || self.viewController.isBeingPresented));
});
} else {
presented = (self.viewController.isViewLoaded && (self.viewController.view.window || self.viewController.isBeingPresented));
}

return presented;
}

+ (UIViewController *)sdl_getCurrentViewController {
Expand Down
64 changes: 33 additions & 31 deletions SmartDeviceLink/SDLLockScreenViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -71,37 +71,39 @@ - (void)setBackgroundColor:(UIColor *_Nullable)backgroundColor {
#pragma mark - Layout

- (void)sdl_layoutViews {
UIColor *iconColor = [self.class sdl_accentColorBasedOnColor:self.backgroundColor];

self.sdlIconImageView.image = [self.class sdl_imageWithName:@"sdl_logo_black"];
self.sdlIconImageView.tintColor = iconColor;

self.arrowUpImageView.image = [self.class sdl_imageWithName:@"lock_arrow_up_black"];
self.arrowUpImageView.tintColor = iconColor;

self.arrowDownImageView.image = [self.class sdl_imageWithName:@"lock_arrow_down_black"];
self.arrowDownImageView.tintColor = iconColor;

self.lockedLabel.textColor = iconColor;

if (self.vehicleIcon != nil && self.appIcon != nil) {
[self sdl_setVehicleAndAppIconsLayout];
} else if (self.vehicleIcon != nil) {
[self sdl_setVehicleIconOnlyLayout];
} else if (self.appIcon != nil) {
[self sdl_setAppIconOnlyLayout];
} else {
[self sdl_setNoIconsLayout];
}

// HAX: The autolayout doesn't scale for 4s, so hide a view so it doesn't look like garbage.
if (CGRectGetHeight([UIScreen mainScreen].bounds) == 480) {
self.sdlIconImageView.hidden = YES;
} else {
self.sdlIconImageView.hidden = NO;
}

[self.view layoutIfNeeded];
dispatch_async(dispatch_get_main_queue(), ^{
UIColor *iconColor = [self.class sdl_accentColorBasedOnColor:self.backgroundColor];

self.sdlIconImageView.image = [self.class sdl_imageWithName:@"sdl_logo_black"];
self.sdlIconImageView.tintColor = iconColor;

self.arrowUpImageView.image = [self.class sdl_imageWithName:@"lock_arrow_up_black"];
self.arrowUpImageView.tintColor = iconColor;

self.arrowDownImageView.image = [self.class sdl_imageWithName:@"lock_arrow_down_black"];
self.arrowDownImageView.tintColor = iconColor;

self.lockedLabel.textColor = iconColor;

if (self.vehicleIcon != nil && self.appIcon != nil) {
[self sdl_setVehicleAndAppIconsLayout];
} else if (self.vehicleIcon != nil) {
[self sdl_setVehicleIconOnlyLayout];
} else if (self.appIcon != nil) {
[self sdl_setAppIconOnlyLayout];
} else {
[self sdl_setNoIconsLayout];
}

// HAX: The autolayout doesn't scale for 4s, so hide a view so it doesn't look like garbage.
if (CGRectGetHeight([UIScreen mainScreen].bounds) == 480) {
self.sdlIconImageView.hidden = YES;
} else {
self.sdlIconImageView.hidden = NO;
}

[self.view layoutIfNeeded];
});
}

- (void)sdl_setVehicleAndAppIconsLayout {
Expand Down
2 changes: 2 additions & 0 deletions SmartDeviceLink/SDLNotificationConstants.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

#import "SDLNotificationConstants.h"

/// These notifications will be returned on a background serial queue

SDLNotificationUserInfoKey const SDLNotificationUserInfoObject = @"SDLNotificationUserInfoObject";


Expand Down
1 change: 0 additions & 1 deletion SmartDeviceLink/SDLNotificationDispatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ NS_ASSUME_NONNULL_BEGIN
* @param info The object to be send along in the `userInfo` dictionary.
*/
- (void)postNotificationName:(NSString *)name infoObject:(nullable id)info;

- (void)postRPCResponseNotification:(NSString *)name response:(__kindof SDLRPCResponse *)response;
- (void)postRPCNotificationNotification:(NSString *)name notification:(__kindof SDLRPCNotification *)rpcNotification;

Expand Down
30 changes: 18 additions & 12 deletions SmartDeviceLink/SDLNotificationDispatcher.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,35 @@

@implementation SDLNotificationDispatcher

- (instancetype)init {
self = [super init];
if (!self) { return nil; }

return self;
}

- (void)postNotificationName:(NSString *)name infoObject:(nullable id)infoObject {
NSDictionary<NSString *, id> *userInfo = nil;
if (infoObject != nil) {
userInfo = @{SDLNotificationUserInfoObject: infoObject};
}

dispatch_async(dispatch_get_main_queue(), ^{
[[NSNotificationCenter defaultCenter] postNotificationName:name object:self userInfo:userInfo];
});

// Runs on `com.sdl.rpcProcessingQueue`
[[NSNotificationCenter defaultCenter] postNotificationName:name object:self userInfo:userInfo];
}

- (void)postRPCResponseNotification:(NSString *)name response:(__kindof SDLRPCResponse *)response {
dispatch_async(dispatch_get_main_queue(), ^{
SDLRPCResponseNotification *notification = [[SDLRPCResponseNotification alloc] initWithName:name object:self rpcResponse:response];
[[NSNotificationCenter defaultCenter] postNotification:notification];
});
SDLRPCResponseNotification *notification = [[SDLRPCResponseNotification alloc] initWithName:name object:self rpcResponse:response];

// Runs on `com.sdl.rpcProcessingQueue`
[[NSNotificationCenter defaultCenter] postNotification:notification];
}

- (void)postRPCNotificationNotification:(NSString *)name notification:(__kindof SDLRPCNotification *)rpcNotification {
dispatch_async(dispatch_get_main_queue(), ^{
SDLRPCNotificationNotification *notification = [[SDLRPCNotificationNotification alloc] initWithName:name object:self rpcNotification:rpcNotification];
[[NSNotificationCenter defaultCenter] postNotification:notification];
});
SDLRPCNotificationNotification *notification = [[SDLRPCNotificationNotification alloc] initWithName:name object:self rpcNotification:rpcNotification];

// Runs on `com.sdl.rpcProcessingQueue`
[[NSNotificationCenter defaultCenter] postNotification:notification];
}

#pragma mark - SDLProxyListener Delegate Methods
Expand Down
15 changes: 8 additions & 7 deletions SmartDeviceLink/SDLProxy.m
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ @interface SDLProxy () {
@property (nullable, nonatomic, strong) SDLDisplayCapabilities *displayCapabilities;
@property (nonatomic, strong) NSMutableDictionary<SDLVehicleMake *, Class> *securityManagers;
@property (nonatomic, strong) NSURLSession* urlSession;
@property (strong, nonatomic) dispatch_queue_t rpcProcessingQueue;

@end

Expand All @@ -68,6 +69,7 @@ - (instancetype)initWithTransport:(SDLAbstractTransport *)transport protocol:(SD
SDLLogD(@"Framework Version: %@", self.proxyVersion);
_debugConsoleGroupName = @"default";
_lsm = [[SDLLockScreenStatusManager alloc] init];
_rpcProcessingQueue = dispatch_queue_create("com.sdl.rpcProcessingQueue", DISPATCH_QUEUE_SERIAL);

_mutableProxyListeners = [NSMutableSet setWithObject:theDelegate];
_securityManagers = [NSMutableDictionary dictionary];
Expand Down Expand Up @@ -629,13 +631,12 @@ - (void)removeDelegate:(NSObject<SDLProxyListener> *)delegate {
}

- (void)invokeMethodOnDelegates:(SEL)aSelector withObject:(nullable id)object {
dispatch_async(dispatch_get_main_queue(), ^{
@autoreleasepool {
for (id<SDLProxyListener> listener in self.proxyListeners) {
if ([listener respondsToSelector:aSelector]) {
// HAX: http://stackoverflow.com/questions/7017281/performselector-may-cause-a-leak-because-its-selector-is-unknown
((void (*)(id, SEL, id))[(NSObject *)listener methodForSelector:aSelector])(listener, aSelector, object);
}
// Occurs on the protocol receive serial queue
dispatch_async(_rpcProcessingQueue, ^{
for (id<SDLProxyListener> listener in self.proxyListeners) {
if ([listener respondsToSelector:aSelector]) {
// HAX: http://stackoverflow.com/questions/7017281/performselector-may-cause-a-leak-because-its-selector-is-unknown
((void (*)(id, SEL, id))[(NSObject *)listener methodForSelector:aSelector])(listener, aSelector, object);
}
}
});
Expand Down