FileMessagePreview.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2015-2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. #import "FileMessagePreview.h"
  21. #import "UTIConverter.h"
  22. #import "ModalNavigationController.h"
  23. #import "ContactUtil.h"
  24. #import "ModalPresenter.h"
  25. #import "ActivityUtil.h"
  26. #import "RectUtil.h"
  27. #import "BundleUtil.h"
  28. #import "ImageData.h"
  29. #import "FileMessagePreviewUnsupportedTypeView.h"
  30. #import "UIImage+ColoredImage.h"
  31. #import "AppDelegate.h"
  32. #import "AppGroup.h"
  33. #import "Utils.h"
  34. #import "Threema-Swift.h"
  35. #import <PassKit/PassKit.h>
  36. #import <QuickLook/QuickLook.h>
  37. #import "MDMSetup.h"
  38. @interface FileMessagePreview () <ModalNavigationControllerDelegate, QLPreviewControllerDataSource, UIDocumentInteractionControllerDelegate>
  39. @property FileMessage *fileMessage;
  40. @property UIViewController *previewBaseController;
  41. @property NSURL *tmpFileUrl;
  42. @end
  43. @implementation FileMessagePreview
  44. + (instancetype)fileMessagePreviewFor:(FileMessage *)fileMessage {
  45. FileMessagePreview *preview = [[FileMessagePreview alloc] init];
  46. preview.fileMessage = fileMessage;
  47. return preview;
  48. }
  49. + (BOOL)shouldExportToURL:(FileMessage *)fileMessage {
  50. if ([fileMessage.mimeType isEqualToString:@"text/vcard"]) {
  51. return NO;
  52. }
  53. return YES;
  54. }
  55. + (UIImage *)thumbnailForFileMessage:(FileMessage *)fileMessage {
  56. UIImage *thumbnailImage;
  57. if (fileMessage.thumbnail) {
  58. thumbnailImage = fileMessage.thumbnail.uiImage;
  59. }
  60. if (thumbnailImage == nil) {
  61. // not existing or invalid data
  62. thumbnailImage = [UTIConverter getDefaultThumbnailForMimeType:fileMessage.mimeType];
  63. // colorize
  64. thumbnailImage = [thumbnailImage imageWithTint:[Colors fontNormal]];
  65. }
  66. return thumbnailImage;
  67. }
  68. - (void)addShareButtonTo:(UIViewController *)viewController {
  69. UIBarButtonItem *shareButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(shareButtonTapped:)];
  70. viewController.navigationItem.rightBarButtonItem = shareButton;
  71. }
  72. - (ModalNavigationController *)setupNavigationControllerWithRootViewController:(UIViewController *)rootViewController {
  73. ModalNavigationController *navigationController = [[ModalNavigationController alloc] initWithRootViewController:rootViewController];
  74. navigationController.showDoneButton = YES;
  75. navigationController.dismissOnTapOutside = YES;
  76. navigationController.showFullScreenOnIPad = YES;
  77. return navigationController;
  78. }
  79. - (void)showOn:(UIViewController *)targetViewController {
  80. if ([UTIConverter isContactMimeType:_fileMessage.mimeType]) {
  81. [self checkContactAccessFor:targetViewController];
  82. }
  83. else if ([UTIConverter isAudioMimeType:_fileMessage.mimeType]) {
  84. MDMSetup *mdmSetup = [[MDMSetup alloc] initWithSetup:false];
  85. if ([mdmSetup disableShareMedia] == false) {
  86. [self showUsingDocumentInteractionControllerOn:targetViewController];
  87. } else {
  88. [self showUsingQuickLookPreviewOn:targetViewController];
  89. }
  90. }
  91. else if ([UTIConverter isPassMimeType:_fileMessage.mimeType]) {
  92. [self showUsingAddPassesControllerOn:targetViewController];
  93. }
  94. else {
  95. [self showUsingQuickLookPreviewOn:targetViewController];
  96. }
  97. }
  98. - (void)checkContactAccessFor:(UIViewController *)targetViewController {
  99. if ([CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts] != CNAuthorizationStatusAuthorized) {
  100. CNContactStore *cnAddressBook = [CNContactStore new];
  101. [cnAddressBook requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {
  102. if (granted == YES) {
  103. dispatch_async(dispatch_get_main_queue(), ^{
  104. [self showContactOn:targetViewController];
  105. });
  106. } else {
  107. dispatch_async(dispatch_get_main_queue(), ^{
  108. [UIAlertTemplate showAlertWithOwner:[[AppDelegate sharedAppDelegate] currentTopViewController] title:NSLocalizedString(@"no_contacts_permission_title", nil) message:NSLocalizedString(@"no_contacts_permission_message", nil) actionOk:nil];
  109. });
  110. }
  111. }];
  112. } else {
  113. [self showContactOn:targetViewController];
  114. }
  115. }
  116. - (void)showContactOn:(UIViewController *)targetViewController {
  117. UIViewController *personViewController = [ContactUtil getContactViewControllerForVCardData:_fileMessage.data.data];
  118. _previewBaseController = [self setupNavigationControllerWithRootViewController:personViewController];
  119. [self addShareButtonTo:personViewController];
  120. [targetViewController presentViewController:_previewBaseController animated:YES completion:nil];
  121. }
  122. - (void)showUsingDocumentInteractionControllerOn:(UIViewController *)targetViewController {
  123. NSString *filename = [FileUtility getTemporarySendableFileNameWithBase:@"file"];
  124. _tmpFileUrl = [_fileMessage tmpURL:filename];
  125. [_fileMessage exportDataToURL:_tmpFileUrl];
  126. _previewBaseController = targetViewController;
  127. UIDocumentInteractionController *dc = [UIDocumentInteractionController interactionControllerWithURL:_tmpFileUrl];
  128. dc.delegate = self;
  129. if ([dc presentPreviewAnimated:YES] == NO) {
  130. [self showNoPreviewAvailableOn:targetViewController];
  131. }
  132. }
  133. - (UIViewController*)noPreviewAvailableViewController {
  134. UIViewController *viewController = [[UIViewController alloc] init];
  135. [self addThumbnailViewTo:viewController.view];
  136. MDMSetup *mdmSetup = [[MDMSetup alloc] initWithSetup:false];
  137. if ([mdmSetup disableShareMedia] == false) {
  138. [self addShareButtonTo:viewController];
  139. }
  140. return viewController;
  141. }
  142. - (void)showNoPreviewAvailableOn:(UIViewController *)targetViewController {
  143. _previewBaseController = [self setupNavigationControllerWithRootViewController:[self noPreviewAvailableViewController]];
  144. [targetViewController presentViewController:_previewBaseController animated:YES completion:nil];
  145. }
  146. - (void)showUsingQuickLookPreviewOn:(UIViewController *)targetViewController {
  147. NSString *filename = [FileUtility getTemporarySendableFileNameWithBase:@"file"];
  148. _tmpFileUrl = [_fileMessage tmpURL:filename];
  149. [_fileMessage exportDataToURL:_tmpFileUrl];
  150. ThreemaQLPreviewController *previewController = [[ThreemaQLPreviewController alloc] init];
  151. previewController.dataSource = self;
  152. if (![ThreemaQLPreviewController canPreviewItem:[self previewController:previewController previewItemAtIndex:0]]) {
  153. [[NSFileManager defaultManager] removeItemAtURL:_tmpFileUrl error:nil];
  154. _tmpFileUrl = nil;
  155. _previewBaseController = [self setupNavigationControllerWithRootViewController:[self noPreviewAvailableViewController]];
  156. } else {
  157. ModalNavigationController *modalNavController = [self setupNavigationControllerWithRootViewController:previewController];
  158. modalNavController.modalDelegate = self;
  159. _previewBaseController = modalNavController;
  160. }
  161. [targetViewController presentViewController:_previewBaseController animated:YES completion:^{}];
  162. }
  163. - (void)showUsingAddPassesControllerOn:(UIViewController *)targetViewController {
  164. NSError *error = nil;
  165. PKPass *pass = [[PKPass alloc] initWithData:_fileMessage.data.data error:&error];
  166. if (pass != nil) {
  167. PKAddPassesViewController *addPassVc = [[PKAddPassesViewController alloc] initWithPass:pass];
  168. [targetViewController presentViewController:addPassVc animated:YES completion:nil];
  169. } else {
  170. [UIAlertTemplate showAlertWithOwner:[[AppDelegate sharedAppDelegate] currentTopViewController] title:error.localizedDescription message:error.localizedFailureReason actionOk:nil];
  171. }
  172. }
  173. - (void)shareButtonTapped:(UIBarButtonItem *)sender {
  174. UIActivityViewController *activityViewController = [ActivityUtil activityViewControllerForMessage:_fileMessage withView:_previewBaseController.view andBarButtonItem:sender];
  175. NSUserDefaults *defaults = [AppGroup userDefaults];
  176. [defaults setDouble:[Utils systemUptime] forKey:@"UIActivityViewControllerOpenTime"];
  177. [defaults synchronize];
  178. [activityViewController setCompletionWithItemsHandler:^(UIActivityType _Nullable activityType, BOOL completed, NSArray * _Nullable returnedItems, NSError * _Nullable activityError) {
  179. NSUserDefaults *defaults = [AppGroup userDefaults];
  180. [defaults removeObjectForKey:@"UIActivityViewControllerOpenTime"];
  181. }];
  182. [ModalPresenter present:activityViewController on:_previewBaseController fromBarButton:sender];
  183. }
  184. - (void)addThumbnailViewTo:(UIView *)view {
  185. FileMessagePreviewUnsupportedTypeView *fileView = [FileMessagePreviewUnsupportedTypeView fileMessagePreviewUnsupportedTypeView];
  186. fileView.fileMessage = _fileMessage;
  187. [view addSubview:fileView];
  188. view.backgroundColor = fileView.backgroundColor;
  189. fileView.frame = [RectUtil rect:fileView.frame centerIn:view.frame round:YES];
  190. }
  191. - (void)willDismissModalNavigationController {
  192. if (_tmpFileUrl) {
  193. [[NSFileManager defaultManager] removeItemAtURL:_tmpFileUrl error:nil];
  194. _tmpFileUrl = nil;
  195. }
  196. }
  197. #pragma mark - QLPreviewControllerDataSource
  198. - (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller {
  199. return 1;
  200. }
  201. - (id<QLPreviewItem>)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index {
  202. return _tmpFileUrl;
  203. }
  204. #pragma mark - UIDocumentInteractionControllerDelegate
  205. - (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller {
  206. return _previewBaseController;
  207. }
  208. - (void)documentInteractionControllerDidEndPreview:(UIDocumentInteractionController *)controller {
  209. if (_tmpFileUrl) {
  210. [[NSFileManager defaultManager] removeItemAtURL:_tmpFileUrl error:nil];
  211. _tmpFileUrl = nil;
  212. }
  213. }
  214. @end