Device battery indicators on your Lock Screen
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.

227 lines
7.7KB

  1. #import "Kai.h"
  2. %group main
  3. %hook KAITarget //This class is defined in %ctor, KAITarget is not a class name.
  4. %property (nonatomic, assign) BOOL hasKai;
  5. -(void)_layoutStackView {
  6. NSInteger lastSlot = [[self stackView].subviews count] -1;
  7. //this code is used to determine if kai is at the bottom of the stack view
  8. if([[self stackView].subviews objectAtIndex:lastSlot] != [KAIBatteryStack sharedInstance] && belowMusic) {
  9. //if it is not, but the option to have kai below music is on, i simply remove from it's current pos.
  10. //and insert into last slot.
  11. [[self stackView] removeArrangedSubview:[KAIBatteryStack sharedInstance]];
  12. [[self stackView] insertArrangedSubview:[KAIBatteryStack sharedInstance] atIndex:lastSlot];
  13. }
  14. //makes kai lay itself out when the stack does
  15. NSLog(@"kai: laying out stack view");
  16. [self KaiUpdate];
  17. %orig;
  18. }
  19. -(void)setStackView:(UIStackView *)arg1 {
  20. if(!KAISelf.hasKai) {
  21. KAIBatteryStack *battery = [[KAIBatteryStack alloc] init];
  22. //Add noti observer
  23. [[NSNotificationCenter defaultCenter] addObserver:self
  24. selector:@selector(KaiInfo)
  25. name:@"KaiInfoChanged"
  26. object:nil];
  27. KAISelf.hasKai = YES;
  28. if(![arg1.subviews containsObject:battery]) { //if not added
  29. //add kai to the stack view
  30. [arg1 addArrangedSubview:battery];
  31. }
  32. [battery updateBattery];
  33. //send the adjusted stackview as arg1
  34. %orig(arg1);
  35. }
  36. }
  37. %new
  38. -(void)KaiUpdate {
  39. KAIBatteryStack *battery = [KAIBatteryStack sharedInstance];
  40. //battery.number = [battery.subviews count];
  41. BCBatteryDeviceController *bcb = [BCBatteryDeviceController sharedInstance];
  42. NSArray *devices = MSHookIvar<NSArray *>(bcb, "_sortedDevices");
  43. for(KAIBatteryCell *cell in battery.subviews) {
  44. //BCBatteryDevice *device = cell.device;
  45. [cell updateInfo];
  46. if(![devices containsObject:cell.device]) {
  47. [UIView animateWithDuration:0.3 animations:^{
  48. cell.alpha = 0;
  49. } completion:^(BOOL finished) {
  50. [cell removeFromSuperview];
  51. [battery removeArrangedSubview:cell];
  52. cell.alpha = 1;
  53. }];
  54. }
  55. }
  56. [UIView animateWithDuration:0.3 animations:^{
  57. if(!battery.heightConstraint) {
  58. battery.heightConstraint.active = NO;
  59. battery.heightConstraint = [battery.heightAnchor constraintEqualToConstant:85];
  60. //set an initial constraint
  61. battery.heightConstraint.active = YES;
  62. } else {
  63. int height = (battery.number * (bannerHeight + spacing)); //big brain math
  64. //battery.heightConstraint.active = NO; //deactivation
  65. battery.heightConstraint.constant = height;
  66. //battery.heightConstraint.active = YES; //forcing reactivation
  67. UIStackView *s = [self stackView];
  68. s.frame = CGRectMake(s.frame.origin.x, s.frame.origin.y, s.frame.size.width, (s.frame.size.height - 1));
  69. //literally does nothing but makes the stack view lay itself out (doesnt adjust frame because translatesAutoreszingMaskIntoConstraints = NO on stack views)
  70. }
  71. }];
  72. }
  73. %new
  74. -(void)KaiInfo {
  75. if(!isUpdating) {
  76. isUpdating = YES;
  77. //NSLog(@"kai: kai info will update");
  78. dispatch_async(dispatch_get_main_queue(), ^{
  79. [[KAIBatteryStack sharedInstance] updateBattery];
  80. [self KaiUpdate];
  81. isUpdating = NO;
  82. });
  83. }
  84. }
  85. %end
  86. %hook BCBatteryDevice
  87. %property (nonatomic, strong) KAIBatteryCell *kaiCell;
  88. - (id)initWithIdentifier:(id)arg1 vendor:(long long)arg2 productIdentifier:(long long)arg3 parts:(unsigned long long)arg4 matchIdentifier:(id)arg5 {
  89. //Posts a notification to self when these keys change
  90. [self addObserver:self forKeyPath:@"charging" options:NSKeyValueObservingOptionNew context:nil];
  91. [self addObserver:self forKeyPath:@"batterySaverModeActive" options:NSKeyValueObservingOptionNew context:nil];
  92. [self addObserver:self forKeyPath:@"percentCharge" options:NSKeyValueObservingOptionNew context:nil];
  93. return %orig;
  94. }
  95. -(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
  96. //if([self isMemberOfClass:[objc_getClass("BCBatteryDevice") class]] && [self respondsToSelector:@selector(_kaiCell)] && object == self && ([keyPath isEqualToString:@"charging"] || [keyPath isEqualToString:@"percentCharge"] || [keyPath isEqualToString:@"batterySaverModeActive"])) {
  97. //sends the noti to update battery info
  98. [[NSNotificationCenter defaultCenter] postNotificationName:@"KaiInfoChanged" object:nil userInfo:nil];
  99. //}
  100. }
  101. %new
  102. -(id)kaiCellForDevice {
  103. if(self && self.kaiCell == nil) {
  104. self.kaiCell = [[KAIBatteryCell alloc] initWithFrame:CGRectMake(0,0,[KAIBatteryStack sharedInstance].frame.size.width,0) device:self]; }
  105. ((KAIBatteryCell *)self.kaiCell).translatesAutoresizingMaskIntoConstraints = NO;
  106. [((KAIBatteryCell *)self.kaiCell).heightAnchor constraintEqualToConstant:bannerHeight + spacing].active = YES;
  107. [(KAIBatteryCell *)self.kaiCell updateInfo];
  108. return self.kaiCell;
  109. }
  110. %new
  111. -(void)resetKaiCellForNewPrefs {
  112. self.kaiCell = [[KAIBatteryCell alloc] initWithFrame:CGRectMake(0,0,[KAIBatteryStack sharedInstance].frame.size.width,0) device:self];
  113. ((KAIBatteryCell *)self.kaiCell).translatesAutoresizingMaskIntoConstraints = NO;
  114. [((KAIBatteryCell *)self.kaiCell).heightAnchor constraintEqualToConstant:bannerHeight + spacing].active = YES;
  115. [(KAIBatteryCell *)self.kaiCell updateInfo];
  116. }
  117. %end
  118. %hook KAICSTarget //Again, not a class
  119. -(void)_transitionChargingViewToVisible:(BOOL)arg1 showBattery:(BOOL)arg2 animated:(BOOL)arg3 {
  120. if(hideChargingAnimation) {
  121. //Yeah bro this just makes the method never call to show the charging thing
  122. %orig(NO,NO,NO);
  123. }
  124. }
  125. -(void)_transitionChargingViewToVisible:(BOOL)arg1 showBattery:(BOOL)arg2 animated:(BOOL)arg3 force:(BOOL)arg4 { //might just be ios12
  126. if(hideChargingAnimation) {
  127. //Same idea
  128. %orig(NO,NO,NO,NO);
  129. }
  130. }
  131. %end
  132. %end
  133. %group drm
  134. %hook SBIconController
  135. -(void)viewDidAppear:(BOOL)arg1 {
  136. UIAlertController* alert2 = [UIAlertController alertControllerWithTitle:@"Unauthorized"
  137. message:@"At this time, only paying users of Multipla have access to kai beta. If you are interested in getting access to kai beta, you can purchase and install Multipla from Chariz."
  138. preferredStyle:UIAlertControllerStyleAlert];
  139. UIAlertAction* yes = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
  140. handler:^(UIAlertAction * action) {
  141. }];
  142. UIAlertAction* buy = [UIAlertAction actionWithTitle:@"Buy Multipla" style:UIAlertActionStyleDefault
  143. handler:^(UIAlertAction * action) {
  144. [[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"https://chariz.com/buy/multipla"]];
  145. }];
  146. [alert2 addAction:yes];
  147. [alert2 addAction:buy];
  148. [self presentViewController:alert2 animated:YES completion:nil];
  149. }
  150. %end
  151. %end
  152. %ctor {
  153. preferencesChanged();
  154. CFNotificationCenterAddObserver(
  155. CFNotificationCenterGetDarwinNotifyCenter(),
  156. &observer,
  157. (CFNotificationCallback)applyPrefs,
  158. kSettingsChangedNotification,
  159. NULL,
  160. CFNotificationSuspensionBehaviorDeliverImmediately
  161. );
  162. //Bro Muirey helped me figure out a logical way to do this because iOS 12-13 classes have changed
  163. Class cls = kCFCoreFoundationVersionNumber > 1600 ? ([objc_getClass("CSAdjunctListView") class]) : ([objc_getClass("SBDashBoardAdjunctListView") class]);
  164. Class CSCls = kCFCoreFoundationVersionNumber > 1600 ? ([objc_getClass("CSCoverSheetViewController") class]) : ([objc_getClass("SBDashBoardViewController") class]);
  165. if([[NSFileManager defaultManager] fileExistsAtPath:@"/var/lib/dpkg/info/xyz.burritoz.thomz.multipla.list"] && [[NSFileManager defaultManager] fileExistsAtPath:@"/var/lib/dpkg/info/xyz.burritoz.thomz.multipla.md5sums"] && enabled) {
  166. %init(main, KAITarget = cls, KAICSTarget = CSCls); //BIG BRAIN BRO!!
  167. } else {
  168. %init(drm);
  169. }
  170. }