Skip to content

Commit

Permalink
Fix WhatsApp lowres image by decrypting the highres image
Browse files Browse the repository at this point in the history
  • Loading branch information
vkedwardli committed Aug 7, 2022
1 parent de4dd1a commit 9cce657
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 27 deletions.
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

0 comments on commit 9cce657

Please sign in to comment.