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.

129 lines
4.3KB

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