MessageActivityItem.m 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 "MessageActivityItem.h"
  21. #import "TextMessage.h"
  22. #import "ImageMessage.h"
  23. #import "VideoMessage.h"
  24. #import "AudioMessage.h"
  25. #import "FileMessage.h"
  26. #import "UTIConverter.h"
  27. #ifdef DEBUG
  28. static const DDLogLevel ddLogLevel = DDLogLevelVerbose;
  29. #else
  30. static const DDLogLevel ddLogLevel = DDLogLevelWarning;
  31. #endif
  32. @interface MessageActivityItem ()
  33. @property BaseMessage *message;
  34. @property NSURL *url;
  35. @property BOOL didExportData;
  36. @end
  37. @implementation MessageActivityItem
  38. + (instancetype)activityItemFor:(BaseMessage *)message {
  39. return [[MessageActivityItem alloc] initWith: message];
  40. }
  41. - (instancetype)initWith:(BaseMessage *)message
  42. {
  43. self = [super init];
  44. if (self) {
  45. self.message = message;
  46. self.didExportData = NO;
  47. }
  48. return self;
  49. }
  50. - (NSURL *)dataUrl {
  51. if ([_message isKindOfClass:[AudioMessage class]]) {
  52. return [self audioUrl];
  53. } else if ([_message isKindOfClass:[ImageMessage class]]) {
  54. return [self imageUrl];
  55. } else if ([_message isKindOfClass:[VideoMessage class]]) {
  56. return [self videoUrl];
  57. } else if ([_message isKindOfClass:[FileMessage class]]) {
  58. NSString *filename = [FileUtility getTemporaryFileName];
  59. return [((FileMessage *)_message) tmpURL:filename];
  60. }
  61. return nil;
  62. }
  63. - (NSURL *)tmpShareDirUrl {
  64. NSURL *tmpDirUrl = [NSURL fileURLWithPath:NSTemporaryDirectory() isDirectory:YES];
  65. return [tmpDirUrl URLByAppendingPathComponent:SHARE_FILE_PREFIX];
  66. }
  67. - (NSURL *)audioUrl {
  68. return [[self tmpShareDirUrl] URLByAppendingPathExtension: MEDIA_EXTENSION_AUDIO];
  69. }
  70. - (NSURL *)imageUrl {
  71. return [[self tmpShareDirUrl] URLByAppendingPathExtension:MEDIA_EXTENSION_IMAGE];
  72. }
  73. - (NSURL *)videoUrl {
  74. return [[self tmpShareDirUrl] URLByAppendingPathExtension: MEDIA_EXTENSION_VIDEO];
  75. }
  76. - (void)exportDataToUrl {
  77. if (_didExportData) {
  78. return;
  79. }
  80. _didExportData = YES;
  81. if ([_message isKindOfClass:[AudioMessage class]]) {
  82. AudioMessage *audioMessage = (AudioMessage*)self.message;
  83. [audioMessage.audio.data writeToURL:_url atomically:NO];
  84. } else if ([_message isKindOfClass:[ImageMessage class]]) {
  85. ImageMessage *imageMessage = (ImageMessage*)self.message;
  86. // strip image metadata
  87. UIImage *image = imageMessage.image.uiImage;
  88. NSData *imageData = UIImageJPEGRepresentation(image, kJPEGCompressionQuality);
  89. [imageData writeToURL:_url atomically:NO];
  90. } else if ([_message isKindOfClass:[VideoMessage class]]) {
  91. VideoMessage *videoMessage = (VideoMessage*)self.message;
  92. [videoMessage.video.data writeToURL:_url atomically:NO];
  93. } else if ([_message isKindOfClass:[FileMessage class]]) {
  94. FileMessage *fileMessage = (FileMessage *)self.message;
  95. [fileMessage exportDataToURL:_url];
  96. }
  97. }
  98. - (NSURL *)getURL {
  99. if (_url == nil) {
  100. _url = [self dataUrl];
  101. }
  102. return _url;
  103. }
  104. #pragma mark - UIActivityItemSource
  105. - (id)activityViewController:(UIActivityViewController *)activityViewController itemForActivityType:(NSString *)activityType {
  106. if ([_message isKindOfClass:[TextMessage class]]) {
  107. return ((TextMessage *)_message).text;
  108. }
  109. if (_url) {
  110. [self exportDataToUrl];
  111. if ([_message isKindOfClass:[FileMessage class]] && [activityType isEqualToString:@"ch.threema.iapp.forwardMsg"]) {
  112. NSNumber *type = ((FileMessage *)_message).type;
  113. if (type == nil) {
  114. type = @0;
  115. }
  116. return @{@"url": _url, @"renderType": type};
  117. } else {
  118. return _url;
  119. }
  120. }
  121. // unsupported message type
  122. DDLogInfo(@"MessageActivityItem: unsupported message type");
  123. return nil;
  124. }
  125. - (id)activityViewControllerPlaceholderItem:(UIActivityViewController *)activityViewController {
  126. if ([_message isKindOfClass:[TextMessage class]]) {
  127. return ((TextMessage *)_message).text;
  128. }
  129. // don't try to return thumbnail image, it won't work
  130. _url = [self dataUrl];
  131. // some activities only appear in menu if file is exported at this point in time
  132. // that's ok since files will be deleted after activity controller finishes (ActivityUtil)
  133. [self exportDataToUrl];
  134. return _url;
  135. }
  136. - (NSString *)activityViewController:(UIActivityViewController *)activityViewController dataTypeIdentifierForActivityType:(NSString *)activityType {
  137. if ([_message isKindOfClass:[TextMessage class]]) {
  138. return UTTYPE_PLAIN_TEXT;
  139. } else if ([_message respondsToSelector:@selector(blobGetUTI)]) {
  140. return [((id<BlobData>)self.message) blobGetUTI];
  141. }
  142. return nil;
  143. }
  144. @end