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
4.2KB

  1. #import "KAIBattery.h"
  2. KAIBattery *instance;
  3. //NSMutableArray *showingCells = [[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. //NSLog(@"kai: IS Updating");
  22. self.isUpdating = YES;
  23. //self.number = 0;
  24. float y = 0;
  25. BCBatteryDeviceController *bcb = [BCBatteryDeviceController sharedInstance];
  26. NSArray *devices = MSHookIvar<NSArray *>(bcb, "_sortedDevices");
  27. if([devices count]!=0) {
  28. //NSLog(@"kai: info is good, will proceed");
  29. __block float ytwo = 0;
  30. for(KAIBatteryCell *cell in self.subviews) {
  31. if([cell respondsToSelector:@selector(updateInfo)] && ![devices containsObject:cell.device]) { //to confirm is a cell and battery device does not exist
  32. dispatch_async(dispatch_get_main_queue(), ^{
  33. [cell removeFromSuperview];
  34. });
  35. } else if([cell respondsToSelector:@selector(updateInfo)]) {
  36. cell.frame = CGRectMake(0, y, self.frame.size.width, bannerHeight);
  37. [cell updateInfo];
  38. ytwo+= bannerHeight + spacing;
  39. }
  40. }
  41. for (BCBatteryDevice *device in devices) {
  42. NSString *deviceName = MSHookIvar<NSString *>(device, "_name");
  43. //double batteryPercentage = MSHookIvar<long long>(device, "_percentCharge");
  44. BOOL charging = MSHookIvar<long long>(device, "_charging");
  45. //BOOL LPM = MSHookIvar<BOOL>(device, "_batterySaverModeActive");
  46. BOOL shouldAdd = NO;
  47. if(showAll) {
  48. shouldAdd = YES;
  49. //NSLog(@"Kai: SHOULD ADD");
  50. } else if(!showAll && charging) {
  51. shouldAdd = YES;
  52. //NSLog(@"Kai: SHOULD ADD");
  53. }
  54. KAIBatteryCell *cell = [KAIBatteryCell cellForDeviceIfExists:device frameToCreateNew:CGRectMake(0, y, self.frame.size.width, bannerHeight)];
  55. cell.frame = CGRectMake(0, y, self.frame.size.width, bannerHeight);
  56. if(cell) {
  57. cell.device = device;
  58. //cell.frame = cell.frame = CGRectMake(0, y, self.frame.size.width, bannerHeight); //bro im like creating my own stack view
  59. //[cell updateInfo];
  60. }
  61. if(shouldAdd && [deviceName length]!=0) {
  62. if(![self.subviews containsObject:cell]) {
  63. cell.frame = CGRectMake(0, y, self.frame.size.width, bannerHeight);
  64. dispatch_async(dispatch_get_main_queue(), ^{
  65. [self addSubview:cell];
  66. });
  67. }
  68. y+=bannerHeight + spacing;
  69. } else if(!shouldAdd) {
  70. dispatch_async(dispatch_get_main_queue(), ^{
  71. [cell removeFromSuperview];
  72. });
  73. }
  74. }
  75. //[self.heightAnchor constraintEqualToConstant:(self.number * 85)].active = YES;
  76. self.number = [self.subviews count];
  77. //[(CSAdjunctListView *)self.superview.superview KaiUpdate];
  78. }
  79. self.isUpdating = NO;
  80. //NSLog(@"kai: finished update");
  81. [(CSAdjunctListView *)self.superview.superview KaiUpdate];
  82. }
  83. });
  84. }
  85. -(void)removeAllAndRefresh {
  86. for( UIView *view in self.subviews ) {
  87. @try {
  88. [view removeFromSuperview];
  89. } @catch (NSException *exception) {
  90. //Panik
  91. }
  92. }
  93. [KAIBatteryCell resetArray];
  94. //self.displayingDevices = [[NSMutableArray alloc] init];
  95. //addedCells = nil;
  96. [self updateBattery];
  97. }
  98. +(KAIBattery *)sharedInstance {
  99. return instance;
  100. }
  101. @end