You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

175 lines
6.4KB

  1. #define kBundlePath @"/Library/Application Support/AVLockBundle.bundle"
  2. @interface SBOrientationLockManager
  3. +(SBOrientationLockManager *)sharedInstance;
  4. -(bool)isUserLocked;
  5. -(void)lock;
  6. -(void)unlock;
  7. -(void)myIsUserLocked;
  8. @end
  9. @interface AVButton : UIView
  10. @end
  11. @interface AVTransportControlsView : UIView
  12. -(void)deviceOrientationDidChange;
  13. @property (assign, nonatomic) AVButton *skipBackButton;
  14. @end
  15. @interface AVPlaybackControlsView
  16. @property (assign, nonatomic) AVTransportControlsView *transportControlsView;
  17. @end
  18. @interface AVPlayerViewControllerContentView
  19. @property (assign, nonatomic) AVPlaybackControlsView *playbackControlsView;
  20. @end
  21. @interface AVFullScreenViewController
  22. @property (assign, nonatomic) AVPlayerViewControllerContentView *contentView;
  23. @end
  24. extern "C" CFNotificationCenterRef CFNotificationCenterGetDistributedCenter(void);
  25. static UIButton *button = nil;
  26. static NSString *myObserver=@"anObserver";
  27. static void toggle(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo){
  28. if([[%c(SBOrientationLockManager) sharedInstance] isUserLocked]){
  29. [[%c(SBOrientationLockManager) sharedInstance] unlock];
  30. }else{
  31. [[%c(SBOrientationLockManager) sharedInstance] lock];
  32. }
  33. }
  34. static void lockButton(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo){
  35. NSBundle *bundle = [[[NSBundle alloc] initWithPath:kBundlePath] autorelease];
  36. NSString *imagePath = [bundle pathForResource:@"Locked@3x" ofType:@"png"];
  37. UIImage *img = [UIImage imageWithContentsOfFile:imagePath];
  38. [button setImage:img forState:UIControlStateNormal];
  39. [button setImage:img forState:UIControlStateHighlighted];
  40. [button setImage:img forState:UIControlStateSelected];
  41. }
  42. static void unlockButton(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo){
  43. NSBundle *bundle = [[[NSBundle alloc] initWithPath:kBundlePath] autorelease];
  44. NSString *imagePath = [bundle pathForResource:@"Unlocked@3x" ofType:@"png"];
  45. UIImage *img = [UIImage imageWithContentsOfFile:imagePath];
  46. [button setImage:img forState:UIControlStateNormal];
  47. [button setImage:img forState:UIControlStateHighlighted];
  48. [button setImage:img forState:UIControlStateSelected];
  49. }
  50. static void firstUpdate(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo){
  51. [[%c(SBOrientationLockManager) sharedInstance] myIsUserLocked];
  52. }
  53. %hook AVFullScreenViewController
  54. -(void)viewDidLayoutSubviews{
  55. %orig;
  56. [self.contentView.playbackControlsView.transportControlsView deviceOrientationDidChange];
  57. }
  58. %end
  59. %hook SBOrientationLockManager
  60. -(SBOrientationLockManager*)init{
  61. self = %orig;
  62. CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(),
  63. (void*)myObserver,
  64. toggle,
  65. CFSTR("avlock.toggle"),
  66. NULL,
  67. CFNotificationSuspensionBehaviorDeliverImmediately);
  68. CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(),
  69. (void*)myObserver,
  70. firstUpdate,
  71. CFSTR("avlock.firstUpdate"),
  72. NULL,
  73. CFNotificationSuspensionBehaviorDeliverImmediately);
  74. return self;
  75. }
  76. %new
  77. -(void)myIsUserLocked{
  78. bool locked = [self isUserLocked];
  79. if(locked){
  80. CFNotificationCenterPostNotification(CFNotificationCenterGetDarwinNotifyCenter(), CFSTR("avlock.lockButton"), (void*)myObserver, NULL, true);
  81. }else{
  82. CFNotificationCenterPostNotification(CFNotificationCenterGetDarwinNotifyCenter(), CFSTR("avlock.unlockButton"), (void*)myObserver, NULL, true);
  83. }
  84. }
  85. -(void)unlock{
  86. %orig;
  87. CFNotificationCenterPostNotification(CFNotificationCenterGetDarwinNotifyCenter(), CFSTR("avlock.unlockButton"), (void*)myObserver, NULL, true);
  88. }
  89. -(void)lock{
  90. %orig;
  91. CFNotificationCenterPostNotification(CFNotificationCenterGetDarwinNotifyCenter(), CFSTR("avlock.lockButton"), (void*)myObserver, NULL, true);
  92. }
  93. %end
  94. %hook AVTransportControlsView
  95. -(AVTransportControlsView *)initWithFrame:(CGRect)frame{
  96. AVTransportControlsView *origself = %orig(frame);
  97. CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(),
  98. (void*)myObserver,
  99. lockButton,
  100. CFSTR("avlock.lockButton"),
  101. NULL,
  102. CFNotificationSuspensionBehaviorDeliverImmediately);
  103. CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(),
  104. (void*)myObserver,
  105. unlockButton,
  106. CFSTR("avlock.unlockButton"),
  107. NULL,
  108. CFNotificationSuspensionBehaviorDeliverImmediately);
  109. button = [UIButton buttonWithType:UIButtonTypeCustom];
  110. [button addTarget:self action:@selector(orientationButtonPressed) forControlEvents:UIControlEventTouchUpInside];
  111. CFNotificationCenterPostNotification(CFNotificationCenterGetDarwinNotifyCenter(), CFSTR("avlock.firstUpdate"), (void*)myObserver, NULL, true);
  112. button.contentMode = UIViewContentModeScaleAspectFit;
  113. button.imageEdgeInsets = UIEdgeInsetsMake(10, 10, 10, 10);
  114. [self addSubview:button];
  115. [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
  116. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceOrientationDidChange) name:UIDeviceOrientationDidChangeNotification object:nil];
  117. return origself;
  118. }
  119. -(void)dealloc{
  120. %orig;
  121. CFNotificationCenterRemoveObserver ( CFNotificationCenterGetDarwinNotifyCenter(), (void*)myObserver, NULL, NULL);
  122. }
  123. %new
  124. -(void)deviceOrientationDidChange{
  125. UIInterfaceOrientation orientation =[UIApplication sharedApplication].statusBarOrientation;
  126. if (orientation == UIInterfaceOrientationLandscapeRight || orientation == UIInterfaceOrientationLandscapeLeft){
  127. button.frame = CGRectMake(self.skipBackButton.frame.origin.x-13.5, -40, 46, 46);
  128. }
  129. else if(orientation == UIInterfaceOrientationPortrait){
  130. button.frame = CGRectMake((self.skipBackButton.frame.origin.x - 62), self.skipBackButton.frame.origin.y, 46, 46);
  131. }
  132. }
  133. %new
  134. -(void)orientationButtonPressed{
  135. CFNotificationCenterPostNotification(CFNotificationCenterGetDarwinNotifyCenter(), CFSTR("avlock.toggle"), (void*)myObserver, NULL, true);
  136. }
  137. %end