-
Notifications
You must be signed in to change notification settings - Fork 5
/
Tweak.xm
1913 lines (1515 loc) · 70.3 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
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
Tweak.xm
FoldMusic
version 1.6.0, February 25th, 2014
(exactly one year and one day ago I was working on Version 1.4.0! ;o)
Copyright (C) 2012-2014 Daniel Ferreira
Ariel Aouizerate
BACON CODING COMPANY, LLC
Special thanks:
David Murray "Cykey" (for being a friend and contributing to the project in tiny but awesome ways)
The Doctor "The Doctor" (for saving the universe)
Dustin Howett "DHowett" (for being a friend, creating theos, logos, nic, and giving awesome code tips)
Max Shavrick "Maximus" (for being a friend, designing most of the UI, and giving awesome code tips)
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
theiostream
**/
/*%%%%%%%%%%%
%% Imports
%%%%%%%%%%%*/
#import "FolderAlbums.h"
#import "FAFolderCell.h"
#import "FAPreferencesHandler.h"
#import "FANotificationHandler.h"
#import "FACalloutView.h"
/*%%%%%%%%%%%
%% Macros
%%%%%%%%%%%*/
#define pxtopt(px) ( px * 72 / 96 )
#define pttopx(pt) ( pt * 96 / 72 )
@interface UIDevice (FolderAlbums_iPad)
- (BOOL)isWildcat;
@end
@interface UIImage (FolderAlbums_BundleImg)
+ (UIImage *)imageNamed:(NSString *)name inBundle:(NSBundle *)bundle;
@end
#define isiPad() (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
#define isPhone5() ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )568 ) < DBL_EPSILON )
// From CyDelete: DHowett is awesome.
#define SBLocalizedString(key) \
[[NSBundle mainBundle] localizedStringForKey:key value:@"None" table:@"SpringBoard"]
#define ASS(name) objc_getAssociatedObject(self, name)
#define SETASS(name, obj, pol) objc_setAssociatedObject(self, name, obj, pol)
/*%%%%%%%%%%%
%% Declarations
%%%%%%%%%%%*/
// Associated object keys for FAFolder
static char _mediaCollectionKey;
static char _keyNameKey;
// Associated object keys for FAFolderView
static char _labelKey;
static char _dataTableKey;
static char _controlsViewKey;
static char _musicButtonKey;
static char _nowPlayingImageKey;
static char _playButtonKey;
static char _artistLabel;
static char _songLabel;
static char _albumLabel;
static char _trackLabelKey;
static char _repeatButton;
static char _shuffleButton;
static char _sliderKey;
static char _wrapperKey;
static char _subtitleLabelKey;
static char _backButtonKey;
static char _forwardButtonKey;
static char _extraViewKey;
// FAFloatyFolderView keys
static char _floatyFolderKey;
static char _floatyControlsViewKey;
static char _floatyDataTableKey;
static char _floatyArtistLabel;
static char _mainViewKey;
// Other globals
static char _iconImageViewKey;
static CGRect groupFrame;
static NSUInteger idx = 0;
// === Make these associated objects?
static NSTimer *seekTimer = nil;
static BOOL wasSeeking = NO;
static NSTimer *progTimer = nil;
static BOOL draggingSlider = NO;
// ===
/*%%%%%%%%%%%
%% Functions
%%%%%%%%%%%*/
// This resizes my image right.
// (To Trevor Harmon) I do not care if it doesn't handle image orientations, at least
// it doesn't completely blur the stupid image when resizing.
static UIImage *UIImageResize(UIImage *image, CGSize newSize) {
if (!image) return nil;
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
/*static BOOL FANotStopped() {
//NSLog(@"returnin fanotstopped %@", [[%c(SBMediaController) sharedInstance] nowPlayingApplication]);
SBApplication *nowPlaying = [[%c(SBMediaController) sharedInstance] nowPlayingApplication];
if (nowPlaying != nil)
if ([[nowPlaying displayIdentifier] isEqualToString:@"com.apple.mobileipod"])
return YES;
return NO;
}*/
static MPMusicRepeatMode FAGetRepeatMode() {
NSString *repMode = [[NSDictionary dictionaryWithContentsOfFile:@"/var/mobile/Library/Preferences/com.apple.mobileipod.plist"] objectForKey:@"MusicRepeatSetting"];
if (!repMode) return MPMusicRepeatModeNone;
return (
[repMode isEqualToString:@"All"] ? MPMusicRepeatModeAll :
[repMode isEqualToString:@"One"] ? MPMusicRepeatModeOne :
MPMusicRepeatModeNone);
}
static MPMusicShuffleMode FAGetShuffleMode() {
NSString *shuMode = [[NSDictionary dictionaryWithContentsOfFile:@"/var/mobile/Library/Preferences/com.apple.mobileipod.plist"] objectForKey:@"MusicShuffleSetting"];
if (!shuMode) return MPMusicShuffleModeOff;
return (
[shuMode isEqualToString:@"Off"] ? MPMusicShuffleModeOff :
MPMusicShuffleModeSongs);
}
static UIImage *MediaPlayerImage(NSString *name) {
return [UIImage imageNamed:name inBundle:[NSBundle bundleWithIdentifier:@"com.apple.MediaPlayer"]];
}
static inline UIImage *PlayOrPauseImage(BOOL play) {
if (kCFCoreFoundationVersionNumber >= 800) {
if (play) return [UIImage imageWithContentsOfFile:@"/System/Library/PrivateFrameworks/MediaPlayerUI.framework/SystemMediaControl-Play-StarkNowPlaying.png"];
return [UIImage imageWithContentsOfFile:@"/System/Library/PrivateFrameworks/MediaPlayerUI.framework/SystemMediaControl-Pause-StarkNowPlaying.png"];
}
const char *imagestr = play ? "play.png" : "pause.png";
NSString *imagename = [NSString stringWithFormat:@"/System/Library/Frameworks/MediaPlayer.framework/%s", imagestr];
NSLog(@"PlayOrPauseImage() returning %@", imagename);
return [UIImage imageWithContentsOfFile:imagename];
}
static inline BOOL FAIsPlaying(MPMusicPlaybackState state) {
/*//if (kCFCoreFoundationVersionNumber >= 800) return [[%c(AVAudioSession) sharedInstance] isOtherAudioPlaying]; // -playbackState is broken on iOS 7.
BOOL ret = state != MPMusicPlaybackStateStopped && state != MPMusicPlaybackStatePaused && state != MPMusicPlaybackStateInterrupted;
if (kCFCoreFoundationVersionNumber < 800) return ret;
return ret ?: [[%c(SBMediaController) sharedInstance] isPlaying];*/
if (kCFCoreFoundationVersionNumber >= 800) return [[%c(SBMediaController) sharedInstance] isPlaying];
return state != MPMusicPlaybackStateStopped && state != MPMusicPlaybackStatePaused && state != MPMusicPlaybackStateInterrupted;
}
/*%%%%%%%%%%%
%% Subclasses
%%%%%%%%%%%*/
/* FAFolderIcon / FAFolder {{{ */
// thanks dhowett
%subclass FAFolderIcon : SBFolderIcon
- (BOOL)allowsUninstall {
return YES;
}
- (NSString *)uninstallAlertTitle {
return [NSString stringWithFormat:SBLocalizedString(@"UNINSTALL_ICON_TITLE"), [self displayName]];
}
- (NSString *)uninstallAlertBody {
return [NSString stringWithFormat:@"Are you sure you want to delete the \"%@\" folder? Your music data will be preserved.", [self displayName]];
}
- (void)completeUninstall {
%orig;
// FIXME: Use FAPreferencesHandler instead of FANotificationHandler
[[FANotificationHandler sharedInstance] removeKeyWithMessageName:nil userInfo:[NSDictionary dictionaryWithObject:[(FAFolder *)[self folder] keyName] forKey:@"Key"]];
}
%end
%subclass FAFolder : SBFolder
- (Class)folderViewClass {
return %c(FAFolderView);
}
// This is somehow wrong.
- (NSArray *)allIcons {
NSMutableArray *ret = [NSMutableArray array];
SBIcon *empty = [[[%c(SBIcon) alloc] init] autorelease];
int iconsTarget = isiPad() ? 19 : isPhone5() ? 15 : 11;
for (int i=0; i<iconsTarget; i++)
[ret addObject:empty];
return ret;
}
- (void)setIsOpen:(BOOL)open {
%log;
if (!open) {
if (progTimer && [progTimer isValid]) {
NSLog(@"[fm] Invalidating prog timer");
[progTimer invalidate];
progTimer = nil;
}
}
%orig;
}
%new(@@:)
- (MPMediaItemCollection *)mediaCollection {
return objc_getAssociatedObject(self, &_mediaCollectionKey);
}
%new(v@:@)
- (void)setMediaCollection:(MPMediaItemCollection *)mediaCollection {
objc_setAssociatedObject(self, &_mediaCollectionKey, mediaCollection, OBJC_ASSOCIATION_RETAIN);
}
%new(@@:)
- (NSString *)keyName {
return objc_getAssociatedObject(self, &_keyNameKey);
}
%new(v@:@)
- (void)setKeyName:(NSString *)keyName {
objc_setAssociatedObject(self, &_keyNameKey, keyName, OBJC_ASSOCIATION_RETAIN);
}
- (void)dealloc {
%log;
%orig;
objc_removeAssociatedObjects(self); // Please tell me this removes all associated object leaks.
}
%end
/* }}} */
/* Common Folder View {{{ */
%subclass FACommonFolderView : SBFolderView
%new(v@:)
- (void)initializeControlViewWithSuperview:(UIView *)controlsView haveExtraView:(BOOL)haveExtraView {
MPMusicPlayerController *music = [MPMusicPlayerController iPodMusicPlayer];
MPMediaItem *nowPlayingItem = [music nowPlayingItem];
NSString *placeholderSong = @"Not Playing";
UIImage *placeholderArtwork = UIImageResize(MediaPlayerImage(@"noartplaceholder.png"), CGSizeMake(130, 130));
MPMusicPlaybackState state;
UIImage *artworkImage = nil;
NSString *album=nil, *song=nil, *artist=nil;
NSInteger cur, tot;
NSTimeInterval dur;
MPMusicRepeatMode repeatMode;
MPMusicShuffleMode shuffleMode;
float pla;
if (nowPlayingItem) {
state = [music playbackState];
MPMediaItemArtwork *artwork = [nowPlayingItem valueForProperty:MPMediaItemPropertyArtwork];
UIImage *artworkImg = UIImageResize([artwork imageWithSize:CGSizeMake(130, 130)], CGSizeMake(130, 130));
if (artworkImg) { artworkImage = artworkImg; }
else { artworkImage = placeholderArtwork; }
song = [nowPlayingItem valueForProperty:MPMediaItemPropertyTitle];
if (!song) song = @"N/A"; // What the actual fuck.
album = [nowPlayingItem valueForProperty:MPMediaItemPropertyAlbumTitle];
artist = [nowPlayingItem valueForProperty:MPMediaItemPropertyArtist];
cur = [music indexOfNowPlayingItem]+1;
tot = [[[music queueAsQuery] items] count];
dur = [[nowPlayingItem valueForProperty:MPMediaItemPropertyPlaybackDuration] floatValue];
pla = [music currentPlaybackTime];
repeatMode = [music repeatMode] == MPMusicRepeatModeDefault ? FAGetRepeatMode() : [music repeatMode];
shuffleMode = [music shuffleMode] == MPMusicShuffleModeDefault ? FAGetShuffleMode() : [music shuffleMode];
}
else {
artworkImage = placeholderArtwork;
state = MPMusicPlaybackStateStopped;
song = placeholderSong;
cur = -1;
tot = -1;
dur = 0;
pla = 0.f;
repeatMode = FAGetRepeatMode();
shuffleMode = FAGetShuffleMode();
}
UIImageView *artworkView = [[[UIImageView alloc] initWithFrame:CGRectZero] autorelease];
[artworkView setImage:artworkImage];
objc_setAssociatedObject(self, &_nowPlayingImageKey, artworkView, OBJC_ASSOCIATION_RETAIN);
[controlsView addSubview:artworkView];
// TODO: Frame correctly! :(
UILabel *artistLabel = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
[artistLabel setText:artist];
[artistLabel setFont:[UIFont fontWithName:kCFCoreFoundationVersionNumber>=800 ? @"HelveticaNeue-Light" : @".HelveticaNeueUI-Bold" size:kCFCoreFoundationVersionNumber>=800 ? 14.f : 12.f]];
[artistLabel setTextAlignment:UITextAlignmentCenter];
[artistLabel setTextColor:[UIColor whiteColor]];
[artistLabel setBackgroundColor:[UIColor clearColor]];
if (kCFCoreFoundationVersionNumber < 800) {
[artistLabel setShadowColor:[UIColor blackColor]];
[artistLabel setShadowOffset:CGSizeMake(0, 1)];
}
[artistLabel setHidden:(artist == nil)];
objc_setAssociatedObject(self, &_artistLabel, artistLabel, OBJC_ASSOCIATION_RETAIN);
[controlsView addSubview:artistLabel];
UILabel *songLabel = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
[songLabel setText:song];
[songLabel setFont:[UIFont fontWithName:kCFCoreFoundationVersionNumber>=800 ? @"HelveticaNeue-Light" : @".HelveticaNeueUI-Bold" size:kCFCoreFoundationVersionNumber>=800 ? 14.f : 12.f]];
[songLabel setTextAlignment:UITextAlignmentCenter];
[songLabel setTextColor:[UIColor whiteColor]];
[songLabel setBackgroundColor:[UIColor clearColor]];
if (kCFCoreFoundationVersionNumber < 800) {
[songLabel setShadowColor:[UIColor blackColor]];
[songLabel setShadowOffset:CGSizeMake(0, 1)];
}
objc_setAssociatedObject(self, &_songLabel, songLabel, OBJC_ASSOCIATION_RETAIN);
[controlsView addSubview:songLabel];
UILabel *albumLabel = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
[albumLabel setText:album];
[albumLabel setFont:[UIFont fontWithName:kCFCoreFoundationVersionNumber>=800 ? @"HelveticaNeue-Light" : @".HelveticaNeueUI-Bold" size:kCFCoreFoundationVersionNumber>=800 ? 14.f : 12.f]];
[albumLabel setTextAlignment:UITextAlignmentCenter];
[albumLabel setTextColor:[UIColor whiteColor]];
[albumLabel setBackgroundColor:[UIColor clearColor]];
if (kCFCoreFoundationVersionNumber < 800) {
[albumLabel setShadowColor:[UIColor blackColor]];
[albumLabel setShadowOffset:CGSizeMake(0, 1)];
}
[albumLabel setHidden:(album == nil)];
objc_setAssociatedObject(self, &_albumLabel, albumLabel, OBJC_ASSOCIATION_RETAIN);
[controlsView addSubview:albumLabel];
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
NSString *backImageName = kCFCoreFoundationVersionNumber >= 800 ? @"/System/Library/PrivateFrameworks/MediaPlayerUI.framework/SystemMediaControl-Rewind-StarkNowPlaying.png" : @"/System/Library/Frameworks/MediaPlayer.framework/prevtrack.png";
[backButton setImage:[UIImage imageWithContentsOfFile:backImageName] forState:UIControlStateNormal];
[backButton addTarget:self action:@selector(pressedBackwardButton) forControlEvents:UIControlEventTouchDown];
[backButton addTarget:self action:@selector(releasedBackwardButton) forControlEvents:UIControlEventTouchUpInside];
[backButton addTarget:self action:@selector(releasedBackwardButton) forControlEvents:UIControlEventTouchDragOutside];
SETASS(&_backButtonKey, backButton, OBJC_ASSOCIATION_RETAIN);
[controlsView addSubview:backButton];
//NSString *playImageName = kCFCoreFoundationVersionNumber >= 800 ? @"/System/Library/PrivateFrameworks/MediaPlayerUI.framework/SystemMediaControl-Play-StarkNowPlaying.png" : @"/System/Library/Frameworks/MediaPlayer.framework/play.png";
//NSString *pauseImageName = kCFCoreFoundationVersionNumber >= 800 ? @"/System/Library/PrivateFrameworks/MediaPlayerUI.framework/SystemMediaControl-Pause-StarkNowPlaying.png" : @"/System/Library/Frameworks/MediaPlayer.framework/pause.png";
UIImage *play = PlayOrPauseImage(YES);
UIImage *pause = PlayOrPauseImage(NO);
UIButton *playButton = [UIButton buttonWithType:UIButtonTypeCustom];
[playButton setImage:(FAIsPlaying(state) ? pause : play) forState:UIControlStateNormal];
[playButton addTarget:self action:@selector(clickedPlayButton:) forControlEvents:UIControlEventTouchUpInside];
objc_setAssociatedObject(self, &_playButtonKey, playButton, OBJC_ASSOCIATION_RETAIN);
[controlsView addSubview:playButton];
UIButton *nextButton = [UIButton buttonWithType:UIButtonTypeCustom];
NSString *nextImageName = kCFCoreFoundationVersionNumber >= 800 ? @"/System/Library/PrivateFrameworks/MediaPlayerUI.framework/SystemMediaControl-Forward-StarkNowPlaying.png" : @"/System/Library/Frameworks/MediaPlayer.framework/nexttrack.png";
[nextButton setImage:[UIImage imageWithContentsOfFile:nextImageName] forState:UIControlStateNormal];
[nextButton addTarget:self action:@selector(pressedForwardButton) forControlEvents:UIControlEventTouchDown];
[nextButton addTarget:self action:@selector(releasedForwardButton) forControlEvents:UIControlEventTouchUpInside];
[nextButton addTarget:self action:@selector(releasedForwardButton) forControlEvents:UIControlEventTouchDragOutside];
SETASS(&_forwardButtonKey, nextButton, OBJC_ASSOCIATION_RETAIN);
[controlsView addSubview:nextButton];
UIView *extraView = haveExtraView ? [[[UIView alloc] initWithFrame:CGRectZero] autorelease] : controlsView;
NSString *trackText;
if (cur > -1 && tot > -1)
trackText = [NSString stringWithFormat:@"Track %ld of %ld", (long)cur, (long)tot];
else
trackText = @"Track -- of --";
UILabel *trackLabel = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
[trackLabel setText:trackText];
[trackLabel setFont:[UIFont fontWithName:kCFCoreFoundationVersionNumber>=800 ? @"HelveticaNeue-Light" : @".HelveticaNeueUI-Bold" size:12.f]];
[trackLabel setTextAlignment:UITextAlignmentCenter];
[trackLabel setTextColor:[UIColor whiteColor]];
[trackLabel setBackgroundColor:[UIColor clearColor]];
if (kCFCoreFoundationVersionNumber < 800) {
[trackLabel setShadowColor:[UIColor blackColor]];
[trackLabel setShadowOffset:CGSizeMake(0, 1)];
}
objc_setAssociatedObject(self, &_trackLabelKey, trackLabel, OBJC_ASSOCIATION_RETAIN);
[extraView addSubview:trackLabel];
MPDetailSlider *slider;
if (kCFCoreFoundationVersionNumber >= 800)
slider = [[[MPDetailSlider alloc] initWithFrame:CGRectZero style:8] autorelease];
else
slider = [[[MPDetailSlider alloc] initWithFrame:CGRectZero] autorelease];
[slider setAllowsDetailScrubbing:YES];
[slider setDuration:dur];
[slider setValue:pla animated:NO];
[slider setDelegate:self];
[self initializeProgTimer];
objc_setAssociatedObject(self, &_sliderKey, slider, OBJC_ASSOCIATION_RETAIN);
[extraView addSubview:slider];
NSString *repeatImageName = (
repeatMode == MPMusicRepeatModeAll ? @"repeat_on.png" :
repeatMode == MPMusicRepeatModeOne ? @"repeat_on_1.png" :
@"repeat_off.png");
UIImage *repeatImage = MediaPlayerImage(repeatImageName);
UIButton *repeatButton = [UIButton buttonWithType:UIButtonTypeCustom];
[repeatButton setImage:repeatImage forState:UIControlStateNormal];
[repeatButton addTarget:self action:@selector(pressedRepeatButton) forControlEvents:UIControlEventTouchUpInside];
objc_setAssociatedObject(self, &_repeatButton, repeatButton, OBJC_ASSOCIATION_RETAIN);
[extraView addSubview:repeatButton];
NSString *shuffleImageName = (
shuffleMode != MPMusicShuffleModeOff ? @"shuffle_on.png" :
@"shuffle_off.png");
UIImage *shuffleImage = MediaPlayerImage(shuffleImageName);
UIButton *shuffleButton = [UIButton buttonWithType:UIButtonTypeCustom];
[shuffleButton setImage:shuffleImage forState:UIControlStateNormal];
[shuffleButton addTarget:self action:@selector(pressedShuffleButton) forControlEvents:UIControlEventTouchUpInside];
objc_setAssociatedObject(self, &_shuffleButton, shuffleButton, OBJC_ASSOCIATION_RETAIN);
[extraView addSubview:shuffleButton];
if (kCFCoreFoundationVersionNumber < kCFCoreFoundationVersionNumber_iOS_6_0) [controlsView setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
if (haveExtraView) {
[controlsView addSubview:extraView];
objc_setAssociatedObject(self, &_extraViewKey, extraView, OBJC_ASSOCIATION_RETAIN);
}
}
%new(@@:)
- (NSArray *)itemKeys {
NSArray *_itemKeys = !([[(FAFolder *)[self folder] mediaCollection] isKindOfClass:[MPMediaPlaylist class]]) ?
[NSArray arrayWithObjects:MPMediaItemPropertyPlaybackDuration, MPMediaItemPropertyPlayCount, nil] :
[NSArray arrayWithObjects:MPMediaItemPropertyPlaybackDuration, MPMediaItemPropertyPlayCount, MPMediaItemPropertyArtist, MPMediaItemPropertyAlbumTitle, nil];
return _itemKeys;
}
%new(f@:@@)
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 38.f;
}
%new(i@:@@)
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [[[(FAFolder *)[self folder] mediaCollection] items] count];
}
%new(i@:@)
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
%new(@@:@@)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"FAFolderCellIdentifier";
MPMediaItemCollection *collection = [(FAFolder *)[self folder] mediaCollection];
NSArray *items = [collection items];
MPMediaItem *item = [items objectAtIndex:[indexPath row]];
FAFolderCell *cell = (FAFolderCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell)
cell = [[[FAFolderCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
[cell setDetailProperty:[[self itemKeys] objectAtIndex:idx] change:NO];
[cell setMediaItem:item];
return cell;
}
%new(v@:@@)
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
MPMediaItemCollection *collection = [(FAFolder *)[self folder] mediaCollection];
MPMusicPlayerController *music = [MPMusicPlayerController iPodMusicPlayer];
MPMediaItem *nowPlayingItem = [music nowPlayingItem];
NSArray *items = [collection items];
MPMediaItem *cellItem = [items objectAtIndex:[indexPath row]];
if (nowPlayingItem) {
MPMusicPlaybackState state = [music playbackState];
NSNumber *nowPlayingPersistent = [nowPlayingItem valueForProperty:MPMediaItemPropertyPersistentID];
NSNumber *cellItemPersistent = [cellItem valueForProperty:MPMediaItemPropertyPersistentID];
if ([nowPlayingPersistent compare:cellItemPersistent] == NSOrderedSame) {
if (FAIsPlaying(state)) {
[music pause];
goto end;
}
// FIXME: Can we trust the state here?
else if (state == MPMusicPlaybackStatePaused) {
goto play;
}
}
}
collection = [(FAFolder *)[self folder] mediaCollection];
[music setQueueWithItemCollection:collection];
[music setNowPlayingItem:cellItem];
play:
[music play];
end:
[tableView deselectRowAtIndexPath:indexPath animated:YES];
//[self receivedTrackChanged];
}
%new(v@:@)
- (void)nextItem:(UIGestureRecognizer *)rec {
NSArray *_itemKeys = [self itemKeys];
idx++;
idx = idx>=[_itemKeys count] ? 0 : idx;
NSArray *visibleCells = [(UITableView *)[rec view] visibleCells];
NSUInteger count = [visibleCells count];
for (NSUInteger i=0; i<count; i++)
[[visibleCells objectAtIndex:i] setDetailProperty:[_itemKeys objectAtIndex:idx] change:YES];
}
%new(v@:)
- (void)initializeProgTimer {
MPMusicPlayerController *music = [MPMusicPlayerController iPodMusicPlayer];
MPMusicPlaybackState state;
if ([music nowPlayingItem]) {
state = [music playbackState];
}
else {
state = MPMusicPlaybackStateStopped;
}
if (FAIsPlaying(state)) {
if (progTimer == nil)
progTimer = [NSTimer scheduledTimerWithTimeInterval:1.f target:self selector:@selector(updateSlider) userInfo:nil repeats:YES];
else if (![progTimer isValid])
progTimer = [NSTimer scheduledTimerWithTimeInterval:1.f target:self selector:@selector(updateSlider) userInfo:nil repeats:YES];
}
else {
if (progTimer) {
if ([progTimer isValid]) [progTimer invalidate];
progTimer = nil;
}
}
/*if (!progTimer || ![progTimer isValid]) progTimer = [NSTimer scheduledTimerWithTimeInterval:1.f target:self selector:@selector(updateSlider) userInfo:nil repeats:YES];
if (!(state != MPMusicPlaybackStateStopped || state != MPMusicPlaybackStatePaused || state != MPMusicPlaybackStateInterrupted)) {
draggingSlider = YES;
}*/
}
%new(v@:)
- (void)receivedTrackChanged {
%log;
NSLog(@"[TRACK] IS PLAYING: %d", FAIsPlaying(0));
MPMusicPlayerController *music = [MPMusicPlayerController iPodMusicPlayer];
MPMediaItem *item = [music nowPlayingItem];
UIImage *artworkImage;
NSString *artist, *album, *song;
NSInteger cur, tot;
NSTimeInterval dur;
float pla;
if (item) {
MPMediaItemArtwork *artwork = [item valueForProperty:MPMediaItemPropertyArtwork];
UIImage *artworkImg = UIImageResize([artwork imageWithSize:CGSizeMake(130, 130)], CGSizeMake(130, 130));
if (artworkImg) artworkImage = artworkImg;
else artworkImage = UIImageResize(MediaPlayerImage(@"noartplaceholder.png"), CGSizeMake(130, 130));
artist = [item valueForProperty:MPMediaItemPropertyArtist];
album = [item valueForProperty:MPMediaItemPropertyAlbumTitle];
song = [item valueForProperty:MPMediaItemPropertyTitle];
if (!song) song = @"N/A";
cur = [music indexOfNowPlayingItem]+1;
tot = [[[music queueAsQuery] items] count];
dur = [[item valueForProperty:MPMediaItemPropertyPlaybackDuration] floatValue];
pla = [music currentPlaybackTime]; // 0.f always. But who knows?!
}
else {
artworkImage = UIImageResize(MediaPlayerImage(@"noartplaceholder.png"), CGSizeMake(130, 130));
artist = nil;
album = nil;
song = @"Not Playing";
cur = -1;
tot = -1;
dur = 0;
pla = 0.f;
}
[objc_getAssociatedObject(self, &_nowPlayingImageKey) setImage:artworkImage];
UILabel *artistLabel = objc_getAssociatedObject(self, &_artistLabel);
[artistLabel setHidden:(artist == nil)];
[artistLabel setText:artist];
UILabel *albumLabel = objc_getAssociatedObject(self, &_albumLabel);
[albumLabel setHidden:(album == nil)];
[albumLabel setText:album];
[objc_getAssociatedObject(self, &_songLabel) setText:song];
NSString *trackText;
UILabel *trackLabel = objc_getAssociatedObject(self, &_trackLabelKey);
if (cur > -1 && tot > -1)
trackText = [NSString stringWithFormat:@"Track %ld of %ld", (long)cur, (long)tot];
else
trackText = @"Track -- of --";
[trackLabel setText:trackText];
MPDetailSlider *slider = objc_getAssociatedObject(self, &_sliderKey);
[slider setDuration:dur];
[slider setValue:pla animated:NO];
//[self receivedStateChanged];
}
%new(v@:)
- (void)receivedStateChanged {
%log;
NSLog(@"[STATE] IS PLAYING: %d", FAIsPlaying(0));
MPMusicPlayerController *music = [MPMusicPlayerController iPodMusicPlayer];
MPMusicPlaybackState state = [music playbackState];
BOOL isPlaying = FAIsPlaying(state);
NSLog(@"STATE: SETTING %@ IMAGE", !isPlaying ? @"Play" : @"Pause");
[objc_getAssociatedObject(self, &_playButtonKey) setImage:PlayOrPauseImage(!isPlaying) forState:UIControlStateNormal];
//if (!progTimer || ![progTimer isValid]) progTimer = [NSTimer scheduledTimerWithTimeInterval:1.f target:self selector:@selector(updateSlider) userInfo:nil repeats:YES];
//if (!(state != MPMusicPlaybackStateStopped || state != MPMusicPlaybackStatePaused || state != MPMusicPlaybackStateInterrupted)) {
// draggingSlider = YES;
//}
if (FAIsPlaying(state)) {
NSLog(@"STATE: INITIALIZE TIMER.");
if (progTimer == nil)
progTimer = [NSTimer scheduledTimerWithTimeInterval:1.f target:self selector:@selector(updateSlider) userInfo:nil repeats:YES];
else if (![progTimer isValid])
progTimer = [NSTimer scheduledTimerWithTimeInterval:1.f target:self selector:@selector(updateSlider) userInfo:nil repeats:YES];
}
else {
if (progTimer) {
if ([progTimer isValid]) [progTimer invalidate];
progTimer = nil;
}
}
}
%new(v@:)
- (void)updateSlider {
%log;
if (draggingSlider)
return;
MPMusicPlayerController *music = [MPMusicPlayerController iPodMusicPlayer];
MPMediaItem *item = [music nowPlayingItem];
if (item) {
NSTimeInterval tim = [music currentPlaybackTime];
[objc_getAssociatedObject(self, &_sliderKey) setValue:(float)tim animated:YES];
}
}
%new(v@:@f)
- (void)detailSlider:(UISlider *)slider didChangeValue:(float)value {
MPMusicPlayerController *music = [MPMusicPlayerController iPodMusicPlayer];
[music setCurrentPlaybackTime:value];
}
%new(v@:@)
- (void)detailSliderTrackingDidBegin:(UISlider *)slider {
draggingSlider = YES;
}
%new(v@:@)
- (void)detailSliderTrackingDidEnd:(UISlider *)slider {
draggingSlider = NO;
if (progTimer != nil) {
if ([progTimer isValid]) {
[progTimer invalidate];
progTimer = [NSTimer scheduledTimerWithTimeInterval:1.f target:self selector:@selector(updateSlider) userInfo:nil repeats:YES];
}
}
}
%new(v@:@)
- (void)detailSliderTrackingDidCancel:(UISlider *)slider {
draggingSlider = NO;
if (progTimer != nil) {
if ([progTimer isValid]) {
[progTimer invalidate];
progTimer = [NSTimer scheduledTimerWithTimeInterval:1.f target:self selector:@selector(updateSlider) userInfo:nil repeats:YES];
}
}
}
%new(v@:@)
- (void)clickedPlayButton:(UIButton *)button {
%log;
NSLog(@"[CLICKED] IS PLAYING: %d", FAIsPlaying(0));
MPMusicPlayerController *music = [MPMusicPlayerController iPodMusicPlayer];
MPMediaItem *item = [music nowPlayingItem];
MPMusicPlaybackState state = [music playbackState];
if (!item) {
MPMediaItemCollection *collection = [(FAFolder *)[self folder] mediaCollection];
[music setQueueWithItemCollection:collection];
}
//UIButton *controlsButton = objc_getAssociatedObject(self, &_playButtonKey);
if (FAIsPlaying(state)) {
//if (![button isEqual:controlsButton]) [button setImage:PlayOrPauseImage(NO) forState:UIControlStateNormal];
[music pause];
//[[%c(SBMediaController) sharedInstance] pause];
}
else {
//if (![button isEqual:controlsButton]) [button setImage:PlayOrPauseImage(YES) forState:UIControlStateNormal];
[music play];
//[[%c(SBMediaController) sharedInstance] play];
}
//[self receivedTrackChanged];
}
%new(v@:)
- (void)pressedForwardButton {
MPMusicPlayerController *music = [MPMusicPlayerController iPodMusicPlayer];
MPMediaItem *item = [music nowPlayingItem];
if (!item)
return;
seekTimer = [NSTimer scheduledTimerWithTimeInterval:.5f target:self selector:@selector(_seekForward) userInfo:nil repeats:NO];
}
%new(v@:)
- (void)pressedBackwardButton {
MPMusicPlayerController *music = [MPMusicPlayerController iPodMusicPlayer];
MPMediaItem *item = [music nowPlayingItem];
if (!item)
return;
seekTimer = [NSTimer scheduledTimerWithTimeInterval:.5f target:self selector:@selector(_seekBackward) userInfo:nil repeats:NO];
}
%new(v@:)
- (void)_seekForward {
MPMusicPlayerController *music = [MPMusicPlayerController iPodMusicPlayer];
[music beginSeekingForward];
wasSeeking = YES;
}
%new(v@:)
- (void)_seekBackward {
MPMusicPlayerController *music = [MPMusicPlayerController iPodMusicPlayer];
[music beginSeekingBackward];
wasSeeking = YES;
}
%new(v@:)
- (void)releasedForwardButton {
MPMusicPlayerController *music = [MPMusicPlayerController iPodMusicPlayer];
MPMediaItem *item = [music nowPlayingItem];
if (!item) return;
if (wasSeeking) {
[music endSeeking];
wasSeeking = NO;
return;
}
[seekTimer invalidate];
[music skipToNextItem];
//[self receivedTrackChanged];
}
%new(v@:)
- (void)releasedBackwardButton {
MPMusicPlayerController *music = [MPMusicPlayerController iPodMusicPlayer];
MPMediaItem *item = [music nowPlayingItem];
if (!item) return;
if (wasSeeking) {
[music endSeeking];
wasSeeking = NO;
return;
}
[seekTimer invalidate];
if ([music currentPlaybackTime] > 2) {
[objc_getAssociatedObject(self, &_sliderKey) setValue:0.f animated:NO];
[music skipToBeginning];
}
else
[music skipToPreviousItem];
//[self receivedTrackChanged];
}
%new(v@:)
- (void)pressedRepeatButton {
MPMusicPlayerController *music = [MPMusicPlayerController iPodMusicPlayer];
MPMusicRepeatMode repeatMode = [music repeatMode] == MPMusicRepeatModeDefault ? FAGetRepeatMode() : [music repeatMode];
MPMusicRepeatMode newMode = (
repeatMode == MPMusicRepeatModeNone ? MPMusicRepeatModeAll :
repeatMode == MPMusicRepeatModeAll ? MPMusicRepeatModeOne :
MPMusicRepeatModeNone);
[music setRepeatMode:newMode];
NSString *imageTitle = (
newMode == MPMusicRepeatModeAll ? @"repeat_on.png" :
newMode == MPMusicRepeatModeOne ? @"repeat_on_1.png" :
@"repeat_off.png");
UIImage *repeatImage = MediaPlayerImage(imageTitle);
[objc_getAssociatedObject(self, &_repeatButton) setImage:repeatImage forState:UIControlStateNormal];
}
%new(v@:)
- (void)pressedShuffleButton {
MPMusicPlayerController *music = [MPMusicPlayerController iPodMusicPlayer];
MPMediaItem *item = [music nowPlayingItem];
MPMusicShuffleMode shuffleMode = [music shuffleMode] == MPMusicShuffleModeDefault ? FAGetShuffleMode() : [music shuffleMode];
MPMusicShuffleMode newMode = (
shuffleMode == MPMusicShuffleModeOff ? MPMusicShuffleModeSongs :
MPMusicShuffleModeOff);
[music setShuffleMode:newMode];
NSString *imageTitle = (
newMode == MPMusicShuffleModeSongs ? @"shuffle_on.png" :
@"shuffle_off.png");
UIImage *shuffleImage = MediaPlayerImage(imageTitle);
[objc_getAssociatedObject(self, &_shuffleButton) setImage:shuffleImage forState:UIControlStateNormal];
NSInteger cur, tot;
if (item) {
cur = [music indexOfNowPlayingItem]+1;
tot = [[[music queueAsQuery] items] count];
}
else {
cur = -1;
tot = -1;
}
NSString *trackText;
UILabel *trackLabel = objc_getAssociatedObject(self, &_trackLabelKey);
if (cur > -1 && tot > -1)
trackText = [NSString stringWithFormat:@"Track %ld of %ld", (long)cur, (long)tot];
else
trackText = @"Track -- of --";
[trackLabel setText:trackText];
}
%end
/* }}} */
/* FAFloatyFolderView {{{ */
%group FAFolderView7x
%hook SBFolderController
- (Class)_contentViewClass {
return [[self folder] isKindOfClass:%c(FAFolder)] ? %c(FAFloatyFolderView) : %orig;
}
%end
%subclass FAFloatyFolderView : FACommonFolderView
%new
- (Class)detailSliderClass {
//return %c(MusicThinDetailSlider);
return %c(MPDetailSlider);
}
%new(@@:)
- (FAFolder *)folder {
return objc_getAssociatedObject(self, &_floatyFolderKey);
}
- (id)initWithFolder:(FAFolder *)folder orientation:(int)orientation {
if ((self = %orig)) {
objc_setAssociatedObject(self, &_floatyFolderKey, folder, OBJC_ASSOCIATION_RETAIN);
objc_setAssociatedObject(self, &_floatyDataTableKey, nil, OBJC_ASSOCIATION_ASSIGN);
[[UIApplication sharedApplication] launchMusicPlayerSuspended];
[[MPMusicPlayerController iPodMusicPlayer] beginGeneratingPlaybackNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receivedStateChanged) name:@"SBMediaNowPlayingChangedNotification" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receivedTrackChanged) name:MPMusicPlayerControllerNowPlayingItemDidChangeNotification object:nil];
UIView *scrollClipView = MSHookIvar<UIView *>(self, "_scrollClipView");
[[scrollClipView subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
[self createFolderAlbumsInView:scrollClipView];
}
return self;
}
- (void)_layoutSubviews {
%log;
%orig;
[self setupArtistLabel];
[self setupFolderAlbumsInView:MSHookIvar<UIView *>(self, "_scrollClipView")];
}
- (void)fadeContentForMinificationFraction:(float)arg1 {
%orig;
[objc_getAssociatedObject(self, &_mainViewKey) setAlpha:1 - arg1];
[objc_getAssociatedObject(self, &_floatyArtistLabel) setAlpha:1 - arg1];
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
NSString *key = [(FAFolder *)[self folder] keyName];
NSString *res = [[textField text] isEqualToString:@""] ? key : [textField text];
NSDictionary *update = [NSDictionary dictionaryWithObject:res forKey:@"fakeTitle"];
[[FAPreferencesHandler sharedInstance] optimizedUpdateKey:key withDictionary:update];
[[self folder] setDisplayName:res];
%orig;
}
%new(v@:@)