OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #import "ios/chrome/browser/ui/qr_scanner/qr_scanner_alerts.h" |
| 6 |
| 7 #import <UIKit/UIKit.h> |
| 8 |
| 9 #include "base/logging.h" |
| 10 #include "components/version_info/version_info.h" |
| 11 #import "ios/chrome/browser/open_url_util.h" |
| 12 #include "ios/chrome/grit/ios_chromium_strings.h" |
| 13 #include "ios/chrome/grit/ios_strings.h" |
| 14 #include "ui/base/l10n/l10n_util.h" |
| 15 #include "ui/base/l10n/l10n_util_mac.h" |
| 16 |
| 17 namespace { |
| 18 |
| 19 // Returns a "Cancel" UIAlertAction for the given |block|. |
| 20 UIAlertAction* CancelAction(qr_scanner::CancelAlertAction block) { |
| 21 NSString* cancelButtonTitle = |
| 22 l10n_util::GetNSString(IDS_IOS_QR_SCANNER_ALERT_CANCEL); |
| 23 return [UIAlertAction actionWithTitle:cancelButtonTitle |
| 24 style:UIAlertActionStyleCancel |
| 25 handler:block]; |
| 26 } |
| 27 |
| 28 // Returns a UIAlertController with a title |title| and message |body| |
| 29 // containing a single "Cancel" button with the action specified by |
| 30 // |cancelBlock|. |
| 31 UIAlertController* AlertWithCancelButton( |
| 32 NSString* title, |
| 33 NSString* body, |
| 34 qr_scanner::CancelAlertAction cancelBlock) { |
| 35 UIAlertController* dialog = |
| 36 [UIAlertController alertControllerWithTitle:title |
| 37 message:body |
| 38 preferredStyle:UIAlertControllerStyleAlert]; |
| 39 if (cancelBlock) { |
| 40 [dialog addAction:CancelAction(cancelBlock)]; |
| 41 } else { |
| 42 [dialog addAction:CancelAction(^void(UIAlertAction*) { |
| 43 [[dialog presentingViewController] |
| 44 dismissViewControllerAnimated:YES |
| 45 completion:nil]; |
| 46 })]; |
| 47 } |
| 48 return dialog; |
| 49 } |
| 50 |
| 51 // Returns a UIAlertController to be displayed when the camera state is |
| 52 // CAMERA_PERMISSION_DENIED. |
| 53 UIAlertController* CameraPermissionDeniedDialog( |
| 54 qr_scanner::CancelAlertAction cancelBlock) { |
| 55 NSURL* settingsURL = [NSURL URLWithString:UIApplicationOpenSettingsURLString]; |
| 56 |
| 57 if (![[UIApplication sharedApplication] canOpenURL:settingsURL]) { |
| 58 // Display a dialog instructing the user how to change the settings. |
| 59 NSString* dialogTitle = l10n_util::GetNSString( |
| 60 IDS_IOS_QR_SCANNER_CAMERA_PERMISSIONS_HELP_TITLE); |
| 61 NSString* dialogBody = l10n_util::GetNSString( |
| 62 IDS_IOS_QR_SCANNER_CAMERA_PERMISSIONS_HELP_DETAIL); |
| 63 return AlertWithCancelButton(dialogTitle, dialogBody, cancelBlock); |
| 64 } |
| 65 |
| 66 // Display a dialog with a link to the Settings app. |
| 67 NSString* dialogTitle = l10n_util::GetNSString( |
| 68 IDS_IOS_QR_SCANNER_CAMERA_PERMISSIONS_HELP_TITLE_GO_TO_SETTINGS); |
| 69 NSString* dialogBody = l10n_util::GetNSString( |
| 70 IDS_IOS_QR_SCANNER_CAMERA_PERMISSIONS_HELP_DETAIL_GO_TO_SETTINGS); |
| 71 NSString* settingsButton = l10n_util::GetNSString( |
| 72 IDS_IOS_QR_SCANNER_CAMERA_PERMISSIONS_HELP_GO_TO_SETTINGS); |
| 73 |
| 74 UIAlertController* dialog = |
| 75 AlertWithCancelButton(dialogTitle, dialogBody, cancelBlock); |
| 76 |
| 77 UIAlertAction* settingsAction = |
| 78 [UIAlertAction actionWithTitle:settingsButton |
| 79 style:UIAlertActionStyleDefault |
| 80 handler:^(UIAlertAction* action) { |
| 81 OpenUrlWithCompletionHandler(settingsURL, nil); |
| 82 }]; |
| 83 [dialog addAction:settingsAction]; |
| 84 [dialog setPreferredAction:settingsAction]; |
| 85 return dialog; |
| 86 } |
| 87 |
| 88 } // namespace |
| 89 |
| 90 namespace qr_scanner { |
| 91 |
| 92 UIAlertController* DialogForCameraState( |
| 93 CameraState state, |
| 94 qr_scanner::CancelAlertAction cancelBlock) { |
| 95 NSString* dialogTitle = nil; |
| 96 NSString* dialogBody = nil; |
| 97 switch (state) { |
| 98 case qr_scanner::CAMERA_AVAILABLE: |
| 99 case qr_scanner::CAMERA_NOT_LOADED: |
| 100 NOTREACHED(); |
| 101 return nil; |
| 102 |
| 103 case qr_scanner::CAMERA_IN_USE_BY_ANOTHER_APPLICATION: |
| 104 dialogTitle = |
| 105 l10n_util::GetNSString(IDS_IOS_QR_SCANNER_CAMERA_IN_USE_ALERT_TITLE); |
| 106 dialogBody = |
| 107 l10n_util::GetNSString(IDS_IOS_QR_SCANNER_CAMERA_IN_USE_ALERT_DETAIL); |
| 108 return AlertWithCancelButton(dialogTitle, dialogBody, cancelBlock); |
| 109 |
| 110 case qr_scanner::MULTIPLE_FOREGROUND_APPS: |
| 111 dialogTitle = l10n_util::GetNSString( |
| 112 IDS_IOS_QR_SCANNER_MULTIPLE_FOREGROUND_APPS_ALERT_TITLE); |
| 113 dialogBody = l10n_util::GetNSString( |
| 114 IDS_IOS_QR_SCANNER_MULTIPLE_FOREGROUND_APPS_ALERT_DETAIL); |
| 115 return AlertWithCancelButton(dialogTitle, dialogBody, cancelBlock); |
| 116 |
| 117 case qr_scanner::CAMERA_PERMISSION_DENIED: |
| 118 return CameraPermissionDeniedDialog(cancelBlock); |
| 119 |
| 120 case qr_scanner::CAMERA_UNAVAILABLE: |
| 121 dialogTitle = l10n_util::GetNSString( |
| 122 IDS_IOS_QR_SCANNER_CAMERA_UNAVAILABLE_ALERT_TITLE); |
| 123 dialogBody = l10n_util::GetNSString( |
| 124 IDS_IOS_QR_SCANNER_CAMERA_UNAVAILABLE_ALERT_DETAIL); |
| 125 return AlertWithCancelButton(dialogTitle, dialogBody, cancelBlock); |
| 126 } |
| 127 } |
| 128 |
| 129 } // namespace qr_scanner |
OLD | NEW |