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.

157 lines
6.2KB

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