-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSwitcherLoader.m
209 lines (148 loc) · 6.13 KB
/
SwitcherLoader.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
//
// libswitcher.m
// libswitcher
//
// Created by Ryan Coffman on 11/21/11.
// Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//
#import "SwitcherLoader.h"
#define PLUGIN_PATH @"/Library/SwitcherLoader/Plugins"
#define SETTINGS_PATH @"/var/mobile/Library/Preferences/com.fr0zensun.switcherloader.plist"
#import <objc/runtime.h>
#import <AppSupport/AppSupport.h>
static NSMutableArray *defaultViews = nil;
static BOOL hasLoadedPlugins = NO;
@implementation SwitcherLoader
@synthesize currentStatus = _currentStatus;
-(id)init {
if((self = [super init])) {
plugins = [[NSMutableArray alloc]init];
identifiers = [[NSMutableArray alloc]init];
CPDistributedMessagingCenter *messagingCenter;
messagingCenter = [CPDistributedMessagingCenter centerNamed:@"com.fr0zensun.switcherloader.message.center"];
[messagingCenter runServerOnCurrentThread];
[messagingCenter registerForMessageName:@"Plugins" target:self selector:@selector(returnPlugins)];
[messagingCenter registerForMessageName:@"LoadPlugins" target:self selector:@selector(loadItems)];
[messagingCenter registerForMessageName:@"Update" target:self selector:@selector(update)];
}
return self;
}
-(void)update {
[self setStatus:Update];
}
-(void)setStatus:(Status)theStatus {
self.currentStatus = theStatus;
}
-(NSDictionary *)returnPlugins {
NSDictionary *dict = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:[self enabledPluginsIndexed], [self disabledPlugins],nil] forKeys:[NSArray arrayWithObjects:@"EnabledPlugins",@"DisabledPlugins",nil]];
return dict;
}
-(BOOL)isOSVersionSupported:(NSString *)minOS {
NSString *systemVersion = [[UIDevice currentDevice] systemVersion];
if (!minOS || minOS.length < 1 ||[systemVersion compare:minOS options:NSNumericSearch] != NSOrderedAscending)
return YES;
else
return NO;
}
-(void)loadItems {
NSLog(@"Loading plugins!!!!!!");
if(!hasLoadedPlugins) {
[plugins removeAllObjects];
[identifiers removeAllObjects];
NSFileManager *fm = [[NSFileManager alloc]init];
NSArray *dirContents = [fm contentsOfDirectoryAtPath:PLUGIN_PATH error:nil];
for(unsigned int i = 0; i < [dirContents count]; i++) {
NSString *path = [PLUGIN_PATH stringByAppendingPathComponent:[dirContents objectAtIndex:i]];
if([[path pathExtension]isEqualToString:@"bundle"]) {
NSBundle *bundle = [NSBundle bundleWithPath:path];
NSError *error = nil;
if ([self isOSVersionSupported:[bundle objectForInfoDictionaryKey:@"MinimumOSVersion"]] ) {
[bundle loadAndReturnError:&error];
if(!error) {
UIView *view = [[[bundle principalClass] alloc]initWithFrame:CGRectMake(0,0,320,94)];
[identifiers addObject:[bundle bundleIdentifier]];
view.tag = [plugins count];
[plugins addObject:view];
[view release];
}
}
}
}
[identifiers addObject:@"springboard.sbnowplayingbarview"];
[identifiers addObject:@"springboard.sbairplaybarview"];
[fm release];
hasLoadedPlugins = YES;
}
}
-(NSMutableArray *)enabledPluginsIndexed {
[self loadItems];
NSMutableArray *array = [NSMutableArray array];
for(int i = 0; i< [identifiers count]; i++) {
NSString *pid = [identifiers objectAtIndex:i];
if([self isPluginEnabled:pid])
[array addObject:pid];
}
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:SETTINGS_PATH];
if ([[dict objectForKey:@"Plugins"]objectForKey:@"Enabled"] ) {
NSArray *dictArray = [NSArray arrayWithArray:[[dict objectForKey:@"Plugins"]objectForKey:@"Enabled"]];
for (int i = 0; i < [dictArray count]; i++) {
NSString *pid = [dictArray objectAtIndex:i];
if([array containsObject:pid])
[array exchangeObjectAtIndex:i withObjectAtIndex:[array indexOfObject:pid]];
}
}
return array;
}
-(UIView *)viewForId:(NSString *)plugin_id {
NSUInteger index = [identifiers indexOfObject:plugin_id];
if (index >= 0) {
for(int i = 0; i <[defaultViews count]; i++) {
if(plugin_id == [[defaultViews objectAtIndex:i]plugin_id])
return [defaultViews objectAtIndex:i];
}
for(int i =0; i < [plugins count]; i++) {
if(index == [[plugins objectAtIndex:i]tag])
return [plugins objectAtIndex:i];
}
}
return nil;
}
-(NSMutableArray *)disabledPlugins {
[self loadItems];
NSMutableArray *array = [NSMutableArray array];
for(int i = 0; i< [identifiers count]; i++) {
NSString *pid = [identifiers objectAtIndex:i];
if(![self isPluginEnabled:pid])
[array addObject:pid];
}
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:SETTINGS_PATH];
if ([[dict objectForKey:@"Plugins"]objectForKey:@"Disabled"] ) {
NSArray *dictArray = [NSArray arrayWithArray:[[dict objectForKey:@"Plugins"]objectForKey:@"Disabled"]];
for (int i = 0; i < [dictArray count]; i++) {
NSString *pid = [dictArray objectAtIndex:i];
if([array containsObject:pid])
[array exchangeObjectAtIndex:i withObjectAtIndex:[array indexOfObject:pid]];
}
}
return array;
}
-(BOOL)isPluginEnabled:(NSString *)plugin_id {
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:SETTINGS_PATH];
if ([[dict objectForKey:@"Plugins"]objectForKey:@"Disabled"] ) {
NSArray *array = [NSArray arrayWithArray:[[dict objectForKey:@"Plugins"]objectForKey:@"Disabled"]];
if(![array containsObject:plugin_id])
return YES;
else return NO;
}
else
return YES;
}
-(NSMutableArray *)defaultViews {
if(!defaultViews)
defaultViews = [[NSMutableArray alloc]init];
return defaultViews;
}
-(NSMutableArray *)plugins {
return plugins;
}
@end