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.

185 lines
9.0KB

  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.leftAnchor constraintEqualToAnchor:blurPlatter.rightAnchor constant:(- 96)].active = YES;
  96. [self.percentLabel.centerYAnchor constraintEqualToAnchor:blurPlatter.centerYAnchor].active = YES;
  97. [self.percentLabel.widthAnchor constraintEqualToConstant:37].active = YES;
  98. [self.percentLabel.heightAnchor constraintEqualToConstant:12].active = YES;
  99. self.label.translatesAutoresizingMaskIntoConstraints = NO;
  100. [self.label.leftAnchor constraintEqualToAnchor:self.glyphView.rightAnchor constant:4.5].active = YES;
  101. [self.label.centerYAnchor constraintEqualToAnchor:blurPlatter.centerYAnchor].active = YES;
  102. if(!hidePercent) {
  103. [self.label.rightAnchor constraintEqualToAnchor:self.percentLabel.leftAnchor constant:-4.5].active = YES;
  104. }
  105. [self.label.heightAnchor constraintEqualToConstant:25].active = YES;
  106. self.glyphView.translatesAutoresizingMaskIntoConstraints = NO;
  107. [self.glyphView.leftAnchor constraintEqualToAnchor:blurPlatter.leftAnchor constant:20.5].active = YES;
  108. [self.glyphView.centerYAnchor constraintEqualToAnchor:blurPlatter.centerYAnchor].active = YES;
  109. [self.glyphView.widthAnchor constraintEqualToConstant:glyphSize].active = YES;
  110. [self.glyphView.heightAnchor constraintEqualToConstant:glyphSize].active = YES;
  111. self.battery.translatesAutoresizingMaskIntoConstraints = NO;
  112. [self.battery.leftAnchor constraintEqualToAnchor:blurPlatter.rightAnchor constant:(- 49)].active = YES;
  113. [self.battery.centerYAnchor constraintEqualToAnchor:blurPlatter.centerYAnchor].active = YES;
  114. [self.battery.widthAnchor constraintEqualToConstant:20].active = YES;
  115. [self.battery.heightAnchor constraintEqualToConstant:10].active = YES;
  116. if(hidePercent) {
  117. [self.label.rightAnchor constraintEqualToAnchor:self.battery.leftAnchor constant:-4.5].active = YES;
  118. }
  119. [self.heightAnchor constraintEqualToConstant:(bannerHeight + spacing)].active = YES;
  120. [self.widthAnchor constraintEqualToConstant:(self.frame.size.width)].active = YES;
  121. }
  122. return self;
  123. }
  124. -(void)traitCollectionDidChange:(id)arg1 {
  125. [super traitCollectionDidChange:arg1];
  126. if(textColor==0) {
  127. if(@available(iOS 12.0, *)) {
  128. if(self.traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark) {
  129. [self.label setTextColor:[UIColor whiteColor]];
  130. [self.percentLabel setTextColor:[UIColor whiteColor]];
  131. } else if(self.traitCollection.userInterfaceStyle == UIUserInterfaceStyleLight) {
  132. [self.label setTextColor:[UIColor blackColor]];
  133. [self.percentLabel setTextColor:[UIColor blackColor]];
  134. }
  135. }
  136. }
  137. }
  138. -(void)updateInfo {
  139. //NSLog(@"kai: updating cell info");
  140. NSString *deviceName = MSHookIvar<NSString *>(self.device, "_name");
  141. double batteryPercentage = MSHookIvar<long long>(self.device, "_percentCharge");
  142. BOOL charging = MSHookIvar<long long>(self.device, "_charging");
  143. BOOL LPM = MSHookIvar<BOOL>(self.device, "_batterySaverModeActive");
  144. self.label.text = [NSString stringWithFormat:@"%@", deviceName];
  145. [self.percentLabel setText:[NSString stringWithFormat:@"%ld%%", (long)((NSInteger) batteryPercentage)]];
  146. self.battery.chargePercent = (batteryPercentage*0.01);
  147. if(charging) { self.battery.chargingState = 1; } else { self.battery.chargingState = 0; }
  148. self.battery.showsInlineChargingIndicator = YES;
  149. if(LPM) { self.battery.saverModeActive = YES; } else { self.battery.saverModeActive = NO; }
  150. if(kCFCoreFoundationVersionNumber > 1600) {
  151. [self.battery setBodyColorAlpha:1.0];
  152. [self.battery setPinColorAlpha:1.0];
  153. }
  154. [self.percentLabel setText:[NSString stringWithFormat:@"%ld%%", (long)((NSInteger) batteryPercentage)]];
  155. self.battery.chargePercent = (batteryPercentage*0.01);
  156. [self.glyphView setImage:[self.device glyph]];
  157. }
  158. @end