-
Notifications
You must be signed in to change notification settings - Fork 90
Expand file tree
/
Copy pathTDApplicationListViewController.m
More file actions
310 lines (246 loc) · 14.2 KB
/
TDApplicationListViewController.m
File metadata and controls
310 lines (246 loc) · 14.2 KB
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 "TDApplicationListViewController.h"
#import "TDAlternateIconViewController.h"
#import "Decryption/TDDecryptionTask.h"
#import "Localize.h"
#import "Extensions/UIImage+Private.h"
#import "Extensions/LSBundleProxy+TrollDecrypt.h"
#import "TDFileManagerViewController.h"
#import <MobileCoreServices/MobileCoreServices.h>
#import "Extensions/LSApplicationProxy+AltList.h"
#import "Extensions/LSApplicationProxy+AppState.h"
static inline NSUInteger getEffectiveIconFormat(void) {
return (UIDevice.currentDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad) ? 8 : 10;
}
@implementation TDApplicationListViewController {
NSArray *_allAvailableApplications;
NSArray *_filteredApplications;
UISearchController *_searchController;
UIImage *_placeholderIcon;
NSMutableDictionary *_iconCache;
UIAlertController *_progressAlert;
}
- (instancetype)init {
self = [super init];
if (self) {
_iconCache = [NSMutableDictionary new];
_placeholderIcon = [UIImage _applicationIconImageForBundleIdentifier:@"com.apple.WebSheet" format:getEffectiveIconFormat() scale:[UIScreen mainScreen].scale];
[[LSApplicationWorkspace defaultWorkspace] addObserver:self];
[self loadAvailableApplications];
}
return self;
}
- (void)dealloc {
[[LSApplicationWorkspace defaultWorkspace] removeObserver:self];
}
- (void)applicationsDidInstall:(NSArray <LSApplicationProxy *> *)apps {
[self refresh];
}
- (void)applicationsDidUninstall:(NSArray <LSApplicationProxy *> *)apps {
[self refresh];
}
- (void)viewDidLoad {
[super viewDidLoad];
[self _configureToolbarItems];
[self _configureSearchController];
}
- (void)_configureToolbarItems {
UIBarButtonItem *manualBundleIdentifierButton = [[UIBarButtonItem alloc] initWithImage:[UIImage systemImageNamed:@"keyboard"] style:UIBarButtonItemStylePlain target:self action:@selector(_manualBundleIdentifierButtonTapped)];
self.navigationItem.rightBarButtonItem = manualBundleIdentifierButton;
if ([UIApplication sharedApplication].supportsAlternateIcons) {
UIBarButtonItem *alternateIconButton = [[UIBarButtonItem alloc] initWithImage:[UIImage systemImageNamed:@"paintbrush.fill"] style:UIBarButtonItemStylePlain target:self action:@selector(_alternateIconButtonTapped)];
self.navigationItem.leftBarButtonItem = alternateIconButton;
}
}
- (void)_configureSearchController {
_searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
_searchController.searchResultsUpdater = self;
_searchController.obscuresBackgroundDuringPresentation = NO;
_searchController.searchBar.placeholder = [Localize localizedStringForKey:@"SEARCH_APPLICATIONS"];
_searchController.searchBar.autocapitalizationType = UITextAutocapitalizationTypeNone;
_searchController.searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
self.navigationItem.searchController = _searchController;
self.definesPresentationContext = YES;
}
- (void)_manualBundleIdentifierButtonTapped {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:[Localize localizedStringForKey:@"MANUAL_ENTRY_TITLE"] message:[Localize localizedStringForKey:@"MANUAL_ENTRY_SUBTITLE"] preferredStyle:UIAlertControllerStyleAlert];
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = @"com.example.app";
}];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:[Localize localizedStringForKey:@"CANCEL"] style:UIAlertActionStyleCancel handler:nil];
UIAlertAction *decryptAction = [UIAlertAction actionWithTitle:[Localize localizedStringForKey:@"DECRYPT"] style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
NSString *bundleIdentifier = alert.textFields.firstObject.text;
LSApplicationProxy *application = [LSApplicationProxy applicationProxyForIdentifier:bundleIdentifier];
if (!application.bundleURL) {
UIAlertController *errorAlert = [UIAlertController alertControllerWithTitle:[Localize localizedStringForKey:@"ERROR"] message:@"No application found with the specified bundle identifier." preferredStyle:UIAlertControllerStyleAlert];
[errorAlert addAction:[UIAlertAction actionWithTitle:[Localize localizedStringForKey:@"OK"] style:UIAlertActionStyleDefault handler:nil]];
[self presentViewController:errorAlert animated:YES completion:nil];
return;
}
[self _decryptApplicationProxy:application];
}];
[alert addAction:cancelAction];
[alert addAction:decryptAction];
[self presentViewController:alert animated:YES completion:nil];
}
- (void)_alternateIconButtonTapped {
// present modally
TDAlternateIconViewController *iconVC = [[TDAlternateIconViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:iconVC];
[self presentViewController:navController animated:YES completion:nil];
}
- (void)refresh {
dispatch_async(dispatch_get_main_queue(), ^{
[self loadAvailableApplications];
[self.tableView reloadData];
});
}
- (void)loadAvailableApplications {
NSMutableArray *apps = [NSMutableArray array];
NSArray <LSApplicationProxy *> *allInstalledApps = [[LSApplicationWorkspace defaultWorkspace] atl_allInstalledApplications];
[allInstalledApps enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(LSApplicationProxy *proxy, NSUInteger idx, BOOL *stop) {
_LSApplicationState *state = proxy.appState;
bool shouldSkip = (![proxy atl_isUserApplication] ||
![proxy atl_bundleIdentifier] ||
![proxy atl_nameToDisplay] ||
![proxy atl_shortVersionString] ||
![proxy bundleURL] ||
!state.isInstalled ||
state.isPlaceholder);
if (shouldSkip) return;
[apps addObject:proxy];
}];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"atl_nameToDisplay" ascending:YES];
_allAvailableApplications = [apps sortedArrayUsingDescriptors:@[sortDescriptor]];
[self _applySearchFilter];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [_filteredApplications count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DecryptionAppCell"];
if (!cell) cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"DecryptionAppCell"];
LSApplicationProxy *application = _filteredApplications[indexPath.row];
cell.textLabel.text = [application atl_nameToDisplay];
cell.detailTextLabel.text = [NSString stringWithFormat:@"%@ • %@", [application atl_shortVersionString], [application atl_bundleIdentifier]];
if ([_iconCache objectForKey:[application atl_bundleIdentifier]]) {
cell.imageView.image = [_iconCache objectForKey:[application atl_bundleIdentifier]];
return cell;
}
cell.imageView.image = _placeholderIcon;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
UIImage *icon = [UIImage _applicationIconImageForBundleIdentifier:[application atl_bundleIdentifier] format:getEffectiveIconFormat() scale:[UIScreen mainScreen].scale];
if (icon) {
[_iconCache setObject:icon forKey:[application atl_bundleIdentifier]];
dispatch_async(dispatch_get_main_queue(), ^{
UITableViewCell *updateCell = [tableView cellForRowAtIndexPath:indexPath];
if (updateCell) {
updateCell.imageView.image = icon;
[updateCell setNeedsLayout];
}
});
}
});
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 80.0f;
}
// TDDecryptionTask
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
LSApplicationProxy *application = _filteredApplications[indexPath.row];
NSString *title = [Localize localizedStringForKey:@"DECRYPT"];
NSString *subtitle = [NSString stringWithFormat:[Localize localizedStringForKey:@"DECRYPTION_PROMPT"], [application atl_nameToDisplay]];
// UIAlertController *alert = [UIAlertController alertControllerWithTitle:[Localize localizedStringForKey:@"DECRYPT"] message:[NSString stringWithFormat:@"Do you want to decrypt %@?", [application atl_nameToDisplay]] preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:subtitle preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:[Localize localizedStringForKey:@"CANCEL"] style:UIAlertActionStyleCancel handler:nil];
[alert addAction:cancelAction];
UIAlertAction *decryptBinaryOnlyAction = [UIAlertAction actionWithTitle:[Localize localizedStringForKey:@"MAIN_BINARY_ONLY"] style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
TDDecryptionTaskOptions options = TDDecryptionTaskOptionsMake(true);
[self _decryptApplicationProxy:application options:options];
}];
[alert addAction:decryptBinaryOnlyAction];
UIAlertAction *decryptAction = [UIAlertAction actionWithTitle:[Localize localizedStringForKey:@"FULL_IPA"] style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
[self _decryptApplicationProxy:application];
}];
[alert addAction:decryptAction];
// iPad popover fix - @NightwindDev
UIPopoverPresentationController *popover = alert.popoverPresentationController;
if (popover) {
popover.sourceView = self.view;
popover.sourceRect = [tableView rectForRowAtIndexPath:indexPath];
popover.permittedArrowDirections = UIPopoverArrowDirectionAny;
}
[self presentViewController:alert animated:YES completion:nil];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (void)_decryptApplicationProxy:(LSApplicationProxy *)application {
[self _decryptApplicationProxy:application options:TDDecryptionTaskDefaultOptions()];
}
- (void)_decryptApplicationProxy:(LSApplicationProxy *)application options:(TDDecryptionTaskOptions)options {
NSLog(@"Starting decryption for %@", [application atl_bundleIdentifier]);
TDDecryptionTask *decryptionTask = [[TDDecryptionTask alloc] initWithApplicationProxy:application];
decryptionTask.progressHandler = ^(NSString *message) {
dispatch_async(dispatch_get_main_queue(), ^{
if (self->_progressAlert) {
self->_progressAlert.message = message;
return;
}
self->_progressAlert =
[UIAlertController alertControllerWithTitle:[Localize localizedStringForKey:@"DECRYPTING"]
message:message
preferredStyle:UIAlertControllerStyleAlert];
[self presentViewController:self->_progressAlert animated:YES completion:nil];
});
};
[decryptionTask executeWithCompletionHandler:^(BOOL success, NSURL *outputURL, NSError *error) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
if (_progressAlert) {
[_progressAlert dismissViewControllerAnimated:YES completion:^{
_progressAlert = nil;
}];
}
UIAlertController *resultAlert;
if (success) {
resultAlert = [UIAlertController alertControllerWithTitle:[Localize localizedStringForKey:@"SUCCESS"] message:[Localize localizedStringForKey:@"DECRYPTION_COMPLETED"] preferredStyle:UIAlertControllerStyleAlert];
// @iCrazeiOS
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"filza://"]]) {
[resultAlert addAction:[UIAlertAction actionWithTitle:[Localize localizedStringForKey:@"SHOW_IN_FILZA"] style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
NSURL *url = [[NSURL URLWithString:@"filza://view"] URLByAppendingPathComponent:[outputURL path]];
[[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
}]];
}
} else {
resultAlert = [UIAlertController alertControllerWithTitle:[Localize localizedStringForKey:@"ERROR"] message:error.localizedDescription preferredStyle:UIAlertControllerStyleAlert];
}
[resultAlert addAction:[UIAlertAction actionWithTitle:[Localize localizedStringForKey:@"OK"] style:UIAlertActionStyleDefault handler:nil]];
[self presentViewController:resultAlert animated:YES completion:nil];
});
} options:options];
}
- (void)updateSearchResultsForSearchController:(UISearchController *)sc {
NSString *q = sc.searchBar.text ?: @"";
q = [q stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceAndNewlineCharacterSet];
static NSString *last = nil;
if (last && [last isEqualToString:q]) return; // no-op if same query
last = [q copy];
[self _applySearchFilter];
[self.tableView reloadData];
}
- (void)_applySearchFilter {
NSString *query = _searchController.searchBar.text ?: @"";
query = [query stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if (query.length == 0) {
_filteredApplications = _allAvailableApplications ?: @[];
return;
}
_filteredApplications = [_allAvailableApplications filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(LSApplicationProxy *application, NSDictionary *bindings) {
NSString *name = [[application atl_nameToDisplay] lowercaseString];
NSString *bundleID = [[application atl_bundleIdentifier] lowercaseString];
NSString *lowercaseQuery = [query lowercaseString];
return ([name containsString:lowercaseQuery] || [bundleID containsString:lowercaseQuery]);
}]];
}
@end