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.

170 lines
8.1KB

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