From 3850ca29b04d71e732cab7cdbdeffb30b5dc3e79 Mon Sep 17 00:00:00 2001 From: Michael Kirk Date: Wed, 20 Mar 2019 14:45:43 -0700 Subject: [PATCH] Bigger hack to fix problem with lesser hack. There were two symptoms to this bad "leave app while dismissing keyboard" state... The first, most noticeable symptom was that the main window no longer respected the device orientation. This was caused by UIKit temporarily disabling autorotate during an interactive keyboard dismissal, and not cleaning up after itself when we hid the window mid dismissal due to our screen protection feature. This was solved previously in: ca0a555f8 The second symptom remained, and is solved by this commit. Wherein after getting in this bad state, the interactive keyboard dismiss function behaves oddly. Normally when interactively dismissing the keyboard in a scroll view, the keyboard top follows your finger, until you lift up your finger, at which point, depending on how close you are to the bottom, the keyboard should completely dismiss, or cancel and return to its fully popped position. In the degraded state, the keyboard would follow your finger, but when you lifted your finger, it would stay where your finger left it, it would not complete/cancel the dismiss. The solution is, instead of only re-enabling autorotate, to use a higher level private method which is called upon complete/cancellation of the interactive dismissal. The method, `UIScrollToDismissSupport#finishScrollViewTransition`, as well as re-enabling autorotate, does some other work to restore the UI to it's normal post interactive-keyboard-dismiss gesture state. For posterity here's the decompiled pseudocode: ``` /* @class UIScrollToDismissSupport */ -(void)finishScrollViewTransition { *(int8_t *)&self->_scrollViewTransitionFinishing = 0x0; [self->_controller setInterfaceAutorotationDisabled:0x0]; [self hideScrollViewHorizontalScrollIndicator:0x0]; ebx = *ivar_offset(_scrollViewNotificationInfo); [*(self + ebx) release]; *(self + ebx) = 0x0; esi = *ivar_offset(_scrollViewForTransition); [*(self + esi) release]; *(self + esi) = 0x0; return; } ``` --- .../ConversationViewController.m | 4 +- SignalMessaging/utils/OWSWindowManager.m | 59 +++++++++++++++---- 2 files changed, 49 insertions(+), 14 deletions(-) diff --git a/Signal/src/ViewControllers/ConversationView/ConversationViewController.m b/Signal/src/ViewControllers/ConversationView/ConversationViewController.m index 3e0728763..2de12e297 100644 --- a/Signal/src/ViewControllers/ConversationView/ConversationViewController.m +++ b/Signal/src/ViewControllers/ConversationView/ConversationViewController.m @@ -4986,7 +4986,9 @@ typedef enum : NSUInteger { // in the content of this view. It's easier to dismiss the // "message actions" window when the device changes orientation // than to try to ensure this works in that case. - [self dismissMenuActions]; + if (OWSWindowManager.sharedManager.isPresentingMenuActions) { + [self dismissMenuActions]; + } // Snapshot the "last visible row". NSIndexPath *_Nullable lastVisibleIndexPath = self.lastVisibleIndexPath; diff --git a/SignalMessaging/utils/OWSWindowManager.m b/SignalMessaging/utils/OWSWindowManager.m index f85485492..5f157fdf3 100644 --- a/SignalMessaging/utils/OWSWindowManager.m +++ b/SignalMessaging/utils/OWSWindowManager.m @@ -681,27 +681,60 @@ const UIWindowLevel UIWindowLevel_MessageActions(void) IMP imp1 = [self.rootWindow methodForSelector:selector1]; BOOL (*func1)(id, SEL) = (void *)imp1; BOOL isDisabled = func1(self.rootWindow, selector1); - OWSLogInfo(@"autorotation is disabled: %d", isDisabled); if (isDisabled) { - // NSString *encodedSelectorString2 = @"endDisablingInterfaceAutorotation".encodedForSelector; - NSString *encodedSelectorString2 = @"dgB1VXoFcnN9egB4WgAGdgR3cnR2UgcGAQQBBnIGegEA"; - NSString *selectorString2 = encodedSelectorString2.decodedForSelector; - if (selectorString2 == nil) { - OWSFailDebug(@"selectorString2 was unexpectedly nil"); + OWSLogInfo(@"autorotation is disabled."); + + // The remainder of this method calls: + // [[UIScrollToDismissSupport supportForScreen:UIScreen.main] finishScrollViewTransition] + // after verifying the methods/classes exist. + + // NSString *encodedKlassString = @"UIScrollToDismissSupport".encodedForSelector; + NSString *encodedKlassString = @"ZlpkdAQBfX1lAVV6BX56BQVkBwICAQQG"; + NSString *_Nullable klassString = encodedKlassString.decodedForSelector; + if (klassString == nil) { + OWSFailDebug(@"klassString was unexpectedly nil"); + return; + } + id klass = NSClassFromString(klassString); + if (klass == nil) { + OWSFailDebug(@"klass was unexpectedly nil"); return; } - SEL selector2 = NSSelectorFromString(selectorString2); - if (![self.rootWindow respondsToSelector:selector2]) { - OWSFailDebug(@"failure: doesn't respond to selector2"); + // NSString *encodedSelector2String = @"supportForScreen:".encodedForSelector; + NSString *encodedSelector2String = @"BQcCAgEEBlcBBGR0BHZ2AEs="; + NSString *_Nullable selector2String = encodedSelector2String.decodedForSelector; + if (selector2String == nil) { + OWSFailDebug(@"selector2String was unexpectedly nil"); + return; + } + SEL selector2 = NSSelectorFromString(selector2String); + if (![klass respondsToSelector:selector2]) { + OWSFailDebug(@"klass didn't respond to selector"); + return; + } + IMP imp2 = [klass methodForSelector:selector2]; + id (*func2)(id, SEL, UIScreen *) = (void *)imp2; + id dismissSupport = func2(klass, selector2, UIScreen.mainScreen); + + // NSString *encodedSelector3String = @"finishScrollViewTransition".encodedForSelector; + NSString *encodedSelector3String = @"d3oAegV5ZHQEAX19Z3p2CWUEcgAFegZ6AQA="; + NSString *_Nullable selector3String = encodedSelector3String.decodedForSelector; + if (selector3String == nil) { + OWSFailDebug(@"selector3String was unexpectedly nil"); + return; + } + SEL selector3 = NSSelectorFromString(selector3String); + if (![dismissSupport respondsToSelector:selector3]) { + OWSFailDebug(@"dismissSupport didn't respond to selector"); return; } + IMP imp3 = [dismissSupport methodForSelector:selector3]; + void (*func3)(id, SEL) = (void *)imp3; + func3(dismissSupport, selector3); - IMP imp2 = [self.rootWindow methodForSelector:selector2]; - void (*func2)(id, SEL) = (void *)imp2; - func2(self.rootWindow, selector2); - OWSLogInfo(@"re-enabling autorotation"); + OWSLogInfo(@"finished scrollView transition"); } }