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.

171 rinda
8.1KB

  1. #import "KAIBattery.h"
  2. KAIBattery *instance;
  3. @implementation KAIBattery
  4. -(instancetype)init {
  5. self = [super init];
  6. instance = self;
  7. if (self) {
  8. [self updateBattery];
  9. self.userInteractionEnabled = NO;
  10. }
  11. return self;
  12. }
  13. long long batteryPercentage;
  14. long long lastPercentage;
  15. -(void)updateBattery {
  16. dispatch_async(dispatch_get_main_queue(), ^{
  17. if(!self.isUpdating) {
  18. self.isUpdating = YES;
  19. self.number = 0;
  20. float y = 0;
  21. BCBatteryDeviceController *bcb = [BCBatteryDeviceController sharedInstance];
  22. NSArray *devices = MSHookIvar<NSArray *>(bcb, "_sortedDevices");
  23. for( UIView *view in self.subviews ) {
  24. @try {
  25. [view removeFromSuperview];
  26. } @catch (NSException *exception) {
  27. //Panik
  28. }
  29. }
  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. BOOL shouldAdd = NO;
  36. if(showAll) {
  37. shouldAdd = YES;
  38. } else if(!showAll && charging) {
  39. shouldAdd = YES;
  40. }
  41. if(shouldAdd) {
  42. UIView *blank;
  43. if(bannerStyle==1) {
  44. if(kCFCoreFoundationVersionNumber > 1600) {
  45. blank = [[[objc_getClass("MTMaterialView") class] alloc] _initWithRecipe:1 configuration:1 initialWeighting:1 scaleAdjustment:nil];
  46. } else if(kCFCoreFoundationVersionNumber < 1600) {
  47. blank = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
  48. }
  49. } else if(bannerStyle==2) {
  50. blank = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleDark]];
  51. } else if(bannerStyle==3) {
  52. blank = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
  53. }
  54. blank.layer.masksToBounds = YES;
  55. blank.layer.continuousCorners = YES;
  56. blank.layer.cornerRadius = cornerRadius;
  57. NSString *labelText = [NSString stringWithFormat:@"%@", deviceName];
  58. UILabel *label = [[UILabel alloc] init];
  59. if(!hideDeviceLabel) {
  60. [label setFont:[UIFont systemFontOfSize:16]];
  61. } else if(hideDeviceLabel) {
  62. [label setFont:[UIFont systemFontOfSize:0]];
  63. }
  64. [label setTextColor:[UIColor whiteColor]];
  65. label.lineBreakMode = NSLineBreakByWordWrapping;
  66. label.numberOfLines = 0;
  67. [label setText:labelText];
  68. _UIBatteryView *battery = [[_UIBatteryView alloc] init];
  69. battery.chargePercent = (batteryPercentage*0.01);
  70. UILabel *percentLabel = [[UILabel alloc] init];
  71. battery.showsPercentage = NO;
  72. if(hidePercent) {
  73. [percentLabel setFont:[UIFont systemFontOfSize:0]];
  74. } else {
  75. [percentLabel setFont:[UIFont systemFontOfSize:14]];
  76. }
  77. [percentLabel setTextColor:[UIColor whiteColor]];
  78. percentLabel.lineBreakMode = NSLineBreakByWordWrapping;
  79. [percentLabel setTextAlignment:NSTextAlignmentRight];
  80. percentLabel.numberOfLines = 0;
  81. [percentLabel setText:[NSString stringWithFormat:@"%ld%%", (long)((NSInteger) batteryPercentage)]];
  82. if(charging) battery.chargingState = 1;
  83. battery.showsInlineChargingIndicator = YES;
  84. if(LPM) battery.saverModeActive = YES;
  85. if(kCFCoreFoundationVersionNumber > 1600) {
  86. [battery setBodyColorAlpha:1.0];
  87. [battery setPinColorAlpha:1.0];
  88. }
  89. UIImage *glyph = [device glyph];
  90. UIImageView *glyphView = [[UIImageView alloc] init];
  91. glyphView.contentMode = UIViewContentModeScaleAspectFit;
  92. [glyphView setImage:glyph];
  93. [self addSubview:blank];
  94. [self addSubview:percentLabel];
  95. [self addSubview:label];
  96. [self addSubview:battery];
  97. [self addSubview:glyphView];
  98. blank.translatesAutoresizingMaskIntoConstraints = NO;
  99. if(bannerAlign==2) { //center
  100. [blank.centerXAnchor constraintEqualToAnchor:self.centerXAnchor constant:horizontalOffset].active = YES;
  101. } else if(bannerAlign==1) { //left
  102. [blank.leftAnchor constraintEqualToAnchor:self.leftAnchor constant:horizontalOffset].active = YES;
  103. } else if(bannerAlign==3) { //right
  104. [blank.rightAnchor constraintEqualToAnchor:self.rightAnchor constant:horizontalOffset].active = YES;
  105. }
  106. [blank.topAnchor constraintEqualToAnchor:self.topAnchor constant:y].active = YES;
  107. [blank.widthAnchor constraintEqualToConstant:((self.superview.bounds.size.width - 16) + bannerWidthFactor)].active = YES;
  108. [blank.heightAnchor constraintEqualToConstant:bannerHeight].active = YES;
  109. //percentLabel.frame = CGRectMake(self.superview.bounds.size.width - 16 - 94,35 + y,36,12);
  110. percentLabel.translatesAutoresizingMaskIntoConstraints = NO;
  111. [percentLabel.leftAnchor constraintEqualToAnchor:blank.rightAnchor constant:(- 94)].active = YES;
  112. [percentLabel.centerYAnchor constraintEqualToAnchor:blank.centerYAnchor].active = YES;
  113. [percentLabel.widthAnchor constraintEqualToConstant:35].active = YES;
  114. [percentLabel.heightAnchor constraintEqualToConstant:12].active = YES;
  115. //label.frame = CGRectMake(65.5,27.5 + y,275,25);
  116. label.translatesAutoresizingMaskIntoConstraints = NO;
  117. [label.leftAnchor constraintEqualToAnchor:glyphView.rightAnchor constant:4.5].active = YES;
  118. [label.centerYAnchor constraintEqualToAnchor:blank.centerYAnchor].active = YES;
  119. [label.rightAnchor constraintEqualToAnchor:percentLabel.leftAnchor constant:-4.5].active = YES;
  120. [label.heightAnchor constraintEqualToConstant:25].active = YES;
  121. //glyphView.frame = CGRectMake(20.5,18.5 + y,40,40);
  122. glyphView.translatesAutoresizingMaskIntoConstraints = NO;
  123. [glyphView.leftAnchor constraintEqualToAnchor:blank.leftAnchor constant:20.5].active = YES;
  124. [glyphView.centerYAnchor constraintEqualToAnchor:blank.centerYAnchor].active = YES;
  125. [glyphView.widthAnchor constraintEqualToConstant:glyphSize].active = YES;
  126. [glyphView.heightAnchor constraintEqualToConstant:glyphSize].active = YES;
  127. //battery.frame = CGRectMake(self.superview.bounds.size.width - 16 - 49,35 + y,20,10);
  128. battery.translatesAutoresizingMaskIntoConstraints = NO;
  129. [battery.leftAnchor constraintEqualToAnchor:blank.rightAnchor constant:(- 49)].active = YES;
  130. [battery.centerYAnchor constraintEqualToAnchor:blank.centerYAnchor].active = YES;
  131. [battery.widthAnchor constraintEqualToConstant:20].active = YES;
  132. [battery.heightAnchor constraintEqualToConstant:10].active = YES;
  133. y+=bannerHeight + spacing;
  134. self.number +=1;
  135. //blank.alpha = 0.8;
  136. }
  137. }
  138. //[self.heightAnchor constraintEqualToConstant:(self.number * 85)].active = YES;
  139. self.isUpdating = NO;
  140. }
  141. });
  142. }
  143. +(KAIBattery *)sharedInstance {
  144. return instance;
  145. }
  146. @end