Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix WhatsApp lowres image by waiting it to decrypt the highres image #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 60 additions & 27 deletions Tweak.xm
Original file line number Diff line number Diff line change
Expand Up @@ -1091,40 +1091,73 @@ static NSString *prefsSayNo(BBServer *server, BBBulletin *bulletin) {
((NSNumber *)dictionary[@"includeImage"]).boolValue) {
BBAttachmentMetadata *metadata = bulletin.primaryAttachment;
if (metadata && metadata.type == 1) { // I assume image type is 1
UIImage *image = nil;
NSURL *URL = metadata.URL;
if (URL) {
UIImage *image = [UIImage imageWithContentsOfFile:URL.path];
if (image) {
NSNumber *imageShrinkFactor = dictionary[@"imageShrinkFactor"];
if (imageShrinkFactor) {
data[@"imageShrinkFactor"] = imageShrinkFactor;
}

NSNumber *imageMaxWidth = dictionary[@"imageMaxWidth"];
NSNumber *imageMaxHeight = dictionary[@"imageMaxHeight"];
CGFloat widthShrinkFactor = 0.0;
CGFloat heightShrinkFactor = 0.0;
image = [UIImage imageWithContentsOfFile:URL.path];
}
//Fixing WhatsApp's lowres image by waiting WhatsApp to decrypt the high res image
if ([bulletin.sectionID isEqualToString:@"net.whatsapp.WhatsApp"]) {
NSData* archivedNotification = bulletin.context[@"UNBulletinContextArchivedUserNotification"];
if (archivedNotification) {
UNNotification* notification = (UNNotification*) [NSKeyedUnarchiver unarchiveTopLevelObjectWithData:archivedNotification error:nil];
if (notification && notification.request && notification.request.content && notification.request.content.userInfo) {

NSString* path = notification.request.content.userInfo[@"WAMessageNotificationMainAppPath"];
if (path) {
NSURL * highresURL = [NSURL fileURLWithPath:path];

//ignore mp4, gif, webp
if ( XEq([[highresURL pathExtension] lowercaseString], @"jpg") || XEq([[highresURL pathExtension] lowercaseString], @"jpeg") || XEq([[highresURL pathExtension] lowercaseString], @"png") ) {

//Takes 0.5 second on iPhone 5s to decrypt the image, let's wait for 4 seconds?
int retries = 20;
for(int i = 0; i < retries; i++){
if ([[NSFileManager defaultManager] fileExistsAtPath:path]){
break;
}
[NSThread sleepForTimeInterval:0.2];
}
UIImage* highresImage = [UIImage imageWithContentsOfFile:path];

if (imageMaxWidth && imageMaxWidth.floatValue > 0.0 &&
image.size.width > imageMaxWidth.floatValue) {
widthShrinkFactor = image.size.width / imageMaxWidth.floatValue;
}
if (imageMaxHeight && imageMaxHeight.floatValue > 0.0 &&
image.size.height > imageMaxHeight.floatValue) {
heightShrinkFactor = image.size.height / imageMaxHeight.floatValue;
if (highresImage) {
image = highresImage;
}
}
}
}
}
}
if (image) {
NSNumber *imageShrinkFactor = dictionary[@"imageShrinkFactor"];
if (imageShrinkFactor) {
data[@"imageShrinkFactor"] = imageShrinkFactor;
}

// if either has a value, shrink image
if (widthShrinkFactor + heightShrinkFactor > 0.0) {
// shrink with the largest factor
CGFloat shrinkFactor = widthShrinkFactor > heightShrinkFactor
? widthShrinkFactor
: heightShrinkFactor;
image = shrinkImage(image, shrinkFactor);
}
NSNumber *imageMaxWidth = dictionary[@"imageMaxWidth"];
NSNumber *imageMaxHeight = dictionary[@"imageMaxHeight"];
CGFloat widthShrinkFactor = 0.0;
CGFloat heightShrinkFactor = 0.0;

data[@"image"] = image;
if (imageMaxWidth && imageMaxWidth.floatValue > 0.0 &&
image.size.width > imageMaxWidth.floatValue) {
widthShrinkFactor = image.size.width / imageMaxWidth.floatValue;
}
if (imageMaxHeight && imageMaxHeight.floatValue > 0.0 &&
image.size.height > imageMaxHeight.floatValue) {
heightShrinkFactor = image.size.height / imageMaxHeight.floatValue;
}

// if either has a value, shrink image
if (widthShrinkFactor + heightShrinkFactor > 0.0) {
// shrink with the largest factor
CGFloat shrinkFactor = widthShrinkFactor > heightShrinkFactor
? widthShrinkFactor
: heightShrinkFactor;
image = shrinkImage(image, shrinkFactor);
}

data[@"image"] = image;
}
// give true value so even if can't figure out how to send image, can
// still tell that there is one
Expand Down
1 change: 1 addition & 0 deletions global.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ typedef enum {
#import "PSSpecifier.h"
#import <BulletinBoard/BBBulletin.h>
#import <BulletinBoard/BBSectionInfo.h> // imports BBSectionInfoSettings
#import <UserNotifications/UserNotifications.h>
#import <SpringBoard/SBApplication.h>
#import <SpringBoard/SBApplicationController.h>
#import <Preferences/PSTableCell.h>
Expand Down