Device battery indicators on your Lock Screen
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

170 rindas
7.8KB

  1. #import "KAIBatteryCell.h"
  2. NSMutableArray *deviceInstances;
  3. @implementation KAIBatteryCell
  4. -(instancetype)initWithFrame:(CGRect)arg1 device:(BCBatteryDevice *)device {
  5. self = [super initWithFrame:arg1];
  6. if(self) {
  7. self.device = device;
  8. NSString *deviceName = MSHookIvar<NSString *>(device, "_name");
  9. double batteryPercentage = MSHookIvar<long long>(device, "_percentCharge");
  10. BOOL charging = MSHookIvar<long long>(device, "_charging");
  11. BOOL LPM = MSHookIvar<BOOL>(device, "_batterySaverModeActive");
  12. UIView *blank;
  13. if(bannerStyle==1) {
  14. if(kCFCoreFoundationVersionNumber > 1600) {
  15. blank = [[[objc_getClass("MTMaterialView") class] alloc] _initWithRecipe:1 configuration:1 initialWeighting:1 scaleAdjustment:nil];
  16. } else if(kCFCoreFoundationVersionNumber < 1600) {
  17. blank = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
  18. }
  19. } else if(bannerStyle==2) {
  20. blank = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleDark]];
  21. } else if(bannerStyle==3) {
  22. blank = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
  23. }
  24. blank.layer.masksToBounds = YES;
  25. blank.layer.continuousCorners = YES;
  26. blank.layer.cornerRadius = cornerRadius;
  27. NSString *labelText = [NSString stringWithFormat:@"%@", deviceName];
  28. self.label = [[UILabel alloc] init];
  29. if(!hideDeviceLabel) {
  30. [self.label setFont:[UIFont systemFontOfSize:16]];
  31. } else if(hideDeviceLabel) {
  32. [self.label setFont:[UIFont systemFontOfSize:0]];
  33. }
  34. [self.label setTextColor:[UIColor whiteColor]];
  35. self.label.lineBreakMode = NSLineBreakByWordWrapping;
  36. self.label.numberOfLines = 1;
  37. [self.label setText:labelText];
  38. self.battery = [[_UIBatteryView alloc] init];
  39. self.battery.chargePercent = (batteryPercentage*0.01);
  40. self.percentLabel = [[UILabel alloc] init];
  41. self.battery.showsPercentage = NO;
  42. if(hidePercent) {
  43. [self.percentLabel setFont:[UIFont systemFontOfSize:0]];
  44. } else {
  45. [self.percentLabel setFont:[UIFont systemFontOfSize:14]];
  46. }
  47. [self.percentLabel setTextColor:[UIColor whiteColor]];
  48. self.percentLabel.lineBreakMode = NSLineBreakByWordWrapping;
  49. [self.percentLabel setTextAlignment:NSTextAlignmentRight];
  50. self.percentLabel.numberOfLines = 1;
  51. [self.percentLabel setText:[NSString stringWithFormat:@"%ld%%", (long)((NSInteger) batteryPercentage)]];
  52. if(charging) self.battery.chargingState = 1;
  53. self.battery.showsInlineChargingIndicator = YES;
  54. if(LPM) self.battery.saverModeActive = YES;
  55. if(kCFCoreFoundationVersionNumber > 1600) {
  56. [self.battery setBodyColorAlpha:1.0];
  57. [self.battery setPinColorAlpha:1.0];
  58. }
  59. UIImage *glyph = [device glyph];
  60. self.glyphView = [[UIImageView alloc] init];
  61. self.glyphView.contentMode = UIViewContentModeScaleAspectFit;
  62. [self.glyphView setImage:glyph];
  63. [self addSubview:blank];
  64. [self addSubview:self.percentLabel];
  65. [self addSubview:self.label];
  66. [self addSubview:self.battery];
  67. [self addSubview:self.glyphView];
  68. blank.translatesAutoresizingMaskIntoConstraints = NO;
  69. if(bannerAlign==2) { //center
  70. [blank.centerXAnchor constraintEqualToAnchor:self.centerXAnchor constant:horizontalOffset].active = YES;
  71. } else if(bannerAlign==1) { //left
  72. [blank.leftAnchor constraintEqualToAnchor:self.leftAnchor constant:horizontalOffset].active = YES;
  73. } else if(bannerAlign==3) { //right
  74. [blank.rightAnchor constraintEqualToAnchor:self.rightAnchor constant:horizontalOffset].active = YES;
  75. }
  76. [blank.topAnchor constraintEqualToAnchor:self.topAnchor].active = YES;
  77. [blank.widthAnchor constraintEqualToConstant:((self.frame.size.width) + bannerWidthFactor)].active = YES;
  78. [blank.heightAnchor constraintEqualToConstant:bannerHeight].active = YES;
  79. self.percentLabel.translatesAutoresizingMaskIntoConstraints = NO;
  80. [self.percentLabel.leftAnchor constraintEqualToAnchor:blank.rightAnchor constant:(- 96)].active = YES;
  81. [self.percentLabel.centerYAnchor constraintEqualToAnchor:blank.centerYAnchor].active = YES;
  82. [self.percentLabel.widthAnchor constraintEqualToConstant:37].active = YES;
  83. [self.percentLabel.heightAnchor constraintEqualToConstant:12].active = YES;
  84. self.label.translatesAutoresizingMaskIntoConstraints = NO;
  85. [self.label.leftAnchor constraintEqualToAnchor:self.glyphView.rightAnchor constant:4.5].active = YES;
  86. [self.label.centerYAnchor constraintEqualToAnchor:blank.centerYAnchor].active = YES;
  87. [self.label.rightAnchor constraintEqualToAnchor:self.percentLabel.leftAnchor constant:-4.5].active = YES;
  88. [self.label.heightAnchor constraintEqualToConstant:25].active = YES;
  89. self.glyphView.translatesAutoresizingMaskIntoConstraints = NO;
  90. [self.glyphView.leftAnchor constraintEqualToAnchor:blank.leftAnchor constant:20.5].active = YES;
  91. [self.glyphView.centerYAnchor constraintEqualToAnchor:blank.centerYAnchor].active = YES;
  92. [self.glyphView.widthAnchor constraintEqualToConstant:glyphSize].active = YES;
  93. [self.glyphView.heightAnchor constraintEqualToConstant:glyphSize].active = YES;
  94. self.battery.translatesAutoresizingMaskIntoConstraints = NO;
  95. [self.battery.leftAnchor constraintEqualToAnchor:blank.rightAnchor constant:(- 49)].active = YES;
  96. [self.battery.centerYAnchor constraintEqualToAnchor:blank.centerYAnchor].active = YES;
  97. [self.battery.widthAnchor constraintEqualToConstant:20].active = YES;
  98. [self.battery.heightAnchor constraintEqualToConstant:10].active = YES;
  99. self.lastChargingState = charging;
  100. self.lastLPM = LPM;
  101. self.lastPercent = batteryPercentage;
  102. }
  103. return self;
  104. }
  105. -(void)updateInfo {
  106. NSString *deviceName = MSHookIvar<NSString *>(self.device, "_name");
  107. double batteryPercentage = MSHookIvar<long long>(self.device, "_percentCharge");
  108. BOOL charging = MSHookIvar<long long>(self.device, "_charging");
  109. BOOL LPM = MSHookIvar<BOOL>(self.device, "_batterySaverModeActive");
  110. self.label.text = [NSString stringWithFormat:@"%@", deviceName];
  111. [self.percentLabel setText:[NSString stringWithFormat:@"%ld%%", (long)((NSInteger) batteryPercentage)]];
  112. self.battery.chargePercent = (batteryPercentage*0.01);
  113. if(charging) { self.battery.chargingState = 1; } else { self.battery.chargingState = 0; }
  114. self.battery.showsInlineChargingIndicator = YES;
  115. if(LPM) self.battery.saverModeActive = YES;
  116. if(kCFCoreFoundationVersionNumber > 1600) {
  117. [self.battery setBodyColorAlpha:1.0];
  118. [self.battery setPinColorAlpha:1.0];
  119. }
  120. [self.percentLabel setText:[NSString stringWithFormat:@"%ld%%", (long)((NSInteger) batteryPercentage)]];
  121. self.battery.chargePercent = (batteryPercentage*0.01);
  122. [self.glyphView setImage:[self.device glyph]];
  123. self.lastChargingState = charging;
  124. self.lastLPM = LPM;
  125. self.lastPercent = batteryPercentage;
  126. }
  127. -(void)removeFromSuperview {
  128. [super removeFromSuperview];
  129. [deviceInstances removeObject:self];
  130. }
  131. +(instancetype)cellForDeviceIfExists:(BCBatteryDevice *)device {
  132. KAIBatteryCell *foundCell;
  133. for(KAIBatteryCell *cell in deviceInstances) {
  134. if(cell.device == device) {
  135. foundCell = cell;
  136. break;
  137. }
  138. }
  139. return foundCell;
  140. }
  141. @end