-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9744 from hansemannn/TIMOB-25048
[TIMOB-25048] iOS: Move ApplicationShortcuts to Ti.UI namespace (parity)
- Loading branch information
Showing
8 changed files
with
285 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/** | ||
* Appcelerator Titanium Mobile | ||
* Copyright (c) 2018 by Appcelerator, Inc. All Rights Reserved. | ||
* Licensed under the terms of the Apache Public License | ||
* Please see the LICENSE included with this distribution for details. | ||
*/ | ||
#import "TiProxy.h" | ||
|
||
#if defined(USE_TI_UIIOSAPPLICATIONSHORTCUTS) || defined(USE_TI_UIAPPLICATIONSHORTCUTS) | ||
@interface TiUIApplicationShortcutsProxy : TiProxy { | ||
} | ||
|
||
@end | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,242 @@ | ||
/** | ||
* Appcelerator Titanium Mobile | ||
* Copyright (c) 2018 by Appcelerator, Inc. All Rights Reserved. | ||
* Licensed under the terms of the Apache Public License | ||
* Please see the LICENSE included with this distribution for details. | ||
*/ | ||
|
||
#if defined(USE_TI_UIIOSAPPLICATIONSHORTCUTS) || defined(USE_TI_UIAPPLICATIONSHORTCUTS) | ||
#import "TiUIApplicationShortcutsProxy.h" | ||
#import "TiUtils.h" | ||
#ifdef USE_TI_CONTACTS | ||
#import "TiContactsPerson.h" | ||
#endif | ||
#import <CommonCrypto/CommonDigest.h> | ||
#import <ContactsUI/ContactsUI.h> | ||
|
||
@implementation TiUIApplicationShortcutsProxy | ||
|
||
- (NSString *)apiName | ||
{ | ||
return @"Ti.UI.ApplicationShortcuts"; | ||
} | ||
|
||
- (NSDictionary *)shortcutItemToDictionary:(UIApplicationShortcutItem *)item | ||
{ | ||
NSMutableDictionary *dict = [NSMutableDictionary | ||
dictionaryWithObjectsAndKeys:item.type, @"identifier", | ||
nil]; | ||
|
||
if (item.localizedTitle != nil) { | ||
[dict setObject:item.localizedTitle forKey:@"title"]; | ||
} | ||
|
||
if (item.localizedSubtitle != nil) { | ||
[dict setObject:item.localizedSubtitle forKey:@"subtitle"]; | ||
} | ||
|
||
if (item.userInfo != nil) { | ||
[dict setObject:item.userInfo forKey:@"userInfo"]; | ||
} | ||
|
||
return dict; | ||
} | ||
|
||
- (NSArray *)listDynamicShortcuts:(id)unused | ||
{ | ||
NSMutableArray *shortcutsToReturn = [NSMutableArray array]; | ||
NSArray *shortcuts = [UIApplication sharedApplication].shortcutItems; | ||
|
||
for (UIApplicationShortcutItem *item in shortcuts) { | ||
[shortcutsToReturn addObject:[self shortcutItemToDictionary:item]]; | ||
} | ||
|
||
return shortcutsToReturn; | ||
} | ||
|
||
- (void)removeAllDynamicShortcuts:(id)unused | ||
{ | ||
[UIApplication sharedApplication].shortcutItems = nil; | ||
} | ||
|
||
- (NSArray *)listStaticShortcuts:(id)unused | ||
{ | ||
NSMutableArray *shortcutsToReturn = [NSMutableArray array]; | ||
NSArray *shortcuts = [NSBundle mainBundle].infoDictionary[@"UIApplicationShortcutItems"]; | ||
|
||
if (shortcuts == nil || [shortcuts count] == 0) { | ||
return @[]; | ||
} | ||
|
||
for (id item in shortcuts) { | ||
// We need to map the plist-keys manually for static shortcuts | ||
NSString *type = [item valueForKey:@"UIApplicationShortcutItemType"]; | ||
NSString *title = [item valueForKey:@"UIApplicationShortcutItemTitle"]; | ||
NSString *subtitle = [item valueForKey:@"UIApplicationShortcutItemSubtitle"]; | ||
UIApplicationShortcutIcon *icon = [UIApplicationShortcutIcon iconWithType:[TiUtils intValue:[item valueForKey:@"UIApplicationShortcutItemIconType"]]]; | ||
NSDictionary *userInfo = [item valueForKey:@"UIApplicationShortcutItemUserInfo"]; | ||
|
||
UIApplicationShortcutItem *shortcut = [[[UIApplicationShortcutItem alloc] initWithType:type | ||
localizedTitle:title | ||
localizedSubtitle:subtitle | ||
icon:icon | ||
userInfo:userInfo] autorelease]; | ||
|
||
[shortcutsToReturn addObject:[self shortcutItemToDictionary:shortcut]]; | ||
} | ||
|
||
return shortcutsToReturn; | ||
} | ||
|
||
- (BOOL)typeExists:(NSString *)type | ||
{ | ||
NSArray *shortcuts = [UIApplication sharedApplication].shortcutItems; | ||
for (UIApplicationShortcutItem *item in shortcuts) { | ||
if ([item.type isEqualToString:type]) { | ||
return YES; | ||
} | ||
} | ||
|
||
return NO; | ||
} | ||
|
||
- (NSNumber *)dynamicShortcutExists:(id)identifier | ||
{ | ||
ENSURE_SINGLE_ARG(identifier, NSString); | ||
|
||
if ([TiUtils stringValue:identifier] == nil) { | ||
NSLog(@"[ERROR] Ti.UI.ApplicationShortcuts: The \"identifier\" property is required."); | ||
return; | ||
} | ||
|
||
return NUMBOOL([self typeExists:[TiUtils stringValue:identifier]]); | ||
} | ||
|
||
- (NSDictionary *)getDynamicShortcut:(id)identifier | ||
{ | ||
ENSURE_SINGLE_ARG(identifier, NSString); | ||
|
||
NSArray *shortcuts = [UIApplication sharedApplication].shortcutItems; | ||
for (UIApplicationShortcutItem *item in shortcuts) { | ||
if ([item.type isEqualToString:[TiUtils stringValue:identifier]]) { | ||
return [self shortcutItemToDictionary:item]; | ||
} | ||
} | ||
|
||
return nil; | ||
} | ||
|
||
- (void)removeDynamicShortcut:(id)identifier | ||
{ | ||
ENSURE_SINGLE_ARG(identifier, NSString); | ||
|
||
NSString *key = [TiUtils stringValue:identifier]; | ||
|
||
if (key == nil) { | ||
NSLog(@"[ERROR] The \"identifier\" property is required."); | ||
return; | ||
} | ||
|
||
if ([self typeExists:key] == NO) { | ||
return; | ||
} | ||
|
||
NSMutableArray *shortcuts = (NSMutableArray *)[UIApplication sharedApplication].shortcutItems; | ||
|
||
for (UIApplicationShortcutItem *item in shortcuts) { | ||
if ([item.type isEqualToString:key]) { | ||
[shortcuts removeObject:item]; | ||
} | ||
} | ||
|
||
[UIApplication sharedApplication].shortcutItems = shortcuts; | ||
} | ||
|
||
- (void)addDynamicShortcut:(id)args | ||
{ | ||
ENSURE_SINGLE_ARG(args, NSDictionary); | ||
|
||
if ([args objectForKey:@"identifier"] == nil) { | ||
NSLog(@"[ERROR] Ti.UI.ApplicationShortcuts: The \"identifier\" property is required."); | ||
return; | ||
} | ||
|
||
if ([args objectForKey:@"title"] == nil) { | ||
NSLog(@"[ERROR] Ti.UI.ApplicationShortcuts: The \"title\" property is required."); | ||
return; | ||
} | ||
|
||
if ([self typeExists:[args objectForKey:@"identifier"]]) { | ||
NSLog(@"[ERROR] Ti.UI.ApplicationShortcuts: The identifier for the shortcut %@ already exists. This field must be unique.", | ||
[args objectForKey:@"identifier"]); | ||
return; | ||
} | ||
|
||
UIApplicationShortcutItem *shortcut = [[[UIApplicationShortcutItem alloc] initWithType:[args objectForKey:@"identifier"] | ||
localizedTitle:[args objectForKey:@"title"] | ||
localizedSubtitle:[args objectForKey:@"subtitle"] | ||
icon:[self findIcon:[args objectForKey:@"icon"]] | ||
userInfo:[args objectForKey:@"userInfo"]] autorelease]; | ||
|
||
NSMutableArray *shortcuts = (NSMutableArray *)[UIApplication sharedApplication].shortcutItems; | ||
[shortcuts addObject:shortcut]; | ||
[UIApplication sharedApplication].shortcutItems = shortcuts; | ||
} | ||
|
||
- (UIApplicationShortcutIcon *)findIcon:(id)value | ||
{ | ||
if (value == nil) { | ||
return nil; | ||
} | ||
|
||
#ifdef USE_TI_CONTACTS | ||
if ([value isKindOfClass:[TiContactsPerson class]]) { | ||
ENSURE_TYPE(value, TiContactsPerson); | ||
return [UIApplicationShortcutIcon iconWithContact:[(TiContactsPerson *)value nativePerson]]; | ||
} | ||
#endif | ||
|
||
if ([value isKindOfClass:[UIApplicationShortcutIcon class]]) { | ||
return (UIApplicationShortcutIcon *)value; | ||
} | ||
|
||
if ([value isKindOfClass:[NSNumber class]]) { | ||
NSInteger iconIndex = [value integerValue]; | ||
return [UIApplicationShortcutIcon iconWithType:iconIndex]; | ||
} | ||
|
||
if ([value isKindOfClass:[NSString class]]) { | ||
return [UIApplicationShortcutIcon iconWithTemplateImageName:[self urlInAssetCatalog:value]]; | ||
} | ||
|
||
NSLog(@"[ERROR] Ti.UI.ApplicationShortcuts: Invalid icon provided, defaulting to use no icon."); | ||
return nil; | ||
} | ||
|
||
- (NSString *)urlInAssetCatalog:(NSString *)url | ||
{ | ||
NSString *resultUrl = nil; | ||
|
||
if ([url hasPrefix:@"/"] == YES) { | ||
url = [url substringFromIndex:1]; | ||
} | ||
|
||
unsigned char digest[CC_SHA1_DIGEST_LENGTH]; | ||
NSData *stringBytes = [url dataUsingEncoding:NSUTF8StringEncoding]; | ||
if (CC_SHA1([stringBytes bytes], (CC_LONG)[stringBytes length], digest)) { | ||
// SHA-1 hash has been calculated and stored in 'digest'. | ||
NSMutableString *sha = [[NSMutableString alloc] init]; | ||
for (int i = 0; i < CC_SHA1_DIGEST_LENGTH; i++) { | ||
[sha appendFormat:@"%02x", digest[i]]; | ||
} | ||
[sha appendString:@"."]; | ||
[sha appendString:[url pathExtension]]; | ||
resultUrl = [NSMutableString stringWithString:sha]; | ||
RELEASE_TO_NIL(sha) | ||
} | ||
|
||
return resultUrl; | ||
} | ||
|
||
@end | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters