SDStatusBarManager.m 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. // --------------------------------------------------------------------------------
  2. // The MIT License (MIT)
  3. //
  4. // Copyright (c) 2014 Shiny Development
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining a copy
  7. // of this software and associated documentation files (the "Software"), to deal
  8. // in the Software without restriction, including without limitation the rights
  9. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. // copies of the Software, and to permit persons to whom the Software is
  11. // furnished to do so, subject to the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be included in all
  14. // copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. // SOFTWARE.
  23. // --------------------------------------------------------------------------------
  24. #import <UIKit/UIKit.h>
  25. #import "SDStatusBarManager.h"
  26. #import "SDStatusBarOverriderPre8_3.h"
  27. #import "SDStatusBarOverriderPost8_3.h"
  28. #import "SDStatusBarOverriderPost9_0.h"
  29. #import "SDStatusBarOverriderPost9_3.h"
  30. #import "SDStatusBarOverriderPost10_0.h"
  31. #import "SDStatusBarOverriderPost10_3.h"
  32. #import "SDStatusBarOverriderPost11_0.h"
  33. #import "SDStatusBarOverriderPost12_0.h"
  34. static NSString * const SDStatusBarManagerUsingOverridesKey = @"using_overrides";
  35. static NSString * const SDStatusBarManagerBluetoothStateKey = @"bluetooth_state";
  36. static NSString * const SDStatusBarManagerNetworkTypeKey = @"network_type";
  37. static NSString * const SDStatusBarManagerCarrierNameKey = @"carrier_name";
  38. static NSString * const SDStatusBarManagerTimeStringKey = @"time_string";
  39. static NSString * const SDStatusBarManagerDateStringKey = @"date_string";
  40. @interface SDStatusBarManager ()
  41. @property (nonatomic, strong) NSUserDefaults *userDefaults;
  42. @property (nonatomic, strong) id <SDStatusBarOverrider> overrider;
  43. @end
  44. @implementation SDStatusBarManager
  45. - (instancetype)init
  46. {
  47. self = [super init];
  48. if (self) {
  49. // Set any defaults for the status bar
  50. self.batteryDetailEnabled = YES;
  51. self.iPadDateEnabled = YES;
  52. self.iPadGsmSignalEnabled = NO;
  53. }
  54. return self;
  55. }
  56. - (void)enableOverrides
  57. {
  58. self.usingOverrides = YES;
  59. self.overrider.timeString = [self localizedTimeString];
  60. self.overrider.dateString = [self localizedDateString];
  61. self.overrider.carrierName = self.carrierName;
  62. self.overrider.bluetoothEnabled = self.bluetoothState != SDStatusBarManagerBluetoothHidden;
  63. self.overrider.bluetoothConnected = self.bluetoothState == SDStatusBarManagerBluetoothVisibleConnected;
  64. self.overrider.batteryDetailEnabled = self.batteryDetailEnabled;
  65. self.overrider.networkType = self.networkType;
  66. self.overrider.iPadDateEnabled = self.iPadDateEnabled;
  67. self.overrider.iPadGsmSignalEnabled = self.iPadGsmSignalEnabled;
  68. [self.overrider enableOverrides];
  69. }
  70. - (void)disableOverrides
  71. {
  72. self.usingOverrides = NO;
  73. [self.overrider disableOverrides];
  74. }
  75. #pragma mark Properties
  76. - (BOOL)usingOverrides
  77. {
  78. return [self.userDefaults boolForKey:SDStatusBarManagerUsingOverridesKey];
  79. }
  80. - (void)setUsingOverrides:(BOOL)usingOverrides
  81. {
  82. [self.userDefaults setBool:usingOverrides forKey:SDStatusBarManagerUsingOverridesKey];
  83. }
  84. - (void)setBluetoothState:(SDStatusBarManagerBluetoothState)bluetoothState
  85. {
  86. if (self.bluetoothState == bluetoothState) return;
  87. [self.userDefaults setValue:@(bluetoothState) forKey:SDStatusBarManagerBluetoothStateKey];
  88. if (self.usingOverrides) {
  89. [self enableOverrides];
  90. }
  91. }
  92. - (SDStatusBarManagerBluetoothState)bluetoothState
  93. {
  94. return [[self.userDefaults valueForKey:SDStatusBarManagerBluetoothStateKey] integerValue];
  95. }
  96. - (void)setNetworkType:(SDStatusBarManagerNetworkType)networkType
  97. {
  98. if (self.networkType == networkType) return;
  99. [self.userDefaults setValue:@(networkType) forKey:SDStatusBarManagerNetworkTypeKey];
  100. if (self.usingOverrides) {
  101. [self enableOverrides];
  102. }
  103. }
  104. - (SDStatusBarManagerNetworkType)networkType
  105. {
  106. return [[self.userDefaults valueForKey:SDStatusBarManagerNetworkTypeKey] integerValue];
  107. }
  108. - (void)setCarrierName:(NSString *)carrierName
  109. {
  110. if ([self.carrierName isEqualToString:carrierName]) return;
  111. [self.userDefaults setObject:carrierName forKey:SDStatusBarManagerCarrierNameKey];
  112. if (self.usingOverrides) {
  113. [self enableOverrides];
  114. }
  115. }
  116. - (NSString *)carrierName
  117. {
  118. return [self.userDefaults valueForKey:SDStatusBarManagerCarrierNameKey];
  119. }
  120. - (void)setTimeString:(NSString *)timeString
  121. {
  122. if ([self.timeString isEqualToString:timeString]) return;
  123. [self.userDefaults setObject:timeString forKey:SDStatusBarManagerTimeStringKey];
  124. if (self.usingOverrides) {
  125. [self enableOverrides];
  126. }
  127. }
  128. - (NSString *)timeString
  129. {
  130. return [self.userDefaults valueForKey:SDStatusBarManagerTimeStringKey];
  131. }
  132. - (void)setDateString:(NSString *)dateString {
  133. if ([self.dateString isEqualToString:dateString]) return;
  134. [self.userDefaults setObject:dateString forKey:SDStatusBarManagerDateStringKey];
  135. if (self.usingOverrides) {
  136. [self enableOverrides];
  137. }
  138. }
  139. - (NSString *)dateString
  140. {
  141. return [self.userDefaults valueForKey:SDStatusBarManagerDateStringKey];
  142. }
  143. - (NSUserDefaults *)userDefaults
  144. {
  145. if (!_userDefaults) {
  146. _userDefaults = [[NSUserDefaults alloc] initWithSuiteName:@"com.shinydevelopment.SDStatusBarManager"];
  147. }
  148. return _userDefaults;
  149. }
  150. + (id<SDStatusBarOverrider>)overriderForSystemVersion:(NSString *)systemVersion
  151. {
  152. id<SDStatusBarOverrider> overrider = nil;
  153. NSProcessInfo *pi = [NSProcessInfo processInfo];
  154. if ([pi isOperatingSystemAtLeastVersion:(NSOperatingSystemVersion){ 12, 0, 0 }]) {
  155. overrider = [SDStatusBarOverriderPost12_0 new];
  156. } else if ([pi isOperatingSystemAtLeastVersion:(NSOperatingSystemVersion){ 11, 0, 0 }]) {
  157. overrider = [SDStatusBarOverriderPost11_0 new];
  158. } else if ([pi isOperatingSystemAtLeastVersion:(NSOperatingSystemVersion){ 10, 3, 0 }]) {
  159. overrider = [SDStatusBarOverriderPost10_3 new];
  160. } else if ([pi isOperatingSystemAtLeastVersion:(NSOperatingSystemVersion){ 10, 0, 0 }]) {
  161. overrider = [SDStatusBarOverriderPost10_0 new];
  162. } else if ([pi isOperatingSystemAtLeastVersion:(NSOperatingSystemVersion){ 9, 3, 0 }]) {
  163. overrider = [SDStatusBarOverriderPost9_3 new];
  164. } else if ([pi isOperatingSystemAtLeastVersion:(NSOperatingSystemVersion){ 9, 0, 0 }]) {
  165. overrider = [SDStatusBarOverriderPost9_0 new];
  166. } else if ([pi isOperatingSystemAtLeastVersion:(NSOperatingSystemVersion){ 8, 3, 0 }]) {
  167. overrider = [SDStatusBarOverriderPost8_3 new];
  168. } else {
  169. overrider = [SDStatusBarOverriderPre8_3 new];
  170. }
  171. return overrider;
  172. }
  173. - (id<SDStatusBarOverrider>)overrider
  174. {
  175. if (!_overrider) {
  176. NSString *systemVersion = [[UIDevice currentDevice] systemVersion];
  177. _overrider = [SDStatusBarManager overriderForSystemVersion:systemVersion];
  178. }
  179. return _overrider;
  180. }
  181. #pragma mark Date helper
  182. - (NSString *)localizedTimeString
  183. {
  184. if (self.timeString.length > 0) {
  185. return self.timeString;
  186. }
  187. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  188. formatter.dateStyle = NSDateFormatterNoStyle;
  189. formatter.timeStyle = NSDateFormatterShortStyle;
  190. NSDateComponents *components = [[NSCalendar currentCalendar] components: NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:[NSDate date]];
  191. components.hour = 9;
  192. components.minute = 41;
  193. return [formatter stringFromDate:[[NSCalendar currentCalendar] dateFromComponents:components]];
  194. }
  195. - (NSString *)localizedDateString
  196. {
  197. if (self.dateString.length > 0) {
  198. return self.dateString;
  199. }
  200. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  201. formatter.dateFormat = [[NSDateFormatter dateFormatFromTemplate:@"EEE MMM d" options:0 locale:NSLocale.currentLocale] stringByReplacingOccurrencesOfString:@"," withString:@""];
  202. NSDateComponents *components = [[NSCalendar currentCalendar] components: NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond fromDate:[NSDate date]];
  203. components.day = 9;
  204. components.month = 1;
  205. components.year = 2007;
  206. return [formatter stringFromDate:[[NSCalendar currentCalendar] dateFromComponents:components]];
  207. }
  208. #pragma mark Singleton instance
  209. + (SDStatusBarManager *)sharedInstance
  210. {
  211. static dispatch_once_t predicate = 0;
  212. __strong static id sharedObject = nil;
  213. dispatch_once(&predicate, ^{ sharedObject = [[self alloc] init]; });
  214. return sharedObject;
  215. }
  216. @end