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.

121 lines
3.9KB

  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(![devices containsObject:cell.device]) {
  35. cell.device = nil;
  36. [cell removeFromSuperview];
  37. [self.displayingDevices removeObject:cell.label.text]; //lmaoo
  38. } else {
  39. [cell updateInfo];
  40. }
  41. }
  42. for (BCBatteryDevice *device in devices) {
  43. NSString *deviceName = MSHookIvar<NSString *>(device, "_name");
  44. //double batteryPercentage = MSHookIvar<long long>(device, "_percentCharge");
  45. BOOL charging = MSHookIvar<long long>(device, "_charging");
  46. //BOOL LPM = MSHookIvar<BOOL>(device, "_batterySaverModeActive");
  47. BOOL shouldAdd = NO;
  48. if(showAll) {
  49. shouldAdd = YES;
  50. NSLog(@"Kai: SHOULD ADD");
  51. } else if(!showAll && charging) {
  52. shouldAdd = YES;
  53. NSLog(@"Kai: SHOULD ADD");
  54. }
  55. KAIBatteryCell *cell = [KAIBatteryCell cellForDeviceIfExists:device];
  56. /*
  57. @property (nonatomic, assign) BOOL lastChargingState;
  58. @property (nonatomic, assign) BOOL lastLPM;
  59. @property (nonatomic, assign) double lastPercent;
  60. */
  61. if(shouldAdd && [deviceName length]!=0) {
  62. if(cell==nil && ![self.displayingDevices containsObject:deviceName]) {
  63. KAIBatteryCell *newCell = [[KAIBatteryCell alloc] initWithFrame:CGRectMake(0, y, self.frame.size.width, bannerHeight) device:device];
  64. [self addSubview:newCell];
  65. [self.displayingDevices addObject:deviceName];
  66. [addedCells addObject:newCell];
  67. //y+=bannerHeight + spacing;
  68. }
  69. //self.number +=1;
  70. y+=bannerHeight + spacing;
  71. } else if(!shouldAdd) {
  72. if([self.displayingDevices containsObject:deviceName]) {
  73. [cell removeFromSuperview];
  74. [self.displayingDevices removeObject:deviceName];
  75. }
  76. }
  77. }
  78. //[self.heightAnchor constraintEqualToConstant:(self.number * 85)].active = YES;
  79. self.isUpdating = NO;
  80. self.number = [self.subviews count];
  81. }
  82. });
  83. }
  84. -(void)removeAllAndRefresh {
  85. for( UIView *view in self.subviews ) {
  86. @try {
  87. [view removeFromSuperview];
  88. } @catch (NSException *exception) {
  89. //Panik
  90. }
  91. }
  92. self.displayingDevices = [[NSMutableArray alloc] init];
  93. addedCells = nil;
  94. [self updateBattery];
  95. }
  96. +(KAIBattery *)sharedInstance {
  97. return instance;
  98. }
  99. @end