Advertisement
Guest User

tweak

a guest
May 19th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.86 KB | None | 0 0
  1. #import <substrate.h>
  2. #import "onlyonenotificationflipswitch/FSSwitchDataSource.h"
  3. #import "onlyonenotificationflipswitch/FSSwitchPanel.h"
  4. #import "onlyonenotificationflipswitch/FSSwitchState.h"
  5. #import "BBBulletin.h"
  6.  
  7.  
  8. @interface NCNotificationRequest : NSObject{
  9.  
  10. }
  11. @property (nonatomic, readonly) BBBulletin *bulletin;
  12. @property (nonatomic, readonly, copy) NSString *categoryIdentifier;
  13.  
  14. @end
  15.  
  16. @interface SBBulletinBannerItem
  17. -(id)seedBulletin;
  18. @end
  19.  
  20. #define DEBUG
  21. #ifdef DEBUG
  22. #define DebugLog(fmt, ...) NSLog((@"[OnlyOneNotification] " fmt), ##__VA_ARGS__)
  23. #else
  24. #define DebugLog(s, ...)
  25. #endif
  26. #define IS_OS_8_OR_HIGHER (UIDevice.currentDevice.systemVersion.floatValue >= 8.0)
  27.  
  28. static NSDictionary *prefs;
  29. static BOOL enabled = YES;
  30. static BOOL disableNoise = YES;
  31. static BOOL blockFirstAsWell = YES;
  32. static NSMutableDictionary *notifs = [[NSMutableDictionary alloc] init];
  33. static BOOL blockAfterFirstOfEachTitle = YES;
  34. static BOOL allowAfterAWhile = YES;
  35. static NSDate *lastNotificationTime = nil;
  36. static BOOL disableWhenRinger = NO;
  37. static BOOL onlyReEnableNoise = YES;
  38. static int timeToWait;
  39.  
  40. static unsigned long long pendingNotificationsCount;
  41.  
  42. static NSMutableDictionary *blacklistedApps = [[NSMutableDictionary alloc] init];
  43.  
  44. static void reloadSettings(CFNotificationCenterRef center,
  45. void *observer,
  46. CFStringRef name,
  47. const void *object,
  48. CFDictionaryRef userInfo)
  49. {
  50. prefs = [NSDictionary dictionaryWithContentsOfFile:@"/var/mobile/Library/Preferences/com.lodc.ios.oonsettings.plist"];
  51. NSArray *keys = [prefs allKeys];
  52. for (NSString *key in keys) {
  53. if ([key hasPrefix:@"blacklisted-"]) {
  54. NSString *bundleID = [key substringFromIndex:12];
  55. [blacklistedApps setObject:[prefs objectForKey:key] forKey:bundleID];
  56. }
  57. }
  58.  
  59. if (IS_OS_8_OR_HIGHER) { //CFPreferences should work on <iOS 8, but in my experience, it hasn't, so we'll stick with the preference plist.
  60. CFStringRef appID = CFSTR("com.lodc.ios.oonsettings");
  61. CFArrayRef keyList = CFPreferencesCopyKeyList(appID, kCFPreferencesCurrentUser, kCFPreferencesAnyHost);
  62. if (!keyList) {
  63. DebugLog(@"There's been an error getting the key list!");
  64. return;
  65. }
  66. prefs = (NSDictionary *)CFBridgingRelease(CFPreferencesCopyMultiple(keyList, appID, kCFPreferencesCurrentUser, kCFPreferencesAnyHost));
  67. }
  68.  
  69. id obj = [prefs objectForKey:@"enabled"];
  70. enabled = obj ? [obj boolValue] : YES;
  71. DebugLog(@"enabled = %d", enabled);
  72.  
  73. obj = [prefs objectForKey:@"disableNoise"];
  74. disableNoise = obj ? [obj boolValue] : YES;
  75. DebugLog(@"disableNoise = %d", disableNoise);
  76.  
  77. obj = [prefs objectForKey:@"blockFirstAsWell"];
  78. blockFirstAsWell = [obj boolValue];
  79. DebugLog(@"blockFirstAsWell = %d", blockFirstAsWell);
  80.  
  81. obj = [prefs objectForKey:@"blockAfterFirstOfEachTitle"];
  82. blockAfterFirstOfEachTitle = obj ? [obj boolValue] : YES;
  83. DebugLog(@"blockAfterFirstOfEachTitle = %d", blockAfterFirstOfEachTitle);
  84.  
  85. obj = [prefs objectForKey:@"allowAfterAWhile"];
  86. allowAfterAWhile = obj ? [obj boolValue] : YES;
  87. DebugLog(@"allowAfterAWhile = %d", allowAfterAWhile);
  88.  
  89. obj = [prefs objectForKey:@"disableWhenRinger"];
  90. disableWhenRinger = [obj boolValue];
  91. DebugLog(@"disableWhenRinger = %d", disableWhenRinger);
  92.  
  93. obj = [prefs objectForKey:@"onlyReEnableNoise"];
  94. onlyReEnableNoise = obj ? [obj boolValue] : YES;
  95. DebugLog(@"onlyReEnableNoise = %d", onlyReEnableNoise);
  96.  
  97. timeToWait = [[prefs objectForKey:@"timeToWait"] intValue] ?: 90;
  98. DebugLog(@"timeToWait = %d", timeToWait);
  99. }
  100.  
  101.  
  102. static BOOL hasItBeenAWhile()
  103. {
  104. NSDate *now = [NSDate date];
  105.  
  106. NSTimeInterval distanceBetweenDates = [now timeIntervalSinceDate:lastNotificationTime];
  107. NSLog(@"OSAMAx DISTANCE BETWEEN DATES !! %f", distanceBetweenDates);
  108. if (distanceBetweenDates > (timeToWait * 60))
  109. //if (distanceBetweenDates > 20)
  110. return true;
  111. return false;
  112. }
  113.  
  114. static int updateCount(NSString *item)
  115. {
  116. if (item == nil)
  117. return 0;
  118.  
  119. id i1 = [notifs objectForKey:item];
  120. int count;
  121. if (i1 == nil)
  122. count = 0;
  123. else
  124. count = [i1 intValue] + 1;
  125.  
  126. [notifs setObject:[NSNumber numberWithInt:count] forKey:item];
  127. return count;
  128. }
  129.  
  130. // iOS 11 stuff
  131. %hook NCNotificationPriorityList
  132.  
  133. - (unsigned long long)count{
  134. pendingNotificationsCount = %orig;
  135. // NSLog(@"OSAMAx pendingNotificationsCount %llu", pendingNotificationsCount);
  136. return %orig;
  137. }
  138.  
  139. %end
  140.  
  141. %hook SBNCScreenController
  142.  
  143. -(void)turnOnScreenIfPossibleForNotificationRequest:(NCNotificationRequest *)arg1{
  144. NSLog(@"OSAMAx PASSED 1 %@", [[arg1 bulletin] title]);
  145. BOOL sectionIDOkay = YES;
  146. BBBulletin *bulletin = [arg1 bulletin];
  147. sectionIDOkay = ![[blacklistedApps objectForKey:[bulletin sectionID]] boolValue];
  148.  
  149.  
  150. BOOL ringer = %c(FSSwitchPanel) ? ([[%c(FSSwitchPanel) sharedPanel] stateForSwitchIdentifier:@"com.a3tweaks.switch.ringer"] == FSSwitchStateOn ? YES : NO) : NO;
  151. BOOL inverse_disableForRinger = disableWhenRinger ? !ringer : YES;
  152. inverse_disableForRinger = onlyReEnableNoise ? YES : inverse_disableForRinger;
  153. if (pendingNotificationsCount > (blockFirstAsWell ? 0 : 1) && enabled && !blockAfterFirstOfEachTitle && inverse_disableForRinger && sectionIDOkay)
  154. {
  155. NSLog(@"OSAMAx We got in the first screen test");
  156. if (allowAfterAWhile) {
  157. NSLog(@"OSAMAx allow after a while is true");
  158. if (hasItBeenAWhile()) {
  159. NSLog(@"OSAMAx it has been a while");
  160.  
  161. } else {
  162. NSLog(@"OSAMAx IT HAS NOT BEEN A WHILE AND I'M BLOCKING A NOTIFICATION");
  163. lastNotificationTime = [NSDate date];
  164. return;
  165. }
  166. } else {
  167.  
  168. NSLog(@"OSAMAx allow after a while is false -- ");
  169. lastNotificationTime = [NSDate date];
  170. return;
  171. }
  172. }
  173.  
  174. if (enabled && blockAfterFirstOfEachTitle && updateCount([[arg1 bulletin] title]) >= (blockFirstAsWell ? 0 : 1) && inverse_disableForRinger && sectionIDOkay)
  175. {
  176. NSLog(@"OSAMAx wwwwe got in the second screen test");
  177. if (allowAfterAWhile)
  178. {
  179. if (hasItBeenAWhile())
  180. { }
  181. else
  182. {
  183. lastNotificationTime = [NSDate date];
  184. return;
  185. }
  186. }
  187. else
  188. {
  189. lastNotificationTime = [NSDate date];
  190. return;
  191. }
  192. }
  193.  
  194. %orig;
  195. lastNotificationTime = [NSDate date];
  196. }
  197.  
  198. %end
  199.  
  200. %hook SBNCSoundController
  201.  
  202. -(BOOL)canPlaySoundForNotificationRequest:(NCNotificationRequest *)arg1{
  203.  
  204. BOOL sectionIDOkay = YES;
  205. BBBulletin *bulletin = [arg1 bulletin];
  206. sectionIDOkay = ![[blacklistedApps objectForKey:[bulletin sectionID]] boolValue];
  207.  
  208. BOOL ringer = %c(FSSwitchPanel) ? ([[%c(FSSwitchPanel) sharedPanel] stateForSwitchIdentifier:@"com.a3tweaks.switch.ringer"] == FSSwitchStateOn ? YES : NO) : NO;
  209. BOOL inverse_disableForRinger = disableWhenRinger ? !ringer : YES;
  210.  
  211. if (((pendingNotificationsCount > 1 && disableNoise && enabled) || (enabled && blockFirstAsWell && disableNoise)) && inverse_disableForRinger && sectionIDOkay)
  212. return NO;
  213. return %orig;
  214. }
  215.  
  216. %end
  217.  
  218. %hook SBLockStateAggregator
  219. -(void)_updateLockState
  220. {
  221. NSLog(@"OSAMAx lock updated !! %@", notifs);
  222. %orig;
  223. notifs = [NSMutableDictionary dictionary]; // Clear state
  224. }
  225. %end
  226.  
  227. %ctor
  228. {
  229. CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), NULL, &reloadSettings, CFSTR("com.lodc.ios.oon/reloadSettings"), NULL, 0);
  230. reloadSettings(nil, nil, nil, nil, nil);
  231. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement