forked from rpetrich/IconRotator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTweak.x
331 lines (283 loc) · 8.44 KB
/
Tweak.x
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#import <SpringBoard/SpringBoard.h>
#import <QuartzCore/QuartzCore.h>
#import <notify.h>
#import <CaptainHook/CaptainHook.h>
#import <CoreMotion/CoreMotion.h>
static CFMutableSetRef icons;
static CATransform3D currentTransform;
static CGFloat reflectionOpacity;
static CMMotionManager *motionManager;
static UIApplication *springboard;
static volatile int orientationStatus;
static BOOL isUnlocked = NO;
@interface SBIconView : UIView
@end
@interface SBNowPlayingBarView : UIView
@property (readonly, nonatomic) UIButton *toggleButton;
@property (readonly, nonatomic) UIButton *airPlayButton;
@end
@interface SBNowPlayingBarMediaControlsView : UIView
@property (readonly, nonatomic) UIButton *prevButton;
@property (readonly, nonatomic) UIButton *playButton;
@property (readonly, nonatomic) UIButton *nextButton;
@property (readonly, nonatomic) UIButton *airPlayButton;
@end
@interface UIView (Springtomize)
- (CGFloat)springtomizeScaleFactor;
@end
@interface SBOrientationLockManager : NSObject {
NSMutableSet *_lockOverrideReasons;
UIInterfaceOrientation _userLockedOrientation;
}
+ (SBOrientationLockManager *)sharedInstance;
- (void)restoreStateFromPrefs;
- (id)init;
- (void)dealloc;
- (void)lock;
- (void)lock:(UIInterfaceOrientation)lock;
- (void)unlock;
- (BOOL)isLocked;
- (UIInterfaceOrientation)userLockOrientation;
- (void)setLockOverrideEnabled:(BOOL)enabled forReason:(id)reason;
- (void)enableLockOverrideForReason:(id)reason suggestOrientation:(UIInterfaceOrientation)orientation;
- (void)enableLockOverrideForReason:(id)reason forceOrientation:(UIInterfaceOrientation)orientation;
- (BOOL)lockOverrideEnabled;
- (void)updateLockOverrideForCurrentDeviceOrientation;
- (void)_updateLockStateWithChanges:(id)changes;
- (void)_updateLockStateWithOrientation:(int)orientation changes:(id)changes;
- (void)_updateLockStateWithOrientation:(int)orientation forceUpdateHID:(BOOL)forceHID changes:(id)changes;
- (BOOL)_effectivelyLocked;
@end
static CATransform3D (*ScaledTransform)(UIView *);
static CATransform3D ScaledTransformSpringtomize(UIView *iconView)
{
CGFloat scale = [iconView springtomizeScaleFactor];
return CATransform3DScale(currentTransform, scale, scale, 1.0f);
}
static CATransform3D ScaledTransformDefault(UIView *iconView)
{
return currentTransform;
}
%hook UIView
- (void)didMoveToWindow
{
if (!self.window)
CFSetRemoveValue(icons, self);
%orig;
}
- (void)dealloc
{
CFSetRemoveValue(icons, self);
%orig;
}
%end
%hook SBIconView
- (void)didMoveToWindow
{
%orig;
if (self.window) {
CFSetSetValue(icons, self);
CALayer *layer = self.layer;
layer.sublayerTransform = ScaledTransform(self);
[layer setValue:@"sublayerTransform" forKey:@"IconRotatorKeyPath"];
CHIvar(self, _reflection, UIImageView *).alpha = reflectionOpacity;
}
}
- (void)didMoveToSuperview
{
%orig;
if (self.superview) {
self.layer.sublayerTransform = ScaledTransform(self);
}
}
%end
static void ApplyRotatedViewTransform(UIView *view)
{
if (view) {
CALayer *layer = view.layer;
layer.transform = ScaledTransform(view);
[layer setValue:@"transform" forKey:@"IconRotatorKeyPath"];
CFSetSetValue(icons, view);
}
}
%hook SBNowPlayingBarView
- (void)didMoveToWindow
{
%orig;
if (self.window) {
ApplyRotatedViewTransform(self.toggleButton);
ApplyRotatedViewTransform(self.airPlayButton);
}
}
%end
%hook SBNowPlayingBarMediaControlsView
- (void)didMoveToWindow
{
%orig;
if (self.window) {
ApplyRotatedViewTransform(self.prevButton);
ApplyRotatedViewTransform(self.playButton);
ApplyRotatedViewTransform(self.nextButton);
ApplyRotatedViewTransform(self.airPlayButton);
}
}
%end
%hook SBSearchController
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *result = %orig;
if (result) {
NSArray *subviews = result.subviews;
if ([subviews count]) {
ApplyRotatedViewTransform([subviews objectAtIndex:0]);
}
}
return result;
}
%end
static void UpdateWithOrientation(UIInterfaceOrientation orientation)
{
//NSLog(@"Calculate new Rotation: %i", orientation);
switch (orientation) {
case UIInterfaceOrientationPortrait:
currentTransform = CATransform3DIdentity;
reflectionOpacity = 1.0f;
orientationStatus = UIInterfaceOrientationPortrait;
break;
case UIInterfaceOrientationPortraitUpsideDown:
//Never called
currentTransform = CATransform3DMakeRotation(M_PI, 0.0f, 0.0f, 1.0f);
reflectionOpacity = 0.0f;
break;
case UIInterfaceOrientationLandscapeRight:
currentTransform = CATransform3DMakeRotation(0.5f * M_PI, 0.0f, 0.0f, 1.0f);
reflectionOpacity = 0.0f;
orientationStatus = UIInterfaceOrientationLandscapeRight;
break;
case UIInterfaceOrientationLandscapeLeft:
currentTransform = CATransform3DMakeRotation(-0.5f * M_PI, 0.0f, 0.0f, 1.0f);
reflectionOpacity = 0.0f;
orientationStatus = UIInterfaceOrientationLandscapeLeft;
break;
default:
return;
}
for (UIView *view in (id)icons) {
CALayer *layer = view.layer;
NSString *keyPath = [layer valueForKey:@"IconRotatorKeyPath"];
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:keyPath];
NSValue *toValue = [NSValue valueWithCATransform3D:ScaledTransform(view)];
animation.toValue = toValue;
animation.duration = 0.2;
animation.removedOnCompletion = YES;
animation.fromValue = [layer.presentationLayer valueForKeyPath:keyPath];
[layer setValue:toValue forKeyPath:keyPath];
[layer addAnimation:animation forKey:@"IconRotator"];
UIImageView **imageView = CHIvarRef(view, _reflection, UIImageView *);
if (imageView)
(*imageView).alpha = reflectionOpacity;
}
}
static void SetAccelerometerEnabled(BOOL enabled)
{
if(enabled){
//NSLog(@"Set Accelerometer enabled");
if(!motionManager){
motionManager = [[CMMotionManager alloc] init];
motionManager.accelerometerUpdateInterval = 0.2;
}
[motionManager startAccelerometerUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMAccelerometerData *accelerometerData, NSError *error)
{
if(accelerometerData){
float x = accelerometerData.acceleration.x;
float y = accelerometerData.acceleration.y;
float z = accelerometerData.acceleration.z; //That it only moves if not lying
//Turn Right
if(x > 0.5 && y > -0.3 && z > -0.7 && z < 0.7 && orientationStatus != 4){
UpdateWithOrientation(UIInterfaceOrientationLandscapeLeft);
}
//Turn Portrait
else if(y < -0.5 && x > -0.3 && x < 0.3 && z > -0.7 && z < 0.7 && orientationStatus != 1){
UpdateWithOrientation(UIInterfaceOrientationPortrait);
}
//Turn Left
else if(x < -0.5 && y > -0.3 && z > -0.7 && z < 0.7 && orientationStatus != 3){
UpdateWithOrientation(UIInterfaceOrientationLandscapeRight);
}
}
}];
}
else{
//NSLog(@"Set Accelerometer disbaled");
if(motionManager){
[motionManager stopAccelerometerUpdates];
}
}
}
%hook SBAwayController
- (void)dimScreen:(BOOL)animated
{
%orig;
SetAccelerometerEnabled(NO);
isUnlocked = NO;
UpdateWithOrientation(UIInterfaceOrientationPortrait);
}
%end;
%hook SpringBoard
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
%orig;
if ([UIView instancesRespondToSelector:@selector(springtomizeScaleFactor)])
ScaledTransform = ScaledTransformSpringtomize;
if(!springboard)
springboard = self;
}
- (void)undim //Springboard is shown
{
%orig;
isUnlocked = YES;
SBOrientationLockManager *olm = [%c(SBOrientationLockManager) sharedInstance];
if (![olm _effectivelyLocked])
SetAccelerometerEnabled(YES);
}
%end
%hook SBOrientationLockManager
- (void)_updateLockStateWithChanges:(id)changes
{
%orig;
if ([self _effectivelyLocked]) {
SetAccelerometerEnabled(NO);
UpdateWithOrientation([self userLockOrientation]);
} else if(isUnlocked){
SetAccelerometerEnabled(YES);
}
}
- (void)_updateLockStateWithOrientation:(UIInterfaceOrientation)orientation forceUpdateHID:(BOOL)updateHID changes:(id)changes
{
%orig;
if ([self _effectivelyLocked]) {
SetAccelerometerEnabled(NO);
UpdateWithOrientation([self userLockOrientation]);
} else if(isUnlocked){
SetAccelerometerEnabled(YES);
}
}
- (void)_updateLockStateWithOrientation:(UIInterfaceOrientation)orientation changes:(id)changes
{
%orig;
if ([self _effectivelyLocked]) {
SetAccelerometerEnabled(NO);
UpdateWithOrientation([self userLockOrientation]);
} else if(isUnlocked) {
SetAccelerometerEnabled(YES);
}
}
%end
%ctor
{
%init;
ScaledTransform = ScaledTransformDefault;
currentTransform = CATransform3DIdentity;
icons = CFSetCreateMutable(kCFAllocatorDefault, 0, NULL);
orientationStatus = 1;
}