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.

166 lines
7.8KB

  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. [self.label setTextColor:[UIColor whiteColor]];
  34. self.label.lineBreakMode = NSLineBreakByWordWrapping;
  35. self.label.numberOfLines = 1;
  36. [self.label setText:labelText];
  37. self.battery = [[_UIBatteryView alloc] init];
  38. self.battery.chargePercent = (batteryPercentage*0.01);
  39. self.percentLabel = [[UILabel alloc] init];
  40. self.battery.showsPercentage = NO;
  41. if(hidePercent) {
  42. [self.percentLabel setFont:[UIFont systemFontOfSize:0]];
  43. } else {
  44. [self.percentLabel setFont:[UIFont systemFontOfSize:14]];
  45. }
  46. [self.percentLabel setTextColor:[UIColor whiteColor]];
  47. self.percentLabel.lineBreakMode = NSLineBreakByWordWrapping;
  48. [self.percentLabel setTextAlignment:NSTextAlignmentRight];
  49. self.percentLabel.numberOfLines = 1;
  50. [self.percentLabel setText:[NSString stringWithFormat:@"%ld%%", (long)((NSInteger) batteryPercentage)]];
  51. if(charging) self.battery.chargingState = 1;
  52. self.battery.showsInlineChargingIndicator = YES;
  53. if(LPM) self.battery.saverModeActive = YES;
  54. if(kCFCoreFoundationVersionNumber > 1600) {
  55. [self.battery setBodyColorAlpha:1.0];
  56. [self.battery setPinColorAlpha:1.0];
  57. }
  58. UIImage *glyph = [device glyph];
  59. self.glyphView = [[UIImageView alloc] init];
  60. self.glyphView.contentMode = UIViewContentModeScaleAspectFit;
  61. [self.glyphView setImage:glyph];
  62. [self addSubview:blank];
  63. [self addSubview:self.percentLabel];
  64. [self addSubview:self.label];
  65. [self addSubview:self.battery];
  66. [self addSubview:self.glyphView];
  67. blank.translatesAutoresizingMaskIntoConstraints = NO;
  68. if(bannerAlign==2) { //center
  69. [blank.centerXAnchor constraintEqualToAnchor:self.centerXAnchor constant:horizontalOffset].active = YES;
  70. } else if(bannerAlign==1) { //left
  71. [blank.leftAnchor constraintEqualToAnchor:self.leftAnchor constant:horizontalOffset].active = YES;
  72. } else if(bannerAlign==3) { //right
  73. [blank.rightAnchor constraintEqualToAnchor:self.rightAnchor constant:horizontalOffset].active = YES;
  74. }
  75. [blank.topAnchor constraintEqualToAnchor:self.topAnchor].active = YES;
  76. [blank.widthAnchor constraintEqualToConstant:((self.frame.size.width) + bannerWidthFactor)].active = YES;
  77. [blank.heightAnchor constraintEqualToConstant:bannerHeight].active = YES;
  78. self.percentLabel.translatesAutoresizingMaskIntoConstraints = NO;
  79. [self.percentLabel.leftAnchor constraintEqualToAnchor:blank.rightAnchor constant:(- 96)].active = YES;
  80. [self.percentLabel.centerYAnchor constraintEqualToAnchor:blank.centerYAnchor].active = YES;
  81. [self.percentLabel.widthAnchor constraintEqualToConstant:37].active = YES;
  82. [self.percentLabel.heightAnchor constraintEqualToConstant:12].active = YES;
  83. self.label.translatesAutoresizingMaskIntoConstraints = NO;
  84. [self.label.leftAnchor constraintEqualToAnchor:self.glyphView.rightAnchor constant:4.5].active = YES;
  85. [self.label.centerYAnchor constraintEqualToAnchor:blank.centerYAnchor].active = YES;
  86. [self.label.rightAnchor constraintEqualToAnchor:self.percentLabel.leftAnchor constant:-4.5].active = YES;
  87. [self.label.heightAnchor constraintEqualToConstant:25].active = YES;
  88. self.glyphView.translatesAutoresizingMaskIntoConstraints = NO;
  89. [self.glyphView.leftAnchor constraintEqualToAnchor:blank.leftAnchor constant:20.5].active = YES;
  90. [self.glyphView.centerYAnchor constraintEqualToAnchor:blank.centerYAnchor].active = YES;
  91. [self.glyphView.widthAnchor constraintEqualToConstant:glyphSize].active = YES;
  92. [self.glyphView.heightAnchor constraintEqualToConstant:glyphSize].active = YES;
  93. self.battery.translatesAutoresizingMaskIntoConstraints = NO;
  94. [self.battery.leftAnchor constraintEqualToAnchor:blank.rightAnchor constant:(- 49)].active = YES;
  95. [self.battery.centerYAnchor constraintEqualToAnchor:blank.centerYAnchor].active = YES;
  96. [self.battery.widthAnchor constraintEqualToConstant:20].active = YES;
  97. [self.battery.heightAnchor constraintEqualToConstant:10].active = YES;
  98. }
  99. return self;
  100. }
  101. -(void)updateInfo {
  102. //NSLog(@"kai: updating cell info");
  103. NSString *deviceName = MSHookIvar<NSString *>(self.device, "_name");
  104. double batteryPercentage = MSHookIvar<long long>(self.device, "_percentCharge");
  105. BOOL charging = MSHookIvar<long long>(self.device, "_charging");
  106. BOOL LPM = MSHookIvar<BOOL>(self.device, "_batterySaverModeActive");
  107. self.label.text = [NSString stringWithFormat:@"%@", deviceName];
  108. [self.percentLabel setText:[NSString stringWithFormat:@"%ld%%", (long)((NSInteger) batteryPercentage)]];
  109. self.battery.chargePercent = (batteryPercentage*0.01);
  110. if(charging) { self.battery.chargingState = 1; } else { self.battery.chargingState = 0; }
  111. self.battery.showsInlineChargingIndicator = YES;
  112. if(LPM) { self.battery.saverModeActive = YES; } else { self.battery.saverModeActive = NO; }
  113. if(kCFCoreFoundationVersionNumber > 1600) {
  114. [self.battery setBodyColorAlpha:1.0];
  115. [self.battery setPinColorAlpha:1.0];
  116. }
  117. [self.percentLabel setText:[NSString stringWithFormat:@"%ld%%", (long)((NSInteger) batteryPercentage)]];
  118. self.battery.chargePercent = (batteryPercentage*0.01);
  119. [self.glyphView setImage:[self.device glyph]];
  120. if(!self.height) {
  121. self.height.active = NO;
  122. self.height = [self.heightAnchor constraintEqualToConstant:(bannerHeight + spacing)];
  123. self.height.active = YES;
  124. } //else {
  125. //int height = (bannerHeight + spacing);
  126. //self.height.constant = height;
  127. //}
  128. if(!self.width) {
  129. self.width.active = NO;
  130. self.width = [self.widthAnchor constraintEqualToConstant:(self.frame.size.width)];
  131. self.width.active = YES;
  132. } //else {
  133. //int width = self.frame.size.width;
  134. //self.width.constant = width;
  135. //}
  136. }
  137. @end