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.

137 lines
5.5KB

  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.batteryLabel = [[UILabel alloc]initWithFrame:CGRectMake(25,-10,220,120)];
  9. [self.batteryLabel setFont:[UIFont systemFontOfSize:13]];
  10. [self.batteryLabel setTextColor:[UIColor whiteColor]];
  11. self.batteryLabel.lineBreakMode = NSLineBreakByWordWrapping;
  12. self.batteryLabel.numberOfLines = 0;*/
  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. blank.frame = CGRectMake(0, 0 + y, self.frame.size.width, 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(57.5,27.5 + y,275,25);
  86. glyphView.frame = CGRectMake(12.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.isUpdating = NO;
  100. [self darkLightMode];
  101. }
  102. });
  103. }
  104. +(KAIBattery *)sharedInstance {
  105. return instance;
  106. }
  107. -(void)darkLightMode {
  108. for(UIVisualEffectView *view in self.subviews) {
  109. if(@available(iOS 12.0, *)) {
  110. if(self.traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark) {
  111. if([view respondsToSelector:@selector(setEffect:)]) view.effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
  112. }
  113. else {
  114. if([view respondsToSelector:@selector(setEffect:)]) view.effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
  115. }
  116. }
  117. }
  118. }
  119. @end