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.

138 line
5.7KB

  1. #import "KAIBattery.h"
  2. KAIBattery *instance;
  3. @implementation KAIBattery
  4. -(instancetype)initWithFrame:(CGRect)arg1 {
  5. self = [super initWithFrame:arg1];
  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 || !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. blank.frame = CGRectMake(0, 0 + y, self.superview.bounds.size.width - 16, 80);
  54. blank.layer.masksToBounds = YES;
  55. blank.layer.cornerRadius = 13;
  56. //[blank setBackgroundColor:[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1]];
  57. NSString *labelText = [NSString stringWithFormat:@"%@", deviceName];
  58. UILabel *label = [[UILabel alloc] init];
  59. [label setFont:[UIFont systemFontOfSize:16]];
  60. [label setTextColor:[UIColor whiteColor]];
  61. label.lineBreakMode = NSLineBreakByWordWrapping;
  62. label.numberOfLines = 0;
  63. [label setText:labelText];
  64. _UIBatteryView *battery = [[_UIBatteryView alloc] init];
  65. battery.chargePercent = (batteryPercentage*0.01);
  66. UILabel *percentLabel = [[UILabel alloc] init];
  67. battery.showsPercentage = NO;
  68. [percentLabel setFont:[UIFont systemFontOfSize:14]];
  69. [percentLabel setTextColor:[UIColor whiteColor]];
  70. percentLabel.lineBreakMode = NSLineBreakByWordWrapping;
  71. [percentLabel setTextAlignment:NSTextAlignmentRight];
  72. percentLabel.numberOfLines = 0;
  73. [percentLabel setText:[NSString stringWithFormat:@"%ld%%", (long)((NSInteger) batteryPercentage)]];
  74. if(charging) battery.chargingState = 1;
  75. battery.showsInlineChargingIndicator = YES;
  76. if(LPM) battery.saverModeActive = YES;
  77. if(kCFCoreFoundationVersionNumber > 1600) {
  78. [battery setBodyColorAlpha:1.0];
  79. [battery setPinColorAlpha:1.0];
  80. }
  81. UIImage *glyph = [device glyph];
  82. UIImageView *glyphView = [[UIImageView alloc] init];
  83. glyphView.contentMode = UIViewContentModeScaleAspectFit;
  84. [glyphView setImage:glyph];
  85. label.frame = CGRectMake(65.5,27.5 + y,275,25);
  86. glyphView.frame = CGRectMake(20.5,18.5 + y,40,40);
  87. battery.frame = CGRectMake(310,35 + y,20,10);
  88. percentLabel.frame = CGRectMake(265,35 + y,36,12);
  89. y+=85;
  90. self.number +=1;
  91. [self addSubview:blank];
  92. [self addSubview:percentLabel];
  93. [self addSubview:label];
  94. [self addSubview:battery];
  95. [self addSubview:glyphView];
  96. //blank.alpha = 0.8;
  97. }
  98. }
  99. //[self.heightAnchor constraintEqualToConstant:(self.number * 85)].active = YES;
  100. self.isUpdating = NO;
  101. [self darkLightMode];
  102. }
  103. });
  104. }
  105. +(KAIBattery *)sharedInstance {
  106. return instance;
  107. }
  108. -(void)darkLightMode {
  109. for(UIVisualEffectView *view in self.subviews) {
  110. if(@available(iOS 12.0, *)) {
  111. if(self.traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark) {
  112. if([view respondsToSelector:@selector(setEffect:)]) view.effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
  113. }
  114. else {
  115. if([view respondsToSelector:@selector(setEffect:)]) view.effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
  116. }
  117. }
  118. }
  119. }
  120. @end