element-ios/Riot/Modules/BugReport/BugReportViewController.m

500 lines
18 KiB
Mathematica
Raw Normal View History

/*
Copyright 2017 Vector Creations Ltd
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#import "BugReportViewController.h"
#import "Riot-Swift.h"
2017-04-27 08:37:39 +00:00
#import "GBDeviceInfo_iOS.h"
@interface BugReportViewController ()
2017-04-27 08:37:39 +00:00
{
MXBugReportRestClient *bugReportRestClient;
// The temporary file used to store the screenshot
NSURL *screenShotFile;
// Observe kThemeServiceDidChangeThemeNotification to handle user interface theme change.
id kThemeServiceDidChangeThemeNotificationObserver;
2017-04-27 08:37:39 +00:00
}
@property (nonatomic) BOOL sendLogs;
@property (nonatomic) BOOL sendScreenshot;
@property (nonatomic) BOOL isSendingLogs;
@property (weak, nonatomic) IBOutlet UIView *overlayView;
@end
@implementation BugReportViewController
#pragma mark - Class methods
+ (UINib *)nib
{
return [UINib nibWithNibName:NSStringFromClass([BugReportViewController class])
bundle:[NSBundle bundleForClass:[BugReportViewController class]]];
}
+ (instancetype)bugReportViewController
{
return [[[self class] alloc] initWithNibName:NSStringFromClass([BugReportViewController class])
bundle:[NSBundle bundleForClass:[BugReportViewController class]]];
}
#pragma mark -
- (void)showInViewController:(UIViewController *)viewController
{
self.providesPresentationContextTransitionStyle = YES;
self.definesPresentationContext = YES;
self.modalPresentationStyle = UIModalPresentationOverFullScreen;
self.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[viewController presentViewController:self animated:YES completion:nil];
}
- (void)viewDidLoad
{
[super viewDidLoad];
2017-04-27 08:46:30 +00:00
_logsDescriptionLabel.text = NSLocalizedStringFromTable(@"bug_report_logs_description", @"Vector", nil);
_sendLogsLabel.text = NSLocalizedStringFromTable(@"bug_report_send_logs", @"Vector", nil);
_sendScreenshotLabel.text = NSLocalizedStringFromTable(@"bug_report_send_screenshot", @"Vector", nil);
_containerView.layer.cornerRadius = 20;
_bugReportDescriptionTextView.layer.borderWidth = 1.0f;
2017-04-27 08:37:39 +00:00
_bugReportDescriptionTextView.text = nil;
_bugReportDescriptionTextView.delegate = self;
2017-04-28 12:37:29 +00:00
if (_reportCrash)
{
_titleLabel.text = NSLocalizedStringFromTable(@"bug_crash_report_title", @"Vector", nil);
_descriptionLabel.text = NSLocalizedStringFromTable(@"bug_crash_report_description", @"Vector", nil);
}
else
{
_titleLabel.text = NSLocalizedStringFromTable(@"bug_report_title", @"Vector", nil);
_descriptionLabel.text = NSLocalizedStringFromTable(@"bug_report_description", @"Vector", nil);
}
[_cancelButton setTitle:[NSBundle mxk_localizedStringForKey:@"cancel"] forState:UIControlStateNormal];
[_cancelButton setTitle:[NSBundle mxk_localizedStringForKey:@"cancel"] forState:UIControlStateHighlighted];
[_sendButton setTitle:NSLocalizedStringFromTable(@"bug_report_send", @"Vector", nil) forState:UIControlStateNormal];
[_sendButton setTitle:NSLocalizedStringFromTable(@"bug_report_send", @"Vector", nil) forState:UIControlStateHighlighted];
[_backgroundButton setTitle:NSLocalizedStringFromTable(@"bug_report_background_mode", @"Vector", nil) forState:UIControlStateNormal];
[_backgroundButton setTitle:NSLocalizedStringFromTable(@"bug_report_background_mode", @"Vector", nil) forState:UIControlStateHighlighted];
2017-04-27 08:37:39 +00:00
// Do not send empty report
_sendButton.enabled = NO;;
2017-04-27 09:18:15 +00:00
_sendingContainer.hidden = YES;
self.sendLogs = YES;
self.sendScreenshot = YES;
2017-04-27 10:30:04 +00:00
// Hide the screenshot button if there is no screenshot
if (!_screenshot)
2017-04-27 10:30:04 +00:00
{
_sendScreenshotContainer.hidden = YES;
_sendScreenshotContainerHeightConstraint.constant = 0;
}
2017-04-27 08:37:39 +00:00
// Listen to sendLogs tap
UITapGestureRecognizer *sendLogsTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onSendLogsTap:)];
[sendLogsTapGesture setNumberOfTouchesRequired:1];
[_sendLogsContainer addGestureRecognizer:sendLogsTapGesture];
_sendLogsContainer.userInteractionEnabled = YES;
// Listen to sendScreenshot tap
UITapGestureRecognizer *sendScreenshotTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onSendScreenshotTap:)];
[sendScreenshotTapGesture setNumberOfTouchesRequired:1];
[_sendScreenshotContainer addGestureRecognizer:sendScreenshotTapGesture];
_sendScreenshotContainer.userInteractionEnabled = YES;
// Add an accessory view in order to retrieve keyboard view
_bugReportDescriptionTextView.inputAccessoryView = [[UIView alloc] initWithFrame:CGRectZero];
// Observe user interface theme change.
kThemeServiceDidChangeThemeNotificationObserver = [[NSNotificationCenter defaultCenter] addObserverForName:kThemeServiceDidChangeThemeNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *notif) {
[self userInterfaceThemeDidChange];
}];
[self userInterfaceThemeDidChange];
}
- (void)userInterfaceThemeDidChange
{
2019-01-11 10:45:27 +00:00
[ThemeService.shared.theme applyStyleOnNavigationBar:self.navigationController.navigationBar];
2019-01-11 10:45:27 +00:00
self.activityIndicator.backgroundColor = ThemeService.shared.theme.overlayBackgroundColor;
2019-01-11 10:45:27 +00:00
self.overlayView.backgroundColor = ThemeService.shared.theme.overlayBackgroundColor;
self.overlayView.alpha = 1.0;
2019-01-11 10:45:27 +00:00
self.containerView.backgroundColor = ThemeService.shared.theme.backgroundColor;
self.sendingContainer.backgroundColor = ThemeService.shared.theme.backgroundColor;
2019-01-11 10:45:27 +00:00
self.bugReportDescriptionTextView.keyboardAppearance = ThemeService.shared.theme.keyboardAppearance;
2019-01-11 10:45:27 +00:00
self.titleLabel.textColor = ThemeService.shared.theme.textPrimaryColor;
self.sendingLabel.textColor = ThemeService.shared.theme.textPrimaryColor;
self.descriptionLabel.textColor = ThemeService.shared.theme.textPrimaryColor;
self.bugReportDescriptionTextView.textColor = ThemeService.shared.theme.textPrimaryColor;
self.bugReportDescriptionTextView.tintColor = ThemeService.shared.theme.tintColor;
self.logsDescriptionLabel.textColor = ThemeService.shared.theme.textPrimaryColor;
self.sendLogsLabel.textColor = ThemeService.shared.theme.textPrimaryColor;
self.sendScreenshotLabel.textColor = ThemeService.shared.theme.textPrimaryColor;
2019-01-11 10:45:27 +00:00
self.sendButton.tintColor = ThemeService.shared.theme.tintColor;
self.cancelButton.tintColor = ThemeService.shared.theme.tintColor;
self.backgroundButton.tintColor = ThemeService.shared.theme.tintColor;
2019-01-11 10:45:27 +00:00
_bugReportDescriptionTextView.layer.borderColor = ThemeService.shared.theme.headerBackgroundColor.CGColor;
2020-07-09 16:58:44 +00:00
self.sendLogsButtonImage.tintColor = ThemeService.shared.theme.tintColor;
self.sendScreenshotButtonImage.tintColor = ThemeService.shared.theme.tintColor;
[self setNeedsStatusBarAppearanceUpdate];
}
- (UIStatusBarStyle)preferredStatusBarStyle
{
2019-01-11 10:45:27 +00:00
return ThemeService.shared.theme.statusBarStyle;
}
- (void)destroy
{
if (kThemeServiceDidChangeThemeNotificationObserver)
{
[[NSNotificationCenter defaultCenter] removeObserver:kThemeServiceDidChangeThemeNotificationObserver];
kThemeServiceDidChangeThemeNotificationObserver = nil;
}
[super destroy];
}
- (void)dismissViewControllerAnimated:(BOOL)flag completion:(void (^)(void))completion
{
[super dismissViewControllerAnimated:flag completion:completion];
[self destroy];
}
- (void)dealloc
{
_bugReportDescriptionTextView.inputAccessoryView = nil;
}
-(void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[self dismissKeyboard];
if (screenShotFile)
{
[[NSFileManager defaultManager] removeItemAtURL:screenShotFile error:nil];
screenShotFile = nil;
}
}
- (void)setSendLogs:(BOOL)sendLogs
{
_sendLogs = sendLogs;
if (_sendLogs)
{
_sendLogsButtonImage.image = [UIImage imageNamed:@"selection_tick"];
}
else
{
_sendLogsButtonImage.image = [UIImage imageNamed:@"selection_untick"];
}
}
- (void)setSendScreenshot:(BOOL)sendScreenshot
{
_sendScreenshot = sendScreenshot;
if (_sendScreenshot)
{
_sendScreenshotButtonImage.image = [UIImage imageNamed:@"selection_tick"];
}
else
{
_sendScreenshotButtonImage.image = [UIImage imageNamed:@"selection_untick"];
}
}
- (void)setIsSendingLogs:(BOOL)isSendingLogs
{
_isSendingLogs = isSendingLogs;
_sendButton.hidden = isSendingLogs;
_sendingContainer.hidden = !isSendingLogs;
_backgroundButton.hidden = !isSendingLogs;
}
#pragma mark - MXKViewController
- (void)dismissKeyboard
{
// Hide the keyboard
[_bugReportDescriptionTextView resignFirstResponder];
}
- (void)onKeyboardShowAnimationComplete
{
self.keyboardView = _bugReportDescriptionTextView.inputAccessoryView.superview;
}
-(void)setKeyboardHeight:(CGFloat)keyboardHeight
{
// In portrait in 6/7 and 6+/7+, make the height of the popup smaller to be able to
// display Cancel and Send buttons.
// Do nothing in landscape or in 5 in portrait and in landscape. There will be not enough
// room to display bugReportDescriptionTextView.
if (self.view.frame.size.height > 568)
{
self.scrollViewBottomConstraint.constant = keyboardHeight;
}
else
{
self.scrollViewBottomConstraint.constant = 0;
}
[self.view layoutIfNeeded];
}
2017-04-27 08:37:39 +00:00
#pragma mark - UITextViewDelegate
- (void)textViewDidChange:(UITextView *)textView
{
_sendButton.enabled = (_bugReportDescriptionTextView.text.length != 0);
}
#pragma mark - User actions
- (IBAction)onSendButtonPress:(id)sender
{
self.isSendingLogs = YES;
2017-04-27 08:37:39 +00:00
// Setup data to send
2020-07-31 06:37:27 +00:00
bugReportRestClient = [[MXBugReportRestClient alloc] initWithBugReportEndpoint:BuildSettings.bugReportEndpointUrlString];
2017-04-27 08:37:39 +00:00
// App info
2020-07-31 06:37:27 +00:00
bugReportRestClient.appName = BuildSettings.bugReportApplicationId;
2017-04-27 08:37:39 +00:00
bugReportRestClient.version = [AppDelegate theDelegate].appVersion;
bugReportRestClient.build = [AppDelegate theDelegate].build;
// Device info
bugReportRestClient.deviceModel = [GBDeviceInfo deviceInfo].modelString;
bugReportRestClient.deviceOS = [NSString stringWithFormat:@"%@ %@", [[UIDevice currentDevice] systemName], [[UIDevice currentDevice] systemVersion]];
2017-05-09 08:52:07 +00:00
// User info (TODO: handle multi-account and find a way to expose them in rageshake API)
NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
MXKAccount *mainAccount = [MXKAccountManager sharedManager].accounts.firstObject;
if (mainAccount.mxSession.myUser.userId)
{
userInfo[@"user_id"] = mainAccount.mxSession.myUser.userId;
}
if (mainAccount.mxSession.matrixRestClient.credentials.deviceId)
{
userInfo[@"device_id"] = mainAccount.mxSession.matrixRestClient.credentials.deviceId;
}
userInfo[@"locale"] = [NSLocale preferredLanguages][0];
userInfo[@"default_app_language"] = [[NSBundle mainBundle] preferredLocalizations][0]; // The language chosen by the OS
userInfo[@"app_language"] = [NSBundle mxk_language] ? [NSBundle mxk_language] : userInfo[@"default_app_language"]; // The language chosen by the user
// Application settings
userInfo[@"lazy_loading"] = [MXKAppSettings standardAppSettings].syncWithLazyLoadOfRoomMembers ? @"ON" : @"OFF";
NSDate *currentDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
userInfo[@"local_time"] = [dateFormatter stringFromDate:currentDate];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
userInfo[@"utc_time"] = [dateFormatter stringFromDate:currentDate];
2017-05-09 08:52:07 +00:00
bugReportRestClient.others = userInfo;
// Screenshot
NSArray<NSURL*> *files;
if (_screenshot && _sendScreenshot)
{
// Store the screenshot into a temporary file
NSData *screenShotData = UIImagePNGRepresentation(_screenshot);
screenShotFile = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent:@"screenshot.png"]];
[screenShotData writeToURL:screenShotFile atomically:YES];
files = @[screenShotFile];
}
2017-05-05 09:44:55 +00:00
// Prepare labels to attach to the GitHub issue
NSMutableArray<NSString*> *gitHubLabels = [NSMutableArray array];
if (_reportCrash)
{
// Label the GH issue as "crash"
[gitHubLabels addObject:@"crash"];
}
// Add a Github label giving information about the version
if (bugReportRestClient.version && bugReportRestClient.build)
{
NSString *build = bugReportRestClient.build;
NSString *versionLabel = bugReportRestClient.version;
// If this is not the app store version, be more accurate on the build origin
if ([build isEqualToString:NSLocalizedStringFromTable(@"settings_config_no_build_info", @"Vector", nil)])
{
// This is a debug session from Xcode
versionLabel = [versionLabel stringByAppendingString:@"-debug"];
}
else if (build && ![build containsString:@"master"])
{
// This is a Jenkins build. Add the branch and the build number
NSString *buildString = [build stringByReplacingOccurrencesOfString:@" " withString:@"-"];
versionLabel = [[versionLabel stringByAppendingString:@"-"] stringByAppendingString:buildString];
}
[gitHubLabels addObject:versionLabel];
}
NSMutableString *bugReportDescription = [NSMutableString stringWithString:_bugReportDescriptionTextView.text];
if (_reportCrash)
{
// Append the crash dump to the user description in order to ease triaging of GH issues
NSString *crashLogFile = [MXLogger crashLog];
NSString *crashLog = [NSString stringWithContentsOfFile:crashLogFile encoding:NSUTF8StringEncoding error:nil];
[bugReportDescription appendFormat:@"\n\n\n--------------------------------------------------------------------------------\n\n%@", crashLog];
}
// starting a background task to have a bit of extra time in case of user forgets about the report and sends the app to background
__block UIBackgroundTaskIdentifier operationBackgroundId = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
[[UIApplication sharedApplication] endBackgroundTask:operationBackgroundId];
operationBackgroundId = UIBackgroundTaskInvalid;
}];
2017-04-27 08:37:39 +00:00
// Submit
[bugReportRestClient sendBugReport:bugReportDescription sendLogs:_sendLogs sendCrashLog:_reportCrash sendFiles:files attachGitHubLabels:gitHubLabels progress:^(MXBugReportState state, NSProgress *progress) {
2017-04-27 08:37:39 +00:00
2017-04-27 09:18:15 +00:00
switch (state)
{
case MXBugReportStateProgressZipping:
self.sendingLabel.text = NSLocalizedStringFromTable(@"bug_report_progress_zipping", @"Vector", nil);
2017-04-27 09:18:15 +00:00
break;
case MXBugReportStateProgressUploading:
self.sendingLabel.text = NSLocalizedStringFromTable(@"bug_report_progress_uploading", @"Vector", nil);
2017-04-27 09:18:15 +00:00
break;
default:
break;
}
2017-04-27 08:37:39 +00:00
self.sendingProgress.progress = progress.fractionCompleted;
2017-04-27 08:37:39 +00:00
} success:^{
self->bugReportRestClient = nil;
2017-04-28 12:37:29 +00:00
if (self.reportCrash)
2017-04-28 12:37:29 +00:00
{
// Erase the crash log
[MXLogger deleteCrashLog];
}
2017-04-27 08:37:39 +00:00
[self dismissViewControllerAnimated:YES completion:nil];
if (operationBackgroundId != UIBackgroundTaskInvalid)
{
[[UIApplication sharedApplication] endBackgroundTask:operationBackgroundId];
operationBackgroundId = UIBackgroundTaskInvalid;
}
2017-04-27 08:37:39 +00:00
} failure:^(NSError *error) {
if (self.presentingViewController)
{
self->bugReportRestClient = nil;
2017-04-27 08:37:39 +00:00
[[AppDelegate theDelegate] showErrorAsAlert:error];
2017-04-27 08:37:39 +00:00
self.isSendingLogs = NO;
}
else
{
[[[UIApplication sharedApplication].windows firstObject].rootViewController presentViewController:self animated:YES completion:^{
self->bugReportRestClient = nil;
[[AppDelegate theDelegate] showErrorAsAlert:error];
self.isSendingLogs = NO;
}];
}
if (operationBackgroundId != UIBackgroundTaskInvalid)
{
[[UIApplication sharedApplication] endBackgroundTask:operationBackgroundId];
operationBackgroundId = UIBackgroundTaskInvalid;
}
2017-04-27 08:37:39 +00:00
}];
}
- (IBAction)onCancelButtonPressed:(id)sender
{
2017-04-27 08:37:39 +00:00
if (bugReportRestClient)
{
// If the submission is in progress, cancel the sending and come back
// to the bug report screen
[bugReportRestClient cancel];
bugReportRestClient = nil;
self.isSendingLogs = NO;
2017-04-27 08:37:39 +00:00
}
else
{
2017-04-28 12:37:29 +00:00
if (_reportCrash)
{
// Erase the crash log
[MXLogger deleteCrashLog];
}
2017-04-27 08:37:39 +00:00
// Else, lease the bug report screen
[self dismissViewControllerAnimated:YES completion:nil];
}
}
- (IBAction)onSendLogsTap:(id)sender
{
self.sendLogs = !self.sendLogs;
}
- (IBAction)onSendScreenshotTap:(id)sender
{
self.sendScreenshot = !self.sendScreenshot;
}
- (IBAction)onBackgroundTap:(id)sender
{
[self dismissViewControllerAnimated:YES completion:^{}];
}
@end