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.

112 lines
4.4KB

  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 addSubview:self.batteryLabel];
  15. }
  16. return self;
  17. }
  18. long long batteryPercentage;
  19. long long lastPercentage;
  20. -(void)updateBattery {
  21. dispatch_async(dispatch_get_main_queue(), ^{
  22. if(!self.isUpdating) {
  23. self.isUpdating = YES;
  24. self.number = 0;
  25. float y = 0;
  26. BCBatteryDeviceController *bcb = [BCBatteryDeviceController sharedInstance];
  27. NSArray *devices = MSHookIvar<NSArray *>(bcb, "_sortedDevices");
  28. for( UIView *view in self.subviews ) {
  29. @try {
  30. [view removeFromSuperview];
  31. } @catch (NSException *exception) {
  32. //Panik
  33. }
  34. }
  35. for (BCBatteryDevice *device in devices) {
  36. NSString *deviceName = MSHookIvar<NSString *>(device, "_name");
  37. double batteryPercentage = MSHookIvar<long long>(device, "_percentCharge");
  38. BOOL charging = MSHookIvar<long long>(device, "_charging");
  39. BOOL LPM = MSHookIvar<BOOL>(device, "_batterySaverModeActive");
  40. if(charging) {
  41. UIVisualEffectView *blank = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleDark]];
  42. blank.frame = CGRectMake(0, 0 + y, self.frame.size.width, 80);
  43. blank.layer.masksToBounds = YES;
  44. blank.layer.cornerRadius = 13;
  45. //[blank setBackgroundColor:[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1]];
  46. NSString *labelText = [NSString stringWithFormat:@"%@", deviceName];
  47. UILabel *label = [[UILabel alloc] init];
  48. [label setFont:[UIFont systemFontOfSize:16]];
  49. [label setTextColor:[UIColor whiteColor]];
  50. label.lineBreakMode = NSLineBreakByWordWrapping;
  51. label.numberOfLines = 0;
  52. [label setText:labelText];
  53. _UIBatteryView *battery = [[_UIBatteryView alloc] init];
  54. battery.chargePercent = (batteryPercentage*0.01);
  55. UILabel *percentLabel = [[UILabel alloc] init];
  56. battery.showsPercentage = NO;
  57. [percentLabel setFont:[UIFont systemFontOfSize:14]];
  58. [percentLabel setTextColor:[UIColor whiteColor]];
  59. percentLabel.lineBreakMode = NSLineBreakByWordWrapping;
  60. [percentLabel setTextAlignment:NSTextAlignmentRight];
  61. percentLabel.numberOfLines = 0;
  62. [percentLabel setText:[NSString stringWithFormat:@"%ld%%", (long)((NSInteger) batteryPercentage)]];
  63. if(charging) battery.chargingState = 1;
  64. battery.showsInlineChargingIndicator = YES;
  65. if(LPM) battery.saverModeActive = YES;
  66. if(kCFCoreFoundationVersionNumber > 1600) {
  67. [battery setBodyColorAlpha:1.0];
  68. [battery setPinColorAlpha:1.0];
  69. }
  70. UIImage *glyph = [device glyph];
  71. UIImageView *glyphView = [[UIImageView alloc] init];
  72. glyphView.contentMode = UIViewContentModeScaleAspectFit;
  73. [glyphView setImage:glyph];
  74. label.frame = CGRectMake(57.5,27.5 + y,275,25);
  75. glyphView.frame = CGRectMake(12.5,18.5 + y,40,40);
  76. battery.frame = CGRectMake(310,35 + y,20,10);
  77. percentLabel.frame = CGRectMake(265,35 + y,36,12);
  78. y+=85;
  79. self.number +=1;
  80. [self addSubview:blank];
  81. [self addSubview:percentLabel];
  82. [self addSubview:label];
  83. [self addSubview:battery];
  84. [self addSubview:glyphView];
  85. blank.alpha = 0.8;
  86. }
  87. }
  88. self.isUpdating = NO;
  89. }
  90. });
  91. }
  92. +(KAIBattery *)sharedInstance {
  93. return instance;
  94. }
  95. @end