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.

171 lines
8.0KB

  1. #import "KAIBatteryCell.h"
  2. NSMutableArray *deviceInstances = [[NSMutableArray alloc] init];
  3. @implementation KAIBatteryCell
  4. -(instancetype)initWithFrame:(CGRect)arg1 device:(BCBatteryDevice *)device {
  5. self = [super initWithFrame:arg1];
  6. if(self) {
  7. self.device = device;
  8. NSString *deviceName = MSHookIvar<NSString *>(device, "_name");
  9. double batteryPercentage = MSHookIvar<long long>(device, "_percentCharge");
  10. BOOL charging = MSHookIvar<long long>(device, "_charging");
  11. BOOL LPM = MSHookIvar<BOOL>(device, "_batterySaverModeActive");
  12. UIView *blank;
  13. if(bannerStyle==1) {
  14. if(kCFCoreFoundationVersionNumber > 1600) {
  15. blank = [[[objc_getClass("MTMaterialView") class] alloc] _initWithRecipe:1 configuration:1 initialWeighting:1 scaleAdjustment:nil];
  16. } else if(kCFCoreFoundationVersionNumber < 1600) {
  17. blank = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
  18. }
  19. } else if(bannerStyle==2) {
  20. blank = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleDark]];
  21. } else if(bannerStyle==3) {
  22. blank = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
  23. }
  24. blank.layer.masksToBounds = YES;
  25. blank.layer.continuousCorners = YES;
  26. blank.layer.cornerRadius = cornerRadius;
  27. NSString *labelText = [NSString stringWithFormat:@"%@", deviceName];
  28. self.label = [[UILabel alloc] init];
  29. if(!hideDeviceLabel) {
  30. [self.label setFont:[UIFont systemFontOfSize:16]];
  31. } else if(hideDeviceLabel) {
  32. [self.label setFont:[UIFont systemFontOfSize:0]];
  33. }
  34. [self.label setTextColor:[UIColor whiteColor]];
  35. self.label.lineBreakMode = NSLineBreakByWordWrapping;
  36. self.label.numberOfLines = 1;
  37. [self.label setText:labelText];
  38. self.battery = [[_UIBatteryView alloc] init];
  39. self.battery.chargePercent = (batteryPercentage*0.01);
  40. self.percentLabel = [[UILabel alloc] init];
  41. self.battery.showsPercentage = NO;
  42. if(hidePercent) {
  43. [self.percentLabel setFont:[UIFont systemFontOfSize:0]];
  44. } else {
  45. [self.percentLabel setFont:[UIFont systemFontOfSize:14]];
  46. }
  47. [self.percentLabel setTextColor:[UIColor whiteColor]];
  48. self.percentLabel.lineBreakMode = NSLineBreakByWordWrapping;
  49. [self.percentLabel setTextAlignment:NSTextAlignmentRight];
  50. self.percentLabel.numberOfLines = 1;
  51. [self.percentLabel setText:[NSString stringWithFormat:@"%ld%%", (long)((NSInteger) batteryPercentage)]];
  52. if(charging) self.battery.chargingState = 1;
  53. self.battery.showsInlineChargingIndicator = YES;
  54. if(LPM) self.battery.saverModeActive = YES;
  55. if(kCFCoreFoundationVersionNumber > 1600) {
  56. [self.battery setBodyColorAlpha:1.0];
  57. [self.battery setPinColorAlpha:1.0];
  58. }
  59. UIImage *glyph = [device glyph];
  60. self.glyphView = [[UIImageView alloc] init];
  61. self.glyphView.contentMode = UIViewContentModeScaleAspectFit;
  62. [self.glyphView setImage:glyph];
  63. [self addSubview:blank];
  64. [self addSubview:self.percentLabel];
  65. [self addSubview:self.label];
  66. [self addSubview:self.battery];
  67. [self addSubview:self.glyphView];
  68. blank.translatesAutoresizingMaskIntoConstraints = NO;
  69. if(bannerAlign==2) { //center
  70. [blank.centerXAnchor constraintEqualToAnchor:self.centerXAnchor constant:horizontalOffset].active = YES;
  71. } else if(bannerAlign==1) { //left
  72. [blank.leftAnchor constraintEqualToAnchor:self.leftAnchor constant:horizontalOffset].active = YES;
  73. } else if(bannerAlign==3) { //right
  74. [blank.rightAnchor constraintEqualToAnchor:self.rightAnchor constant:horizontalOffset].active = YES;
  75. }
  76. [blank.topAnchor constraintEqualToAnchor:self.topAnchor].active = YES;
  77. [blank.widthAnchor constraintEqualToConstant:((self.frame.size.width) + bannerWidthFactor)].active = YES;
  78. [blank.heightAnchor constraintEqualToConstant:bannerHeight].active = YES;
  79. self.percentLabel.translatesAutoresizingMaskIntoConstraints = NO;
  80. [self.percentLabel.leftAnchor constraintEqualToAnchor:blank.rightAnchor constant:(- 96)].active = YES;
  81. [self.percentLabel.centerYAnchor constraintEqualToAnchor:blank.centerYAnchor].active = YES;
  82. [self.percentLabel.widthAnchor constraintEqualToConstant:37].active = YES;
  83. [self.percentLabel.heightAnchor constraintEqualToConstant:12].active = YES;
  84. self.label.translatesAutoresizingMaskIntoConstraints = NO;
  85. [self.label.leftAnchor constraintEqualToAnchor:self.glyphView.rightAnchor constant:4.5].active = YES;
  86. [self.label.centerYAnchor constraintEqualToAnchor:blank.centerYAnchor].active = YES;
  87. [self.label.rightAnchor constraintEqualToAnchor:self.percentLabel.leftAnchor constant:-4.5].active = YES;
  88. [self.label.heightAnchor constraintEqualToConstant:25].active = YES;
  89. self.glyphView.translatesAutoresizingMaskIntoConstraints = NO;
  90. [self.glyphView.leftAnchor constraintEqualToAnchor:blank.leftAnchor constant:20.5].active = YES;
  91. [self.glyphView.centerYAnchor constraintEqualToAnchor:blank.centerYAnchor].active = YES;
  92. [self.glyphView.widthAnchor constraintEqualToConstant:glyphSize].active = YES;
  93. [self.glyphView.heightAnchor constraintEqualToConstant:glyphSize].active = YES;
  94. self.battery.translatesAutoresizingMaskIntoConstraints = NO;
  95. [self.battery.leftAnchor constraintEqualToAnchor:blank.rightAnchor constant:(- 49)].active = YES;
  96. [self.battery.centerYAnchor constraintEqualToAnchor:blank.centerYAnchor].active = YES;
  97. [self.battery.widthAnchor constraintEqualToConstant:20].active = YES;
  98. [self.battery.heightAnchor constraintEqualToConstant:10].active = YES;
  99. [deviceInstances addObject:self];
  100. }
  101. return self;
  102. }
  103. -(void)updateInfo {
  104. //NSLog(@"kai: updating cell info");
  105. NSString *deviceName = MSHookIvar<NSString *>(self.device, "_name");
  106. double batteryPercentage = MSHookIvar<long long>(self.device, "_percentCharge");
  107. BOOL charging = MSHookIvar<long long>(self.device, "_charging");
  108. BOOL LPM = MSHookIvar<BOOL>(self.device, "_batterySaverModeActive");
  109. self.label.text = [NSString stringWithFormat:@"%@", deviceName];
  110. [self.percentLabel setText:[NSString stringWithFormat:@"%ld%%", (long)((NSInteger) batteryPercentage)]];
  111. self.battery.chargePercent = (batteryPercentage*0.01);
  112. if(charging) { self.battery.chargingState = 1; } else { self.battery.chargingState = 0; }
  113. self.battery.showsInlineChargingIndicator = YES;
  114. if(LPM) { self.battery.saverModeActive = YES; } else { self.battery.saverModeActive = NO; }
  115. if(kCFCoreFoundationVersionNumber > 1600) {
  116. [self.battery setBodyColorAlpha:1.0];
  117. [self.battery setPinColorAlpha:1.0];
  118. }
  119. [self.percentLabel setText:[NSString stringWithFormat:@"%ld%%", (long)((NSInteger) batteryPercentage)]];
  120. self.battery.chargePercent = (batteryPercentage*0.01);
  121. [self.glyphView setImage:[self.device glyph]];
  122. }
  123. +(instancetype)cellForDeviceIfExists:(BCBatteryDevice *)device frameToCreateNew:(CGRect)arg2 {
  124. KAIBatteryCell *foundCell;
  125. //NSString *deviceName = MSHookIvar<NSString *>(device, "_name");
  126. for(KAIBatteryCell *cell in deviceInstances) {
  127. if(cell.device == device || [cell.device.identifier isEqualToString:device.identifier]) {
  128. foundCell = cell;
  129. break;
  130. }
  131. }
  132. if(foundCell == nil) {
  133. foundCell = [[KAIBatteryCell alloc] initWithFrame:arg2 device:device];
  134. }
  135. return foundCell;
  136. //return deviceInstances;
  137. }
  138. +(void)resetArray {
  139. deviceInstances = [[NSMutableArray alloc] init];
  140. }
  141. @end