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.

183 lines
6.9KB

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