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.

103 lines
3.7KB

  1. #import "KAIBattery.h"
  2. KAIBattery *instance;
  3. @implementation KAIBattery
  4. -(instancetype)init {
  5. self = [super init];
  6. instance = self;
  7. if (self) {
  8. self.displayingDevices = [[NSMutableArray alloc] init];
  9. [self updateBattery];
  10. self.userInteractionEnabled = NO;
  11. }
  12. return self;
  13. }
  14. long long batteryPercentage;
  15. long long lastPercentage;
  16. -(void)updateBattery {
  17. dispatch_async(dispatch_get_main_queue(), ^{
  18. if(!self.isUpdating) {
  19. self.isUpdating = YES;
  20. self.number = 0;
  21. float y = 0;
  22. BCBatteryDeviceController *bcb = [BCBatteryDeviceController sharedInstance];
  23. NSArray *devices = MSHookIvar<NSArray *>(bcb, "_sortedDevices");
  24. /*for( UIView *view in self.subviews ) {
  25. @try {
  26. [view removeFromSuperview];
  27. } @catch (NSException *exception) {
  28. //Panik
  29. }
  30. }*/
  31. for (BCBatteryDevice *device in devices) {
  32. NSString *deviceName = MSHookIvar<NSString *>(device, "_name");
  33. double batteryPercentage = MSHookIvar<long long>(device, "_percentCharge");
  34. BOOL charging = MSHookIvar<long long>(device, "_charging");
  35. BOOL LPM = MSHookIvar<BOOL>(device, "_batterySaverModeActive");
  36. BOOL shouldAdd = NO;
  37. if(showAll) {
  38. shouldAdd = YES;
  39. NSLog(@"Kai: SHOULD ADD");
  40. } else if(!showAll && charging) {
  41. shouldAdd = YES;
  42. NSLog(@"Kai: SHOULD ADD");
  43. }
  44. BOOL shouldRefresh = NO;
  45. KAIBatteryCell *cell = [KAIBatteryCell cellForDeviceIfExists:device];
  46. /*
  47. @property (nonatomic, assign) BOOL lastChargingState;
  48. @property (nonatomic, assign) BOOL lastLPM;
  49. @property (nonatomic, assign) double lastPercent;
  50. */
  51. if(cell.lastChargingState != charging || cell.lastLPM != LPM || cell.lastPercent != batteryPercentage) {
  52. shouldRefresh = YES;
  53. NSLog(@"Kai: SHOULD REFRESH");
  54. }
  55. if(shouldAdd && [deviceName length]!=0) {
  56. if([self.displayingDevices containsObject:device] && shouldRefresh) {
  57. NSLog(@"Kai: Updating cell: %@ for device:%@", cell, device);
  58. } else if(![self.displayingDevices containsObject:deviceName]) {
  59. KAIBatteryCell *newCell = [[KAIBatteryCell alloc] initWithFrame:CGRectMake(0, y, self.frame.size.width, bannerHeight) device:device];
  60. [self addSubview:newCell];
  61. [self.displayingDevices addObject:deviceName];
  62. //y+=bannerHeight + spacing;
  63. NSLog(@"Kai: Added cell: %@ for device:%@", cell, device);
  64. }
  65. self.number +=1;
  66. y+=bannerHeight + spacing;
  67. NSLog(@"Kai: incremented y, so now it is %f", y);
  68. [cell updateInfo];
  69. } else if(!shouldAdd) {
  70. if([self.displayingDevices containsObject:device]) {
  71. [cell removeFromSuperview];
  72. [self.displayingDevices removeObject:device];
  73. NSLog(@"Kai: Removed cell: %@ for device: %@", cell, device);
  74. }
  75. }
  76. }
  77. //[self.heightAnchor constraintEqualToConstant:(self.number * 85)].active = YES;
  78. self.isUpdating = NO;
  79. }
  80. });
  81. }
  82. +(KAIBattery *)sharedInstance {
  83. return instance;
  84. }
  85. @end