diff --git a/Classes/ChatConversationTableView.h b/Classes/ChatConversationTableView.h index cce625051..551a22a4d 100644 --- a/Classes/ChatConversationTableView.h +++ b/Classes/ChatConversationTableView.h @@ -28,7 +28,6 @@ @protocol ChatConversationDelegate - (BOOL)startImageUpload:(UIImage *)image assetId:(NSString *)phAssetId withQuality:(float)quality; -- (BOOL)startFileUpload:(NSData *)data withUrl:(NSURL *)url; - (void)resendChat:(NSString *)message withExternalUrl:(NSString *)url; - (void)tableViewIsScrolling; diff --git a/Classes/ChatConversationView.h b/Classes/ChatConversationView.h index 0a2f99c0c..422cad12a 100644 --- a/Classes/ChatConversationView.h +++ b/Classes/ChatConversationView.h @@ -32,7 +32,7 @@ #include "linphone/linphonecore.h" @interface ChatConversationView - : TPMultiLayoutViewController { OrderedDictionary *imageQualities; BOOL scrollOnGrowingEnabled; @@ -61,6 +61,7 @@ @property (weak, nonatomic) IBOutlet UIIconButton *infoButton; @property (weak, nonatomic) IBOutlet UILabel *particpantsLabel; @property (nonatomic, strong) UIDocumentInteractionController *documentInteractionController; +@property (nonatomic, strong) UIDocumentPickerViewController *documentPicker; @property NSMutableArray *imagesArray; @property NSMutableArray *assetIdsArray; @property NSMutableArray *qualitySettingsArray; @@ -80,7 +81,8 @@ - (IBAction)onDeleteClick:(id)sender; - (IBAction)onEditionChangeClick:(id)sender; - (void)update; -- (void)openResults:(NSString *) filePath; +- (void)getIcloudFiles; +- (void)openFile:(NSString *) filePath; - (void)clearMessageView; @end diff --git a/Classes/ChatConversationView.m b/Classes/ChatConversationView.m index 09acbb8f8..cd0d3edd4 100644 --- a/Classes/ChatConversationView.m +++ b/Classes/ChatConversationView.m @@ -233,43 +233,34 @@ static UICompositeViewDescription *compositeDescription = nil; - (void)shareFile { NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:groupName]; - - NSDictionary *dict = [defaults valueForKey:@"img"]; - NSDictionary *dictWeb = [defaults valueForKey:@"web"]; - NSDictionary *dictFile = [defaults valueForKey:@"mov"]; - NSDictionary *dictText = [defaults valueForKey:@"text"]; + NSDictionary *dict = [defaults valueForKey:@"photoData"]; + NSDictionary *dictFile = [defaults valueForKey:@"icloudData"]; + NSDictionary *dictUrl = [defaults valueForKey:@"url"]; if (dict) { - //share photo - NSData *data = dict[@"nsData"]; - UIImage *image = [[UIImage alloc] initWithData:data]; - NSString *filename = dict[@"url"]; - if (filename) { - NSMutableDictionary * assetDict = [LinphoneUtils photoAssetsDictionary]; - [self chooseImageQuality:image assetId:[[assetDict objectForKey:filename] localIdentifier]]; - } else - [self chooseImageQuality:image assetId:@""]; - [defaults removeObjectForKey:@"img"]; - } else if (dictWeb) { - //share url, if local file, then upload file - NSString *url = dictWeb[@"url"]; - NSURL *fileUrl = [NSURL fileURLWithPath:url]; - if ([url hasPrefix:@"file"]) { - //local file - NSData *data = dictWeb[@"nsData"]; - [self confirmShare:data url:fileUrl text:nil]; + //file shared from photo lib + NSString *fileName = dict[@"url"]; + NSString *key = [[fileName componentsSeparatedByString:@"."] firstObject]; + NSMutableDictionary * assetDict = [LinphoneUtils photoAssetsDictionary]; + if ([fileName hasSuffix:@"JPG"] || [fileName hasSuffix:@"PNG"]) { + UIImage *image = [[UIImage alloc] initWithData:dict[@"nsData"]]; + [self chooseImageQuality:image assetId:[[assetDict objectForKey:key] localIdentifier]]; + } else if ([fileName hasSuffix:@"MOV"]) { + [self confirmShare:dict[@"nsData"] url:nil fileName:nil assetId:[[assetDict objectForKey:key] localIdentifier]]; } else { - [self confirmShare:nil url:nil text:url]; + LOGE(@"Unable to parse file %@",fileName); } - [defaults removeObjectForKey:@"web"]; - }else if (dictFile) { - //share file - NSData *data = dictFile[@"nsData"]; - [self confirmShare:data url:[NSURL fileURLWithPath:dictFile[@"url"]] text:nil]; - [defaults removeObjectForKey:@"mov"]; - }else if (dictText) { - //share text - [self confirmShare:nil url:nil text:dictText[@"name"]]; - [defaults removeObjectForKey:@"text"]; + + [defaults removeObjectForKey:@"photoData"]; + } else if (dictFile) { + NSString *fileName = dictFile[@"url"]; + [self confirmShare:dictFile[@"nsData"] url:nil fileName:fileName assetId:nil]; + + [defaults removeObjectForKey:@"icloudData"]; + } else if (dictUrl) { + NSString *url = dictUrl[@"url"]; + [self confirmShare:nil url:url fileName:nil assetId:nil]; + + [defaults removeObjectForKey:@"url"]; } } @@ -363,17 +354,17 @@ static UICompositeViewDescription *compositeDescription = nil; }); } -- (void)confirmShare:(NSData *)data url:(NSURL *)url text:(NSString *)text { +- (void)confirmShare:(NSData *)data url:(NSString *)url fileName:(NSString *)fileName assetId:(NSString *)phAssetId { DTActionSheet *sheet = [[DTActionSheet alloc] initWithTitle:NSLocalizedString(@"", nil)]; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ - [sheet addButtonWithTitle:@"send to this friend" block:^() { - if(data && url) - [self startFileUpload:data withUrl:url]; + if (url) + [self sendMessage:url withExterlBodyUrl:nil withInternalURL:nil]; + else if (fileName) + [self startFileUpload:data withName:fileName]; else - [self sendMessage:text withExterlBodyUrl:nil withInternalURL:nil]; - + [self startFileUpload:data assetId:phAssetId]; }]; [sheet addCancelButtonWithTitle:NSLocalizedString(@"Cancel", nil) block:nil]; @@ -635,9 +626,16 @@ static UICompositeViewDescription *compositeDescription = nil; return TRUE; } -- (BOOL)startFileUpload:(NSData *)data withUrl:(NSURL *)url { +- (BOOL)startFileUpload:(NSData *)data assetId:phAssetId { FileTransferDelegate *fileTransfer = [[FileTransferDelegate alloc] init]; - [fileTransfer uploadFile:data forChatRoom:_chatRoom withUrl:url]; + [fileTransfer uploadVideo:data withassetId:phAssetId forChatRoom:_chatRoom]; + [_tableController scrollToBottom:true]; + return TRUE; +} + +- (BOOL)startFileUpload:(NSData *)data withName:(NSString *)name { + FileTransferDelegate *fileTransfer = [[FileTransferDelegate alloc] init]; + [fileTransfer uploadFile:data forChatRoom:_chatRoom withName:name]; [_tableController scrollToBottom:true]; return TRUE; } @@ -909,7 +907,17 @@ void on_chat_room_conference_left(LinphoneChatRoom *cr, const LinphoneEventLog * [view.tableController scrollToBottom:true]; } -- (void)openResults:(NSString *) filePath +- (void)getIcloudFiles +{ + _documentPicker = [[UIDocumentPickerViewController alloc] initWithDocumentTypes:@[@"public.data"] + inMode:UIDocumentPickerModeImport]; + _documentPicker.delegate = self; + + _documentPicker.modalPresentationStyle = UIModalPresentationFormSheet; + [self presentViewController:_documentPicker animated:YES completion:nil]; +} + +- (void)openFile:(NSString *) filePath { // Open the controller. _documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:filePath]]; @@ -923,6 +931,21 @@ void on_chat_room_conference_left(LinphoneChatRoom *cr, const LinphoneEventLog * } } +- (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentAtURL:(NSURL *)url { + + NSFileCoordinator *fileCoordinator = [[NSFileCoordinator alloc] initWithFilePresenter:nil]; + [fileCoordinator coordinateReadingItemAtURL:url options:NSFileCoordinatorReadingWithoutChanges error:nil byAccessor:^(NSURL * _Nonnull newURL) { + + NSString *fileName = [newURL lastPathComponent]; + NSData *data = [NSData dataWithContentsOfURL:newURL]; + + NSString *filePath = [[LinphoneManager cacheDirectory] stringByAppendingPathComponent:fileName]; + + [[NSFileManager defaultManager] createFileAtPath:filePath contents:data attributes:nil]; + [self openFile:filePath]; + }]; +} + - (void)deleteImageWithAssetId:(NSString *)assetId { NSUInteger key = [_assetIdsArray indexOfObject:assetId]; [_imagesArray removeObjectAtIndex:key]; diff --git a/Classes/LinphoneUI/UIChatBubblePhotoCell.m b/Classes/LinphoneUI/UIChatBubblePhotoCell.m index b10d52f2e..ca81111a7 100644 --- a/Classes/LinphoneUI/UIChatBubblePhotoCell.m +++ b/Classes/LinphoneUI/UIChatBubblePhotoCell.m @@ -129,35 +129,6 @@ }]; } -- (void) loadVideoAsset: (AVAsset *) asset { - // Calculate a time for the snapshot - I'm using the half way mark. - CMTime duration = [asset duration]; - CMTime snapshot = CMTimeMake(duration.value / 2, duration.timescale); - // Create a generator and copy image at the time. - // I'm not capturing the actual time or an error. - AVAssetImageGenerator *generator = - [AVAssetImageGenerator assetImageGeneratorWithAsset:asset]; - CGImageRef imageRef = [generator copyCGImageAtTime:snapshot - actualTime:nil - error:nil]; - - UIImage *thumb = [UIImage imageWithCGImage:imageRef]; - CGImageRelease(imageRef); - - UIGraphicsBeginImageContext(videoDefaultSize); - [thumb drawInRect:CGRectMake(0, 0, videoDefaultSize.width, videoDefaultSize.height)]; - UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); - UIGraphicsEndImageContext(); - - [self loadImageAsset:nil image:image]; - - // put the play button in the top - CGRect newFrame = _playButton.frame; - newFrame.origin.x = _finalImage.frame.origin.x/2; - newFrame.origin.y = _finalImage.frame.origin.y/2; - _playButton.frame = newFrame; -} - - (void) loadFileAsset { dispatch_async(dispatch_get_main_queue(), ^{ _fileName.hidden = NO; @@ -210,28 +181,16 @@ if (localImage) { // we did not load the image yet, so start doing so if (_messageImageView.image == nil) { - [_messageImageView startLoading]; - PHFetchResult *assets = [PHAsset fetchAssetsWithLocalIdentifiers:[NSArray arrayWithObject:localImage] options:nil]; - UIImage *img = [chatTableView.imagesInChatroom objectForKey:localImage]; - if (![assets firstObject]) - [self loadPlaceholder]; - PHAsset *asset = [assets firstObject]; - if (img) - [self loadImageAsset:asset image:img]; - else - [self loadAsset:asset]; + [self loadFirstImage:localImage type:PHAssetMediaTypeImage]; } - } else if (localVideo) { + } + else if (localVideo) { if (_messageImageView.image == nil) { - [_messageImageView startLoading]; - // read video from Documents - NSString *filePath = [LinphoneManager documentFile:localVideo]; - NSURL *url = [NSURL fileURLWithPath:filePath]; - AVAsset *asset = [AVAsset assetWithURL:url]; - if (asset) - [self loadVideoAsset:asset]; + [self loadFirstImage:localVideo type:PHAssetMediaTypeVideo]; + _imageGestureRecognizer.enabled = NO; } - } else if (localFile) { + } + else if (localFile) { NSString *text = [NSString stringWithFormat:@"📎 %@",localFile]; _fileName.text = text; [self loadFileAsset]; @@ -256,8 +215,25 @@ } } +- (void)loadFirstImage:(NSString *)key type:(PHAssetMediaType)type { + [_messageImageView startLoading]; + PHFetchResult *assets = [PHAsset fetchAssetsWithLocalIdentifiers:[NSArray arrayWithObject:key] options:nil]; + UIImage *img = nil; + + img = [chatTableView.imagesInChatroom objectForKey:key]; + PHAsset *asset = [assets firstObject]; + if (!asset) + [self loadPlaceholder]; + else if (asset.mediaType == type) + img = nil; + if (img) + [self loadImageAsset:asset image:img]; + else + [self loadAsset:asset]; +} + - (void)fileErrorBlock { - DTActionSheet *sheet = [[DTActionSheet alloc] initWithTitle:NSLocalizedString(@"Can't open this file", nil)]; + DTActionSheet *sheet = [[DTActionSheet alloc] initWithTitle:NSLocalizedString(@"Can't find this file", nil)]; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ [sheet addCancelButtonWithTitle:NSLocalizedString(@"OK", nil) block:nil]; dispatch_async(dispatch_get_main_queue(), ^{ @@ -278,33 +254,35 @@ } - (IBAction)onPlayClick:(id)sender { - NSString *localVideo = [LinphoneManager getMessageAppDataForKey:@"localvideo" inMessage:self.message]; - NSString *filePath = [LinphoneManager documentFile:localVideo]; - NSFileManager *fileManager = [NSFileManager defaultManager]; - - if ([fileManager fileExistsAtPath:filePath]) { - // create a player view controller - AVPlayer *player = [AVPlayer playerWithURL:[[NSURL alloc] initFileURLWithPath:filePath]]; - AVPlayerViewController *controller = [[AVPlayerViewController alloc] init]; - [PhoneMainView.instance presentViewController:controller animated:YES completion:nil]; - controller.player = player; - [player play]; - } else { - [self fileErrorBlock]; - } + PHAsset *asset = [_messageImageView asset]; + PHVideoRequestOptions *options = [[PHVideoRequestOptions alloc] init]; + // options.synchronous = TRUE; + [[PHImageManager defaultManager] requestPlayerItemForVideo:asset options:options resultHandler:^(AVPlayerItem * _Nullable playerItem, NSDictionary * _Nullable info) { + if(playerItem) { + AVPlayer *player = [AVPlayer playerWithPlayerItem:playerItem]; + AVPlayerViewController *controller = [[AVPlayerViewController alloc] init]; + [PhoneMainView.instance presentViewController:controller animated:YES completion:nil]; + controller.player = player; + [player play]; + } + else { + [self fileErrorBlock]; + } + }]; } - (IBAction)onOpenClick:(id)event { - NSString *localFile = [LinphoneManager getMessageAppDataForKey:@"localfile" inMessage:self.message]; - NSString *filePath = [LinphoneManager documentFile:localFile]; - NSFileManager *fileManager = [NSFileManager defaultManager]; - - if ([fileManager fileExistsAtPath:filePath]) { - ChatConversationView *view = VIEW(ChatConversationView); - [view openResults:filePath]; - } else { - [self fileErrorBlock]; - } + ChatConversationView *view = VIEW(ChatConversationView); + NSString *cachedFile = [LinphoneManager getMessageAppDataForKey:@"cachedfile" inMessage:self.message]; + if (cachedFile) { + NSFileManager *fileManager = [NSFileManager defaultManager]; + if ([fileManager fileExistsAtPath:cachedFile]) { + [view openFile:cachedFile]; + } else { + [self fileErrorBlock]; + } + } else + [view getIcloudFiles]; } diff --git a/Classes/LinphoneUI/UIChatBubbleTextCell.m b/Classes/LinphoneUI/UIChatBubbleTextCell.m index 0623e4204..85b20dce9 100644 --- a/Classes/LinphoneUI/UIChatBubbleTextCell.m +++ b/Classes/LinphoneUI/UIChatBubbleTextCell.m @@ -235,9 +235,11 @@ if (linphone_chat_message_get_file_transfer_information(_message) != NULL) { NSString *localImage = [LinphoneManager getMessageAppDataForKey:@"localimage" inMessage:_message]; NSNumber *uploadQuality =[LinphoneManager getMessageAppDataForKey:@"uploadQuality" inMessage:_message]; - NSString *localVideo = [LinphoneManager getMessageAppDataForKey:@"localvideo" inMessage:_message]; - NSString *localFile = [LinphoneManager getMessageAppDataForKey:@"localfile" inMessage:_message]; - NSString *fileName = localVideo ? localVideo : localFile; + + // TODO: do resend for video and files + /*NSString *localVideo = [LinphoneManager getMessageAppDataForKey:@"localvideo" inMessage:_message]; + NSString *localFile = [LinphoneManager getMessageAppDataForKey:@"localfile" inMessage:_message];*/ + [self onDelete]; if(localImage){ ChatConversationTableView *tableView = VIEW(ChatConversationView).tableController; @@ -266,10 +268,10 @@ } }]; } - } else if(fileName) { + } /*else if(fileName) { NSString *filePath = [LinphoneManager documentFile:fileName]; [_chatRoomDelegate startFileUpload:[NSData dataWithContentsOfFile:filePath] withUrl:[NSURL URLWithString:filePath]]; - } + }*/ } else { [self onDelete]; double delayInSeconds = 0.4; @@ -356,16 +358,16 @@ static const CGFloat CELL_MESSAGE_Y_MARGIN = 52; // 44; if(localFile) { CGSize fileSize = CGSizeMake(200, 80); size = [self getMediaMessageSizefromOriginalSize:fileSize withWidth:width]; - } else if (localVideo) { - CGSize videoSize = CGSizeMake(320, 240); - size = [self getMediaMessageSizefromOriginalSize:videoSize withWidth:width]; - size.height += CELL_MESSAGE_X_MARGIN; } else { - if (!localImage) { + if (!localImage && !localVideo) { //We are loading the image return CGSizeMake(CELL_MIN_WIDTH + CELL_MESSAGE_X_MARGIN, CELL_MIN_HEIGHT + CELL_MESSAGE_Y_MARGIN); } - PHFetchResult *assets = [PHAsset fetchAssetsWithLocalIdentifiers:[NSArray arrayWithObject:localImage] options:nil]; + PHFetchResult *assets; + if(localImage) + assets = [PHAsset fetchAssetsWithLocalIdentifiers:[NSArray arrayWithObject:localImage] options:nil]; + else + assets = [PHAsset fetchAssetsWithLocalIdentifiers:[NSArray arrayWithObject:localVideo] options:nil]; if (![assets firstObject]) { return CGSizeMake(CELL_MIN_WIDTH, CELL_MIN_HEIGHT); } diff --git a/Classes/Utils/FileTransferDelegate.h b/Classes/Utils/FileTransferDelegate.h index 17a4bcc5a..867285eef 100644 --- a/Classes/Utils/FileTransferDelegate.h +++ b/Classes/Utils/FileTransferDelegate.h @@ -13,7 +13,8 @@ @interface FileTransferDelegate : NSObject - (void)upload:(UIImage *)image withassetId:(NSString *)phAssetId forChatRoom:(LinphoneChatRoom *)chatRoom withQuality:(float)quality; -- (void)uploadFile:(NSData *)data forChatRoom:(LinphoneChatRoom *)chatRoom withUrl:(NSURL *)url; +- (void)uploadFile:(NSData *)data forChatRoom:(LinphoneChatRoom *)chatRoom withName:(NSString *)name; +- (void)uploadVideo:(NSData *)data withassetId:(NSString *)phAssetId forChatRoom:(LinphoneChatRoom *)chatRoom; - (void)cancel; - (BOOL)download:(LinphoneChatMessage *)message; - (void)stopAndDestroy; diff --git a/Classes/Utils/FileTransferDelegate.m b/Classes/Utils/FileTransferDelegate.m index 9268ff0d6..4b2fd6915 100644 --- a/Classes/Utils/FileTransferDelegate.m +++ b/Classes/Utils/FileTransferDelegate.m @@ -116,22 +116,76 @@ static void linphone_iphone_file_transfer_recv(LinphoneChatMessage *message, con CFRelease((__bridge CFTypeRef)thiz); }); }]; - } else { + } else if([fileType isEqualToString:@"video"]) { + CFBridgingRetain(thiz); [[LinphoneManager.instance fileTransferDelegates] removeObject:thiz]; + NSString *name =[NSString stringWithUTF8String:linphone_content_get_name(content)]; + NSString *filePath = [[LinphoneManager cacheDirectory] stringByAppendingPathComponent:name]; + [[NSFileManager defaultManager] createFileAtPath:filePath + contents:thiz.data + attributes:nil]; + // until image is properly saved, keep a reminder on it so that the + // chat bubble is aware of the fact that image is being saved to device + [LinphoneManager setValueInMessageAppData:@"saving..." forKey:@"localvideo" inMessage:message]; - NSString *key = [fileType isEqualToString:@"file"] ? @"localfile" : @"localvideo"; + __block PHObjectPlaceholder *placeHolder; + [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{ + PHAssetCreationRequest *request = [PHAssetCreationRequest creationRequestForAssetFromVideoAtFileURL:[NSURL fileURLWithPath:filePath]]; + placeHolder = [request placeholderForCreatedAsset]; + } completionHandler:^(BOOL success, NSError * _Nullable error) { + dispatch_async(dispatch_get_main_queue(), ^{ + if (error) { + LOGE(@"Cannot save video data downloaded [%@]", [error localizedDescription]); + [LinphoneManager setValueInMessageAppData:nil forKey:@"localvideo" inMessage:message]; + UIAlertController *errView = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Transfer error", nil) + message:NSLocalizedString(@"Cannot write video to photo library", + nil) + preferredStyle:UIAlertControllerStyleAlert]; + + UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" + style:UIAlertActionStyleDefault + handler:^(UIAlertAction * action) {}]; + + [errView addAction:defaultAction]; + [PhoneMainView.instance presentViewController:errView animated:YES completion:nil]; + } else { + LOGI(@"video saved to [%@]", [placeHolder localIdentifier]); + [LinphoneManager setValueInMessageAppData:[placeHolder localIdentifier] + forKey:@"localvideo" + inMessage:message]; + } + [NSNotificationCenter.defaultCenter + postNotificationName:kLinphoneFileTransferRecvUpdate + object:thiz + userInfo:@{ + @"state" : @(LinphoneChatMessageStateDelivered), // we dont want to + // trigger + // FileTransferDone here + @"progress" : @(1.f), + }]; + + [thiz stopAndDestroy]; + CFRelease((__bridge CFTypeRef)thiz); + }); + }]; + + } else { + [[LinphoneManager.instance fileTransferDelegates] removeObject:thiz]; + NSString *key = @"localfile" ; NSString *name =[NSString stringWithUTF8String:linphone_content_get_name(content)]; [LinphoneManager setValueInMessageAppData:@"saving..." forKey:key inMessage:message]; //write file to path dispatch_async(dispatch_get_main_queue(), ^{ - NSString *filePath = [LinphoneManager documentFile:name]; + NSString *filePath = [[LinphoneManager cacheDirectory] stringByAppendingPathComponent:name]; + //NSString *filePath = [LinphoneManager documentFile:name]; [[NSFileManager defaultManager] createFileAtPath:filePath contents:thiz.data attributes:nil]; [LinphoneManager setValueInMessageAppData:name forKey:key inMessage:message]; + [LinphoneManager setValueInMessageAppData:filePath forKey:@"cachedfile" inMessage:message]; [NSNotificationCenter.defaultCenter postNotificationName:kLinphoneFileTransferRecvUpdate @@ -209,13 +263,14 @@ static LinphoneBuffer *linphone_iphone_file_transfer_send(LinphoneChatMessage *m linphone_content_set_name(content, [name UTF8String]); linphone_content_set_size(content, _data.length); _message = linphone_chat_room_create_file_transfer_message(chatRoom, content); - linphone_chat_message_add_text_content(_message, [_text UTF8String]); + //linphone_chat_message_add_text_content(_message, [_text UTF8String]); linphone_content_unref(content); linphone_chat_message_cbs_set_file_transfer_send(linphone_chat_message_get_callbacks(_message), linphone_iphone_file_transfer_send); // internal url is saved in the appdata for display and later save + LOGE(@"nnnn %@ %@",key, keyData); [LinphoneManager setValueInMessageAppData:keyData forKey:key inMessage:_message]; [LinphoneManager setValueInMessageAppData:qualityData forKey:@"uploadQuality" inMessage:_message]; @@ -235,18 +290,16 @@ static LinphoneBuffer *linphone_iphone_file_transfer_send(LinphoneChatMessage *m [self uploadData:UIImageJPEGRepresentation(image, quality) forChatRoom:chatRoom type:@"image" subtype:@"jpeg" name:name key:@"localimage" keyData:nil qualityData:nil]; } -- (void)uploadFile:(NSData *)data forChatRoom:(LinphoneChatRoom *)chatRoom withUrl:(NSURL *)url { - NSString *name = [url lastPathComponent]; - //save file to Documents - NSString *filePath = [LinphoneManager documentFile:name]; - [[NSFileManager defaultManager] createFileAtPath:filePath - contents:[NSMutableData dataWithData:data] - attributes:nil]; - - if ([[url pathExtension] isEqualToString:@"MOV"]) - [self uploadData:data forChatRoom:chatRoom type:nil subtype:nil name:name key:@"localvideo" keyData:name qualityData:nil]; +- (void)uploadVideo:(NSData *)data withassetId:(NSString *)phAssetId forChatRoom:(LinphoneChatRoom *)chatRoom { + NSString *name = [NSString stringWithFormat:@"IMG-%f.MOV", [NSDate timeIntervalSinceReferenceDate]]; + if (phAssetId) + [self uploadData:data forChatRoom:chatRoom type:@"video" subtype:nil name:name key:@"localvideo" keyData:phAssetId qualityData:nil]; else - [self uploadData:data forChatRoom:chatRoom type:@"file" subtype:nil name:name key:@"localfile" keyData:name qualityData:nil]; + [self uploadData:data forChatRoom:chatRoom type:@"video" subtype:nil name:name key:@"localvideo" keyData:@"ending..." qualityData:nil]; +} + +- (void)uploadFile:(NSData *)data forChatRoom:(LinphoneChatRoom *)chatRoom withName:(NSString *)name { + [self uploadData:data forChatRoom:chatRoom type:@"file" subtype:nil name:name key:@"localfile" keyData:name qualityData:nil]; } diff --git a/Classes/Utils/Utils.m b/Classes/Utils/Utils.m index e5e9c8f2c..8227fa484 100644 --- a/Classes/Utils/Utils.m +++ b/Classes/Utils/Utils.m @@ -73,6 +73,23 @@ return assetDict; } +/*+ (NSMutableDictionary *)videoAssetsDictionary { + NSMutableDictionary *assetDict = [NSMutableDictionary dictionary]; + + PHFetchOptions *options = [[PHFetchOptions alloc] init]; + [options setIncludeHiddenAssets:YES]; + [options setIncludeAllBurstAssets:YES]; + + PHFetchResult *fetchRes = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeVideo options:options]; + + for (PHAsset *asset in fetchRes) { + NSString *key = [asset valueForKey:@"filename"]; + [assetDict setObject:asset forKey:[[key componentsSeparatedByString:@"."] firstObject]]; + } + + return assetDict; +}*/ + + (NSString *)timeToString:(time_t)time withFormat:(LinphoneDateFormat)format { NSString *formatstr; NSDate *todayDate = [[NSDate alloc] init]; diff --git a/linphone.xcodeproj/project.pbxproj b/linphone.xcodeproj/project.pbxproj index 008655730..aa32a22ff 100755 --- a/linphone.xcodeproj/project.pbxproj +++ b/linphone.xcodeproj/project.pbxproj @@ -3531,6 +3531,9 @@ com.apple.Push = { enabled = 1; }; + com.apple.iCloud = { + enabled = 0; + }; }; }; 5E3128FF20D7A37E00CF3AAE = { diff --git a/linphoneExtension/ShareViewController.m b/linphoneExtension/ShareViewController.m index 33a1c06c4..56c9a40a8 100644 --- a/linphoneExtension/ShareViewController.m +++ b/linphoneExtension/ShareViewController.m @@ -20,34 +20,35 @@ static NSString* groupName = @"group.belledonne-communications.linphone"; - (void)didSelectPost { // This is called after the user selects Post. Do the upload of contentText and/or NSExtensionContext attachments. - + BOOL support = TRUE; // Inform the host that we're done, so it un-blocks its UI. Note: Alternatively you could call super's -didSelectPost, which will similarly complete the extension context. for (NSExtensionItem *item in self.extensionContext.inputItems) { for (NSItemProvider *provider in item.attachments) { NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:groupName]; // TODO: Use [provider registeredTypeIdentifiersWithFileOptions:0]; to get all type identifiers of the provider instead of this if/else if structure + support = TRUE; if ([provider hasItemConformingToTypeIdentifier:@"public.jpeg"]) { - [self loadItem:provider typeIdentifier:@"public.jpeg" defaults:defaults key:@"img"]; + [self loadItem:provider typeIdentifier:@"public.jpeg" defaults:defaults]; } else if ([provider hasItemConformingToTypeIdentifier:@"com.compuserve.gif"]) { - [self loadItem:provider typeIdentifier:@"com.compuserve.gif" defaults:defaults key:@"img"]; + [self loadItem:provider typeIdentifier:@"com.compuserve.gif" defaults:defaults]; } else if ([provider hasItemConformingToTypeIdentifier:@"public.url"]) { - [self loadItem:provider typeIdentifier:@"public.url" defaults:defaults key:@"web"]; + [self loadItem:provider typeIdentifier:@"public.url" defaults:defaults]; } else if ([provider hasItemConformingToTypeIdentifier:@"public.movie"]) { - [self loadItem:provider typeIdentifier:@"public.movie" defaults:defaults key:@"mov"]; - } else if ([provider hasItemConformingToTypeIdentifier:@"public.plain-text"]) { - [self loadItem:provider typeIdentifier:@"public.plain-text" defaults:defaults key:@"text"]; - } else if ([provider hasItemConformingToTypeIdentifier:@"com.adobe.pdf"]) { - [self loadItem:provider typeIdentifier:@"com.adobe.pdf" defaults:defaults key:@"web"]; - } - /*else if ([provider hasItemConformingToTypeIdentifier:@"public.png"]) { - [self loadItem:provider typeIdentifier:@"public.png" defaults:defaults key:@"img"]; - }*/ - else{ + [self loadItem:provider typeIdentifier:@"public.movie" defaults:defaults]; + } else if ([provider hasItemConformingToTypeIdentifier:@"com.apple.mapkit.map-item"]) { + [self loadItem:provider typeIdentifier:@"com.apple.mapkit.map-item" defaults:defaults]; + } else if ([provider hasItemConformingToTypeIdentifier:@"com.adobe.pdf"]) { + [self loadItem:provider typeIdentifier:@"com.adobe.pdf" defaults:defaults]; + } else if ([provider hasItemConformingToTypeIdentifier:@"public.png"]) { + [self loadItem:provider typeIdentifier:@"public.png" defaults:defaults]; + } else{ NSLog(@"Unkown itemprovider = %@", provider); - [self.extensionContext completeRequestReturningItems:@[] completionHandler:nil]; + support = false; } } } + if (!support) + [self.extensionContext completeRequestReturningItems:@[] completionHandler:nil]; } - (NSArray *)configurationItems { @@ -55,57 +56,47 @@ static NSString* groupName = @"group.belledonne-communications.linphone"; return @[]; } -- (void)loadItem:(NSItemProvider *)provider typeIdentifier:(NSString *)typeIdentifier defaults:(NSUserDefaults *)defaults key:(NSString *)key { +- (void)loadItem:(NSItemProvider *)provider typeIdentifier:(NSString *)typeIdentifier defaults:(NSUserDefaults *)defaults +{ [provider loadItemForTypeIdentifier:typeIdentifier options:nil completionHandler:^(id _Nullable item, NSError * _Null_unspecified error) { - if([(NSObject*)item isKindOfClass:[NSDictionary class]]) { + /*if([(NSObject*)item isKindOfClass:[NSDictionary class]]) { NSDictionary *dico = (NSDictionary *)item; if (dico) { return; } - } else if([(NSObject*)item isKindOfClass:[NSURL class]]) { + }*/ + if([(NSObject*)item isKindOfClass:[NSURL class]]) { NSURL *url = (NSURL *)item; NSData *nsData = [NSData dataWithContentsOfURL:url]; + if (nsData) { - NSDictionary *dict; - // We get the corresponding PHAsset identifier so we can display the image in the app without having to duplicate it. NSString *imgPath = url.path; - if ([imgPath hasPrefix:@"/var/mobile/Media/"] && ![imgPath containsString:@"OutgoingTemp"]) { // The image comes from the photos app - NSString *filename; - for ( NSString *comp in [imgPath componentsSeparatedByString:@"/"] ) { - if ([comp containsString:@"IMG_"]) { - filename = [[comp componentsSeparatedByString:@"."] firstObject]; - break; - } - } - dict = @{@"nsData" : nsData, - @"url" : filename}; - } else { - dict = @{@"nsData" : nsData}; + NSString *filename = [imgPath lastPathComponent]; + if([imgPath containsString:@"var/mobile/Media/PhotoData"]) { + // We get the corresponding PHAsset identifier so we can display the image in the app without having to duplicate it. + NSDictionary *dict = @{@"nsData" : nsData, + @"url" : filename}; + [defaults setObject:dict forKey:@"photoData"]; + } else if ([imgPath containsString:@"var/mobile/Library/Mobile Documents/com~apple~CloudDocs"]) { + // shared files from icloud drive + NSDictionary *dict = @{@"nsData" : nsData, + @"url" : filename}; + [defaults setObject:dict forKey:@"icloudData"]; + }else { + //Others + NSDictionary *dict = @{@"url" : [url absoluteString]}; + [defaults setObject:dict forKey:@"url"]; } - [defaults setObject:dict forKey:key]; } else { NSLog(@"NSExtensionItem Error, provider = %@", provider); [self.extensionContext completeRequestReturningItems:@[] completionHandler:nil]; } + } else { + //share text + NSDictionary *dict = @{@"url" : self.contentText}; + [defaults setObject:dict forKey:@"url"]; } - else if ([(NSObject*)item isKindOfClass:[UIImage class]]) { - NSLog(@"SHARED PHOTO UIIMAGE"); - NSData *imgData = UIImagePNGRepresentation((UIImage*)item); - if (imgData) { - NSDictionary *dict = @{@"nsData" : imgData, - }; - [defaults setObject:dict forKey:key]; - } else { - NSLog(@"NSExtensionItem Error, provider = %@", provider); - [self.extensionContext completeRequestReturningItems:@[] completionHandler:nil]; - } - } - else { - NSLog(@"SHARED PHOTO OTHER"); - NSDictionary *dict = @{@"name" : self.contentText}; - [defaults setObject:dict forKey:key]; - } - + UIResponder *responder = self; while (responder != nil) { if ([responder respondsToSelector:@selector(openURL:)]) {