Device battery indicators on your Lock Screen
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

96 行
3.1KB

  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. 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(KAIBatteryCell *cell in self.subviews) {
  27. if([cell respondsToSelector:@selector(updateInfo)] && ![devices containsObject:cell.device]) { //to confirm is a cell and battery device does not exist
  28. [cell removeFromSuperview];
  29. } else if([cell respondsToSelector:@selector(updateInfo)]) {
  30. [cell updateInfo];
  31. }
  32. }
  33. for (BCBatteryDevice *device in devices) {
  34. NSString *deviceName = MSHookIvar<NSString *>(device, "_name");
  35. //double batteryPercentage = MSHookIvar<long long>(device, "_percentCharge");
  36. BOOL charging = MSHookIvar<long long>(device, "_charging");
  37. //BOOL LPM = MSHookIvar<BOOL>(device, "_batterySaverModeActive");
  38. BOOL shouldAdd = NO;
  39. if(showAll) {
  40. shouldAdd = YES;
  41. //NSLog(@"Kai: SHOULD ADD");
  42. } else if(!showAll && charging) {
  43. shouldAdd = YES;
  44. //NSLog(@"Kai: SHOULD ADD");
  45. }
  46. KAIBatteryCell *cell = [KAIBatteryCell cellForDeviceIfExists:device frameToCreateNew:CGRectMake(0, y, self.frame.size.width, bannerHeight)];
  47. if(shouldAdd && [deviceName length]!=0) {
  48. if(![self.subviews containsObject:cell]) {
  49. cell.frame = CGRectMake(0, y, self.frame.size.width, bannerHeight);
  50. [self addSubview:cell];
  51. }
  52. y+=bannerHeight + spacing;
  53. } else if(!shouldAdd) {
  54. [cell removeFromSuperview];
  55. }
  56. }
  57. //[self.heightAnchor constraintEqualToConstant:(self.number * 85)].active = YES;
  58. self.isUpdating = NO;
  59. self.number = [self.subviews count];
  60. }
  61. });
  62. }
  63. -(void)removeAllAndRefresh {
  64. for( UIView *view in self.subviews ) {
  65. @try {
  66. [view removeFromSuperview];
  67. } @catch (NSException *exception) {
  68. //Panik
  69. }
  70. }
  71. //self.displayingDevices = [[NSMutableArray alloc] init];
  72. //addedCells = nil;
  73. [self updateBattery];
  74. }
  75. +(KAIBattery *)sharedInstance {
  76. return instance;
  77. }
  78. @end