diff --git a/Makefile b/Makefile index 8e23227..3538a62 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ export ARCHS = armv7 arm64 arm64e export SYSROOT = $(THEOS)/sdks/iPhoneOS14.5.sdk INSTALL_TARGET_PROCESSES = SpringBoard -SUBPROJECTS += Tweak +SUBPROJECTS += Tweak Preferences include $(THEOS)/makefiles/common.mk include $(THEOS_MAKE_PATH)/aggregate.mk diff --git a/Preferences/Makefile b/Preferences/Makefile new file mode 100644 index 0000000..38379e9 --- /dev/null +++ b/Preferences/Makefile @@ -0,0 +1,13 @@ +TARGET := iphone:clang:latest:7.0 + +include $(THEOS)/makefiles/common.mk + +BUNDLE_NAME = iOSDiscordPresencePreferences + +iOSDiscordPresencePreferences_FILES = iOSDiscordPresenceRootListController.m +iOSDiscordPresencePreferences_FRAMEWORKS = UIKit +iOSDiscordPresencePreferences_PRIVATE_FRAMEWORKS = Preferences +iOSDiscordPresencePreferences_INSTALL_PATH = /Library/PreferenceBundles +iOSDiscordPresencePreferences_CFLAGS = -fobjc-arc + +include $(THEOS_MAKE_PATH)/bundle.mk diff --git a/Preferences/Resources/Info.plist b/Preferences/Resources/Info.plist new file mode 100644 index 0000000..5903c08 --- /dev/null +++ b/Preferences/Resources/Info.plist @@ -0,0 +1,24 @@ + + + + + CFBundleDevelopmentRegion + English + CFBundleExecutable + iOSDiscordPresencePreferences + CFBundleIdentifier + pink.kirameki.yuzu.iosdiscordpresencepreferences + CFBundleInfoDictionaryVersion + 6.0 + CFBundlePackageType + BNDL + CFBundleShortVersionString + 1.0.0 + CFBundleSignature + ???? + CFBundleVersion + 1.0 + NSPrincipalClass + iOSDiscordPresenceRootListController + + diff --git a/Preferences/Resources/Root.plist b/Preferences/Resources/Root.plist new file mode 100644 index 0000000..8a5c429 --- /dev/null +++ b/Preferences/Resources/Root.plist @@ -0,0 +1,69 @@ + + + + + items + + + cell + PSStaticTextCell + height + 150 + label + + icon + banner.png + + + cell + PSGroupCell + label + General + + + cell + PSSwitchCell + default + + defaults + pink.kirameki.yuzu.iosdiscordpresencepreferences + key + isEnabled + label + Enabled + PostNotification + pink.kirameki.yuzu.iosdiscordpresencepreferences/PreferencesChanged + + + cell + PSSecureEditTextCell + defaults + pink.kirameki.yuzu.iosdiscordpresencepreferences + key + discordToken + label + Discord Token + placeholder + Put your discord token here + PostNotification + pink.kirameki.yuzu.iosdiscordpresencepreferences/PreferencesChanged + + + cell + PSGroupCell + label + Information + + + cell + PSButtonCell + label + GitHub + action + openGitHubLink + + + title + iOS-DiscordPresence + + diff --git a/Preferences/Resources/banner.png b/Preferences/Resources/banner.png new file mode 100755 index 0000000..37c512a Binary files /dev/null and b/Preferences/Resources/banner.png differ diff --git a/Preferences/Resources/icon.png b/Preferences/Resources/icon.png new file mode 100755 index 0000000..ec2fbc5 Binary files /dev/null and b/Preferences/Resources/icon.png differ diff --git a/Preferences/Resources/icon@2x.png b/Preferences/Resources/icon@2x.png new file mode 100755 index 0000000..33d9927 Binary files /dev/null and b/Preferences/Resources/icon@2x.png differ diff --git a/Preferences/iOSDiscordPresenceRootListController.h b/Preferences/iOSDiscordPresenceRootListController.h new file mode 100644 index 0000000..143160c --- /dev/null +++ b/Preferences/iOSDiscordPresenceRootListController.h @@ -0,0 +1,6 @@ +#import +#import + +@interface iOSDiscordPresenceRootListController : PSListController + +@end diff --git a/Preferences/iOSDiscordPresenceRootListController.m b/Preferences/iOSDiscordPresenceRootListController.m new file mode 100644 index 0000000..4ede6d3 --- /dev/null +++ b/Preferences/iOSDiscordPresenceRootListController.m @@ -0,0 +1,36 @@ +#import +#import "iOSDiscordPresenceRootListController.h" + +@implementation iOSDiscordPresenceRootListController + +- (NSArray *)specifiers { + if (!_specifiers) { + _specifiers = [self loadSpecifiersFromPlistName:@"Root" target:self]; + } + + return _specifiers; +} + +-(id)readPreferenceValue:(PSSpecifier*)specifier { + NSDictionary * prefs = [NSDictionary dictionaryWithContentsOfFile:[NSString stringWithFormat:@"/var/mobile/Library/Preferences/%@.plist", [specifier.properties objectForKey:@"defaults"]]]; + if (![prefs objectForKey:[specifier.properties objectForKey:@"key"]]) { + return [specifier.properties objectForKey:@"default"]; + } + return [prefs objectForKey:[specifier.properties objectForKey:@"key"]]; + } + +-(void)setPreferenceValue:(id)value specifier:(PSSpecifier*)specifier { + NSMutableDictionary * prefs = [[NSMutableDictionary alloc] initWithContentsOfFile:[NSString stringWithFormat:@"/var/mobile/Library/Preferences/%@.plist", [specifier.properties objectForKey:@"defaults"]]]; + [prefs setObject:value forKey:[specifier.properties objectForKey:@"key"]]; + [prefs writeToFile:[NSString stringWithFormat:@"/var/mobile/Library/Preferences/%@.plist", [specifier.properties objectForKey:@"defaults"]] atomically:YES]; + if([specifier.properties objectForKey:@"PostNotification"]) { + CFNotificationCenterPostNotification(CFNotificationCenterGetDarwinNotifyCenter(), (CFStringRef)[specifier.properties objectForKey:@"PostNotification"], NULL, NULL, YES); + } + [super setPreferenceValue:value specifier:specifier]; +} + +- (void)openGitHubLink { + [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://github.com/YuzuZensai/iOS-DiscordPresence"]]; +} + +@end diff --git a/Preferences/layout/Library/PreferenceLoader/Preferences/iOSDiscordPresencePreferences.plist b/Preferences/layout/Library/PreferenceLoader/Preferences/iOSDiscordPresencePreferences.plist new file mode 100644 index 0000000..0207ab5 --- /dev/null +++ b/Preferences/layout/Library/PreferenceLoader/Preferences/iOSDiscordPresencePreferences.plist @@ -0,0 +1,21 @@ + + + + + entry + + bundle + iOSDiscordPresencePreferences + cell + PSLinkCell + detail + iOSDiscordPresenceRootListController + icon + icon.png + isController + + label + iOS-DiscordPresence + + + diff --git a/Tweak/Tweak.x b/Tweak/Tweak.x index d993ac8..1945ddf 100644 --- a/Tweak/Tweak.x +++ b/Tweak/Tweak.x @@ -1,5 +1,9 @@ #import +// Variable to store value from preferences +Boolean isEnabled = false; +NSString* discordToken = nil; + SBApplication* focusedApplication = nil; NSString* lastKnownBundleIdentifier = nil; Boolean isDeviceLocked = true; @@ -9,6 +13,7 @@ Boolean isDeviceLocked = true; -(void)frontDisplayDidChange:(id)arg1 { %orig; + // Switched to SpringBoard if(arg1 == nil) { // If switched from SpringBoard to SpringBoard, ignore @@ -72,18 +77,13 @@ Boolean isDeviceLocked = true; -(void)updateDiscordPresence:(id)arg1 withState:(NSString *)state { if(![arg1 isKindOfClass:[%c(SBApplication) class]] && arg1 != nil) return; + + if(!isEnabled) return; + NSString *accessToken = @""; - // TODO: Make settings preferences or somehow fetch Discord token from the app? - // Load discord token from text file at /var/mobile/Documents/DiscordToken.txt - NSString* content = [NSString - stringWithContentsOfFile: @"/var/mobile/Documents/DiscordToken.txt" - encoding:NSUTF8StringEncoding - error:NULL - ]; - - if(content != nil) - accessToken = content; + if(discordToken != nil) + accessToken = discordToken; // If SBApplication is passed if(arg1 != nil) { @@ -159,6 +159,7 @@ Boolean isDeviceLocked = true; -(void)_authenticationStateChanged:(id)arg1 { %orig; + // Get the state changed notification, and find the SBFUserAuthenticationStateWasAuthenticatedKey value NSConcreteNotification *notification = arg1; int state = [[[notification userInfo] objectForKey: @"SBFUserAuthenticationStateWasAuthenticatedKey"] integerValue]; @@ -170,6 +171,7 @@ Boolean isDeviceLocked = true; -(void)_sendUILockStateChangedNotification { %orig; + // The phone is locked or focused application is nil, ignore if(isDeviceLocked || focusedApplication == nil) return; // If the phone is unlocked, then the user is peeking at the notification or didn't swipe up to unlock yet. @@ -184,3 +186,22 @@ Boolean isDeviceLocked = true; } } %end + +static void loadPreferences() +{ + NSString *preferencesPath = @"/var/mobile/Library/Preferences/pink.kirameki.yuzu.iosdiscordpresencepreferences.plist"; + NSMutableDictionary *preferences = [[NSMutableDictionary alloc] initWithContentsOfFile: preferencesPath]; + if(preferences) + { + NSLog(@"Updating preferences"); + + isEnabled = ( [preferences objectForKey:@"isEnabled"] ? [[preferences objectForKey:@"isEnabled"] boolValue] : isEnabled ); + discordToken = ( [preferences objectForKey:@"discordToken"] ? [preferences objectForKey:@"discordToken"] : discordToken ); + } +} + +%ctor +{ + CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), NULL, (CFNotificationCallback)loadPreferences, CFSTR("pink.kirameki.yuzu.iosdiscordpresencepreferences/PreferencesChanged"), NULL, CFNotificationSuspensionBehaviorCoalesce); + loadPreferences(); +} \ No newline at end of file diff --git a/control b/control index aae800a..8f16267 100644 --- a/control +++ b/control @@ -6,4 +6,4 @@ Description: Jailbreak tweak that implements Discord Playing Presence like Samsu Maintainer: YuzuZensai Author: YuzuZensai Section: Tweaks -Depends: mobilesubstrate (>= 0.9.5000) +Depends: mobilesubstrate, preferenceloader