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.

KAIBattery.mm 4.2KB

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