Skip to content

Commit

Permalink
dismiss carplay-hosted windows if the carplay screen becomes unavailable
Browse files Browse the repository at this point in the history
  • Loading branch information
EthanArbuckle committed Jan 6, 2021
1 parent f52c88f commit 2bd47c9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/CRCarplayWindow.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include <UIKit/UIKit.h>

id getCarplayCADisplay(void);

@interface CRCarPlayWindow : NSObject

@property (nonatomic, retain) UIWindow *rootWindow;
Expand Down
24 changes: 22 additions & 2 deletions src/hooks/SpringBoard.xm
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ When an app icon is tapped on the Carplay dashboard
{
objcInvoke(liveCarplayWindow, @"dismiss");
}

// Launch the requested app
liveCarplayWindow = [[CRCarPlayWindow alloc] initWithBundleIdentifier:identifier];
objc_setAssociatedObject(self, &kPropertyKey_liveCarplayWindow, liveCarplayWindow, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
Expand All @@ -85,12 +85,15 @@ Invoked when SpringBoard finishes launching
// Setup to receive App Launch notifications from the CarPlay process
[[objc_getClass("NSDistributedNotificationCenter") defaultCenter] addObserver:self selector:NSSelectorFromString(@"handleCarPlayLaunchNotification:") name:@"com.carplayenable" object:nil];

// Receive notifications for Carplay connect/disconnect events. When a Carplay screen becomes unavailable while an app is being hosted on it, that app window needs to be closed
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(uiscreenDidDisconnect) name:@"CarPlayIsConnectedDidChange" object:nil];

NSMutableArray *appIdentifiersToIgnoreLockAssertions = [[NSMutableArray alloc] init];
objc_setAssociatedObject(self, &kPropertyKey_lockAssertionIdentifiers, appIdentifiersToIgnoreLockAssertions, OBJC_ASSOCIATION_RETAIN_NONATOMIC);

%orig;

// Upload any relevant crashlogs
// Upload any relevant crashlogs
symbolicateAndUploadCrashlogs();
}

Expand All @@ -100,6 +103,23 @@ Invoked when SpringBoard finishes launching
return objc_getAssociatedObject(self, &kPropertyKey_liveCarplayWindow);
}

/*
A Carplay connected/disconnected event
*/
%new
- (void)uiscreenDidDisconnect
{
LOG_LIFECYCLE_EVENT;
// If a window is being hosted, and the carplay UIScreen disconnected, close the window
id liveCarplayWindow = objcInvoke(self, @"liveCarplayWindow");
if (liveCarplayWindow && !getCarplayCADisplay())
{
dispatch_sync(dispatch_get_main_queue(), ^(void) {
objcInvoke(liveCarplayWindow, @"dismiss");
});
}
}

%end

%hook SBSceneView
Expand Down

0 comments on commit 2bd47c9

Please sign in to comment.