-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRePower.xm
217 lines (177 loc) · 7.28 KB
/
RePower.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
//
// RePower.xm
// PowerDown Info
//
// Created by Haifisch on 09.01.2014.
// Copyright (c) 2014 Haifisch. All rights reserved.
//
/*
Todo:
Implement _UIBackdropView
Battery time left
Slide to ACTION
Note to reader:
I'm truely sorry for doing what I did here. It's bad, I know, but I'm sorry. If you'd like to fix this up, be my guest. It just works.
I'd also like to say sorry in advanced for any future mistakes.
For friendly neighbourhood developer,
Haifisch
It's okay.
From friendly neighbourhood developer,
insanj
*/
#import <Foundation/Foundation.h>
#import <QuartzCore/QuartzCore.h>
#import <UIKit/UIKit.h>
#import "Headers/RePowerHeaders.h"
%group main
%hook SBPowerDownView
UIView *firstButton, *secondButton;
UILabel *uptimeLabel;
NSTimer *batterytimer;
%new -(float)batteryLevel{
@try{
UIDevice *device = [UIDevice currentDevice];
device.batteryMonitoringEnabled = YES;
CGFloat batteryCharge = [device batteryLevel] * 1000.f;
if(batteryCharge > 0.0f)
return batteryCharge;
else
return -1;
}
@catch (NSException *exception){
return -1; // Error out
}
}
%new -(BOOL)charging {
@try{
UIDevice *device = [UIDevice currentDevice];
device.batteryMonitoringEnabled = YES;
return ([device batteryState] == UIDeviceBatteryStateCharging || [device batteryState] == UIDeviceBatteryStateFull);
}
@catch (NSException *exception){
return false; // Error out
}
}
%new -(BOOL)fullyCharged{
@try {
UIDevice *device = [UIDevice currentDevice];
device.batteryMonitoringEnabled = YES;
return ([device batteryState] == UIDeviceBatteryStateFull);
}
@catch (NSException *exception){
return false; // Error out
}
}
%new -(BOOL)doesDoubleTap{
return [[[NSDictionary dictionaryWithContentsOfFile:[NSHomeDirectory() stringByAppendingPathComponent:@"/Library/Preferences/ws.hbang.repower.plist"]] objectForKey:@"doubletap"] boolValue];
}
%new -(void)respring{
[(SpringBoard *)[UIApplication sharedApplication] _relaunchSpringBoardNow];
}
%new -(void)reboot{
[(SpringBoard *)[UIApplication sharedApplication] _rebootNow];
}
%new -(void)sliderDidSlide:(MBSliderView *)slideView{
NSLog(@"[RePower]: Received message from %@...", slideView);
if ([slideView.text isEqual:@"Slide to reboot"])
NSLog(@"[RePower]: Rebooting...");
}
-(void)animateIn{
//[UIDevice currentDevice].batteryMonitoringEnabled = YES;
//[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(batteryStateChanged:) name:@"UIDeviceBatteryStateDidChangeNotification" object:[UIDevice currentDevice]];
//batterytimer = [NSTimer scheduledTimerWithTimeInterval:30 target:self selector:@selector(batteryLevelChanged) userInfo:nil repeats:YES];
// First button view
firstButton = [[UIView alloc] initWithFrame:CGRectMake(16.f, 135.f, 288.f, 65.f)];
firstButton.layer.cornerRadius = 5.f;
firstButton.layer.masksToBounds = YES;
firstButton.opaque = NO;
firstButton.alpha = 0.8f;
firstButton.backgroundColor = [UIColor colorWithRed:184.f/255.f green:111.f/255.f blue:39.f/255.f alpha:0.9f];
// Second button view
secondButton = [[UIView alloc] initWithFrame:CGRectMake(16,220,288,65)];
secondButton.layer.cornerRadius = 5;
secondButton.layer.masksToBounds = YES;
secondButton.opaque = NO;
secondButton.alpha = 0.8;
secondButton.backgroundColor = [UIColor colorWithRed:70.f/255.f green:160.f/255.f blue:46.f/255.f alpha:0.9f];
// Uptime text label
uptimeLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.f, 0.f, firstButton.bounds.size.width, 30.f)];
uptimeLabel.textAlignment = NSTextAlignmentCenter;
uptimeLabel.textColor = [UIColor whiteColor];
[uptimeLabel setFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:17.f]];
if ([self charging])
uptimeLabel.text = [NSString stringWithFormat:@"Charging at %f", [self batteryLevel]];
// Respring Button
UIButton *respringBTN = [UIButton buttonWithType:UIButtonTypeRoundedRect];
if (![self doesDoubleTap]){
[respringBTN setTitle:@"Double tap to Respring" forState:UIControlStateNormal];
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(respring)];
tapRecognizer.numberOfTapsRequired = 2;
tapRecognizer.numberOfTouchesRequired = 1;
[respringBTN addGestureRecognizer:tapRecognizer];
}
else{
[respringBTN setTitle:@"Tap to Respring" forState:UIControlStateNormal];
[respringBTN addTarget:self action:@selector(respring) forControlEvents:UIControlEventTouchDown];
}
respringBTN.frame = CGRectMake(0.f, 0.f, secondButton.bounds.size.width, secondButton.bounds.size.height);
[respringBTN.titleLabel setFont:[UIFont fontWithName:@"HelveticaNeue" size:25.f]];
[respringBTN setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
// Reboot button
UIButton *rebootBTN = [UIButton buttonWithType:UIButtonTypeRoundedRect];
if (![self doesDoubleTap]){
[rebootBTN setTitle:@"Double tap to Reboot" forState:UIControlStateNormal];
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(reboot)];
tapRecognizer.numberOfTapsRequired = 2;
tapRecognizer.numberOfTouchesRequired = 1;
[rebootBTN addGestureRecognizer:tapRecognizer];
}
else{
[rebootBTN setTitle:@"Tap to Reboot" forState:UIControlStateNormal];
[rebootBTN addTarget:self action:@selector(reboot) forControlEvents:UIControlEventTouchDown];
}
rebootBTN.frame = CGRectMake(0.f, 0.f, firstButton.bounds.size.width, firstButton.bounds.size.height);
[rebootBTN.titleLabel setFont:[UIFont fontWithName:@"HelveticaNeue" size:25.f]];
[rebootBTN setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
// Slide to views
MBSliderView *s1 = [[MBSliderView alloc] initWithFrame:CGRectMake(15.f, 0.f, firstButton.frame.size.width, firstButton.frame.size.height)];
[s1 setText:@"slide to reboot"]; // set the label text
[s1 setDelegate:self]; // set the MBSliderView delegate
// Add views
[self addSubview:firstButton];
[self addSubview:secondButton];
//[debugView addSubview:uptimeLabel];
[firstButton addSubview:s1];
//[firstButton addSubview:rebootBTN];
[secondButton addSubview:respringBTN];
// Process nice fade in
CATransition *applicationLoadViewIn =[CATransition animation];
[applicationLoadViewIn setDuration:0.75f];
[applicationLoadViewIn setType:kCATransitionReveal];
[applicationLoadViewIn setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
[[firstButton layer] addAnimation:applicationLoadViewIn forKey:kCATransitionReveal];
[[secondButton layer] addAnimation:applicationLoadViewIn forKey:kCATransitionReveal];
%orig;
}
-(void)animateOut{
// Process nice fade out
CATransition *transition = [CATransition animation];
transition.duration = 0.75f;
[transition setType:kCATransitionFade];
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
[transition setFillMode:@"extended"];
[[firstButton layer] addAnimation:transition forKey:nil];
[[secondButton layer] addAnimation:transition forKey:nil];
[CATransaction commit];
// Remove views
[secondButton removeFromSuperview];
[firstButton removeFromSuperview];
%orig;
}
%end //%hook
%end //%group
%ctor{
@autoreleasepool{
%init(main);
}
}