-
Notifications
You must be signed in to change notification settings - Fork 2
/
Tweak.xm
1001 lines (813 loc) · 30 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
#import <substrate.h>
#import <QuartzCore/QuartzCore.h>
#import <Foundation/Foundation.h>
#import <Foundation/NSDistributedNotificationCenter.h>
#import <AVFoundation/AVFoundation.h>
#import <AudioToolbox/AudioServices.h>
#import <Accelerate/Accelerate.h>
#import <AppSupport/AppSupport.h>
#import "MediaRemote.h"
#import <UIKit/UIKit.h>
#import "BeatVisualizerView.h"
#import "rocketbootstrap.h"
#import "prismheaders.h"
#import "LEColorPicker.h"
#import "PrismAudioManager.h"
#define LEFT_CHANNEL (0)
#define RIGHT_CHANNEL (1)
#define MYVolumeUnitMeterView_CALIBRATION 12.0f
#define kDefaultMusicColor [[UIColor alloc] initWithRed:1.0f green:1.0f blue:1.0f alpha:1.0f]
#define kDefaultBlackColor [[UIColor alloc] initWithRed:0.0f green:0.0f blue:0.0f alpha:1.0f]
#define kDefaultRedColor [[UIColor alloc] initWithRed:1.0f green:0.0f blue:0.0f alpha:1.0f]
#define kDefaultCyanColor [[UIColor alloc] initWithRed:0.0f green:1.0f blue:1.0f alpha:1.0f]
#define kDefaultMagenta [[UIColor alloc] initWithRed:1.0f green:0.0f blue:1.0f alpha:1.0f]
#define prefPath @"/User/Library/Preferences/com.joshdoctors.prism.plist"
static BOOL tweakEnabled = NO;
static CGFloat theme = 0.0;
static NSInteger colorStyle = 0;
static BOOL overlayAlbumArt = NO;
static BOOL lyricsVisible = NO;
static BOOL wasVisible = NO;
static BOOL isVisible = NO;
static NSInteger position = 0;
static UIColor * overlayColor;
static UIColor * beatPrimaryColor;
static UIColor * beatSecondaryColor;
static UIColor * spectrumPrimaryColor;
static NSInteger spectrumBarCount = 30;
static NSInteger spectrumStyle = 0;
static float transparency = 100;
static BOOL visualizerVisibile = NO;
static float barHeight = 100;
static UIColor * prismFlowPrimary;
static UIColor * prismFlowSecondary;
static float leftVol = 0.0f;
static float rightVol = 0.0f;
static float avgVol = 0.0;
static float playerVolume = 1.0;
static BOOL shouldUpdatePrismDefaults = YES;
static NSMutableArray * fftData = nil;
static NSMutableArray * lsfftData = nil;
static NSMutableArray * outData = nil;
static NSInteger outDataLength = 1024;
static NSInteger appState = 2;
static double mag = 0;
static BOOL isPaused = NO;
static BOOL pastStatus = NO;
static BOOL isPlaying = false;
static BOOL hasProcessed = false;
static MPAVItem *item = nil;
static bool useDefaultLS = NO;
static CPDistributedMessagingCenter * messagingCenter = nil;
static MPAVItem *itemWithTap = nil;
static NSString * cachedTitle = nil;
static UIColor* parseColorFromPreferences(NSString* string) {
NSArray *prefsarray = [string componentsSeparatedByString: @":"];
NSString *hexString = [prefsarray objectAtIndex:0];
double alpha = [[prefsarray objectAtIndex:1] doubleValue];
unsigned rgbValue = 0;
NSScanner *scanner = [NSScanner scannerWithString:hexString];
[scanner setScanLocation:1]; // bypass '#' character
[scanner scanHexInt:&rgbValue];
return [[UIColor alloc] initWithRed:((rgbValue & 0xFF0000) >> 16)/255.0 green:((rgbValue & 0xFF00) >> 8)/255.0 blue:(rgbValue & 0xFF)/255.0 alpha:alpha];
}
static bool isNumeric(NSString* checkText)
{
return [[NSScanner scannerWithString:checkText] scanFloat:NULL];
}
static NSString* stringFromColor(UIColor* color)
{
const CGFloat * components = CGColorGetComponents([color CGColor]);
return [NSString stringWithFormat:@"{%0.3f, %0.3f, %0.3f, %0.3f}", components[0], components[1], components[2], components[3]];
}
static UIColor* colorWithString(NSString * stringToConvert)
{
NSString *cString = [stringToConvert stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
// Proper color strings are denoted with braces
if (![cString hasPrefix:@"{"]) return nil;
if (![cString hasSuffix:@"}"]) return nil;
// Remove braces
cString = [cString substringFromIndex:1];
cString = [cString substringToIndex:([cString length] - 1)];
// Separate into components by removing commas and spaces
NSArray *components = [cString componentsSeparatedByString:@", "];
if ([components count] != 4) return nil;
// Create the color
return [UIColor colorWithRed:[[components objectAtIndex:0] floatValue]
green:[[components objectAtIndex:1] floatValue]
blue:[[components objectAtIndex:2] floatValue]
alpha:[[components objectAtIndex:3] floatValue]];
}
%group NowPlayingArtView
%hook MPAVController
-(void)_itemDidChange:(id)arg1{
%orig;
NSLog(@"[Prism]Changing the music item.");
if(!tweakEnabled)
{
NSLog(@"[Prism]Tweak was not enabled, not attempting to tap into the audio stream.");
return;
}
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"didChangeSpectrumData" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didChangeSpectrumData:) name:@"didChangeSpectrumData" object:nil];
if([self.currentItem playerItem].audioMix == nil)
{
//Audio is not coming from the music app if the sharedAVPlayer is nil
if([%c(MusicAVPlayer) sharedAVPlayer] == nil)
return;
else
[[PrismAudioManager defaultManager] tapStreamFromItem:[self currentItem]];
}
}
%end
%hook MusicAVPlayer
%new -(void)didChangeSpectrumData:(NSNotification*)notification{
dispatch_async(dispatch_get_main_queue(), ^
{
MusicAVPlayer * mp = NULL;
if( [[[UIDevice currentDevice] systemVersion] isEqualToString:@"8.4"])
{
MusicApplicationDelegate * del = (MusicApplicationDelegate*)[[UIApplication sharedApplication] delegate];
MusicRemoteController * rc = [del remoteController];
mp = [rc player];
}
else
{
MARemoteController * rc = [[UIApplication sharedApplication] remoteController];
mp = [rc player];
}
appState = [[UIApplication sharedApplication] applicationState];
if([mp rate]==0)
{
if([BeatVisualizerView sharedInstance].alpha==(transparency/100.0))
{
[UIView animateWithDuration:.4
animations:^{
[BeatVisualizerView sharedInstance].alpha = 0;
}];
}
//If player is paused and audio was going through springboard, stop from sb get audio
if(appState == 0)
return;
}
if([BeatVisualizerView sharedInstance].alpha==0)
{
[UIView animateWithDuration:.4
animations:^{
[BeatVisualizerView sharedInstance].alpha = (transparency/100.0);
}];
}
/*if(pastStatus != [mp rate])
{
if([mp rate] ==0 )
{
NSLog(@"Pause");
[[BeatVisualizerView sharedInstance] pause];
}
else
{
NSLog(@"Play");
[[BeatVisualizerView sharedInstance] play];
}
}
if([mp rate] == 0)
pastStatus = 0;
else
pastStatus = 1;*/
NSArray * spectrumData = [notification.userInfo objectForKey:@"spectrumData"];
avgVol = [[notification.userInfo objectForKey:@"avgVol"] floatValue];
NSInteger length = [[notification.userInfo objectForKey:@"spectrumDataLength"] intValue];
playerVolume = [[[mp avPlayer] _player] volume];
//state = 0 -> inside the app
//state = 1 -> on the springboard
//state = 2 -> on the lockscreen
if(appState == 0)
{
if(shouldUpdatePrismDefaults)
{
[[BeatVisualizerView sharedInstance] setBeatPrimaryColor:beatPrimaryColor];
[[BeatVisualizerView sharedInstance] setBeatSecondaryColor:beatSecondaryColor];
[[BeatVisualizerView sharedInstance] setSpectrumPrimaryColor:spectrumPrimaryColor];
[[BeatVisualizerView sharedInstance] setAlpha:(transparency/100.0)];
[[BeatVisualizerView sharedInstance] setBarHeight:(barHeight/100.0)];
[[BeatVisualizerView sharedInstance] setOverlayAlbumArt:overlayAlbumArt];
[[BeatVisualizerView sharedInstance] setOverlayColor:overlayColor];
[[BeatVisualizerView sharedInstance] setColorStyle:colorStyle];
[[BeatVisualizerView sharedInstance] setSpectrumStyle:spectrumStyle];
[[BeatVisualizerView sharedInstance] setNumBars:spectrumBarCount];
shouldUpdatePrismDefaults = NO;
}
[[BeatVisualizerView sharedInstance] updateWithLevel:avgVol withData:spectrumData withLength:length withVol:playerVolume withType:theme];
}
else if (appState == 2)
{
if(!messagingCenter)
{
messagingCenter = [CPDistributedMessagingCenter centerNamed:@"com.joshdoctors.prism"];
rocketbootstrap_distributedmessagingcenter_apply(messagingCenter);
}
[messagingCenter sendMessageName:@"getAudioData" userInfo:[NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithFloat:avgVol], @"level",
[NSNumber numberWithFloat:playerVolume], @"playerVolume",
spectrumData, @"fftData",[NSNumber numberWithInt:length], @"outDataLength", nil]];
}
});
}
%end
%hook SBMediaController
-(void)setNowPlayingInfo:(id)info{
%orig;
if(!tweakEnabled)
{
NSLog(@"[Prism]Tweak is not enabled, will not attempt to generate colors.");
return;
}
NSString * ident = [[[%c(SBMediaController) sharedInstance] nowPlayingApplication] bundleIdentifier];
if(ident)
{
if(![ident isEqualToString:@"com.apple.Music"])
{
NSLog(@"[Prism]Audio is not from the music app, quitting attempt to generate colors.");
return;
}
}
/*[[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:@"GeneratedPrismColors" object:nil userInfo:@{
@"volume" : @"Hi"
}]];*/
MRMediaRemoteGetNowPlayingInfo(dispatch_get_main_queue(), ^(CFDictionaryRef result)
{
//NSLog(@"[Prism]Getting info dictionary");
NSDictionary * dict = (__bridge NSDictionary*)result;
if(!dict)
{
//NSLog(@"[Prism]Dictionary is nil");
return;
}
NSString * trackTitle = [dict objectForKey:(__bridge NSString*)kMRMediaRemoteNowPlayingInfoTitle];
if(!trackTitle)
{
//NSLog(@"[Prism]TrackTitle is nil");
return;
}
if(!cachedTitle)
{
//NSLog(@"[Prism]Cached is nil");
cachedTitle = [[NSString alloc] init];
}
else
{
if([trackTitle isEqualToString:cachedTitle])
{
//NSLog(@"[Prism]The track colors have already been generated");
return;
}
}
UIImage * image = [UIImage imageWithData:[dict objectForKey:(__bridge NSData*)kMRMediaRemoteNowPlayingInfoArtworkData]];
if(!image)
{
//NSLog(@"[Prism]Image is nil");
return;
}
LEColorPicker * colorPicker = [[LEColorPicker alloc] init];
LEColorScheme *colorScheme = [colorPicker colorSchemeFromImage:image];
//NSLog(@"[Prism] Valid Image %@ and scheme: %@", image, colorScheme);
int numComponents = CGColorGetNumberOfComponents([[colorScheme backgroundColor] CGColor]);
if(numComponents==4)
{
const CGFloat * components = CGColorGetComponents([[colorScheme backgroundColor] CGColor]);
prismFlowPrimary = [UIColor colorWithRed:components[0] green:components[1] blue:components[2] alpha:components[3]];
}
numComponents = CGColorGetNumberOfComponents([[colorScheme primaryTextColor] CGColor]);
if(numComponents==4)
{
const CGFloat * components = CGColorGetComponents([[colorScheme primaryTextColor] CGColor]);
prismFlowSecondary = [UIColor colorWithRed:components[0] green:components[1] blue:components[2] alpha:components[3]];
}
if(!prismFlowPrimary || !prismFlowSecondary)
{
//NSLog(@"[Prism]One of the colors is nil");
return;
}
[[BeatVisualizerView sharedInstance] setPrismFlowPrimary:prismFlowPrimary];
[[BeatVisualizerView sharedInstance] setPrismFlowSecondary:prismFlowSecondary];
NSInteger ranRed = arc4random()%255;
NSInteger ranGreen = arc4random()%255;
NSInteger ranBlue = arc4random()%255;
UIColor * randColor = [UIColor colorWithRed:ranRed/255.0f green:ranGreen/255.0f blue:ranBlue/255.0f alpha:1.0f];
[[BeatVisualizerView sharedInstance] setRandomColorPrimary:randColor];
ranRed = arc4random()%255;
ranGreen = arc4random()%255;
ranBlue = arc4random()%255;
UIColor * randColor2 = [UIColor colorWithRed:ranRed/255.0f green:ranGreen/255.0f blue:ranBlue/255.0f alpha:1.0f];
[[BeatVisualizerView sharedInstance] setRandomColorSecondary:randColor2];
//NSLog(@"[Prism]Colors have been generated, reassigning the cache and releasing.");
if(cachedTitle)cachedTitle = nil;
cachedTitle = [NSString stringWithFormat:@"%@", trackTitle];
//NSLog(@"[Prism]cachedTitle is %@ and trackTitle is %@ (should be same)", cachedTitle, trackTitle);
colorScheme = nil;
colorPicker = nil;
});
}
%end
//MusicNowPlayingViewController < 8.4 has artworknotification, didUpdateArtworkImage, etc
%hook MusicNowPlayingViewController
-(void)_updateTitles {
%orig;
//NSLog(@"_updateTitles");
//this utility will only be used for iOS devices on 8.4+
if(![[[UIDevice currentDevice] systemVersion] isEqualToString:@"8.4"] || !tweakEnabled)
return;
UIView *view = [self view];
if(!view)
return;
//NSLog(@"[Prism]Attemtpting to add gesture recognizer from MNPVC.");
BOOL added = NO;
for (UIGestureRecognizer * recognizer in view.gestureRecognizers) {
if([recognizer isKindOfClass:[%c(UILongPressGestureRecognizer) class]])
added = YES;
}
if(!added)
{
UIGestureRecognizer * longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
[view addGestureRecognizer:longPress];
}
}
%new -(void)longPress:(UILongPressGestureRecognizer*)gesture {
if(gesture.state == UIGestureRecognizerStateEnded)
{
[[BeatVisualizerView sharedInstance] toggleVisibility];
}
}
%end
%hook MPUSlantedTextPlaceholderArtworkView
- (void)layoutSubviews
{
%orig;
if(useDefaultLS || !tweakEnabled)
return;
UIView * superview = [self superview];
if(!superview)
return;
UIViewController * vc = MSHookIvar<UIViewController*>(superview,"_viewDelegate");
if(vc && [vc isKindOfClass:[%c(MusicNowPlayingViewController) class]])
{
useDefaultLS = YES;
BOOL added = NO;
for (UIGestureRecognizer * recognizer in self.gestureRecognizers) {
if([recognizer isKindOfClass:[%c(UILongPressGestureRecognizer) class]])
added = YES;
}
if(!added)
{
UIGestureRecognizer * longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
[self addGestureRecognizer:longPress];
}
for (UIView * view in self.subviews) {
if([view isKindOfClass:[%c(BeatVisualizerView) class]])
{
useDefaultLS = NO;
return;
}
}
[[BeatVisualizerView sharedInstance] setFrame:self.bounds];
NSLog(@"[Prism]Added visualizer to the Music App.: %@", self);
[self addSubview:[BeatVisualizerView sharedInstance]];
useDefaultLS = NO;
}
}
/*-(void)setPlaceholderTitle:(NSString*)title{
%orig;
if(!tweakEnabled)
{
//NSLog(@"[Prism]Tweak was not enabled, not adding visualizer or gesture recognizers to < 8.4.");
return;
}
UIView * superview = [self superview];
if(!superview)
return;
UIViewController * vc = MSHookIvar<UIViewController*>(superview,"_viewDelegate");
if(vc && [vc isKindOfClass:[%c(MusicNowPlayingViewController) class]])
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[self generatePrismColors];
});
for (UIView * view in self.subviews) {
if([view isKindOfClass:[%c(BeatVisualizerView) class]])
return;
}
[[BeatVisualizerView sharedInstance] setFrame:self.bounds];
NSLog(@"[Prism]Added visualizer to the Music App.: %@", self);
[self addSubview:[BeatVisualizerView sharedInstance]];
BOOL added = NO;
for (UIGestureRecognizer * recognizer in self.gestureRecognizers) {
if([recognizer isKindOfClass:[%c(UILongPressGestureRecognizer) class]])
added = YES;
}
if(!added)
{
UIGestureRecognizer * longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
[self addGestureRecognizer:longPress];
}
}
}*/
%new -(void)longPress:(UILongPressGestureRecognizer*)gesture {
if(gesture.state == UIGestureRecognizerStateEnded)
{
[[BeatVisualizerView sharedInstance] toggleVisibility];
}
}
-(void)_setTouchHighlighted:(BOOL)b animated:(BOOL)b2 {
if(!tweakEnabled)
%orig;
else
%orig(NO, NO);
}
%new - (void)generatePrismColors {
MRMediaRemoteGetNowPlayingInfo(dispatch_get_main_queue(), ^(CFDictionaryRef result)
{
//NSLog(@"[Prism]Getting info dictionary");
NSDictionary * dict = (__bridge NSDictionary*)result;
if(!dict)
{
//NSLog(@"[Prism]Dictionary is nil");
return;
}
NSString * trackTitle = [dict objectForKey:(__bridge NSString*)kMRMediaRemoteNowPlayingInfoTitle];
if(!trackTitle)
{
//NSLog(@"[Prism]TrackTitle is nil");
return;
}
if(!cachedTitle)
{
//NSLog(@"[Prism]Cached is nil");
cachedTitle = [[NSString alloc] init];
}
else
{
if([trackTitle isEqualToString:cachedTitle])
{
//NSLog(@"[Prism]The track colors have already been generated");
return;
}
}
UIImage * image = [UIImage imageWithData:[dict objectForKey:(__bridge NSData*)kMRMediaRemoteNowPlayingInfoArtworkData]];
if(!image)
{
//NSLog(@"[Prism]Image is nil");
return;
}
LEColorPicker * colorPicker = [[LEColorPicker alloc] init];
LEColorScheme *colorScheme = [colorPicker colorSchemeFromImage:image];
//NSLog(@"[Prism] Valid Image %@ and scheme: %@", image, colorScheme);
int numComponents = CGColorGetNumberOfComponents([[colorScheme backgroundColor] CGColor]);
if(numComponents==4)
{
const CGFloat * components = CGColorGetComponents([[colorScheme backgroundColor] CGColor]);
prismFlowPrimary = [UIColor colorWithRed:components[0] green:components[1] blue:components[2] alpha:components[3]];
}
numComponents = CGColorGetNumberOfComponents([[colorScheme primaryTextColor] CGColor]);
if(numComponents==4)
{
const CGFloat * components = CGColorGetComponents([[colorScheme primaryTextColor] CGColor]);
prismFlowSecondary = [UIColor colorWithRed:components[0] green:components[1] blue:components[2] alpha:components[3]];
}
if(!prismFlowPrimary || !prismFlowSecondary)
{
//NSLog(@"[Prism]One of the colors is nil");
return;
}
[[BeatVisualizerView sharedInstance] setPrismFlowPrimary:prismFlowPrimary];
[[BeatVisualizerView sharedInstance] setPrismFlowSecondary:prismFlowSecondary];
NSInteger ranRed = arc4random()%255;
NSInteger ranGreen = arc4random()%255;
NSInteger ranBlue = arc4random()%255;
UIColor * randColor = [UIColor colorWithRed:ranRed/255.0f green:ranGreen/255.0f blue:ranBlue/255.0f alpha:1.0f];
[[BeatVisualizerView sharedInstance] setRandomColorPrimary:randColor];
ranRed = arc4random()%255;
ranGreen = arc4random()%255;
ranBlue = arc4random()%255;
UIColor * randColor2 = [UIColor colorWithRed:ranRed/255.0f green:ranGreen/255.0f blue:ranBlue/255.0f alpha:1.0f];
[[BeatVisualizerView sharedInstance] setRandomColorSecondary:randColor2];
/*if(!messagingCenter)
{
messagingCenter = [CPDistributedMessagingCenter centerNamed:@"com.joshdoctors.prism"];
rocketbootstrap_distributedmessagingcenter_apply(messagingCenter);
}
[messagingCenter sendMessageName:@"getColorData" userInfo:[NSDictionary dictionaryWithObjectsAndKeys:
prismFlowPrimary, @"prismFlowPrimary",
prismFlowSecondary, @"prismFlowSecondary",
randColor, @"randomColorPrimary",randColor2, @"randomColorSecondary", nil]];*/
//NSLog(@"[Prism]Colors have been generated, reassigning the cache and releasing.");
if(cachedTitle)cachedTitle = nil;
cachedTitle = [NSString stringWithFormat:@"%@", trackTitle];
//NSLog(@"[Prism]cachedTitle is %@ and trackTitle is %@ (should be same)", cachedTitle, trackTitle);
colorScheme = nil;
colorPicker = nil;
});
}
%end
%hook MusicNowPlayingItemViewController
-(id)artworkImage {
id orig = %orig;
//NSLog(@"[Prism]Attempting to generate prism colors from the artwork image, %@", orig);
if(orig && tweakEnabled)
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[self generatePrismColors:orig];
});
}
return orig;
}
%new - (void)generatePrismColors:(UIImage*)image {
LEColorPicker * colorPicker = [[LEColorPicker alloc] init];
LEColorScheme *colorScheme = [colorPicker colorSchemeFromImage:image];
//NSLog(@"[Prism] Valid Image %@ and scheme: %@", image, colorScheme);
int numComponents = CGColorGetNumberOfComponents([[colorScheme backgroundColor] CGColor]);
if(numComponents==4)
{
const CGFloat * components = CGColorGetComponents([[colorScheme backgroundColor] CGColor]);
prismFlowPrimary = [UIColor colorWithRed:components[0] green:components[1] blue:components[2] alpha:components[3]];
}
numComponents = CGColorGetNumberOfComponents([[colorScheme primaryTextColor] CGColor]);
if(numComponents==4)
{
const CGFloat * components = CGColorGetComponents([[colorScheme primaryTextColor] CGColor]);
prismFlowSecondary = [UIColor colorWithRed:components[0] green:components[1] blue:components[2] alpha:components[3]];
}
if(!prismFlowPrimary || !prismFlowSecondary)
{
//NSLog(@"[Prism]One of the colors is nil");
return;
}
[[BeatVisualizerView sharedInstance] setPrismFlowPrimary:prismFlowPrimary];
[[BeatVisualizerView sharedInstance] setPrismFlowSecondary:prismFlowSecondary];
NSInteger ranRed = arc4random()%255;
NSInteger ranGreen = arc4random()%255;
NSInteger ranBlue = arc4random()%255;
UIColor * randColor = [UIColor colorWithRed:ranRed/255.0f green:ranGreen/255.0f blue:ranBlue/255.0f alpha:1.0f];
[[BeatVisualizerView sharedInstance] setRandomColorPrimary:randColor];
ranRed = arc4random()%255;
ranGreen = arc4random()%255;
ranBlue = arc4random()%255;
UIColor * randColor2 = [UIColor colorWithRed:ranRed/255.0f green:ranGreen/255.0f blue:ranBlue/255.0f alpha:1.0f];
[[BeatVisualizerView sharedInstance] setRandomColorSecondary:randColor2];
/*if(!messagingCenter)
{
messagingCenter = [CPDistributedMessagingCenter centerNamed:@"com.joshdoctors.prism"];
rocketbootstrap_distributedmessagingcenter_apply(messagingCenter);
}
[messagingCenter sendMessageName:@"getColorData" userInfo:[NSDictionary dictionaryWithObjectsAndKeys:
prismFlowPrimary, @"prismFlowPrimary",
prismFlowSecondary, @"prismFlowSecondary",
randColor, @"randomColorPrimary",randColor2, @"randomColorSecondary", nil]];*/
//NSLog(@"[Prism]Colors have been generated.");
colorScheme = nil;
colorPicker = nil;
}
%end
%hook MusicArtworkView
- (void)layoutSubviews
{
//NSLog(@"[Prism]layoutSubviews");
%orig;
if(!tweakEnabled)
{
//NSLog(@"[Prism]Tweak was not enabled, not adding visualizer from layoutSubviews.");
return;
}
//NSLog(@"Tweak is enabled");
UIView * superview = [self superview];
if(!superview)
{
//NSLog(@"[Prism]Superview(layoutSubviews) was null.");
return;
}
//NSLog(@"superview: %@", superview);
UIViewController * vc = MSHookIvar<UIViewController*>(superview,"_viewDelegate");
if(vc && [vc isKindOfClass:[%c(MusicNowPlayingItemViewController) class]])
{
[[BeatVisualizerView sharedInstance] removeFromSuperview];
[[BeatVisualizerView sharedInstance] setFrame:self.bounds];
NSLog(@"[Prism]Added visualizer to the Music App, %@", self);
[self addSubview:[BeatVisualizerView sharedInstance]];
}
}
%new - (void)toggleVisualizerVisibility:(UITapGestureRecognizer*)sender {
[[BeatVisualizerView sharedInstance] toggleVisibility];
}
-(void)_setTouchHighlighted:(BOOL)b animated:(BOOL)b2 {
if(!tweakEnabled)
%orig;
else
%orig(NO, NO);
}
%end
%hook _NowPlayingArtView
- (id)initWithFrame:(CGRect)frame
{
//NSLog(@"[Prism]LockScreen initWithFrame");
id orig = %orig;
if(!tweakEnabled)
return orig;
NSString * ident = [[[%c(SBMediaController) sharedInstance] nowPlayingApplication] bundleIdentifier];
if(ident)
{
if(![ident isEqualToString:@"com.apple.Music"])
{
NSLog(@"[Prism]Tweak is enabled but audio is not coming from the stock Music application.");
return orig;
}
}
if(!messagingCenter)
{
messagingCenter = [CPDistributedMessagingCenter centerNamed:@"com.joshdoctors.prism"];
}
if(![messagingCenter doesServerExist])
{
rocketbootstrap_distributedmessagingcenter_apply(messagingCenter);
[messagingCenter runServerOnCurrentThread];
[messagingCenter registerForMessageName:@"getAudioData" target:self selector:@selector(getAudioData:withUserInfo:)];
//[messagingCenter registerForMessageName:@"getColorData" target:self selector:@selector(getColorData:withUserInfo:)];
}
BOOL added = NO;
for (UIGestureRecognizer * recognizer in self.gestureRecognizers) {
if([recognizer isKindOfClass:[%c(UILongPressGestureRecognizer) class]])
added = YES;
}
if(!added)
{
UIGestureRecognizer * longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
[self addGestureRecognizer:longPress];
}
return orig;
}
%new -(void)longPress:(UILongPressGestureRecognizer*)gesture {
if(gesture.state == UIGestureRecognizerStateEnded)
{
[[BeatVisualizerView sharedInstance] toggleVisibility];
}
}
/*%new - (void)getColorData:(NSString*)name withUserInfo:(NSDictionary*)dict
{
NSLog(@"GetColorData");
[[BeatVisualizerView sharedInstance] setPrismFlowPrimary:dict[@"prismFlowPrimary"]];
[[BeatVisualizerView sharedInstance] setPrismFlowSecondary:dict[@"prismFlowSecondary"]];
[[BeatVisualizerView sharedInstance] setRandomColorPrimary:dict[@"randomColorPrimary"]];
[[BeatVisualizerView sharedInstance] setRandomColorSecondary:dict[@"randomColorSecondary"]];
}*/
%new - (void)getAudioData:(NSString*)name withUserInfo:(NSDictionary*)dict
{
avgVol = [dict[@"level"] floatValue];
playerVolume = [dict[@"playerVolume"] floatValue];
outDataLength = [dict[@"outDataLength"] intValue];
if(dict[@"fftData"] && [dict[@"fftData"] isKindOfClass:[NSMutableArray class]])
fftData = dict[@"fftData"];
/*if(pastStatus != [[%c(SBMediaController) sharedInstance] isPaused])
{
if([[%c(SBMediaController) sharedInstance] isPaused])
[[BeatVisualizerView sharedInstance] pause];
else
[[BeatVisualizerView sharedInstance] play];
}
pastStatus = [[%c(SBMediaController) sharedInstance] isPaused];*/
if([[%c(SBMediaController) sharedInstance] isPaused])
{
if([BeatVisualizerView sharedInstance].alpha==(transparency/100.0))
{
[UIView animateWithDuration:.4
animations:^{
[BeatVisualizerView sharedInstance].alpha = 0;
}];
}
return;
}
if([BeatVisualizerView sharedInstance].alpha==0)
{
[UIView animateWithDuration:.4
animations:^{
[BeatVisualizerView sharedInstance].alpha = (transparency/100.0);
}];
}
if(shouldUpdatePrismDefaults)
{
[[BeatVisualizerView sharedInstance] setBeatPrimaryColor:beatPrimaryColor];
[[BeatVisualizerView sharedInstance] setBeatSecondaryColor:beatSecondaryColor];
[[BeatVisualizerView sharedInstance] setSpectrumPrimaryColor:spectrumPrimaryColor];
[[BeatVisualizerView sharedInstance] setAlpha:(transparency/100.0)];
[[BeatVisualizerView sharedInstance] setBarHeight:(barHeight/100.0)];
[[BeatVisualizerView sharedInstance] setOverlayAlbumArt:overlayAlbumArt];
[[BeatVisualizerView sharedInstance] setOverlayColor:overlayColor];
[[BeatVisualizerView sharedInstance] setColorStyle:colorStyle];
[[BeatVisualizerView sharedInstance] setSpectrumStyle:spectrumStyle];
[[BeatVisualizerView sharedInstance] setNumBars:spectrumBarCount];
shouldUpdatePrismDefaults = NO;
}
[[BeatVisualizerView sharedInstance] updateWithLevel:avgVol withData:fftData withLength:outDataLength withVol:playerVolume withType:theme];
}
- (void)layoutSubviews
{
%orig;
if(useDefaultLS || !tweakEnabled)
return;
NSString * ident = [[[%c(SBMediaController) sharedInstance] nowPlayingApplication] bundleIdentifier];
if(ident)
{
if(![ident isEqualToString:@"com.apple.Music"])
return;
}
useDefaultLS = YES;
[[BeatVisualizerView sharedInstance] removeFromSuperview];
[[BeatVisualizerView sharedInstance] setFrame:[self artworkView].frame];
NSLog(@"[Prism]Added visualizer to the LockScreen, %@", self);
[self addSubview:[BeatVisualizerView sharedInstance]];
useDefaultLS = NO;
}
%end
%end
static void loadPrefs()
{
NSDictionary *settings = [NSDictionary dictionaryWithContentsOfFile:prefPath];
NSLog(@"[Prism]Settings: %@", settings);
if([settings objectForKey:@"enableTweak"]) {
tweakEnabled = [settings[@"enableTweak"] boolValue];
} else {
tweakEnabled = NO;
}
if (tweakEnabled) {
NSLog(@"[Prism]is enabled");
} else {
NSLog(@"[Prism]is NOT enabled");
}
if([settings objectForKey:@"colorStyle"]) {
colorStyle = [settings[@"colorStyle"] intValue];
} else {
colorStyle = 0;
}
if([settings objectForKey:@"overlayAlbumArt"]) {
overlayAlbumArt = [settings[@"overlayAlbumArt"] boolValue];
} else {
overlayAlbumArt = NO;
}
if([settings objectForKey:@"theme"]) {
theme = [settings[@"theme"] floatValue];
} else {
theme = 0.0;
}
if([settings objectForKey:@"overlayColor"]) {
overlayColor = parseColorFromPreferences(settings[@"overlayColor"]);
} else {
overlayColor = kDefaultMusicColor;
}
if([settings objectForKey:@"beatPrimaryColor"]) {
beatPrimaryColor = parseColorFromPreferences(settings[@"beatPrimaryColor"]);
} else {
beatPrimaryColor = kDefaultCyanColor;
}
if([settings objectForKey:@"beatSecondaryColor"]) {
beatSecondaryColor = parseColorFromPreferences(settings[@"beatSecondaryColor"]);
} else {
beatSecondaryColor = kDefaultMagenta;
}
if([settings objectForKey:@"spectrumPrimaryColor"]) {
spectrumPrimaryColor = parseColorFromPreferences(settings[@"spectrumPrimaryColor"]);
} else {
spectrumPrimaryColor = kDefaultCyanColor;
}
if([settings objectForKey:@"spectrumStyle"]) {
spectrumStyle = [settings[@"spectrumStyle"] intValue];
} else {
spectrumStyle = 0;
}
if([settings objectForKey:@"spectrumBarCount"]) {
NSString * temp = settings[@"spectrumBarCount"];
spectrumBarCount = isNumeric(temp) ? [temp intValue] : 30;
spectrumBarCount = spectrumBarCount > 0 ? spectrumBarCount : 30;
} else {
spectrumBarCount = 30;
}
if([settings objectForKey:@"transparency"]) {
transparency = [settings[@"transparency"] floatValue];
} else {
transparency = 100;
}
if([settings objectForKey:@"barHeight"]) {
barHeight = [settings[@"barHeight"] floatValue];
} else {
barHeight = 100;
}
shouldUpdatePrismDefaults = YES;
}
%ctor
{
NSLog(@"[Prism]Loading Prism");
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(),
NULL,
(CFNotificationCallback)loadPrefs,
CFSTR("com.joshdoctors.prism/settingschanged"),
NULL,
CFNotificationSuspensionBehaviorCoalesce);
loadPrefs();
dlopen("/System/Library/SpringBoardPlugins/NowPlayingArtLockScreen.lockbundle/NowPlayingArtLockScreen", 2);
%init(NowPlayingArtView);