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

189 行
8.6KB

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