]> git.saurik.com Git - safemode-ios.git/blob - Tweak.xm
On iOS 7, fix Exit Safe Mode text (parrotgeek1).
[safemode-ios.git] / Tweak.xm
1 /* Cydia Substrate - Powerful Code Insertion Platform
2 * Copyright (C) 2008-2013 Jay Freeman (saurik)
3 */
4
5 /* GNU Lesser General Public License, Version 3 {{{ */
6 /*
7 * Substrate is free software: you can redistribute it and/or modify it under
8 * the terms of the GNU Lesser General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
11 *
12 * Substrate is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 * License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with Substrate. If not, see <http://www.gnu.org/licenses/>.
19 **/
20 /* }}} */
21
22 #import <CoreFoundation/CoreFoundation.h>
23 #import <Foundation/Foundation.h>
24 #import <CoreGraphics/CGGeometry.h>
25 #import <UIKit/UIKit.h>
26
27 #include <substrate.h>
28
29 Class $SafeModeAlertItem;
30
31 @interface SBAlertItem : NSObject {
32 }
33 - (UIAlertView *) alertSheet;
34 - (void) dismiss;
35 @end
36
37 @interface SBAlertItemsController : NSObject {
38 }
39 + (SBAlertItemsController *) sharedInstance;
40 - (void) activateAlertItem:(SBAlertItem *)item;
41 @end
42
43 @interface SBStatusBarTimeView : UIView {
44 }
45 - (id) textFont;
46 @end
47
48 @interface UIApplication (CydiaSubstrate)
49 - (void) applicationOpenURL:(id)url;
50 @end
51
52 @interface UIAlertView (CydiaSubstrate)
53 - (void) setForceHorizontalButtonsLayout:(BOOL)force;
54 - (void) setBodyText:(NSString *)body;
55 - (void) setNumberOfRows:(NSInteger)rows;
56 @end
57
58 void SafeModeButtonClicked(int button) {
59 switch (button) {
60 case 1:
61 break;
62
63 case 2:
64 if (kCFCoreFoundationVersionNumber >= 700)
65 system("killall backboardd");
66 else
67 // XXX: there are better ways of restarting SpringBoard that would actually save state
68 exit(0);
69 break;
70
71 case 3:
72 [[UIApplication sharedApplication] applicationOpenURL:[NSURL URLWithString:@"http://cydia.saurik.com/safemode/"]];
73 break;
74 }
75 }
76
77 void SafeModeAlertItem$alertSheet$buttonClicked$(id self, SEL sel, id sheet, int button) {
78 SafeModeButtonClicked(button);
79 [self dismiss];
80 }
81
82 void SafeModeAlertItem$alertView$clickedButtonAtIndex$(id self, SEL sel, id sheet, NSInteger button) {
83 SafeModeButtonClicked(button + 1);
84 [self dismiss];
85 }
86
87 void SafeModeAlertItem$configure$requirePasscodeForActions$(id self, SEL sel, BOOL configure, BOOL require) {
88 UIAlertView *sheet([self alertSheet]);
89
90 [sheet setDelegate:self];
91 [sheet setBodyText:@"We apologize for the inconvenience, but SpringBoard has just crashed.\n\nMobileSubstrate /did not/ cause this problem: it has protected you from it.\n\nYour device is now running in Safe Mode. All extensions that support this safety system are disabled.\n\nReboot (or restart SpringBoard) to return to the normal mode. To return to this dialog touch the status bar.\n\nTap \"Help\" below for more tips."];
92 [sheet addButtonWithTitle:@"OK"];
93 [sheet addButtonWithTitle:@"Restart"];
94 [sheet addButtonWithTitle:@"Help"];
95 [sheet setNumberOfRows:1];
96
97 if ([sheet respondsToSelector:@selector(setForceHorizontalButtonsLayout:)])
98 [sheet setForceHorizontalButtonsLayout:YES];
99 }
100
101 void SafeModeAlertItem$performUnlockAction(id self, SEL sel) {
102 [[%c(SBAlertItemsController) sharedInstance] activateAlertItem:self];
103 }
104
105 static void MSAlert() {
106 if ($SafeModeAlertItem == nil)
107 $SafeModeAlertItem = objc_lookUpClass("SafeModeAlertItem");
108 if ($SafeModeAlertItem == nil) {
109 $SafeModeAlertItem = objc_allocateClassPair(objc_getClass("SBAlertItem"), "SafeModeAlertItem", 0);
110 if ($SafeModeAlertItem == nil)
111 return;
112
113 class_addMethod($SafeModeAlertItem, @selector(alertSheet:buttonClicked:), (IMP) &SafeModeAlertItem$alertSheet$buttonClicked$, "v@:@i");
114 class_addMethod($SafeModeAlertItem, @selector(alertView:clickedButtonAtIndex:), (IMP) &SafeModeAlertItem$alertView$clickedButtonAtIndex$, "v@:@i");
115 class_addMethod($SafeModeAlertItem, @selector(configure:requirePasscodeForActions:), (IMP) &SafeModeAlertItem$configure$requirePasscodeForActions$, "v@:cc");
116 class_addMethod($SafeModeAlertItem, @selector(performUnlockAction), (IMP) SafeModeAlertItem$performUnlockAction, "v@:");
117 objc_registerClassPair($SafeModeAlertItem);
118 }
119
120 if (%c(SBAlertItemsController) != nil)
121 [[%c(SBAlertItemsController) sharedInstance] activateAlertItem:[[[$SafeModeAlertItem alloc] init] autorelease]];
122 }
123
124
125 // XXX: on iOS 5.0, we really would prefer avoiding
126
127 %hook SBStatusBar
128 - (void) touchesEnded:(id)touches withEvent:(id)event {
129 MSAlert();
130 %orig(touches, event);
131 } %end
132
133 %hook SBStatusBar
134 - (void) mouseDown:(void *)event {
135 MSAlert();
136 %orig(event);
137 } %end
138
139 %hook UIStatusBar
140 - (void) touchesBegan:(void *)touches withEvent:(void *)event {
141 MSAlert();
142 %orig(touches, event);
143 } %end
144
145
146 // this fairly complex code came from Grant, to solve the "it Safe Mode"-in-bar bug
147
148 %hook SBStatusBarDataManager
149 - (void) _updateTimeString {
150 char *_data(&MSHookIvar<char>(self, "_data"));
151 if (_data == NULL)
152 return;
153
154 Ivar _itemIsEnabled(object_getInstanceVariable(self, "_itemIsEnabled", NULL));
155 if (_itemIsEnabled == NULL)
156 return;
157
158 Ivar _itemIsCloaked(object_getInstanceVariable(self, "_itemIsCloaked", NULL));
159 if (_itemIsCloaked == NULL)
160 return;
161
162 size_t enabledOffset(ivar_getOffset(_itemIsEnabled));
163 size_t cloakedOffset(ivar_getOffset(_itemIsCloaked));
164 if (enabledOffset >= cloakedOffset)
165 return;
166
167 size_t offset(cloakedOffset - enabledOffset);
168 char *timeString(_data + offset);
169 strcpy(timeString, "Exit Safe Mode");
170 } %end
171
172
173 // this /insanely/ complex code came from that parrot guy... omg this is getting bad
174
175 @interface SBStatusBarStateAggregator : NSObject
176 - (void) _stopTimeItemTimer;
177 @end
178
179 %hook SBStatusBarStateAggregator
180
181 - (void) _updateTimeItems {
182 if ([self respondsToSelector:@selector(_stopTimeItemTimer)])
183 [self _stopTimeItemTimer];
184 %orig;
185 }
186
187 - (void) _restartTimeItemTimer {
188 }
189
190 - (void) _resetTimeItemFormatter {
191 %orig;
192 if (NSDateFormatter *df = MSHookIvar<NSDateFormatter *>(self, "_timeItemDateFormatter"))
193 [df setDateFormat:@"'Exit' 'Safe' 'Mode'"];
194 }
195
196 %end
197
198
199 static bool alerted_;
200
201 static void AlertIfNeeded() {
202 if (alerted_)
203 return;
204 alerted_ = true;
205 MSAlert();
206 }
207
208
209 // on iOS 4.3 and above we can use this advertisement, which seems to check every time the user unlocks
210 // XXX: verify that this still works on iOS 5.0
211
212 %hook AAAccountManager
213 + (void) showMobileMeOfferIfNecessary {
214 AlertIfNeeded();
215 } %end
216
217
218 // -[SBIconController showInfoAlertIfNeeded] explains how to drag icons around the iPhone home screen
219 // it used to be shown to users when they unlocked their screen for the first time, and happened every unlock
220 // however, as of iOS 4.3, it got relegated to only appearing once the user installed an app or web clip
221
222 %hook SBIconController
223 - (void) showInfoAlertIfNeeded {
224 AlertIfNeeded();
225 } %end
226
227
228 // the icon state, including crazy configurations like Five Icon Dock, is stored in SpringBoard's defaults
229 // unfortunately, SpringBoard on iOS 2.0 and 2.1 (maybe 2.2 as well) buffer overrun with more than 4 icons
230 // there is a third party package called IconSupport that remedies this, but not everyone is using it yet
231
232 %hook SBButtonBar
233 - (int) maxIconColumns {
234 static int max;
235 if (max == 0) {
236 max = %orig();
237 if (NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults])
238 if (NSDictionary *iconState = [defaults objectForKey:@"iconState"])
239 if (NSDictionary *buttonBar = [iconState objectForKey:@"buttonBar"])
240 if (NSArray *iconMatrix = [buttonBar objectForKey:@"iconMatrix"])
241 if ([iconMatrix count] != 0)
242 if (NSArray *row = [iconMatrix objectAtIndex:0]) {
243 int count([row count]);
244 if (max < count)
245 max = count;
246 }
247 } return max;
248 } %end
249
250
251 %hook SBUIController
252 - (id) init {
253 if ((self = %orig()) != nil) {
254 UIView *&_contentLayer(MSHookIvar<UIView *>(self, "_contentLayer"));
255 UIView *&_contentView(MSHookIvar<UIView *>(self, "_contentView"));
256
257 UIView *layer;
258 if (&_contentLayer != NULL)
259 layer = _contentLayer;
260 else if (&_contentView != NULL)
261 layer = _contentView;
262 else
263 layer = nil;
264
265 if (layer != nil)
266 [layer setBackgroundColor:[UIColor darkGrayColor]];
267 } return self;
268 } %end
269
270 #define Paper_ "/Library/MobileSubstrate/MobileSafety.png"
271
272 %hook UIImage
273 + (UIImage *) defaultDesktopImage {
274 return [UIImage imageWithContentsOfFile:@Paper_];
275 } %end
276
277 %hook SBStatusBarTimeView
278 - (void) tile {
279 NSString *&_time(MSHookIvar<NSString *>(self, "_time"));
280 CGRect &_textRect(MSHookIvar<CGRect>(self, "_textRect"));
281 if (_time != nil)
282 [_time release];
283 _time = [@"Exit Safe Mode" retain];
284 id font([self textFont]);
285 CGSize size([_time sizeWithFont:font]);
286 CGRect frame([self frame]);
287 _textRect.size = size;
288 _textRect.origin.x = (frame.size.width - size.width) / 2;
289 _textRect.origin.y = (frame.size.height - size.height) / 2;
290 } %end
291
292
293 // notification widgets ("wee apps" or "bulletin board sections") are capable of crashing SpringBoard
294 // unfortunately, which ones are in use are stored in SpringBoard's defaults, so we need to turn them off
295
296 %hook BBSectionInfo
297 - (BOOL) showsInNotificationCenter {
298 return NO;
299 } %end
300
301
302 // on iOS 6.0, Apple split parts of SpringBoard into a daemon called backboardd, including app launches
303 // in order to allow safe mode to propogate into applications, we need to then tell backboardd here
304 // XXX: (all of this should be replaced, however, with per-process launchd-mediated exception handling)
305
306 %hook BKSApplicationLaunchSettings
307 - (void) setEnvironment:(NSDictionary *)original {
308 if (original == nil)
309 return %orig(nil);
310
311 NSMutableDictionary *modified([original mutableCopy]);
312 [modified setObject:@"1" forKey:@"_MSSafeMode"];
313 return %orig(modified);
314 } %end
315
316 %ctor {
317 NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]);
318
319 // on iOS 6, backboardd is in charge of brightness, and freaks out when SpringBoard restarts :(
320 // the result is that the device is super dark until we attempt to update the brightness here.
321
322 if (kCFCoreFoundationVersionNumber >= 700) {
323 if (void (*GSEventSetBacklightLevel)(float) = reinterpret_cast<void (*)(float)>(dlsym(RTLD_DEFAULT, "GSEventSetBacklightLevel")))
324 if (NSMutableDictionary *defaults = [NSMutableDictionary dictionaryWithContentsOfFile:[NSString stringWithFormat:@"%@/Library/Preferences/com.apple.springboard.plist", NSHomeDirectory()]])
325 if (NSNumber *level = [defaults objectForKey:@"SBBacklightLevel2"])
326 GSEventSetBacklightLevel([level floatValue]);
327 }
328
329 [pool release];
330 }