forked from janka102/proximity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProximityAppController.m
310 lines (233 loc) · 8.75 KB
/
ProximityAppController.m
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
#import <IOKit/IOKitLib.h>
#import "ProximityAppController.h"
#import "NSWorkspace+runFileAtPath.h"
#import "StatusItem.h"
#import "UDKeys.h"
#define UD [NSUserDefaults standardUserDefaults]
@implementation ProximityAppController
#pragma mark -
#pragma mark Delegate Methods
- (void)awakeFromNib {
[[NSUserDefaults standardUserDefaults] registerDefaults:[NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"ud" ofType:@"plist"]]];
monitor = [[ProximityBluetoothMonitor alloc] init];
monitor.delegate = self;
[self userDefaultsLoad];
[self createMenuBar];
//update icon
[monitor refresh];
[monitor start];
}
- (void)applicationWillTerminate:(NSNotification *)aNotification {
[monitor stop];
}
- (void)windowWillClose:(NSNotification *)aNotification {
if ([UD boolForKey:UDMonitoringEnabledKey] && monitor.device) {
monitor.requiredSignalStrength = [[UD objectForKey:UDRequiredSignalKey] integerValue];
monitor.timeInterval = [UD stringForKey:UDCheckIntervalKey].doubleValue;
[monitor refresh];
[monitor start];
statusItem.paused = NO;
} else {
[monitor stop];
statusItem.paused = YES;
}
[self userDefaultsSave];
}
#pragma mark -
#pragma mark AppController Methods
- (void)createMenuBar {
NSMenu *menu = [[NSMenu alloc] init];
NSMenuItem *menuItem = [menu addItemWithTitle:@"Preferences..." action:@selector(showWindow:) keyEquivalent:@""];
[menuItem setTarget:self];
[menu addItem:[NSMenuItem separatorItem]];
[menu addItemWithTitle:@"Quit" action:@selector(terminate:) keyEquivalent:@""];
// Space on status bar
statusItem = [[ProximityStatusItem alloc] initWithStandardThickness];
statusItem.showMenuOnLeftMouseDown = YES;
statusItem.menu = menu;
}
- (void)inRange {
statusItem.inRange = YES;
[self runInRangeScript:YES];
}
//http://www.danandcheryl.com/2010/06/how-to-check-the-system-idle-time-using-cocoa
int64_t SystemIdleTime(void) {
int64_t idlesecs = -1;
io_iterator_t iter = 0;
if (IOServiceGetMatchingServices(kIOMasterPortDefault, IOServiceMatching("IOHIDSystem"), &iter) == KERN_SUCCESS) {
io_registry_entry_t entry = IOIteratorNext(iter);
if (entry) {
CFMutableDictionaryRef dict = NULL;
if (IORegistryEntryCreateCFProperties(entry, &dict, kCFAllocatorDefault, 0) == KERN_SUCCESS) {
CFNumberRef obj = CFDictionaryGetValue(dict, CFSTR("HIDIdleTime"));
if (obj) {
int64_t nanoseconds = 0;
if (CFNumberGetValue(obj, kCFNumberSInt64Type, &nanoseconds)) {
idlesecs = (nanoseconds >> 30); // Divide by 10^9 to convert from nanoseconds to seconds.
}
}
CFRelease(dict);
}
IOObjectRelease(entry);
}
IOObjectRelease(iter);
}
return idlesecs;
}
- (void)outOfRange {
statusItem.inRange = NO;
if ([UD boolForKey:UDIdleCheckEnabledkey]) {
NSLog(@"Idle time: %lli",SystemIdleTime());
if (SystemIdleTime() >= [[UD objectForKey:UDIdleMinTimeKey] integerValue]) {
[self runOutOfRangeScript:YES];
}
} else {
[self runOutOfRangeScript:YES];
}
}
- (void)runScript:(NSURL*)pathUrl arguments:(NSArray*)args silent:(BOOL)silent {
if (!pathUrl)
return;
NSError *error = nil;
BOOL b = [[NSWorkspace sharedWorkspace] runFileAtPath:pathUrl.path arguments:args error:&error];
if (!silent && !b) {
if (!error)
error = [NSError errorWithDomain:@"NSWorkspace" code:0 userInfo:@{NSLocalizedDescriptionKey : @"unknown error"}];
[NSApp presentError:error];
}
}
- (void)runInRangeScript:(BOOL)silent {
if (inRangeScriptURL) {
[self runScript:inRangeScriptURL arguments:@[@"in"] silent:silent];
}
}
- (void)runOutOfRangeScript:(BOOL)silent {
if (outOfRangeScriptURL) {
[self runScript:outOfRangeScriptURL arguments:@[@"out"] silent:silent];
}
}
#pragma mark -
- (void)userDefaultsLoad {
//Timer interval
if ([UD stringForKey:UDCheckIntervalKey].length > 0 ) {
monitor.timeInterval = [UD stringForKey:UDCheckIntervalKey].doubleValue;
}
//signal strength
if ([UD stringForKey:UDRequiredSignalKey].length > 0 ) {
monitor.requiredSignalStrength = [[UD objectForKey:UDRequiredSignalKey] integerValue];
}
// Device
NSData *deviceAsData = [UD objectForKey:UDDeviceKey];
if (deviceAsData.length > 0) {
id device = [NSKeyedUnarchiver unarchiveObjectWithData:deviceAsData];
[_deviceName setStringValue:[NSString stringWithFormat:@"%@ (%@)", [device name], [device addressString]]];
monitor.device = device;
}
// In range script path
if ([UD URLForKey:UDInRangeActionKey]) {
inRangeScriptURL = [UD URLForKey:UDInRangeActionKey];
_inRangeScriptPath.stringValue = inRangeScriptURL.path;
}
// Out of range script path
if ([UD URLForKey:UDOutOfRangeActionKey]) {
outOfRangeScriptURL = [UD URLForKey:UDOutOfRangeActionKey];
_outOfRangeScriptPath.stringValue = outOfRangeScriptURL.path;
}
}
- (void)userDefaultsSave {
// In range script
[UD setURL:inRangeScriptURL forKey:UDInRangeActionKey];
// Out of range script
[UD setURL:outOfRangeScriptURL forKey:UDOutOfRangeActionKey];
[UD synchronize];
}
- (IBAction)updateDeviceStatus:(id)sender {
if (!monitor.device) {
_deviceStatus.stringValue = @"Please select a device";
NSLog(@"No device = no status");
return;
}
[_progressIndicator startAnimation:nil];
// Refresh status
ProximityBluetoothMonitor *testMon = [[ProximityBluetoothMonitor alloc] initWithDevice:monitor.device];
testMon.requiredSignalStrength = monitor.requiredSignalStrength = [[UD objectForKey:UDRequiredSignalKey] integerValue];
[testMon refresh];
// Update signal bar
[_currentSignalStrength setIntegerValue:[testMon getRange:YES]];
NSLog(@"Signal strength: %li",(long)_currentSignalStrength.integerValue);
// Update status
switch (testMon.status) {
case ProximityBluetoothStatusInRange:
_deviceStatus.stringValue = @"In range";
break;
case ProximityBluetoothStatusOutOfRange:
_deviceStatus.stringValue = @"Out of range";
break;
case ProximityBluetoothStatusUndefined:
_deviceStatus.stringValue = @"Not found";
break;
}
[testMon stop];
testMon = nil;
[_progressIndicator stopAnimation:nil];
}
#pragma mark -
#pragma mark Interface Methods
- (IBAction)changeDevice:(id)sender {
IOBluetoothDeviceSelectorController *deviceSelector = [IOBluetoothDeviceSelectorController deviceSelector];
[deviceSelector beginSheetModalForWindow:_prefsWindow modalDelegate:self didEndSelector:@selector(sheetDidEnd:returnCode:contextInfo:) contextInfo:NULL];
}
- (void)sheetDidEnd:(IOBluetoothDeviceSelectorController *)controller returnCode:(int)returnCode contextInfo:(void *)contextInfo {
if (returnCode == kIOBluetoothUISuccess) {
id device = controller.getResults[0];
_deviceName.stringValue = [NSString stringWithFormat:@"%@ (%@)", [device name], [device addressString]];
monitor.device = device;
[self updateDeviceStatus:nil];
// Device
if (monitor.device) {
NSData *deviceAsData = [NSKeyedArchiver archivedDataWithRootObject:monitor.device];
[[NSUserDefaults standardUserDefaults] setObject:deviceAsData forKey:UDDeviceKey];
}
}
}
#pragma mark -
- (NSURL*)chooseScript {
NSOpenPanel *op = [NSOpenPanel openPanel];
[op setAllowsMultipleSelection:NO];
if ([op runModal] == NSOKButton) {
NSArray *files = op.URLs;
if (files.count)
return files[0];
}
return nil;
}
- (IBAction)inRangeScriptChange:(id)sender {
NSURL *file = [self chooseScript];
if (file) {
inRangeScriptURL = file;
_inRangeScriptPath.stringValue = file.path;
}
}
- (IBAction)inRangeScriptTest:(id)sender {
[self runInRangeScript:NO];
}
- (IBAction)outOfRangeScriptChange:(id)sender {
NSURL *file = [self chooseScript];
if (file) {
outOfRangeScriptURL = file;
_outOfRangeScriptPath.stringValue = file.path;
}
}
- (IBAction)outOfRangeScriptTest:(id)sender {
[self runOutOfRangeScript:NO];
}
#pragma mark -
- (void)showWindow:(id)sender {
[NSApp activateIgnoringOtherApps:YES];
[_prefsWindow makeKeyAndOrderFront:self];
// Clear out status
_deviceStatus.stringValue = @"";
[monitor stop];
statusItem.paused = YES;
}
@end