Device battery indicators on your Lock Screen
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

199 lines
9.4KB

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