2017-03-15 14:23:21 +01:00
|
|
|
//
|
|
|
|
// Copyright (c) 2017 Open Whisper Systems. All rights reserved.
|
|
|
|
//
|
2015-01-14 22:30:01 +01:00
|
|
|
|
2015-12-22 12:45:09 +01:00
|
|
|
#import "TSVideoAttachmentAdapter.h"
|
2017-03-15 14:23:21 +01:00
|
|
|
#import "AttachmentUploadView.h"
|
|
|
|
#import "JSQMediaItem+OWS.h"
|
2015-01-29 04:50:41 +01:00
|
|
|
#import "MIMETypeUtil.h"
|
2017-05-11 21:26:37 +02:00
|
|
|
#import "Signal-Swift.h"
|
2016-03-11 19:34:39 +01:00
|
|
|
#import "TSAttachmentStream.h"
|
2016-07-13 20:03:50 +02:00
|
|
|
#import "TSMessagesManager.h"
|
2015-12-22 12:45:09 +01:00
|
|
|
#import "TSStorageManager+keyingMaterial.h"
|
2017-04-29 00:00:24 +02:00
|
|
|
#import "UIColor+JSQMessages.h"
|
2017-04-20 19:43:33 +02:00
|
|
|
#import "UIColor+OWS.h"
|
2017-05-12 19:01:45 +02:00
|
|
|
#import "UIDevice+TSHardwareVersion.h"
|
2017-04-20 19:43:33 +02:00
|
|
|
#import "UIFont+OWS.h"
|
2017-04-20 15:44:25 +02:00
|
|
|
#import "UIView+OWS.h"
|
2017-04-29 00:00:24 +02:00
|
|
|
#import "ViewControllerUtils.h"
|
2016-07-13 20:03:50 +02:00
|
|
|
#import <JSQMessagesViewController/JSQMessagesMediaViewBubbleImageMasker.h>
|
2016-03-11 19:34:39 +01:00
|
|
|
#import <MobileCoreServices/MobileCoreServices.h>
|
2017-04-20 19:43:33 +02:00
|
|
|
#import <SignalServiceKit/MIMETypeUtil.h>
|
2017-04-20 15:44:25 +02:00
|
|
|
|
2017-03-29 20:49:26 +02:00
|
|
|
NS_ASSUME_NONNULL_BEGIN
|
|
|
|
|
2017-05-11 21:27:46 +02:00
|
|
|
@interface TSVideoAttachmentAdapter ()
|
2015-01-14 22:30:01 +01:00
|
|
|
|
2017-03-15 14:23:21 +01:00
|
|
|
@property (nonatomic) UIImage *image;
|
2017-04-29 00:00:24 +02:00
|
|
|
@property (nonatomic, nullable) UIView *cachedMediaView;
|
2017-03-15 14:23:21 +01:00
|
|
|
@property (nonatomic) TSAttachmentStream *attachment;
|
2017-03-29 20:49:26 +02:00
|
|
|
@property (nonatomic, nullable) UIButton *audioPlayPauseButton;
|
2017-04-29 00:00:24 +02:00
|
|
|
@property (nonatomic, nullable) UILabel *audioBottomLabel;
|
2017-05-11 21:26:37 +02:00
|
|
|
@property (nonatomic, nullable) AudioProgressView *audioProgressView;
|
2015-01-25 00:48:40 +01:00
|
|
|
@property (nonatomic) BOOL incoming;
|
2017-03-29 20:49:26 +02:00
|
|
|
@property (nonatomic, nullable) AttachmentUploadView *attachmentUploadView;
|
2017-04-20 16:06:31 +02:00
|
|
|
@property (nonatomic) BOOL isAudioPlaying;
|
2017-04-29 00:00:24 +02:00
|
|
|
@property (nonatomic) CGFloat audioProgressSeconds;
|
|
|
|
@property (nonatomic) CGFloat audioDurationSeconds;
|
2017-04-20 16:06:31 +02:00
|
|
|
@property (nonatomic) BOOL isPaused;
|
2017-04-21 20:19:30 +02:00
|
|
|
|
|
|
|
// See comments on OWSMessageMediaAdapter.
|
|
|
|
@property (nonatomic, nullable, weak) id lastPresentingCell;
|
2015-01-22 05:08:12 +01:00
|
|
|
|
2015-01-14 22:30:01 +01:00
|
|
|
@end
|
|
|
|
|
2017-03-29 20:49:26 +02:00
|
|
|
#pragma mark -
|
|
|
|
|
2015-01-14 22:30:01 +01:00
|
|
|
@implementation TSVideoAttachmentAdapter
|
|
|
|
|
2015-12-22 12:45:09 +01:00
|
|
|
- (instancetype)initWithAttachment:(TSAttachmentStream *)attachment incoming:(BOOL)incoming {
|
2015-01-22 05:08:12 +01:00
|
|
|
self = [super initWithFileURL:[attachment mediaURL] isReadyToPlay:YES];
|
|
|
|
|
2015-12-22 12:45:09 +01:00
|
|
|
if (self) {
|
2015-01-14 22:30:01 +01:00
|
|
|
_image = attachment.image;
|
2017-04-29 00:00:24 +02:00
|
|
|
_cachedMediaView = nil;
|
2015-01-14 22:30:01 +01:00
|
|
|
_attachmentId = attachment.uniqueId;
|
|
|
|
_contentType = attachment.contentType;
|
2015-01-25 00:48:40 +01:00
|
|
|
_attachment = attachment;
|
|
|
|
_incoming = incoming;
|
2015-01-14 22:30:01 +01:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2017-04-19 03:27:59 +02:00
|
|
|
- (void)clearAllViews
|
|
|
|
{
|
2017-05-11 21:26:37 +02:00
|
|
|
[self.cachedMediaView removeFromSuperview];
|
|
|
|
self.cachedMediaView = nil;
|
|
|
|
self.attachmentUploadView = nil;
|
|
|
|
self.audioProgressView = nil;
|
2017-04-19 03:27:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)clearCachedMediaViews
|
|
|
|
{
|
|
|
|
[super clearCachedMediaViews];
|
|
|
|
[self clearAllViews];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setAppliesMediaViewMaskAsOutgoing:(BOOL)appliesMediaViewMaskAsOutgoing
|
|
|
|
{
|
|
|
|
[super setAppliesMediaViewMaskAsOutgoing:appliesMediaViewMaskAsOutgoing];
|
|
|
|
[self clearAllViews];
|
|
|
|
}
|
|
|
|
|
2015-12-22 12:45:09 +01:00
|
|
|
- (BOOL)isAudio {
|
2015-01-29 04:50:41 +01:00
|
|
|
return [MIMETypeUtil isSupportedAudioMIMEType:_contentType];
|
2015-01-14 22:30:01 +01:00
|
|
|
}
|
|
|
|
|
2015-12-22 12:45:09 +01:00
|
|
|
- (BOOL)isVideo {
|
2015-01-29 04:50:41 +01:00
|
|
|
return [MIMETypeUtil isSupportedVideoMIMEType:_contentType];
|
2015-01-14 22:30:01 +01:00
|
|
|
}
|
|
|
|
|
2017-04-29 00:00:24 +02:00
|
|
|
- (void)setAudioProgress:(CGFloat)progress duration:(CGFloat)duration
|
|
|
|
{
|
|
|
|
OWSAssert([NSThread isMainThread]);
|
|
|
|
|
|
|
|
self.audioProgressSeconds = progress;
|
2017-05-11 21:26:37 +02:00
|
|
|
if (duration > 0) {
|
|
|
|
self.audioDurationSeconds = duration;
|
|
|
|
}
|
|
|
|
|
|
|
|
[self updateAudioProgressView];
|
2017-04-29 00:00:24 +02:00
|
|
|
|
|
|
|
[self updateAudioBottomLabel];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)updateAudioBottomLabel
|
|
|
|
{
|
|
|
|
if (self.isAudioPlaying && self.audioProgressSeconds > 0 && self.audioDurationSeconds > 0) {
|
2017-05-03 16:50:26 +02:00
|
|
|
self.audioBottomLabel.text =
|
|
|
|
[NSString stringWithFormat:@"%@ / %@",
|
|
|
|
[ViewControllerUtils formatDurationSeconds:(long)round(self.audioProgressSeconds)],
|
|
|
|
[ViewControllerUtils formatDurationSeconds:(long)round(self.audioDurationSeconds)]];
|
2017-04-29 00:00:24 +02:00
|
|
|
} else {
|
2017-05-11 21:26:37 +02:00
|
|
|
self.audioBottomLabel.text = [NSString
|
|
|
|
stringWithFormat:@"%@", [ViewControllerUtils formatDurationSeconds:(long)round(self.audioDurationSeconds)]];
|
2017-04-29 00:00:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-11 21:26:37 +02:00
|
|
|
- (void)setAudioIcon:(UIImage *)icon iconColor:(UIColor *)iconColor
|
2017-04-29 00:00:24 +02:00
|
|
|
{
|
2017-05-11 21:26:37 +02:00
|
|
|
icon = [icon imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
|
|
|
|
[_audioPlayPauseButton setImage:icon forState:UIControlStateNormal];
|
|
|
|
[_audioPlayPauseButton setImage:icon forState:UIControlStateDisabled];
|
|
|
|
_audioPlayPauseButton.imageView.tintColor = iconColor;
|
2017-04-29 00:00:24 +02:00
|
|
|
}
|
|
|
|
|
2015-01-25 00:48:40 +01:00
|
|
|
- (void)setAudioIconToPlay {
|
2017-05-11 21:26:37 +02:00
|
|
|
[self setAudioIcon:[UIImage imageNamed:@"audio_play_black_40"]
|
|
|
|
iconColor:[self audioColorWithOpacity:self.incoming ? 0.2f : 0.1f]];
|
2015-01-25 00:48:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setAudioIconToPause {
|
2017-05-11 21:26:37 +02:00
|
|
|
[self setAudioIcon:[UIImage imageNamed:@"audio_pause_black_40"]
|
|
|
|
iconColor:[self audioColorWithOpacity:self.incoming ? 0.2f : 0.1f]];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setIsAudioPlaying:(BOOL)isAudioPlaying
|
|
|
|
{
|
|
|
|
_isAudioPlaying = isAudioPlaying;
|
|
|
|
|
|
|
|
[self updateAudioProgressView];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)updateAudioProgressView
|
|
|
|
{
|
|
|
|
[self.audioProgressView
|
|
|
|
setProgress:(self.audioDurationSeconds > 0 ? self.audioProgressSeconds / self.audioDurationSeconds : 0.f)];
|
|
|
|
|
|
|
|
self.audioProgressView.horizontalBarColor = [self audioColorWithOpacity:0.75f];
|
|
|
|
self.audioProgressView.progressColor
|
|
|
|
= (self.isAudioPlaying ? [self audioColorWithOpacity:self.incoming ? 0.2f : 0.1f]
|
|
|
|
: [self audioColorWithOpacity:0.4f]);
|
2015-01-22 05:08:12 +01:00
|
|
|
}
|
|
|
|
|
2015-01-14 22:30:01 +01:00
|
|
|
#pragma mark - JSQMessageMediaData protocol
|
|
|
|
|
2017-05-11 21:26:37 +02:00
|
|
|
- (CGFloat)audioBubbleHeight
|
2017-04-29 00:00:24 +02:00
|
|
|
{
|
2017-05-12 15:11:25 +02:00
|
|
|
return 50.f;
|
2017-04-29 00:00:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
- (CGFloat)iconSize
|
|
|
|
{
|
|
|
|
return 40.f;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (CGFloat)vMargin
|
|
|
|
{
|
2017-05-11 21:26:37 +02:00
|
|
|
return 5.f;
|
2017-04-29 00:00:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
- (UIColor *)audioTextColor
|
|
|
|
{
|
2017-05-11 21:26:37 +02:00
|
|
|
return (self.incoming ? [UIColor colorWithWhite:0.2f alpha:1.f] : [UIColor whiteColor]);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (UIColor *)audioColorWithOpacity:(CGFloat)alpha
|
|
|
|
{
|
|
|
|
return [self.audioTextColor blendWithColor:self.bubbleBackgroundColor alpha:alpha];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (UIColor *)bubbleBackgroundColor
|
|
|
|
{
|
|
|
|
return self.incoming ? [UIColor jsq_messageBubbleLightGrayColor] : [UIColor ows_materialBlueColor];
|
2017-04-29 00:00:24 +02:00
|
|
|
}
|
|
|
|
|
2015-12-22 12:45:09 +01:00
|
|
|
- (UIView *)mediaView {
|
2015-01-22 05:08:12 +01:00
|
|
|
if ([self isVideo]) {
|
2017-04-29 00:00:24 +02:00
|
|
|
if (self.cachedMediaView == nil) {
|
2017-05-11 21:26:37 +02:00
|
|
|
self.cachedMediaView = [self createVideoMediaView];
|
2015-01-14 22:30:01 +01:00
|
|
|
}
|
2015-01-22 05:08:12 +01:00
|
|
|
} else if ([self isAudio]) {
|
2017-04-29 00:00:24 +02:00
|
|
|
if (self.cachedMediaView == nil) {
|
2017-05-11 21:26:37 +02:00
|
|
|
self.cachedMediaView = [self createAudioMediaView];
|
2017-03-15 14:23:21 +01:00
|
|
|
}
|
|
|
|
|
2017-04-20 15:44:25 +02:00
|
|
|
if (self.isAudioPlaying) {
|
|
|
|
[self setAudioIconToPause];
|
|
|
|
} else {
|
|
|
|
[self setAudioIconToPlay];
|
|
|
|
}
|
2017-03-15 14:23:21 +01:00
|
|
|
} else {
|
|
|
|
// Unknown media type.
|
|
|
|
OWSAssert(0);
|
2015-01-14 22:30:01 +01:00
|
|
|
}
|
2017-04-29 00:00:24 +02:00
|
|
|
return self.cachedMediaView;
|
2015-01-14 22:30:01 +01:00
|
|
|
}
|
|
|
|
|
2017-05-11 21:26:37 +02:00
|
|
|
- (UIView *)createVideoMediaView
|
|
|
|
{
|
|
|
|
OWSAssert([self isVideo]);
|
|
|
|
|
|
|
|
CGSize size = [self mediaViewDisplaySize];
|
|
|
|
|
|
|
|
UIImageView *imageView = [[UIImageView alloc] initWithImage:self.image];
|
|
|
|
imageView.contentMode = UIViewContentModeScaleAspectFill;
|
|
|
|
imageView.frame = CGRectMake(0.0f, 0.0f, size.width, size.height);
|
|
|
|
imageView.clipsToBounds = YES;
|
|
|
|
[JSQMessagesMediaViewBubbleImageMasker applyBubbleImageMaskToMediaView:imageView
|
|
|
|
isOutgoing:self.appliesMediaViewMaskAsOutgoing];
|
|
|
|
UIImage *img = [UIImage imageNamed:@"play_button"];
|
|
|
|
UIImageView *videoPlayButton = [[UIImageView alloc] initWithImage:img];
|
|
|
|
videoPlayButton.frame = CGRectMake((size.width / 2) - 18, (size.height / 2) - 18, 37, 37);
|
|
|
|
[imageView addSubview:videoPlayButton];
|
|
|
|
|
|
|
|
if (!_incoming) {
|
|
|
|
self.attachmentUploadView = [[AttachmentUploadView alloc] initWithAttachment:self.attachment
|
|
|
|
superview:imageView
|
|
|
|
attachmentStateCallback:^(BOOL isAttachmentReady) {
|
|
|
|
videoPlayButton.hidden = !isAttachmentReady;
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
|
|
|
return imageView;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)isVoiceMessage
|
|
|
|
{
|
|
|
|
OWSAssert([self isAudio]);
|
|
|
|
|
2017-05-12 15:22:56 +02:00
|
|
|
// We want to treat "pre-voice messages flag" messages as voice messages if
|
|
|
|
// they have no file name.
|
|
|
|
//
|
|
|
|
// TODO: Remove this after the flag has been in production for a few months.
|
2017-05-11 21:26:37 +02:00
|
|
|
return (self.attachment.isVoiceMessage || self.attachment.filename.length < 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (UIView *)createAudioMediaView
|
|
|
|
{
|
|
|
|
OWSAssert([self isAudio]);
|
|
|
|
|
|
|
|
[self ensureAudioDurationSeconds];
|
|
|
|
|
|
|
|
CGSize viewSize = [self mediaViewDisplaySize];
|
|
|
|
UIColor *textColor = [self audioTextColor];
|
|
|
|
|
|
|
|
UIView *mediaView = [[UIView alloc] initWithFrame:CGRectMake(0.f, 0.f, viewSize.width, viewSize.height)];
|
|
|
|
|
|
|
|
mediaView.backgroundColor = self.bubbleBackgroundColor;
|
|
|
|
[JSQMessagesMediaViewBubbleImageMasker applyBubbleImageMaskToMediaView:mediaView isOutgoing:!self.incoming];
|
|
|
|
|
|
|
|
const CGFloat kBubbleTailWidth = 6.f;
|
|
|
|
CGRect contentFrame = CGRectMake(self.incoming ? kBubbleTailWidth : 0.f,
|
|
|
|
self.vMargin,
|
|
|
|
viewSize.width - kBubbleTailWidth - 15,
|
|
|
|
viewSize.height - self.vMargin * 2);
|
|
|
|
|
|
|
|
CGRect iconFrame = CGRectMake((CGFloat)round(contentFrame.origin.x + 5.f),
|
|
|
|
(CGFloat)round(contentFrame.origin.y + (contentFrame.size.height - self.iconSize) * 0.5f),
|
|
|
|
self.iconSize,
|
|
|
|
self.iconSize);
|
|
|
|
_audioPlayPauseButton = [[UIButton alloc] initWithFrame:iconFrame];
|
|
|
|
_audioPlayPauseButton.enabled = NO;
|
|
|
|
[mediaView addSubview:_audioPlayPauseButton];
|
|
|
|
|
|
|
|
const CGFloat kLabelHSpacing = 3;
|
|
|
|
const CGFloat kLabelVSpacing = 2;
|
|
|
|
NSString *topText = [[self.attachment.filename stringByDeletingPathExtension]
|
|
|
|
stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
|
|
|
|
if (topText.length < 1) {
|
|
|
|
topText = [MIMETypeUtil fileExtensionForMIMEType:self.attachment.contentType].uppercaseString;
|
|
|
|
}
|
|
|
|
if (topText.length < 1) {
|
|
|
|
topText = NSLocalizedString(@"GENERIC_ATTACHMENT_LABEL", @"A label for generic attachments.");
|
|
|
|
}
|
|
|
|
if (self.isVoiceMessage) {
|
|
|
|
topText = nil;
|
|
|
|
}
|
|
|
|
UILabel *topLabel = [UILabel new];
|
|
|
|
topLabel.text = topText;
|
|
|
|
topLabel.textColor = [textColor colorWithAlphaComponent:0.85f];
|
|
|
|
topLabel.lineBreakMode = NSLineBreakByTruncatingMiddle;
|
|
|
|
topLabel.font = [UIFont ows_regularFontWithSize:ScaleFromIPhone5To7Plus(11.f, 13.f)];
|
|
|
|
[topLabel sizeToFit];
|
|
|
|
[mediaView addSubview:topLabel];
|
|
|
|
|
|
|
|
AudioProgressView *audioProgressView = [AudioProgressView new];
|
|
|
|
self.audioProgressView = audioProgressView;
|
|
|
|
[self updateAudioProgressView];
|
|
|
|
[mediaView addSubview:audioProgressView];
|
|
|
|
|
|
|
|
UILabel *bottomLabel = [UILabel new];
|
|
|
|
self.audioBottomLabel = bottomLabel;
|
|
|
|
[self updateAudioBottomLabel];
|
|
|
|
bottomLabel.textColor = [textColor colorWithAlphaComponent:0.85f];
|
|
|
|
bottomLabel.lineBreakMode = NSLineBreakByTruncatingMiddle;
|
|
|
|
bottomLabel.font = [UIFont ows_regularFontWithSize:ScaleFromIPhone5To7Plus(11.f, 13.f)];
|
|
|
|
[bottomLabel sizeToFit];
|
|
|
|
[mediaView addSubview:bottomLabel];
|
|
|
|
|
|
|
|
const CGFloat topLabelHeight = ceil(topLabel.font.lineHeight);
|
|
|
|
const CGFloat kAudioProgressViewHeight = 12.f;
|
|
|
|
const CGFloat bottomLabelHeight = ceil(bottomLabel.font.lineHeight);
|
|
|
|
CGRect labelsBounds = CGRectZero;
|
|
|
|
labelsBounds.origin.x = (CGFloat)round(iconFrame.origin.x + iconFrame.size.width + kLabelHSpacing);
|
|
|
|
labelsBounds.size.width = contentFrame.origin.x + contentFrame.size.width - labelsBounds.origin.x;
|
|
|
|
labelsBounds.size.height = topLabelHeight + kAudioProgressViewHeight + bottomLabelHeight + kLabelVSpacing * 2;
|
|
|
|
labelsBounds.origin.y
|
|
|
|
= (CGFloat)round(contentFrame.origin.y + (contentFrame.size.height - labelsBounds.size.height) * 0.5f);
|
|
|
|
|
|
|
|
topLabel.frame = CGRectMake(labelsBounds.origin.x, labelsBounds.origin.y, labelsBounds.size.width, topLabelHeight);
|
|
|
|
audioProgressView.frame = CGRectMake(labelsBounds.origin.x,
|
|
|
|
labelsBounds.origin.y + topLabelHeight + kLabelVSpacing,
|
|
|
|
labelsBounds.size.width,
|
|
|
|
kAudioProgressViewHeight);
|
|
|
|
bottomLabel.frame = CGRectMake(labelsBounds.origin.x,
|
|
|
|
labelsBounds.origin.y + topLabelHeight + kAudioProgressViewHeight + kLabelVSpacing * 2,
|
|
|
|
labelsBounds.size.width,
|
|
|
|
bottomLabelHeight);
|
|
|
|
|
|
|
|
if (!self.incoming) {
|
|
|
|
self.attachmentUploadView = [[AttachmentUploadView alloc] initWithAttachment:self.attachment
|
|
|
|
superview:mediaView
|
|
|
|
attachmentStateCallback:nil];
|
|
|
|
}
|
|
|
|
|
|
|
|
return mediaView;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)ensureAudioDurationSeconds
|
|
|
|
{
|
|
|
|
if (self.audioDurationSeconds == 0.f) {
|
|
|
|
NSError *error;
|
|
|
|
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:self.fileURL error:&error];
|
2017-05-12 16:12:06 +02:00
|
|
|
if (error && [error.domain isEqualToString:NSOSStatusErrorDomain]
|
|
|
|
&& (error.code == kAudioFileInvalidFileError || error.code == kAudioFileStreamError_InvalidFile)) {
|
|
|
|
// Ignore "invalid audio file" errors.
|
|
|
|
return;
|
|
|
|
}
|
2017-05-11 21:26:37 +02:00
|
|
|
OWSAssert(!error);
|
|
|
|
if (!error) {
|
|
|
|
self.audioDurationSeconds = (CGFloat)[audioPlayer duration];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-22 12:45:09 +01:00
|
|
|
- (CGSize)mediaViewDisplaySize {
|
2016-06-28 15:50:12 +02:00
|
|
|
CGSize size = [super mediaViewDisplaySize];
|
|
|
|
if ([self isAudio]) {
|
2017-05-12 19:01:45 +02:00
|
|
|
if ([[UIDevice currentDevice] isiPhoneVersionSixOrMore]) {
|
|
|
|
size.width *= 1.2;
|
|
|
|
}
|
2017-05-11 21:26:37 +02:00
|
|
|
size.height = (CGFloat)ceil(self.audioBubbleHeight + self.vMargin * 2);
|
2016-10-19 06:32:48 +02:00
|
|
|
} else if ([self isVideo]) {
|
|
|
|
return [self ows_adjustBubbleSize:size forImage:self.image];
|
2015-01-22 05:08:12 +01:00
|
|
|
}
|
2016-06-28 15:50:12 +02:00
|
|
|
return size;
|
2015-01-22 05:08:12 +01:00
|
|
|
}
|
|
|
|
|
2015-12-22 12:45:09 +01:00
|
|
|
- (UIView *)mediaPlaceholderView {
|
2015-01-22 05:08:12 +01:00
|
|
|
return [self mediaView];
|
|
|
|
}
|
|
|
|
|
2015-12-22 12:45:09 +01:00
|
|
|
- (NSUInteger)hash {
|
2015-01-22 05:08:12 +01:00
|
|
|
return [super hash];
|
|
|
|
}
|
|
|
|
|
2016-03-11 19:34:39 +01:00
|
|
|
#pragma mark - OWSMessageEditing Protocol
|
|
|
|
|
|
|
|
- (BOOL)canPerformEditingAction:(SEL)action
|
|
|
|
{
|
|
|
|
if ([self isVideo]) {
|
|
|
|
return (action == @selector(copy:) || action == NSSelectorFromString(@"save:"));
|
|
|
|
} else if ([self isAudio]) {
|
|
|
|
return (action == @selector(copy:));
|
|
|
|
}
|
|
|
|
|
|
|
|
NSString *actionString = NSStringFromSelector(action);
|
|
|
|
DDLogError(
|
|
|
|
@"Unexpected action: %@ for VideoAttachmentAdapter with contentType: %@", actionString, self.contentType);
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)performEditingAction:(SEL)action
|
|
|
|
{
|
|
|
|
if ([self isVideo]) {
|
|
|
|
if (action == @selector(copy:)) {
|
2017-05-08 17:13:51 +02:00
|
|
|
NSString *utiType = [MIMETypeUtil utiTypeForMIMEType:_contentType];
|
|
|
|
if (!utiType) {
|
|
|
|
OWSAssert(0);
|
|
|
|
utiType = (NSString *)kUTTypeVideo;
|
|
|
|
}
|
2016-03-11 19:34:39 +01:00
|
|
|
NSData *data = [NSData dataWithContentsOfURL:self.fileURL];
|
2017-05-08 17:13:51 +02:00
|
|
|
[UIPasteboard.generalPasteboard setData:data forPasteboardType:utiType];
|
2016-03-11 19:34:39 +01:00
|
|
|
return;
|
|
|
|
} else if (action == NSSelectorFromString(@"save:")) {
|
|
|
|
if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(self.fileURL.path)) {
|
|
|
|
UISaveVideoAtPathToSavedPhotosAlbum(self.fileURL.path, self, nil, nil);
|
|
|
|
} else {
|
|
|
|
DDLogWarn(@"cowardly refusing to save incompatible video attachment");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if ([self isAudio]) {
|
|
|
|
if (action == @selector(copy:)) {
|
2017-05-08 17:13:51 +02:00
|
|
|
NSString *utiType = [MIMETypeUtil utiTypeForMIMEType:_contentType];
|
|
|
|
if (!utiType) {
|
2017-05-08 21:14:27 +02:00
|
|
|
OWSAssert(0);
|
|
|
|
utiType = (NSString *)kUTTypeAudio;
|
2016-03-11 19:34:39 +01:00
|
|
|
}
|
2017-05-08 17:13:51 +02:00
|
|
|
|
|
|
|
NSData *data = [NSData dataWithContentsOfURL:self.fileURL];
|
|
|
|
OWSAssert(data);
|
|
|
|
[UIPasteboard.generalPasteboard setData:data forPasteboardType:utiType];
|
2016-03-11 19:34:39 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Shouldn't get here, as only supported actions should be exposed via canPerformEditingAction
|
|
|
|
NSString *actionString = NSStringFromSelector(action);
|
|
|
|
DDLogError(
|
|
|
|
@"Unexpected action: %@ for VideoAttachmentAdapter with contentType: %@", actionString, self.contentType);
|
2017-04-11 18:14:26 +02:00
|
|
|
OWSAssert(NO);
|
2016-03-11 19:34:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 03:27:59 +02:00
|
|
|
#pragma mark - OWSMessageMediaAdapter
|
|
|
|
|
|
|
|
- (void)setCellVisible:(BOOL)isVisible
|
|
|
|
{
|
|
|
|
// Ignore.
|
|
|
|
}
|
|
|
|
|
2017-04-21 20:19:30 +02:00
|
|
|
- (void)clearCachedMediaViewsIfLastPresentingCell:(id)cell
|
2017-04-21 15:56:31 +02:00
|
|
|
{
|
|
|
|
OWSAssert(cell);
|
|
|
|
|
2017-04-21 20:19:30 +02:00
|
|
|
if (cell == self.lastPresentingCell) {
|
2017-04-21 15:56:31 +02:00
|
|
|
[self clearCachedMediaViews];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-14 22:30:01 +01:00
|
|
|
@end
|
2017-03-29 20:49:26 +02:00
|
|
|
|
|
|
|
NS_ASSUME_NONNULL_END
|