Device battery indicators on your Lock Screen
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

176 líneas
5.0KB

  1. #import "Kai.h"
  2. %hook KAITarget //This class is defined in %ctor, KAITarget is not a class name.
  3. %property (nonatomic, assign) BOOL hasKai;
  4. -(void)_layoutStackView {
  5. NSInteger lastSlot = [[self stackView].subviews count] -1;
  6. //this code is used to determine if kai is at the bottom of the stack view
  7. if([[self stackView].subviews objectAtIndex:lastSlot] != [KAIBattery sharedInstance] && belowMusic) {
  8. //if it is not, but the option to have kai below music is on, i simply remove from it's current pos.
  9. //and insert into last slot.
  10. [[self stackView] removeArrangedSubview:[KAIBattery sharedInstance]];
  11. [[self stackView] insertArrangedSubview:[KAIBattery sharedInstance] atIndex:lastSlot];
  12. }
  13. //makes kai lay itself out when the stack does
  14. NSLog(@"kai: laying out stack view");
  15. [self KaiUpdate];
  16. %orig;
  17. }
  18. -(void)setStackView:(UIStackView *)arg1 {
  19. if(!KAISelf.hasKai) {
  20. KAIBattery *battery = [[KAIBattery alloc] init];
  21. //Add noti observer
  22. [[NSNotificationCenter defaultCenter] addObserver:self
  23. selector:@selector(KaiInfo)
  24. name:@"KaiInfoChanged"
  25. object:nil];
  26. KAISelf.hasKai = YES;
  27. if(![arg1.subviews containsObject:battery]) { //if not added
  28. //add kai to the stack view
  29. [arg1 addArrangedSubview:battery];
  30. }
  31. //send the adjusted stackview as arg1
  32. %orig(arg1);
  33. }
  34. }
  35. %new
  36. -(void)KaiUpdate {
  37. KAIBattery *battery = [KAIBattery sharedInstance];
  38. [UIView animateWithDuration:0.3 animations:^{
  39. if(!battery.heightConstraint) {
  40. battery.heightConstraint.active = NO;
  41. battery.heightConstraint = [battery.heightAnchor constraintEqualToConstant:85];
  42. //set an initial constraint
  43. battery.heightConstraint.active = YES;
  44. } else {
  45. int height = (battery.number * (bannerHeight + spacing)); //big brain math
  46. battery.heightConstraint.active = NO; //deactivation
  47. battery.heightConstraint.constant = height;
  48. battery.heightConstraint.active = YES; //forcing reactivation
  49. UIStackView *s = [self stackView];
  50. s.frame = CGRectMake(s.frame.origin.x, s.frame.origin.y, s.frame.size.width, (s.frame.size.height - 1));
  51. //literally does nothing but makes the stack view lay itself out (doesnt adjust frame because translatesAutoreszingMaskIntoConstraints = NO on stack views)
  52. }
  53. }];
  54. }
  55. %new
  56. -(void)KaiInfo {
  57. if(!isUpdating) {
  58. isUpdating = YES;
  59. //NSLog(@"kai: kai info will update");
  60. dispatch_async(dispatch_get_main_queue(), ^{
  61. [[KAIBattery sharedInstance] updateBattery];
  62. [self KaiUpdate];
  63. isUpdating = NO;
  64. });
  65. /*isUpdating = YES;
  66. [UIView animateWithDuration:0.3 animations:^{
  67. //nice fade out
  68. [KAIBattery sharedInstance].alpha = 0;
  69. } completion:^(BOOL finished){
  70. [[KAIBattery sharedInstance] updateBattery];
  71. [self KaiUpdate];
  72. [UIView animateWithDuration:0.35 animations:^{
  73. //fade back in
  74. [KAIBattery sharedInstance].alpha = 1;
  75. } completion:^(BOOL finished){
  76. isUpdating = NO;
  77. }];
  78. }];*/
  79. }
  80. }
  81. %end
  82. %hook BCBatteryDevice
  83. - (id)initWithIdentifier:(id)arg1 vendor:(long long)arg2 productIdentifier:(long long)arg3 parts:(unsigned long long)arg4 matchIdentifier:(id)arg5 {
  84. //Posts a notification to self when these keys change
  85. [self addObserver:self forKeyPath:@"charging" options:NSKeyValueObservingOptionNew context:nil];
  86. [self addObserver:self forKeyPath:@"batterySaverModeActive" options:NSKeyValueObservingOptionNew context:nil];
  87. [self addObserver:self forKeyPath:@"percentCharge" options:NSKeyValueObservingOptionNew context:nil];
  88. return %orig;
  89. }
  90. -(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
  91. dispatch_async(dispatch_get_main_queue(), ^{
  92. //sends the noti to update battery info
  93. [[NSNotificationCenter defaultCenter] postNotificationName:@"KaiInfoChanged" object:nil userInfo:nil];
  94. });
  95. }
  96. %end
  97. %hook KAICSTarget //Again, not a class
  98. -(void)_transitionChargingViewToVisible:(BOOL)arg1 showBattery:(BOOL)arg2 animated:(BOOL)arg3 {
  99. if(hideChargingAnimation) {
  100. //Yeah bro this just makes the method never call to show the charging thing
  101. %orig(NO,NO,NO);
  102. }
  103. }
  104. -(void)_transitionChargingViewToVisible:(BOOL)arg1 showBattery:(BOOL)arg2 animated:(BOOL)arg3 force:(BOOL)arg4 { //might just be ios12
  105. if(hideChargingAnimation) {
  106. //Same idea
  107. %orig(NO,NO,NO,NO);
  108. }
  109. }
  110. %end
  111. %ctor {
  112. preferencesChanged();
  113. CFNotificationCenterAddObserver(
  114. CFNotificationCenterGetDarwinNotifyCenter(),
  115. &observer,
  116. (CFNotificationCallback)applyPrefs,
  117. kSettingsChangedNotification,
  118. NULL,
  119. CFNotificationSuspensionBehaviorDeliverImmediately
  120. );
  121. //Bro Muirey helped me figure out a logical way to do this because iOS 12-13 classes have changed
  122. Class cls = kCFCoreFoundationVersionNumber > 1600 ? ([objc_getClass("CSAdjunctListView") class]) : ([objc_getClass("SBDashBoardAdjunctListView") class]);
  123. Class CSCls = kCFCoreFoundationVersionNumber > 1600 ? ([objc_getClass("CSCoverSheetViewController") class]) : ([objc_getClass("SBDashBoardViewController") class]);
  124. if(enabled) {
  125. %init(KAITarget = cls, KAICSTarget = CSCls); //BIG BRAIN BRO!!
  126. }
  127. }