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.

192 lines
9.3KB

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