-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathCalendar.xm
executable file
·279 lines (244 loc) · 10.5 KB
/
Calendar.xm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#import "core/ANEMSettingsManager.h"
#import "UIColor+HTMLColors.h"
@interface SBCalendarApplicationIcon : NSObject
- (UIFont *)numberFont;
- (UIColor *)colorForDayOfWeek;
- (void)drawTextIntoCurrentContextWithImageSize:(CGSize)imageSize iconBase:(UIImage *)base;
@end
static NSDictionary *dateSettings, *daySettings;
static bool calendarSettingsLoaded = NO;
static CGFloat dateXoffset = 0.0f;
static CGFloat dateYoffset = 0.0f;
static CGFloat dateShadowXoffset = 0.0f;
static CGFloat dateShadowYoffset = 0.0f;
static CGFloat dateShadowBlurRadius = 0.0f;
static UIColor *dateTextColor = nil;
static NSString *dateTextCase = nil;
static UIColor *dateShadowColor = nil;
static NSString *dayFont = nil;
static CGFloat dayFontSize = 10.0f;
static CGFloat dayXoffset = 0.0f;
static CGFloat dayYoffset = 0.0f;
static CGFloat dayShadowXoffset = 0.0f;
static CGFloat dayShadowYoffset = 0.0f;
static CGFloat dayShadowBlurRadius = 0.0f;
static NSString *dayTextCase = nil;
static UIColor *dayShadowColor = nil;
static NSMutableArray *themesToShame = nil;
static void getCalendarSettings(){
if (calendarSettingsLoaded)
return;
calendarSettingsLoaded = YES;
if ([[[NSBundle mainBundle] bundleIdentifier] isEqualToString:@"com.anemonetheming.anemone"]){
calendarSettingsLoaded = NO;
}
NSArray *themes = [[%c(ANEMSettingsManager) sharedManager] themeSettings];
NSString *themesDir = [[%c(ANEMSettingsManager) sharedManager] themesDir];
for (NSString *theme in themes)
{
NSString *path = [NSString stringWithFormat:@"%@/%@.theme/Info.plist",themesDir,theme];
if (SupportsNoExtensionDir && ![[NSFileManager defaultManager] fileExistsAtPath:path]){
path = [NSString stringWithFormat:@"%@/%@/Info.plist",themesDir,theme];
}
NSDictionary *themeDict = [NSDictionary dictionaryWithContentsOfFile:path];
if (dateSettings == nil)
{
dateSettings = themeDict[@"CalendarIconDateSettings"];
}
if (daySettings == nil){
daySettings = themeDict[@"CalendarIconDaySettings"];
}
if (!(dateSettings == nil || [dateSettings isKindOfClass:[NSDictionary class]]) || !(daySettings == nil || [daySettings isKindOfClass:[NSDictionary class]])){
dateSettings = nil;
daySettings = nil;
if (themesToShame == nil){
themesToShame = [[NSMutableArray alloc] init];
}
[themesToShame addObject:theme];
}
}
[dateSettings retain];
[daySettings retain];
}
static void loadCalendarSettings(){
if (calendarSettingsLoaded)
return;
getCalendarSettings();
dateXoffset = 0.0f;
dateYoffset = 0.0f;
dateShadowXoffset = 0.0f;
dateShadowYoffset = 0.0f;
dateShadowBlurRadius = 0.0f;
dateTextColor = nil;
dateShadowColor = nil;
[dateTextCase release];
dateTextCase = nil;
dayFont = nil;
dayFontSize = 10.0f;
dayXoffset = 0.0f;
dayYoffset = 0.0f;
dayShadowXoffset = 0.0f;
dayShadowYoffset = 0.0f;
dayShadowBlurRadius = 0.0f;
dayShadowColor = nil;
[dayTextCase release];
dayTextCase = nil;
dateTextColor = [UIColor blackColor];
dateShadowColor = [UIColor clearColor];
dayFont = @"HelveticaNeue";
if (kCFCoreFoundationVersionNumber > 1240)
dayFont = @".SFUIText-Regular";
if (kCFCoreFoundationVersionNumber > 1333)
dayFont = @".SFUIText";
dayShadowColor = [UIColor clearColor];
if ([dateSettings objectForKey:@"TextXoffset"])
dateXoffset = [[dateSettings objectForKey:@"TextXoffset"] floatValue];
if ([dateSettings objectForKey:@"TextYoffset"])
dateYoffset = [[dateSettings objectForKey:@"TextYoffset"] floatValue];
if ([dateSettings objectForKey:@"TextColor"])
dateTextColor = [[UIColor anem_colorWithCSS:[dateSettings objectForKey:@"TextColor"]] retain];
if ([dateSettings objectForKey:@"TextCase"])
dateTextCase = [[[dateSettings objectForKey:@"TextCase"] lowercaseString] retain];
if ([dateSettings objectForKey:@"ShadowXoffset"])
dateShadowXoffset = [[dateSettings objectForKey:@"ShadowXoffset"] floatValue];
if ([dateSettings objectForKey:@"ShadowYoffset"])
dateShadowYoffset = [[dateSettings objectForKey:@"ShadowYoffset"] floatValue];
if ([dateSettings objectForKey:@"ShadowBlurRadius"])
dateShadowBlurRadius = [[dateSettings objectForKey:@"ShadowBlurRadius"] floatValue];
if ([dateSettings objectForKey:@"ShadowColor"])
dateShadowColor = [[UIColor anem_colorWithCSS:[dateSettings objectForKey:@"ShadowColor"]] retain];
if ([daySettings objectForKey:@"FontName"])
dayFont = [daySettings objectForKey:@"FontName"];
if ([daySettings objectForKey:@"FontSize"])
dayFontSize = [[daySettings objectForKey:@"FontSize"] floatValue];
if ([daySettings objectForKey:@"TextCase"])
dayTextCase = [[[daySettings objectForKey:@"TextCase"] lowercaseString] retain];
if ([daySettings objectForKey:@"TextXoffset"])
dayXoffset = [[daySettings objectForKey:@"TextXoffset"] floatValue];
if ([daySettings objectForKey:@"TextYoffset"])
dayYoffset = [[daySettings objectForKey:@"TextYoffset"] floatValue];
if ([daySettings objectForKey:@"ShadowXoffset"])
dayShadowXoffset = [[daySettings objectForKey:@"ShadowXoffset"] floatValue];
if ([daySettings objectForKey:@"ShadowYoffset"])
dayShadowYoffset = [[daySettings objectForKey:@"ShadowYoffset"] floatValue];
if ([daySettings objectForKey:@"ShadowBlurRadius"])
dayShadowBlurRadius = [[daySettings objectForKey:@"ShadowBlurRadius"] floatValue];
if ([daySettings objectForKey:@"ShadowColor"])
dayShadowColor = [[UIColor anem_colorWithCSS:[daySettings objectForKey:@"ShadowColor"]] retain];
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad){
dateYoffset+=14.0f;
dayYoffset+=8.0f;
} else{
dateYoffset+=12.0f;
dayYoffset+=6.0f;
}
}
%hook SBCalendarApplicationIcon
- (UIImage *)_compositedIconImageForFormat:(int)format withBaseImageProvider:(UIImage *(^)())imageProvider {
UIImage *baseImage = imageProvider();
UIGraphicsBeginImageContextWithOptions(baseImage.size, NO, baseImage.scale);
[self drawTextIntoCurrentContextWithImageSize:baseImage.size iconBase:baseImage];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
- (void)_drawIconIntoCurrentContextWithImageSize:(CGSize)imageSize iconBase:(UIImage *)base {
[self drawTextIntoCurrentContextWithImageSize:imageSize iconBase:base];
}
%new;
- (void)drawTextIntoCurrentContextWithImageSize:(CGSize)imageSize iconBase:(UIImage *)base {
loadCalendarSettings();
CGContextRef ctx = UIGraphicsGetCurrentContext();
if (ctx == nil)
return;
[base drawInRect:CGRectMake(0,0,imageSize.width,imageSize.height)];
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateStyle:NSDateFormatterNoStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
[dateFormatter setLocale:[NSLocale currentLocale]];
NSString *dateFont = @".SFUIDisplay-Ultralight";
CGFloat dateFontSize = 39.5;
if ([self respondsToSelector:@selector(numberFont)]){
dateFont = [[self numberFont] fontName];
dateFontSize = [[self numberFont] pointSize];
}
if ([dateSettings objectForKey:@"FontName"])
dateFont = [dateSettings objectForKey:@"FontName"];
if ([dateSettings objectForKey:@"FontSize"])
dateFontSize = [[dateSettings objectForKey:@"FontSize"] floatValue];
NSDate *date = [NSDate date];
[dateFormatter setDateFormat:@"d"];
NSString *day = [dateFormatter stringFromDate:date];
if ([dateTextCase isEqualToString:@"lowercase"])
day = [day lowercaseString];
else if ([dateTextCase isEqualToString:@"uppercase"])
day = [day uppercaseString];
UIFont *numberFont = [UIFont fontWithName:dateFont size:dateFontSize];
CGSize size = CGSizeZero;
if (numberFont)
size = [day sizeWithAttributes:@{NSFontAttributeName:numberFont}];
CGContextSetShadowWithColor(ctx, CGSizeMake(dateShadowXoffset,dateShadowYoffset), dateShadowBlurRadius, dateShadowColor.CGColor);
CGContextSetAlpha(ctx, CGColorGetAlpha(dateTextColor.CGColor));
[day drawAtPoint:CGPointMake(dateXoffset + ((imageSize.width-size.width)/2.0f),dateYoffset) withAttributes:@{NSFontAttributeName:numberFont, NSForegroundColorAttributeName:dateTextColor}];
UIColor *dayTextColor = [UIColor redColor];
if ([self respondsToSelector:@selector(colorForDayOfWeek)])
dayTextColor = [self colorForDayOfWeek];
if ([daySettings objectForKey:@"TextColor"])
dayTextColor = [UIColor anem_colorWithCSS:[daySettings objectForKey:@"TextColor"]];
[dateFormatter setDateFormat:@"EEEE"];
NSString *dayOfWeek = [dateFormatter stringFromDate:date];
if ([dayTextCase isEqualToString:@"lowercase"])
dayOfWeek = [dayOfWeek lowercaseString];
else if ([dayTextCase isEqualToString:@"uppercase"])
dayOfWeek = [dayOfWeek uppercaseString];
UIFont *dayOfWeekFont = [UIFont fontWithName:dayFont size:dayFontSize];
if (dayOfWeekFont)
size = [dayOfWeek sizeWithAttributes:@{NSFontAttributeName:dayOfWeekFont}];
CGContextSetShadowWithColor(ctx, CGSizeMake(dayShadowXoffset,dayShadowYoffset), dayShadowBlurRadius, dayShadowColor.CGColor);
CGContextSetAlpha(ctx, CGColorGetAlpha(dayTextColor.CGColor));
[dayOfWeek drawAtPoint:CGPointMake(dayXoffset + ((imageSize.width-size.width)/2.0f),dayYoffset) withAttributes:@{NSFontAttributeName:dayOfWeekFont, NSForegroundColorAttributeName:dayTextColor}];
}
%end
%hook SBLockScreenViewController
-(void)finishUIUnlockFromSource:(int)source {
%orig;
if (!themesToShame)
return;
NSString *themesToShameString = [themesToShame componentsJoinedByString:@","];
NSString *messageString = [NSString stringWithFormat:@"The following theme(s) have bad Calendar icon settings in their Info.plist file: %@. Their calendar settings have been disabled. Please contact the theme designer to fix it. You may delete their Info.plist files (or disable the themes) to remove this error message.",themesToShameString];
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Crash Prevented"
message:messageString
delegate:nil
cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[alert show];
[alert release];
#pragma clang diagnostic pop
}
%end
@interface AnemoneCalendarIconEventHandler: NSObject <AnemoneEventHandler>
- (void)reloadTheme;
@end
@implementation AnemoneCalendarIconEventHandler
- (void)reloadTheme {
calendarSettingsLoaded = NO;
if (dateSettings){
[dateSettings release];
dateSettings = nil;
}
if (daySettings){
[daySettings release];
daySettings = nil;
}
}
@end
%ctor {
if (kCFCoreFoundationVersionNumber > MaxSupportedCFVersion)
return;
if (objc_getClass("ANEMSettingsManager") == nil){
dlopen("/Library/MobileSubstrate/DynamicLibraries/AnemoneCore.dylib",RTLD_LAZY);
}
[[%c(ANEMSettingsManager) sharedManager] addEventHandler:[AnemoneCalendarIconEventHandler new]];
}