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.

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