-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTweak.xm
133 lines (105 loc) · 3.5 KB
/
Tweak.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
#import <Foundation/Foundation.h>
#import <CoreMotion/CoreMotion.h>
#import <FlipSwitch/FlipSwitch.h>
@interface FlatVibe: NSObject
@property (retain) CMMotionManager *mm;
@property (retain) FSSwitchPanel *fsp;
@property (retain) NSTimer *timer;
@property FSSwitchState vibrate;
@end
@implementation FlatVibe
- (id) init {
if (self = [super init]) {
_vibrate = FSSwitchStateIndeterminate;
_fsp = [FSSwitchPanel sharedPanel];
_mm = [[CMMotionManager alloc] init];
}
return self;
}
- (void) dealloc {
NSLog(@"FlatVibe:dealloc");
[self.mm release];
[super dealloc];
}
- (void) loadPreferences {
NSLog(@"FlatVibe: loadPreferences");
NSMutableDictionary *prefs = [[NSMutableDictionary alloc] initWithContentsOfFile:@"/var/mobile/Library/Preferences/com.lazypulse.flatvibeprefs.plist"];
if (!prefs) {
NSLog(@"FlatVibe: no prefs??");
return;
}
id o = [prefs objectForKey:@"enabled"];
if (nil == o) {
NSLog(@"FlatVibe: nil == o");
}
BOOL enable = [o boolValue];
NSLog(@"FlatVibe: enable = %d", enable);
if (enable) {
if (!self.timer) {
NSLog(@"FlatVibe: enabling");
if(self.mm.accelerometerAvailable) {
if (!self.mm.accelerometerActive) {
[self.mm startAccelerometerUpdates];
}
else {
NSLog(@"FlatVibe: accelerometer was active already..");
}
self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(tick:) userInfo:nil repeats:YES];
}
else {
NSLog(@"FlatVibe: no accelerometer available -- doing.. nothing");
}
}
}
else {
if (self.timer) {
NSLog(@"FlatVibe: disabling");
[self.timer invalidate];
[self.timer release];
self.timer = nil;
[self.mm stopAccelerometerUpdates];
}
}
[prefs release];
}
- (void) tick:(NSTimer *)timer {
if (!self.mm.accelerometerActive) { // TODO: ??
NSLog(@"FlatVibe: accelerometer not active");
return;
}
if (!self.mm.accelerometerData) { // TODO: ??
NSLog(@"FlatVibe: no accelerometer data");
return;
}
CMAcceleration a = self.mm.accelerometerData.acceleration;
float epsilon = 0.1;
FSSwitchState vibrate =
fabs(a.x) < epsilon && fabs(a.y) < epsilon && fabs(a.z) - 1 < epsilon?
FSSwitchStateOff:
FSSwitchStateOn;
if (self.vibrate != vibrate) {
NSLog(@"FlatVibe: vibrate: %d -> %d", self.vibrate, vibrate);
self.vibrate = vibrate;
[self setVibration];
}
}
static NSString *switchIdentifier = @"com.a3tweaks.switch.vibration";
- (void) setVibration {
[self.fsp setState:self.vibrate forSwitchIdentifier:switchIdentifier];
[self.fsp applyActionForSwitchIdentifier:switchIdentifier];
}
@end
static FlatVibe *flatvibe;
static void loadPrefs() {
[flatvibe loadPreferences];
}
%ctor {
flatvibe = [[[FlatVibe alloc] init] autorelease];
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(),
NULL,
(CFNotificationCallback)loadPrefs,
CFSTR("com.lazypulse.flatvibeprefs/changed"),
NULL,
CFNotificationSuspensionBehaviorCoalesce);
[flatvibe loadPreferences];
}