Commit 49ed2904 authored by Felix Paul Kühne's avatar Felix Paul Kühne

add basic CoreSpotlight support for files and album tracks

parent 4abee8fe
......@@ -26,6 +26,7 @@
*****************************************************************************/
#import <CoreData/CoreData.h>
#import <CoreSpotlight/CoreSpotlight.h>
@class MLShowEpisode;
@class MLAlbumTrack;
......@@ -122,6 +123,9 @@ extern NSString *const MLFileThumbnailWasUpdated;
- (size_t)fileSizeInBytes;
- (CSSearchableItemAttributeSet *)coreSpotlightAttributeSet;
- (void)updateCoreSpotlightEntry;
@end
......
......@@ -30,6 +30,11 @@
#import <CoreData/CoreData.h>
#if TARGET_OS_IPHONE
#ifndef __IPHONE_9_0
#error "This project uses features only available in iOS SDK 9.0 and later."
#endif
// FIXME
#define HAVE_BLOCK 0
......@@ -53,6 +58,8 @@
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
#define SYSTEM_RUNS_IOS9 SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"9.0")
#ifdef __IPHONE_7_0
#define SYSTEM_RUNS_IOS7 SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")
#else
......
......@@ -415,4 +415,90 @@ NSString *const MLFileThumbnailWasUpdated = @"MLFileThumbnailWasUpdated";
return [fileSize unsignedLongLongValue];
}
- (CSSearchableItemAttributeSet *)coreSpotlightAttributeSet
{
#if TARGET_OS_IPHONE
if (!SYSTEM_RUNS_IOS9)
return nil;
NSString *workString;
CSSearchableItemAttributeSet* attributeSet = [[CSSearchableItemAttributeSet alloc] initWithItemContentType:@"public.audiovisual-content"];
attributeSet.title = self.title;
attributeSet.metadataModificationDate = [NSDate date];
attributeSet.addedDate = [NSDate date];
attributeSet.duration = @(self.duration.intValue / 1000);
attributeSet.streamable = @(0);
attributeSet.deliveryType = @(0);
attributeSet.local = @(1);
attributeSet.playCount = @(0);
UIImage *computedThumb = self.computedThumbnail;
if (computedThumb) {
attributeSet.thumbnailData = UIImageJPEGRepresentation(computedThumb, .9);
computedThumb = nil;
}
NSArray *tracks = [[self tracks] allObjects];
NSUInteger trackCount = tracks.count;
NSMutableArray *codecs = [NSMutableArray new];
NSMutableArray *languages = [NSMutableArray new];
for (NSUInteger x = 0; x < trackCount; x++) {
NSManagedObject *track = tracks[x];
[codecs addObject:[track valueForKey:@"codec"]];
NSString *language = [track valueForKey:@"language"];
if (language != nil)
[languages addObject:language];
NSString *trackEntityName = [[track entity] name];
if ([trackEntityName isEqualToString:@"VideoTrackInformation"]) {
attributeSet.videoBitRate = [track valueForKey:@"bitrate"];
} else if ([trackEntityName isEqualToString:@"AudioTrackInformation"]) {
attributeSet.audioSampleRate = [track valueForKey:@"sampleRate"];
attributeSet.audioChannelCount = [track valueForKey:@"channelsNumber"];
attributeSet.audioBitRate = [track valueForKey:@"bitrate"];
}
}
attributeSet.codecs = [NSArray arrayWithArray:codecs];
attributeSet.languages = [NSArray arrayWithArray:languages];
workString = self.genre;
if (workString)
attributeSet.genre = workString;
MLAlbumTrack *albumTrack = self.albumTrack;
if (albumTrack) {
attributeSet.artist = albumTrack.artist;
attributeSet.title = albumTrack.title;
attributeSet.audioTrackNumber = albumTrack.trackNumber;
MLAlbum *album = albumTrack.album;
if (album) {
attributeSet.album = album.name;
}
if (attributeSet.genre == nil)
attributeSet.genre = albumTrack.genre;
}
return attributeSet;
#else
return nil;
#endif
}
- (void)updateCoreSpotlightEntry
{
#if TARGET_OS_IPHONE
if (SYSTEM_RUNS_IOS9) {
/* create final CS item, which will replace the earlier entity */
CSSearchableItemAttributeSet *attributeSet = [self coreSpotlightAttributeSet];
CSSearchableItem *item;
item = [[CSSearchableItem alloc] initWithUniqueIdentifier:self.objectID.URIRepresentation.absoluteString
domainIdentifier:[[MLMediaLibrary sharedMediaLibrary] applicationGroupIdentifier]
attributeSet:attributeSet];
[[CSSearchableIndex defaultSearchableIndex] indexSearchableItems:@[item] completionHandler:nil];
}
#endif
}
@end
......@@ -24,6 +24,8 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#import <CommonCrypto/CommonDigest.h> // for MD5
#import "MLFileParserQueue.h"
#import "MLFile.h"
#import "MLMediaLibrary.h"
......@@ -31,7 +33,6 @@
#import "MLAlbumTrack.h"
#import "MLAlbum.h"
#import "MLTitleDecrapifier.h"
#import <CommonCrypto/CommonDigest.h> // for MD5
#import "MLThumbnailerQueue.h"
#ifdef MLKIT_READONLY_TARGET
......@@ -295,6 +296,7 @@
@catch (NSException *exception) {
APLog(@"failed to set that we fetch info for the file");
}
[file updateCoreSpotlightEntry];
[self endParsing];
}
......
......@@ -39,6 +39,10 @@
#import "MLMediaLibrary+Migration.h"
#import <sys/sysctl.h> // for sysctlbyname
#if TARGET_OS_IPHONE
#import <CoreSpotlight/CoreSpotlight.h>
#endif
#if HAVE_BLOCK
#import "MLMovieInfoGrabber.h"
#import "MLTVShowInfoGrabber.h"
......@@ -676,6 +680,14 @@ static NSString *kDecrapifyTitles = @"MLDecrapifyTitles";
#pragma mark -
#pragma mark Adding file to the DB
#ifdef MLKIT_READONLY_TARGET
- (void)addFilePaths:(NSArray *)filepaths
{
}
#else
- (void)addFilePath:(NSString *)filePath
{
APLog(@"Adding Path %@", filePath);
......@@ -713,6 +725,25 @@ static NSString *kDecrapifyTitles = @"MLDecrapifyTitles";
else
file.title = [title stringByDeletingPathExtension];
#if TARGET_OS_IPHONE
if (SYSTEM_RUNS_IOS9) {
/* add a preliminary CS item, which will be replaced once we have more information */
CSSearchableItemAttributeSet* attributeSet = [[CSSearchableItemAttributeSet alloc] initWithItemContentType:@"public.audiovisual-content"];
attributeSet.title = file.title;
attributeSet.displayName = file.title;
attributeSet.metadataModificationDate = [NSDate date];
CSSearchableItem *item;
item = [[CSSearchableItem alloc] initWithUniqueIdentifier:file.objectID.URIRepresentation.absoluteString
domainIdentifier:_applicationGroupIdentifier
attributeSet:attributeSet];
// Index the item.
[[CSSearchableIndex defaultSearchableIndex] indexSearchableItems:@[item] completionHandler:^(NSError * __nullable error) {
NSLog(@"Search item indexed");
}];
}
#endif
[[MLFileParserQueue sharedFileParserQueue] addFile:file];
}
......@@ -756,7 +787,7 @@ static NSString *kDecrapifyTitles = @"MLDecrapifyTitles";
for (NSString* path in filePathsToAdd)
[self addFilePath:path];
}
#endif
#pragma mark -
#pragma mark DB Updates
......@@ -775,6 +806,14 @@ static NSString *kDecrapifyTitles = @"MLDecrapifyTitles";
}
#endif
#ifdef MLKIT_READONLY_TARGET
- (void)updateMediaDatabase
{
}
#else
- (void)updateMediaDatabase
{
[self libraryDidDisappear];
......@@ -829,6 +868,15 @@ static NSString *kDecrapifyTitles = @"MLDecrapifyTitles";
bool thumbExists = [fileManager fileExistsAtPath:thumbPath];
if (thumbExists)
[fileManager removeItemAtPath:thumbPath error:nil];
if (SYSTEM_RUNS_IOS9) {
/* remove file from CoreSpotlight */
[[CSSearchableIndex defaultSearchableIndex] deleteSearchableItemsWithIdentifiers:@[file.objectID.URIRepresentation.absoluteString]
completionHandler:^(NSError * __nullable error) {
NSLog(@"Removed %@ from index", file.objectID.URIRepresentation);
}];
}
[moc deleteObject:file];
#endif
}
......@@ -909,6 +957,7 @@ static NSString *kDecrapifyTitles = @"MLDecrapifyTitles";
/* Update every hour - FIXME: Preferences key */
[self performSelector:@selector(updateMediaDatabase) withObject:nil afterDelay:60 * 60];
}
#endif
- (void)applicationWillExit
{
......
......@@ -132,8 +132,10 @@
APLog(@"Finished thumbnail for %@", file.title);
if (thumbnail) {
UIImage *thumbnailImage = [UIImage imageWithCGImage:thumbnail];
if (thumbnailImage)
if (thumbnailImage) {
file.computedThumbnail = [UIImage imageWithCGImage:thumbnail];
[file updateCoreSpotlightEntry];
}
}
[self endThumbnailing];
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment