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.

173 line
8.3KB

  1. #import "KAIBattery.h"
  2. KAIBattery *instance;
  3. @implementation KAIBattery
  4. -(instancetype)init {
  5. self = [super init];
  6. instance = self;
  7. if (self) {
  8. /*self.translatesAutoresizingMaskIntoConstraints = NO;
  9. [self.leftAnchor constraintEqualToAnchor:self.leftAnchor constant:8].active = YES;
  10. [self.topAnchor constraintEqualToAnchor:self.topAnchor constant:arg1.origin.y].active = YES;
  11. [self.widthAnchor constraintEqualToConstant:UIScreen.mainScreen.bounds.size.width - 16].active = YES;
  12. [self.heightAnchor constraintEqualToConstant:(self.number * 85)].active = YES;*/
  13. [self updateBattery];
  14. self.userInteractionEnabled = NO;
  15. //[self addSubview:self.batteryLabel];
  16. }
  17. return self;
  18. }
  19. long long batteryPercentage;
  20. long long lastPercentage;
  21. -(void)updateBattery {
  22. dispatch_async(dispatch_get_main_queue(), ^{
  23. if(!self.isUpdating) {
  24. self.isUpdating = YES;
  25. self.number = 0;
  26. float y = 0;
  27. BCBatteryDeviceController *bcb = [BCBatteryDeviceController sharedInstance];
  28. NSArray *devices = MSHookIvar<NSArray *>(bcb, "_sortedDevices");
  29. for( UIView *view in self.subviews ) {
  30. @try {
  31. [view removeFromSuperview];
  32. } @catch (NSException *exception) {
  33. //Panik
  34. }
  35. }
  36. for (BCBatteryDevice *device in devices) {
  37. NSString *deviceName = MSHookIvar<NSString *>(device, "_name");
  38. double batteryPercentage = MSHookIvar<long long>(device, "_percentCharge");
  39. BOOL charging = MSHookIvar<long long>(device, "_charging");
  40. BOOL LPM = MSHookIvar<BOOL>(device, "_batterySaverModeActive");
  41. BOOL shouldAdd = NO;
  42. if(showAll) {
  43. shouldAdd = YES;
  44. } else if(!showAll && charging) {
  45. shouldAdd = YES;
  46. }
  47. if(shouldAdd) {
  48. UIView *blank;
  49. if(bannerStyle==1) {
  50. blank = [[[objc_getClass("MTMaterialView") class] alloc] _initWithRecipe:1 configuration:1 initialWeighting:1 scaleAdjustment:nil];
  51. } else if(bannerStyle==2) {
  52. blank = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleDark]];
  53. } else if(bannerStyle==3) {
  54. blank = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
  55. }
  56. blank.layer.masksToBounds = YES;
  57. blank.layer.continuousCorners = YES;
  58. blank.layer.cornerRadius = cornerRadius;
  59. NSString *labelText = [NSString stringWithFormat:@"%@", deviceName];
  60. UILabel *label = [[UILabel alloc] init];
  61. if(!hideDeviceLabel) {
  62. [label setFont:[UIFont systemFontOfSize:16]];
  63. } else if(hideDeviceLabel) {
  64. [label setFont:[UIFont systemFontOfSize:0]];
  65. }
  66. [label setTextColor:[UIColor whiteColor]];
  67. label.lineBreakMode = NSLineBreakByWordWrapping;
  68. label.numberOfLines = 0;
  69. [label setText:labelText];
  70. _UIBatteryView *battery = [[_UIBatteryView alloc] init];
  71. battery.chargePercent = (batteryPercentage*0.01);
  72. UILabel *percentLabel = [[UILabel alloc] init];
  73. battery.showsPercentage = NO;
  74. if(hidePercent) {
  75. [percentLabel setFont:[UIFont systemFontOfSize:0]];
  76. } else {
  77. [percentLabel setFont:[UIFont systemFontOfSize:14]];
  78. }
  79. [percentLabel setTextColor:[UIColor whiteColor]];
  80. percentLabel.lineBreakMode = NSLineBreakByWordWrapping;
  81. [percentLabel setTextAlignment:NSTextAlignmentRight];
  82. percentLabel.numberOfLines = 0;
  83. [percentLabel setText:[NSString stringWithFormat:@"%ld%%", (long)((NSInteger) batteryPercentage)]];
  84. if(charging) battery.chargingState = 1;
  85. battery.showsInlineChargingIndicator = YES;
  86. if(LPM) battery.saverModeActive = YES;
  87. if(kCFCoreFoundationVersionNumber > 1600) {
  88. [battery setBodyColorAlpha:1.0];
  89. [battery setPinColorAlpha:1.0];
  90. }
  91. UIImage *glyph = [device glyph];
  92. UIImageView *glyphView = [[UIImageView alloc] init];
  93. glyphView.contentMode = UIViewContentModeScaleAspectFit;
  94. [glyphView setImage:glyph];
  95. [self addSubview:blank];
  96. [self addSubview:percentLabel];
  97. [self addSubview:label];
  98. [self addSubview:battery];
  99. [self addSubview:glyphView];
  100. blank.translatesAutoresizingMaskIntoConstraints = NO;
  101. if(bannerAlign==2) { //center
  102. [blank.centerXAnchor constraintEqualToAnchor:self.centerXAnchor].active = YES;
  103. } else if(bannerAlign==1) { //left
  104. [blank.leftAnchor constraintEqualToAnchor:self.leftAnchor].active = YES;
  105. } else if(bannerAlign==3) { //right
  106. [blank.rightAnchor constraintEqualToAnchor:self.rightAnchor].active = YES;
  107. }
  108. [blank.topAnchor constraintEqualToAnchor:self.topAnchor constant:y].active = YES;
  109. [blank.widthAnchor constraintEqualToConstant:((self.superview.bounds.size.width - 16) + bannerWidthFactor)].active = YES;
  110. [blank.heightAnchor constraintEqualToConstant:bannerHeight].active = YES;
  111. //percentLabel.frame = CGRectMake(self.superview.bounds.size.width - 16 - 94,35 + y,36,12);
  112. percentLabel.translatesAutoresizingMaskIntoConstraints = NO;
  113. [percentLabel.leftAnchor constraintEqualToAnchor:blank.rightAnchor constant:(- 94)].active = YES;
  114. [percentLabel.centerYAnchor constraintEqualToAnchor:blank.centerYAnchor].active = YES;
  115. [percentLabel.widthAnchor constraintEqualToConstant:36].active = YES;
  116. [percentLabel.heightAnchor constraintEqualToConstant:12].active = YES;
  117. //label.frame = CGRectMake(65.5,27.5 + y,275,25);
  118. label.translatesAutoresizingMaskIntoConstraints = NO;
  119. [label.leftAnchor constraintEqualToAnchor:glyphView.rightAnchor constant:4.5].active = YES;
  120. [label.centerYAnchor constraintEqualToAnchor:blank.centerYAnchor].active = YES;
  121. [label.rightAnchor constraintEqualToAnchor:percentLabel.leftAnchor constant:7].active = YES;
  122. [label.heightAnchor constraintEqualToConstant:25].active = YES;
  123. //glyphView.frame = CGRectMake(20.5,18.5 + y,40,40);
  124. glyphView.translatesAutoresizingMaskIntoConstraints = NO;
  125. [glyphView.leftAnchor constraintEqualToAnchor:blank.leftAnchor constant:20.5].active = YES;
  126. [glyphView.centerYAnchor constraintEqualToAnchor:blank.centerYAnchor].active = YES;
  127. [glyphView.widthAnchor constraintEqualToConstant:glyphSize].active = YES;
  128. [glyphView.heightAnchor constraintEqualToConstant:glyphSize].active = YES;
  129. //battery.frame = CGRectMake(self.superview.bounds.size.width - 16 - 49,35 + y,20,10);
  130. battery.translatesAutoresizingMaskIntoConstraints = NO;
  131. [battery.leftAnchor constraintEqualToAnchor:blank.rightAnchor constant:(- 49)].active = YES;
  132. [battery.centerYAnchor constraintEqualToAnchor:blank.centerYAnchor].active = YES;
  133. [battery.widthAnchor constraintEqualToConstant:20].active = YES;
  134. [battery.heightAnchor constraintEqualToConstant:10].active = YES;
  135. y+=bannerHeight + spacing;
  136. self.number +=1;
  137. //blank.alpha = 0.8;
  138. }
  139. }
  140. //[self.heightAnchor constraintEqualToConstant:(self.number * 85)].active = YES;
  141. self.isUpdating = NO;
  142. }
  143. });
  144. }
  145. +(KAIBattery *)sharedInstance {
  146. return instance;
  147. }
  148. @end