/
Tweak.xm
942 lines (758 loc) · 28.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
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
/*
* ShowSongMetadata
* Tweak.xm
*
* 2015 Kye Wei
*/
#import "ShowSongMetadataHeader.h"
#define settingsPath @"/User/Library/Preferences/com.kyewei.showsongmetadata.plist"
#ifndef kCFCoreFoundationVersionNumber_iOS_8_4
#define kCFCoreFoundationVersionNumber_iOS_8_4 1145.15
#endif
static BOOL tweakEnabled = YES;
static BOOL songButtonEnabled = YES;
static BOOL nowPlayingEnabled = YES;
static BOOL hideNull = YES;
static BOOL showTitle = YES;
static BOOL showArtist = YES;
static BOOL showAlbumArtist = YES;
static BOOL showComposer = YES;
static BOOL showGenre = YES;
static BOOL showYear = YES;
static BOOL showReleaseDate = YES;
static BOOL showComments = YES;
static BOOL showPlayCount = YES;
static BOOL showSkipCount = YES;
static BOOL showPlaysSinceSync = YES;
static BOOL showSkipsSinceSync = YES;
static BOOL showLastPlayed = YES;
static BOOL showBitrate = YES;
static BOOL showSampleRate = YES;
static void updatePrefs() {
NSDictionary *tweakSettings = [NSDictionary dictionaryWithContentsOfFile:settingsPath];
if (!tweakSettings){
return;
}
// Entire tweak enable/disable
NSNumber *tweakEnabledNum = tweakSettings[@"tweakEnabled"];
tweakEnabled = tweakEnabledNum ? [tweakEnabledNum boolValue] : 1;
// Song list cell button enable/disable
NSNumber *songButtonEnabledNum = tweakSettings[@"songButtonEnabled"];
songButtonEnabled = songButtonEnabledNum ? [songButtonEnabledNum boolValue] : 1;
// Now Playing button enable/disable
NSNumber *nowPlayingEnabledNum = tweakSettings[@"nowPlayingEnabled"];
nowPlayingEnabled = nowPlayingEnabledNum ? [nowPlayingEnabledNum boolValue] : 1;
// Hide "nil"
NSNumber *hideNullNum = tweakSettings[@"hideNull"];
hideNull = hideNullNum ? [hideNullNum boolValue] : 1;
//Metadata to show
NSNumber *showTitleNum = tweakSettings[@"showTitle"];
showTitle = showTitleNum ? [showTitleNum boolValue] : 1;
NSNumber *showArtistNum = tweakSettings[@"showArtist"];
showArtist = showArtistNum ? [showArtistNum boolValue] : 1;
NSNumber *showAlbumArtistNum = tweakSettings[@"showAlbumArtist"];
showAlbumArtist = showAlbumArtistNum ? [showAlbumArtistNum boolValue] : 1;
NSNumber *showComposerNum = tweakSettings[@"showComposer"];
showComposer = showComposerNum ? [showComposerNum boolValue] : 1;
NSNumber *showGenreNum = tweakSettings[@"showGenre"];
showGenre = showGenreNum ? [showGenreNum boolValue] : 1;
NSNumber *showYearNum = tweakSettings[@"showYear"];
showYear = showYearNum ? [showYearNum boolValue] : 1;
NSNumber *showReleaseDateNum = tweakSettings[@"showReleaseDate"];
showReleaseDate = showReleaseDateNum ? [showReleaseDateNum boolValue] : 1;
NSNumber *showCommentsNum = tweakSettings[@"showComments"];
showComments = showCommentsNum ? [showCommentsNum boolValue] : 1;
NSNumber *showPlayCountNum = tweakSettings[@"showPlayCount"];
showPlayCount = showPlayCountNum ? [showPlayCountNum boolValue] : 1;
NSNumber *showSkipCountNum = tweakSettings[@"showSkipCount"];
showSkipCount = showSkipCountNum ? [showSkipCountNum boolValue] : 1;
NSNumber *showPlaysSinceSyncNum = tweakSettings[@"showPlaysSinceSync"];
showPlaysSinceSync = showPlaysSinceSyncNum ? [showPlaysSinceSyncNum boolValue] : 1;
NSNumber *showSkipsSinceSyncNum = tweakSettings[@"showSkipsSinceSync"];
showSkipsSinceSync = showSkipsSinceSyncNum ? [showSkipsSinceSyncNum boolValue] : 1;
NSNumber *showLastPlayedNum = tweakSettings[@"showLastPlayed"];
showLastPlayed = showLastPlayedNum ? [showLastPlayedNum boolValue] : 1;
NSNumber *showBitrateNum = tweakSettings[@"showBitrate"];
showBitrate = showBitrateNum ? [showBitrateNum boolValue] : 1;
NSNumber *showSampleRateNum = tweakSettings[@"showSampleRate"];
showSampleRate = showSampleRateNum ? [showSampleRateNum boolValue] : 1;
}
static AudioFileID getAudioFileID(ExtAudioFileRef fileRef) {
OSStatus status;
AudioFileID result = NULL;
UInt32 size = sizeof(result);
status = ExtAudioFileGetProperty(fileRef, kExtAudioFileProperty_AudioFile, &size, &result);
assert(status == noErr);
return result;
}
static UInt32 getBitRate(AudioFileID audioFileId) {
OSStatus status;
UInt32 result = 0;
UInt32 size = sizeof(result);
status = AudioFileGetProperty(audioFileId, kAudioFilePropertyBitRate, &size, &result);
assert(status == noErr);
return result;
}
void showPopup (MPConcreteMediaItem *songEntity) {
NSURL *assetURL = [songEntity assetURL];
AVAssetTrack *audioTrack;
UInt32 bitRate;
if (assetURL){
AVURLAsset *asset = [AVAsset assetWithURL:assetURL];
NSArray *audioTracks = [asset tracksWithMediaType:@"soun"]; // AVMediaTypeAudio=@"soun"
if (!audioTracks || 1 > [audioTracks count]){
NSLog(@"showPopup audioTracks:MPConcreteMediaItem:%d %lu",0,(unsigned long)[audioTracks count]);
return;
}
audioTrack = [audioTracks objectAtIndex:0];
//float bitRate = [audioTrack estimatedDataRate];
//int sampleRate = [audioTrack naturalTimeScale];
// Found here:
//https://stackoverflow.com/questions/23241957/how-to-get-the-bit-rate-of-existing-mp3-or-aac-in-ios
ExtAudioFileRef extAudioFileRef;
OSStatus result = noErr;
result = ExtAudioFileOpenURL((__bridge CFURLRef) assetURL, &extAudioFileRef);
AudioFileID audioFileId = getAudioFileID(extAudioFileRef);
bitRate = getBitRate(audioFileId);
} else {
audioTrack = nil;
bitRate = 0;
}
// Entire string:
NSString *info = [NSString stringWithFormat:@"%@%@%@%@%@%@%@%@%@%@%@%@%@%@%@",
showTitle && (!hideNull || [songEntity title]) ? [NSString stringWithFormat:@"Title: %@\n", [songEntity title]] : @"",
showArtist && (!hideNull || [songEntity artist]) ? [NSString stringWithFormat:@"Artist: %@\n", [songEntity artist]] : @"",
showAlbumArtist && (!hideNull || [songEntity albumArtist]) ? [NSString stringWithFormat:@"Album Artist: %@\n", [songEntity albumArtist]] : @"",
showComposer && (!hideNull || [songEntity composer]) ? [NSString stringWithFormat:@"Composer: %@\n", [songEntity composer]] : @"",
showGenre && (!hideNull || [songEntity genre]) ? [NSString stringWithFormat:@"Genre: %@\n", [songEntity genre]] : @"",
showYear && (1 || [songEntity year]) ? [NSString stringWithFormat:@"Year: %llu\n", [songEntity year]] : @"",
showReleaseDate && (!hideNull || [songEntity releaseDate]) ? [NSString stringWithFormat:@"Release Date: %@\n", [[songEntity releaseDate] dateWithCalendarFormat:@"%Y-%m-%d" timeZone:nil]] : @"",
showComments && (!hideNull || [songEntity comments]) ? [NSString stringWithFormat:@"Comments: %@\n", [songEntity comments]] : @"",
showPlayCount && (1 || [songEntity playCount]) ? [NSString stringWithFormat:@"Play Count: %llu\n", [songEntity playCount]] : @"",
showSkipCount && (1 || [songEntity skipCount]) ? [NSString stringWithFormat:@"Skip Count: %llu\n", [songEntity skipCount]] : @"",
showPlaysSinceSync && (1 || [songEntity playCountSinceSync]) ? [NSString stringWithFormat:@"Plays Since Sync: %llu\n", [songEntity playCountSinceSync]] : @"",
showSkipsSinceSync && (1 || [songEntity skipCountSinceSync]) ? [NSString stringWithFormat:@"Skips Since Sync: %llu\n", [songEntity skipCountSinceSync]] : @"",
showLastPlayed && (!hideNull || [songEntity lastPlayedDate]) ? [NSString stringWithFormat:@"Last Played: %@\n", [[songEntity lastPlayedDate] dateWithCalendarFormat:@"%Y-%m-%d" timeZone:nil]] : @"",
//(int)[audioTrack estimatedDataRate]/1000 ,// bitrate, but only works for AAC files (i.e. .m4a extension)
showBitrate ? [NSString stringWithFormat:@"Bitrate: %dkbps\n", (int)bitRate/1000] : @"",
showSampleRate ? [NSString stringWithFormat:@"Sample Rate: %dHz\n", [audioTrack naturalTimeScale]] : @""]; //sampleRate
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:@"Song Metadata"
message:[info stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]
delegate:nil
cancelButtonTitle:@"Done"
otherButtonTitles:nil];
[alertView show];
[alertView release];
}
/*%hook UITapGestureRecognizer
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
BOOL a = touch.view.tag==22096;
if (a) {
return NO;
}
//return %orig;
return NO;
}
%end
%hook UIGestureRecognizer
-(id)initWithTarget:(id)arg1 action:(SEL)arg2 {
id result = %orig;
[self setCancelsTouchesInView:NO];
return result;
}
- (BOOL)cancelsTouchesInView
{
return NO;
}
%end*/
UIButton *getDetailButton(UITableViewCell *cell) {
UITableViewCellDetailDisclosureView * disclosureView = MSHookIvar<UITableViewCellDetailDisclosureView*>(cell, "_accessoryView");
if (!disclosureView) {
NSLog(@"Did not get disclosureView.");
return nil;
}
[disclosureView setUserInteractionEnabled:YES];
UIButton * infoButton = MSHookIvar<UIButton*>(disclosureView, "_infoButton");
if (!infoButton) {
NSLog(@"Did not get infoButton.");
return nil;
}
[infoButton setUserInteractionEnabled:YES];
return infoButton;
}
UITableView *getTableView(UITableViewCell *cell) {
id view = [cell superview];
while (view && [view isKindOfClass:[UITableView class]] == NO) {
view = [view superview];
}
UITableView *tableView = (UITableView *)view;
return tableView;
}
%group iOS8_0
%hook MusicTableViewCell
%new
-(void) displayPopup: (UIButton*) sender {
if ([[sender class] isSubclassOfClass:[UIButton class]]){
MPConcreteMediaItem *songEntity = [self getMediaItem:self];
if (!songEntity) {
NSLog(@"Did not get songEntity.");
return;
}
showPopup(songEntity);
} else {
NSLog(@"Sender is not a UIButton??");
}
}
%end
%hook MusicCollectionTrackTableViewCell
%new
- (id) getMediaItem:(UITableViewCell*)cell {
// Table View
MusicTableView *tableView = (MusicTableView *)getTableView(self);
if (!tableView) {
NSLog(@"Did not get tableView:MusicCollectionTrackTableViewCell.");
return nil;
}
// Table's View Controller
MusicAlbumsDetailViewController *controller = [tableView delegate]; // or [tableView dataSource];
if (!controller) {
NSLog(@"Did not get controller:MusicCollectionTrackTableViewCell.");
return nil;
}
// Want to get cell's position in Table View, which can then be used as an index
NSIndexPath *cellPosition = [tableView indexPathForCell:self];
if (!cellPosition) {
NSLog(@"Did not get cellPosition:MusicCollectionTrackTableViewCell.");
return nil;
}
int section = cellPosition.section;
int row = cellPosition.row;
// MusicActionTableViewCell (The Shuffle Button that appear when there is more than one album)
// shifts the sections by +1
// Detect, and reverse this:
UITableViewCell *checkCell = [tableView cellForRowAtIndexPath: [NSIndexPath indexPathForRow:0 inSection:0]];
/*NSString *type = MSHookIvar<NSString*>(checkCell, "_reuseIdentifier");
if ([type isEqualToString:@"MusicShuffleActionCellConfiguration"]) {
section--;
}*/
if (/*checkCell && */[checkCell isKindOfClass:[self class]] == YES
|| [NSStringFromClass([self class]) isEqualToString:@"MusicFlipsideAlbumTrackTableViewCell"]) {
// For some reason MusicFlipsideAlbumTrackTableViewCell's tableView's cellForRowAtIndexPath produces null..
// It is not a shuffle cell, don't do anything
} else {
// Then the shuffle section exists, and we have an off-by-one error;
//Fix:
section--;
}
if (section<0)
section=0;
/*NSDictionary *appliedProperties = MSHookIvar<NSDictionary*>(tableView, "_cellClassDict");
id lookup = [appliedProperties objectForKey:@"MusicShuffleActionCellConfiguration"];
NSLog(@"%d\n", (int)lookup);*/
//NSLog(@"Clicked section %d, row %d\n", section, row);
MusicArtistAlbumsDataSource *dataSource = [controller dataSource];
if (!dataSource) {
NSLog(@"Did not get dataSource:MusicCollectionTrackTableViewCell.");
return nil;
}
NSArray *mediaEntities = [dataSource sectionEntities];
if (!mediaEntities) {
NSLog(@"Did not get mediaEntities:MusicCollectionTrackTableViewCell.");
return nil;
}
//NSLog(@"%mediaEntities: length %d\n",[mediaEntities count]);
if (section >= [mediaEntities count]){
NSLog(@"Outofbounds mediaEntities:MusicCollectionTrackTableViewCell:%d %lu",section,(unsigned long)[mediaEntities count]);
return nil;
}
MPConcreteMediaItemCollection *sectionCollection = [mediaEntities objectAtIndex: section];
NSArray *songCollection = [sectionCollection items];
if (!songCollection) {
NSLog(@"Did not get songCollection.");
return nil;
}
if (row >= [songCollection count]){
NSLog(@"Outofbounds songCollection:MusicCollectionTrackTableViewCell:%d %lu",row,(unsigned long)[songCollection count]);
return nil;
}
MPConcreteMediaItem *songEntity = [songCollection objectAtIndex: row];
return songEntity;
}
- (id)initWithStyle:(int)arg1 reuseIdentifier:(id)arg2 {
id result = %orig;
if (!(tweakEnabled && songButtonEnabled)) {
return result;
}
/*NSString *cellType = [self reuseIdentifier];
if (!([cellType isEqualToString:@"MusicAlbumTracksCellConfiguration"])) {
return result;
}*/
[self setAccessoryType:UITableViewCellAccessoryDetailDisclosureButton];
// UITableViewCellAccessoryDetailDisclosureButton is 2
// UITableViewCellAccessoryDetailButton is 4
UIButton * infoButton = getDetailButton(self);
if (! infoButton) {
NSLog(@"MusicCollectionTrackTableViewCell accessory button not made!");
}
infoButton.tag = 22096;
[infoButton addTarget:self
action:@selector(displayPopup:)
forControlEvents:UIControlEventTouchDown];
//UIControlEventTouchDown is 1
//UIControlEventTouchUpInside is 64, but doesn't work, maybe absorbed by other Gesture Recognizers?
return result;
}
%end
%hook MusicSongListTableViewCell
%new
- (int) rowsBeforeSection:(int)section inTable:(UITableView*)tableView {
int start = 0;
UITableViewCell *checkCell = [tableView cellForRowAtIndexPath: [NSIndexPath indexPathForRow:0 inSection:0]];
if (checkCell && [checkCell isKindOfClass:[self class]] == YES) {
// It is not a shuffle cell, don't do anything
} else {
start = 1;
}
int cellCount=0;
for (int i = start; i< section; ++i) {
cellCount += [tableView numberOfRowsInSection:i];
}
return cellCount;
}
%new
- (id) getMediaItem:(UITableViewCell*)cell {
// Table View
MusicTableView *tableView = (MusicTableView *)getTableView(self);
if (!tableView) {
NSLog(@"Did not get tableView:MusicSongListTableViewCell.");
return nil;
}
// Table's View Controller
MusicSongsViewController *controller = [tableView delegate]; // or [tableView dataSource];
if (!controller) {
NSLog(@"Did not get controller:MusicSongListTableViewCell.");
return nil;
}
// Want to get cell's position in Table View, which can then be used as an index
NSIndexPath *cellPosition = [tableView indexPathForCell:self];
if (!cellPosition) {
NSLog(@"Did not get cellPosition:MusicSongListTableViewCell.");
return nil;
}
int section = cellPosition.section;
int row = cellPosition.row;
int combinedNumber = [self rowsBeforeSection:section inTable:tableView];
//NSLog(@"%d\n", combinedNumber);
//NSLog(@"Clicked section %d, row %d\n", section, row);
MusicSongsDataSource *dataSource = [controller dataSource];
if (!dataSource) {
NSLog(@"Did not get dataSource:MusicSongListTableViewCell.");
return nil;
}
NSArray *songCollection = [dataSource entities];
if (!dataSource) {
NSLog(@"Did not get songCollection:MusicSongListTableViewCell.");
return nil;
}
if (combinedNumber+row+1-1 >= [songCollection count]){
NSLog(@"Outofbounds songCollection:MusicSongListTableViewCell:%d %lu",combinedNumber+row+1-1,(unsigned long)[songCollection count]);
return nil;
}
MPConcreteMediaItem *songEntity = [songCollection objectAtIndex: combinedNumber+row+1-1];
return songEntity;
}
- (id)initWithStyle:(int)arg1 reuseIdentifier:(id)arg2 {
id result = %orig;
if (!(tweakEnabled && songButtonEnabled)) {
return result;
}
// Only hook if it is current class, not subclass
Class $MusicSearchTableViewCell = objc_getClass("MusicSearchTableViewCell");
if ([self isKindOfClass:[$MusicSearchTableViewCell class]]) {
return result;
}
/*NSString *cellType = [self reuseIdentifier];
if (!([cellType isEqualToString:@"MusicSongListCellConfiguration"]
|| [cellType isEqualToString:@"MusicPlaylistSongCellConfiguration"])) {
return result;
}*/
[self setAccessoryType:UITableViewCellAccessoryDetailDisclosureButton];
// UITableViewCellAccessoryDetailDisclosureButton is 2
// UITableViewCellAccessoryDetailButton is 4
UIButton * infoButton = getDetailButton(self);
if (! infoButton) {
NSLog(@"MusicSongListTableViewCell accessory button not made!");
}
infoButton.tag = 22096;
[infoButton addTarget:self
action:@selector(displayPopup:)
forControlEvents:UIControlEventTouchDown];
//UIControlEventTouchDown is 1
//UIControlEventTouchUpInside is 64, but doesn't work, maybe absorbed by other Gesture Recognizers?
return result;
}
%end
%hook MusicSearchTableViewCell
- (id)initWithStyle:(int)arg1 reuseIdentifier:(id)arg2 {
id result = %orig;
if (!(tweakEnabled && songButtonEnabled)) {
return result;
}
NSString *cellType = [self reuseIdentifier];
if (!([cellType isEqualToString:@"MusicSearchSongCellConfiguration"])) {
return result;
}
[self setAccessoryType:UITableViewCellAccessoryDetailDisclosureButton];
UIButton * infoButton = getDetailButton(self);
if (! infoButton) {
NSLog(@"MusicSearchTableViewCell accessory button not made!");
}
infoButton.tag = 22096;
[infoButton addTarget:self
action:@selector(displayPopup:)
forControlEvents:UIControlEventTouchDown];
return result;
}
%new
- (id) getMediaItem:(UITableViewCell*)cell {
// Table View
MusicSearchTableView *tableView = (MusicSearchTableView *)getTableView(self);
if (!tableView) {
NSLog(@"Did not get tableView:MusicSearchTableViewCell.");
return nil;
}
// Table's View Controller
MusicSearchViewController *controller = [tableView delegate]; // or [tableView dataSource];
if (!controller) {
NSLog(@"Did not get controller:MusicSearchTableViewCell.");
return nil;
}
// Want to get cell's position in Table View, which can then be used as an index
NSIndexPath *cellPosition = [tableView indexPathForCell:self];
if (!cellPosition) {
NSLog(@"Did not get cellPosition:MusicSearchTableViewCell.");
return nil;
}
//int section = cellPosition.section;
int row = cellPosition.row;
//This represents the search results for the sections (Albums, artists, songs, etc)
// that are NON-EMPTY (i.e. something actually found)
NSArray *searchResults = MSHookIvar<NSArray*>(controller, "_nonEmptySearchDataSources");
if (!searchResults) {
NSLog(@"Did not get searchResults:MusicSearchTableViewCell.");
return nil;
}
// This contains the search query data
MPUSearchDataSource *resultWeWant;
for (MPUSearchDataSource *element in searchResults) {
if ([[element query] groupingType] == 0) {//MPMediaGroupingTitle = 0
resultWeWant = element;
break;
}
}
if (!resultWeWant) {
NSLog(@"Did not get resultWeWant:MusicSearchTableViewCell.");
return nil;
}
NSArray *songMediaItemList = resultWeWant.query.items;
if (!songMediaItemList) {
NSLog(@"Did not get songMediaItemList:MusicSongListTableViewCell.");
return nil;
}
if (row >= [songMediaItemList count]){
NSLog(@"Outofbounds songMediaItemList:MusicSearchTableViewCell:%d %lu",row,(unsigned long)[songMediaItemList count]);
return nil;
}
MPConcreteMediaItem *songEntity = [songMediaItemList objectAtIndex: row];
return songEntity;
}
%end
%hook MusicNowPlayingViewController
%new
- (BOOL)isLoaded {
NSNumber * _isLoaded = objc_getAssociatedObject(self, @selector(isLoaded));
return _isLoaded ? [_isLoaded boolValue] : NO;
}
%new
- (void)setLoaded:(BOOL)value {
objc_setAssociatedObject(self, @selector(isLoaded), @(value), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
%new
- (float)titleWidthGap {
NSNumber * _isLoaded = objc_getAssociatedObject(self, @selector(titleWidthGap));
return [_isLoaded floatValue];
}
%new
- (void)setTitleWidthGap:(float)value {
objc_setAssociatedObject(self, @selector(titleWidthGap), @(value), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
-(void)viewDidAppear:(BOOL)animated {
%orig;
[self setLoaded:YES]; // So future addButtonToView calls are allowed
//NSLog(@"DISAPPLEAR");
[self addButtonToView];
}
-(void)viewDidDisappear:(BOOL)animated {
%orig;
[self setLoaded:NO];
}
//float titleWidthGap=0;
%new
-(void) addButtonToView {
if (!(tweakEnabled && nowPlayingEnabled)) {
return;
}
UINavigationItem *navigationItem = [self navigationItem];
UINavigationBar *bar = [navigationItem navigationBar];
UIView *potentialLabel = navigationItem.titleView;
UILabel *label;
if (![self isLoaded] || ![potentialLabel isKindOfClass:[UILabel class]]) {
return;
} else {
if (![potentialLabel isKindOfClass:[UILabel class]]){ // Is an UIView
for (UIView * element in potentialLabel.subviews) {
if ([element isKindOfClass:[UILabel class]]) {
label = (UILabel*) element;
} else if ([element isKindOfClass:[UIButton class]]) {
[element removeFromSuperview];
}
}
[potentialLabel removeFromSuperview];
} else { // original label
label = (UILabel*) potentialLabel;
}
}
[label removeFromSuperview];
UIView *newView = [[UIView alloc] initWithFrame:CGRectMake(0,0,bar.frame.size.width/2,22)];
navigationItem.titleView = newView;
CGRect oldLabelFrame = label.frame;
CGRect newLabelFrame;
if (oldLabelFrame.origin.x !=0)
[self setTitleWidthGap: oldLabelFrame.origin.x];
CGFloat xStart = [self titleWidthGap] - bar.frame.size.width/4; // centers title: half width of halved width view - 1/2 label width
newLabelFrame = CGRectMake (xStart, 11- oldLabelFrame.size.height/2, oldLabelFrame.size.width, oldLabelFrame.size.height);
label.frame = newLabelFrame;
UIButton *button = [UIButton buttonWithType:2]; //UIButtonTypeDetailDisclosure=2
button.frame = CGRectMake ((int)(xStart + oldLabelFrame.size.width + 16),0 , button.frame.size.width, button.frame.size.height);
[navigationItem.titleView addSubview:label];
[navigationItem.titleView addSubview:button];
button.tag = 22096;
[button addTarget:self
action:@selector(displayPopup:)
forControlEvents:UIControlEventTouchUpInside];
}
%new
- (id) getMediaItem:(UIViewController*)view {
MPAVItem *item = MSHookIvar<MPAVItem*>(self, "_item");
MPConcreteMediaItem *mediaItem = [item mediaItem];
return mediaItem;
}
%new
-(void) displayPopup: (UIButton*) sender {
if ([[sender class] isSubclassOfClass:[UIButton class]]){
MPConcreteMediaItem *songEntity = [self getMediaItem:self];
if (!songEntity) {
NSLog(@"Did not get songEntity.");
return;
}
showPopup(songEntity);
} else {
NSLog(@"Sender is not a UIButton??");
}
}
%end
%hook MusicNowPlayingPlaybackControlsView
-(void)reloadView {
%orig;
[self.delegate addButtonToView];
}
%end
%end //iOS8.0 to <8.4
%group iOS8_4
void addButtonToCell(id cell) {
MusicEntityTracklistItemTableViewCell *c = cell;
MusicCoalescingEntityValueProvider *cp = [c entityValueProvider];
if ([NSStringFromClass([[cp baseEntityValueProvider] class]) isEqualToString:@"MPConcreteMediaItem"] &&
c.accessoryType !=UITableViewCellAccessoryDetailDisclosureButton) {
[c setAccessoryType:UITableViewCellAccessoryDetailDisclosureButton];
UIButton * infoButton = getDetailButton(c);
if (!infoButton) {
NSLog(@"MusicEntityTracklistItemTableViewCell accessory button not made!");
}
infoButton.tag = 22096;
[infoButton addTarget:c
action:@selector(displayPopup:)
forControlEvents:UIControlEventTouchUpInside];
}
}
%hook MusicProfileAlbumsViewController
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
%orig;
if (!(tweakEnabled && songButtonEnabled))
return;
addButtonToCell(cell);
}
%end
%hook MusicProductTracklistTableViewController
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
%orig;
if (!(tweakEnabled && songButtonEnabled))
return;
addButtonToCell(cell);
}
%end
%hook MusicLibraryBrowseTableViewController
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
%orig;
if (!(tweakEnabled && songButtonEnabled))
return;
addButtonToCell(cell);
}
%end
%hook MusicEntityTracklistItemTableViewCell
%new
-(void) displayPopup: (UIButton*) sender {
if ([[sender class] isSubclassOfClass:[UIButton class]]){
MPConcreteMediaItem *songEntity = [self getMediaItem:self];
if (!songEntity) {
NSLog(@"Did not get songEntity.");
return;
}
showPopup(songEntity);
} else {
NSLog(@"Sender is not a UIButton??");
}
}
// did not override -initWithStyle, I found a better way by overriding the ViewControllers
%new
- (id) getMediaItem:(UITableViewCell*)cell {
// New iOS 8.4? SDK content, makes everything easier
// Too bad it (probably?) doesn't work on earlier iOS
MusicCoalescingEntityValueProvider *cp = [self entityValueProvider];
if (![NSStringFromClass([[cp baseEntityValueProvider] class]) isEqualToString:@"MPConcreteMediaItem"] ) // make sure not shuffle tableviewcell
return nil;
MPConcreteMediaItem *songEntity = [cp baseEntityValueProvider];
return songEntity;
}
%end
%hook MusicEntityHorizontalLockupTableViewCell
%new
-(void) displayPopup: (UIButton*) sender {
if ([[sender class] isSubclassOfClass:[UIButton class]]){
MPConcreteMediaItem *songEntity = [self getMediaItem:self];
if (!songEntity) {
NSLog(@"Did not get songEntity.");
return;
}
showPopup(songEntity);
} else {
NSLog(@"Sender is not a UIButton??");
}
}
%new
- (id) getMediaItem:(UITableViewCell*)cell {
MusicCoalescingEntityValueProvider *cp = [self entityValueProvider];
if (![NSStringFromClass([[cp baseEntityValueProvider] class]) isEqualToString:@"MPConcreteMediaItem"] )
return nil;
MPConcreteMediaItem *songEntity = [cp baseEntityValueProvider];
return songEntity;
}
%end
%hook MusicNowPlayingViewController
%new
- (MusicNowPlayingFloatingButton *)metadataButton {
MusicNowPlayingFloatingButton * _metadataButton = objc_getAssociatedObject(self, @selector(metadataButton));
return _metadataButton;
}
%new
- (void)setMetadataButton:(MusicNowPlayingFloatingButton *)button {
objc_setAssociatedObject(self, @selector(metadataButton), button, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
%new
- (id) getMediaItem:(UIViewController*)view {
MusicQueryNowPlayingItem *item = MSHookIvar<MusicQueryNowPlayingItem*>(self, "_currentItem");
MPConcreteMediaItem *mediaItem = [item mediaItem];
return mediaItem;
}
%new
-(void) displayPopup: (UIButton*) sender {
if ([[sender class] isSubclassOfClass:[UIButton class]]){
MPConcreteMediaItem *songEntity = [self getMediaItem:self];
if (!songEntity) {
NSLog(@"Did not get songEntity.");
return;
}
showPopup(songEntity);
} else {
NSLog(@"Sender is not a UIButton??");
}
}
- (void)viewDidLayoutSubviews {
%orig;
if (!(tweakEnabled && nowPlayingEnabled)) {
if ([self metadataButton])
[[self metadataButton] removeFromSuperview];
return;
}
MusicNowPlayingFloatingButton *dismiss = MSHookIvar<MusicNowPlayingFloatingButton*>(self, "_dismissButton");
if (dismiss) {
MusicNowPlayingFloatingButton *button;
if (![self metadataButton]) {
button = [[dismiss.class alloc] initWithFrame:
CGRectMake(dismiss.superview.frame.size.width - dismiss.frame.origin.x - dismiss.frame.size.width,
dismiss.frame.origin.y,
dismiss.frame.size.width,
dismiss.frame.size.height)];
UIButton *regular = [UIButton buttonWithType:2];
[regular setTintColor:[UIColor blackColor]];
CGFloat bt = regular.frame.size.width;
CGFloat size = dismiss.frame.size.width;
CGFloat offset = size-bt;
//8-16 x
//4-20 y
//24
// crop i
UIGraphicsBeginImageContextWithOptions(CGSizeMake(size/3, size*2/3), NO, 0.0);
[regular.currentImage drawAtPoint:CGPointMake(-size/3 + offset/2, -size/8)];
UIImage *crop = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[button setGlyphImage:crop];
[button setEffect:dismiss.effect];
[self setMetadataButton:button];
button.tag = 22096;
[button addTarget:self
action:@selector(displayPopup:)
forControlEvents:UIControlEventTouchUpInside];
} else {
button = [self metadataButton];
}
[button removeFromSuperview];
if ([self getMediaItem:self]) { // only display if used with local or streamed files, not radio
UIView *parent = dismiss.superview;
// adjust position so its mirroring other button
button.frame = CGRectMake(dismiss.superview.frame.size.width - dismiss.frame.origin.x - dismiss.frame.size.width,
dismiss.frame.origin.y,
dismiss.frame.size.width,
dismiss.frame.size.height);
[parent addSubview:button];
}
}
}
%end
%end
%ctor {
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(),
NULL,
(CFNotificationCallback)updatePrefs,
CFSTR("com.kyewei.showsongmetadata/settingschanged"),
NULL, CFNotificationSuspensionBehaviorCoalesce);
updatePrefs();
if (kCFCoreFoundationVersionNumber >=kCFCoreFoundationVersionNumber_iOS_8_4) {
%init(iOS8_4);
} else {
%init(iOS8_0);
}
}
// #include "Everything.xm"