Device battery indicators on your Lock Screen
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

189 行
9.2KB

  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. if(kaiAlign==0) {
  121. [self.widthAnchor constraintEqualToConstant:(self.frame.size.width)].active = YES;
  122. } else {
  123. [self.widthAnchor constraintEqualToAnchor:blurPlatter.widthAnchor].active = YES;
  124. }
  125. }
  126. return self;
  127. }
  128. -(void)traitCollectionDidChange:(id)arg1 {
  129. [super traitCollectionDidChange:arg1];
  130. if(textColor==0) {
  131. if(@available(iOS 12.0, *)) {
  132. if(self.traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark) {
  133. [self.label setTextColor:[UIColor whiteColor]];
  134. [self.percentLabel setTextColor:[UIColor whiteColor]];
  135. } else if(self.traitCollection.userInterfaceStyle == UIUserInterfaceStyleLight) {
  136. [self.label setTextColor:[UIColor blackColor]];
  137. [self.percentLabel setTextColor:[UIColor blackColor]];
  138. }
  139. }
  140. }
  141. }
  142. -(void)updateInfo {
  143. //NSLog(@"kai: updating cell info");
  144. NSString *deviceName = MSHookIvar<NSString *>(self.device, "_name");
  145. double batteryPercentage = MSHookIvar<long long>(self.device, "_percentCharge");
  146. BOOL charging = MSHookIvar<long long>(self.device, "_charging");
  147. BOOL LPM = MSHookIvar<BOOL>(self.device, "_batterySaverModeActive");
  148. self.label.text = [NSString stringWithFormat:@"%@", deviceName];
  149. [self.percentLabel setText:[NSString stringWithFormat:@"%ld%%", (long)((NSInteger) batteryPercentage)]];
  150. self.battery.chargePercent = (batteryPercentage*0.01);
  151. if(charging) { self.battery.chargingState = 1; } else { self.battery.chargingState = 0; }
  152. self.battery.showsInlineChargingIndicator = YES;
  153. if(LPM) { self.battery.saverModeActive = YES; } else { self.battery.saverModeActive = NO; }
  154. if(kCFCoreFoundationVersionNumber > 1600) {
  155. [self.battery setBodyColorAlpha:1.0];
  156. [self.battery setPinColorAlpha:1.0];
  157. }
  158. [self.percentLabel setText:[NSString stringWithFormat:@"%ld%%", (long)((NSInteger) batteryPercentage)]];
  159. self.battery.chargePercent = (batteryPercentage*0.01);
  160. [self.glyphView setImage:[self.device glyph]];
  161. }
  162. @end