mirror of https://github.com/oxen-io/session-ios
Merge branch 'charlesmchen/saeScreenLock' into release/2.24.0
commit
83a9c1e9eb
@ -0,0 +1,29 @@
|
||||
//
|
||||
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
|
||||
//
|
||||
|
||||
typedef NS_ENUM(NSUInteger, ScreenLockUIState) {
|
||||
ScreenLockUIStateNone,
|
||||
// Shown while app is inactive or background, if enabled.
|
||||
ScreenLockUIStateScreenProtection,
|
||||
// Shown while app is active, if enabled.
|
||||
ScreenLockUIStateScreenLock,
|
||||
};
|
||||
|
||||
NSString *NSStringForScreenLockUIState(ScreenLockUIState value);
|
||||
|
||||
@protocol ScreenLockViewDelegate <NSObject>
|
||||
|
||||
- (void)unlockButtonWasTapped;
|
||||
|
||||
@end
|
||||
|
||||
#pragma mark -
|
||||
|
||||
@interface ScreenLockViewController : UIViewController
|
||||
|
||||
@property (nonatomic, weak) id<ScreenLockViewDelegate> delegate;
|
||||
|
||||
- (void)updateUIWithState:(ScreenLockUIState)uiState isLogoAtTop:(BOOL)isLogoAtTop animated:(BOOL)animated;
|
||||
|
||||
@end
|
@ -0,0 +1,139 @@
|
||||
//
|
||||
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
|
||||
//
|
||||
|
||||
#import "ScreenLockViewController.h"
|
||||
#import "UIColor+OWS.h"
|
||||
#import "UIFont+OWS.h"
|
||||
#import "UIView+OWS.h"
|
||||
#import <SignalMessaging/SignalMessaging-Swift.h>
|
||||
|
||||
NSString *NSStringForScreenLockUIState(ScreenLockUIState value)
|
||||
{
|
||||
switch (value) {
|
||||
case ScreenLockUIStateNone:
|
||||
return @"ScreenLockUIStateNone";
|
||||
case ScreenLockUIStateScreenProtection:
|
||||
return @"ScreenLockUIStateScreenProtection";
|
||||
case ScreenLockUIStateScreenLock:
|
||||
return @"ScreenLockUIStateScreenLock";
|
||||
}
|
||||
}
|
||||
|
||||
@interface ScreenLockViewController ()
|
||||
|
||||
@property (nonatomic) UIView *screenBlockingImageView;
|
||||
@property (nonatomic) UIView *screenBlockingButton;
|
||||
@property (nonatomic) NSArray<NSLayoutConstraint *> *screenBlockingConstraints;
|
||||
@property (nonatomic) NSString *screenBlockingSignature;
|
||||
|
||||
@end
|
||||
|
||||
#pragma mark -
|
||||
|
||||
@implementation ScreenLockViewController
|
||||
|
||||
- (void)loadView
|
||||
{
|
||||
[super loadView];
|
||||
|
||||
self.view.backgroundColor = UIColor.ows_materialBlueColor;
|
||||
|
||||
UIView *edgesView = [UIView containerView];
|
||||
[self.view addSubview:edgesView];
|
||||
[edgesView autoPinEdgeToSuperviewEdge:ALEdgeTop];
|
||||
[edgesView autoPinEdgeToSuperviewEdge:ALEdgeBottom];
|
||||
[edgesView autoPinWidthToSuperview];
|
||||
|
||||
UIImage *image = [UIImage imageNamed:@"logoSignal"];
|
||||
UIImageView *imageView = [UIImageView new];
|
||||
imageView.image = image;
|
||||
[edgesView addSubview:imageView];
|
||||
[imageView autoHCenterInSuperview];
|
||||
|
||||
const CGSize screenSize = UIScreen.mainScreen.bounds.size;
|
||||
const CGFloat shortScreenDimension = MIN(screenSize.width, screenSize.height);
|
||||
const CGFloat imageSize = round(shortScreenDimension / 3.f);
|
||||
[imageView autoSetDimension:ALDimensionWidth toSize:imageSize];
|
||||
[imageView autoSetDimension:ALDimensionHeight toSize:imageSize];
|
||||
|
||||
const CGFloat kButtonHeight = 40.f;
|
||||
OWSFlatButton *button =
|
||||
[OWSFlatButton buttonWithTitle:NSLocalizedString(@"SCREEN_LOCK_UNLOCK_SIGNAL",
|
||||
@"Label for button on lock screen that lets users unlock Signal.")
|
||||
font:[OWSFlatButton fontForHeight:kButtonHeight]
|
||||
titleColor:[UIColor ows_materialBlueColor]
|
||||
backgroundColor:[UIColor whiteColor]
|
||||
target:self
|
||||
selector:@selector(showUnlockUI)];
|
||||
[edgesView addSubview:button];
|
||||
|
||||
[button autoSetDimension:ALDimensionHeight toSize:kButtonHeight];
|
||||
[button autoPinLeadingToSuperviewMarginWithInset:50.f];
|
||||
[button autoPinTrailingToSuperviewMarginWithInset:50.f];
|
||||
const CGFloat kVMargin = 65.f;
|
||||
[button autoPinBottomToSuperviewMarginWithInset:kVMargin];
|
||||
|
||||
self.screenBlockingImageView = imageView;
|
||||
self.screenBlockingButton = button;
|
||||
}
|
||||
|
||||
// The "screen blocking" window has three possible states:
|
||||
//
|
||||
// * "Just a logo". Used when app is launching and in app switcher. Must match the "Launch Screen"
|
||||
// storyboard pixel-for-pixel.
|
||||
// * "Screen Lock, local auth UI presented". Move the Signal logo so that it is visible.
|
||||
// * "Screen Lock, local auth UI not presented". Move the Signal logo so that it is visible,
|
||||
// show "unlock" button.
|
||||
- (void)updateUIWithState:(ScreenLockUIState)uiState isLogoAtTop:(BOOL)isLogoAtTop animated:(BOOL)animated
|
||||
{
|
||||
OWSAssertIsOnMainThread();
|
||||
|
||||
BOOL shouldShowBlockWindow = uiState != ScreenLockUIStateNone;
|
||||
BOOL shouldHaveScreenLock = uiState == ScreenLockUIStateScreenLock;
|
||||
|
||||
self.screenBlockingImageView.hidden = !shouldShowBlockWindow;
|
||||
|
||||
NSString *signature = [NSString stringWithFormat:@"%d %d", shouldHaveScreenLock, isLogoAtTop];
|
||||
if ([NSObject isNullableObject:self.screenBlockingSignature equalTo:signature]) {
|
||||
// Skip redundant work to avoid interfering with ongoing animations.
|
||||
return;
|
||||
}
|
||||
|
||||
[NSLayoutConstraint deactivateConstraints:self.screenBlockingConstraints];
|
||||
|
||||
NSMutableArray<NSLayoutConstraint *> *screenBlockingConstraints = [NSMutableArray new];
|
||||
|
||||
self.screenBlockingButton.hidden = !shouldHaveScreenLock;
|
||||
|
||||
if (isLogoAtTop) {
|
||||
const CGFloat kVMargin = 60.f;
|
||||
[screenBlockingConstraints addObject:[self.screenBlockingImageView autoPinEdge:ALEdgeTop
|
||||
toEdge:ALEdgeTop
|
||||
ofView:self.view
|
||||
withOffset:kVMargin]];
|
||||
} else {
|
||||
[screenBlockingConstraints addObject:[self.screenBlockingImageView autoVCenterInSuperview]];
|
||||
}
|
||||
|
||||
self.screenBlockingConstraints = screenBlockingConstraints;
|
||||
self.screenBlockingSignature = signature;
|
||||
|
||||
if (animated) {
|
||||
[UIView animateWithDuration:0.35f
|
||||
animations:^{
|
||||
[self.view layoutIfNeeded];
|
||||
}];
|
||||
} else {
|
||||
[self.view layoutIfNeeded];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)showUnlockUI
|
||||
{
|
||||
OWSAssertIsOnMainThread();
|
||||
|
||||
[self.delegate unlockButtonWasTapped];
|
||||
}
|
||||
|
||||
@end
|
@ -0,0 +1,18 @@
|
||||
//
|
||||
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
|
||||
//
|
||||
|
||||
#import "OWSViewController.h"
|
||||
#import <SignalMessaging/ScreenLockViewController.h>
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@protocol ShareViewDelegate;
|
||||
|
||||
@interface SAEScreenLockViewController : ScreenLockViewController
|
||||
|
||||
- (instancetype)initWithShareViewDelegate:(id<ShareViewDelegate>)shareViewDelegate;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
@ -0,0 +1,185 @@
|
||||
//
|
||||
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
|
||||
//
|
||||
|
||||
#import "SAEScreenLockViewController.h"
|
||||
#import "UIColor+OWS.h"
|
||||
#import <SignalMessaging/SignalMessaging-Swift.h>
|
||||
#import <SignalServiceKit/AppContext.h>
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@interface SAEScreenLockViewController () <ScreenLockViewDelegate>
|
||||
|
||||
@property (nonatomic, readonly, weak) id<ShareViewDelegate> shareViewDelegate;
|
||||
|
||||
@property (nonatomic) BOOL hasShownAuthUIOnce;
|
||||
|
||||
@property (nonatomic) BOOL isShowingAuthUI;
|
||||
|
||||
@end
|
||||
|
||||
#pragma mark -
|
||||
|
||||
@implementation SAEScreenLockViewController
|
||||
|
||||
- (instancetype)initWithShareViewDelegate:(id<ShareViewDelegate>)shareViewDelegate
|
||||
{
|
||||
self = [super init];
|
||||
if (!self) {
|
||||
return self;
|
||||
}
|
||||
|
||||
_shareViewDelegate = shareViewDelegate;
|
||||
|
||||
self.delegate = self;
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)loadView
|
||||
{
|
||||
[super loadView];
|
||||
|
||||
self.view.backgroundColor = [UIColor ows_materialBlueColor];
|
||||
|
||||
self.title = NSLocalizedString(@"SHARE_EXTENSION_VIEW_TITLE", @"Title for the 'share extension' view.");
|
||||
|
||||
self.navigationItem.leftBarButtonItem =
|
||||
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemStop
|
||||
target:self
|
||||
action:@selector(dismissPressed:)];
|
||||
}
|
||||
|
||||
- (void)viewWillAppear:(BOOL)animated
|
||||
{
|
||||
[super viewWillAppear:animated];
|
||||
|
||||
[self ensureUI];
|
||||
}
|
||||
|
||||
- (void)viewDidAppear:(BOOL)animated
|
||||
{
|
||||
[super viewDidAppear:animated];
|
||||
|
||||
[self ensureUI];
|
||||
|
||||
// Auto-show the auth UI f
|
||||
if (!self.hasShownAuthUIOnce) {
|
||||
self.hasShownAuthUIOnce = YES;
|
||||
|
||||
[self tryToPresentAuthUIToUnlockScreenLock];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)dealloc
|
||||
{
|
||||
// Surface memory leaks by logging the deallocation of view controllers.
|
||||
DDLogVerbose(@"Dealloc: %@", self.class);
|
||||
}
|
||||
|
||||
- (void)tryToPresentAuthUIToUnlockScreenLock
|
||||
{
|
||||
OWSAssertIsOnMainThread();
|
||||
|
||||
if (self.isShowingAuthUI) {
|
||||
// We're already showing the auth UI; abort.
|
||||
return;
|
||||
}
|
||||
DDLogInfo(@"%@, try to unlock screen lock", self.logTag);
|
||||
|
||||
self.isShowingAuthUI = YES;
|
||||
|
||||
[OWSScreenLock.sharedManager tryToUnlockScreenLockWithSuccess:^{
|
||||
OWSAssertIsOnMainThread();
|
||||
|
||||
DDLogInfo(@"%@ unlock screen lock succeeded.", self.logTag);
|
||||
|
||||
self.isShowingAuthUI = NO;
|
||||
|
||||
[self.shareViewDelegate shareViewWasUnlocked];
|
||||
}
|
||||
failure:^(NSError *error) {
|
||||
OWSAssertIsOnMainThread();
|
||||
|
||||
DDLogInfo(@"%@ unlock screen lock failed.", self.logTag);
|
||||
|
||||
self.isShowingAuthUI = NO;
|
||||
|
||||
[self ensureUI];
|
||||
|
||||
[self showScreenLockFailureAlertWithMessage:error.localizedDescription];
|
||||
}
|
||||
unexpectedFailure:^(NSError *error) {
|
||||
OWSAssertIsOnMainThread();
|
||||
|
||||
DDLogInfo(@"%@ unlock screen lock unexpectedly failed.", self.logTag);
|
||||
|
||||
self.isShowingAuthUI = NO;
|
||||
|
||||
// Local Authentication isn't working properly.
|
||||
// This isn't covered by the docs or the forums but in practice
|
||||
// it appears to be effective to retry again after waiting a bit.
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
[self ensureUI];
|
||||
});
|
||||
}
|
||||
cancel:^{
|
||||
OWSAssertIsOnMainThread();
|
||||
|
||||
DDLogInfo(@"%@ unlock screen lock cancelled.", self.logTag);
|
||||
|
||||
self.isShowingAuthUI = NO;
|
||||
|
||||
[self ensureUI];
|
||||
}];
|
||||
|
||||
[self ensureUI];
|
||||
}
|
||||
|
||||
- (void)ensureUI
|
||||
{
|
||||
[self updateUIWithState:ScreenLockUIStateScreenLock isLogoAtTop:NO animated:NO];
|
||||
}
|
||||
|
||||
- (void)showScreenLockFailureAlertWithMessage:(NSString *)message
|
||||
{
|
||||
OWSAssertIsOnMainThread();
|
||||
|
||||
[OWSAlerts showAlertWithTitle:NSLocalizedString(@"SCREEN_LOCK_UNLOCK_FAILED",
|
||||
@"Title for alert indicating that screen lock could not be unlocked.")
|
||||
message:message
|
||||
buttonTitle:nil
|
||||
buttonAction:^(UIAlertAction *action) {
|
||||
// After the alert, update the UI.
|
||||
[self ensureUI];
|
||||
}
|
||||
fromViewController:self];
|
||||
}
|
||||
|
||||
- (void)dismissPressed:(id)sender
|
||||
{
|
||||
DDLogDebug(@"%@ tapped dismiss share button", self.logTag);
|
||||
|
||||
[self cancelShareExperience];
|
||||
}
|
||||
|
||||
- (void)cancelShareExperience
|
||||
{
|
||||
[self.shareViewDelegate shareViewWasCancelled];
|
||||
}
|
||||
|
||||
#pragma mark - ScreenLockViewDelegate
|
||||
|
||||
- (void)unlockButtonWasTapped
|
||||
{
|
||||
OWSAssertIsOnMainThread();
|
||||
|
||||
DDLogInfo(@"%@ unlockButtonWasTapped", self.logTag);
|
||||
|
||||
[self tryToPresentAuthUIToUnlockScreenLock];
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
Loading…
Reference in New Issue