Signal-iOS/SignalUI/ViewControllers/UIViewController+Permissions.m
Evan Hahn 370ff654e7
Change license to AGPL
Change license to AGPL

This commit:

- Updates the `LICENSE` file

- Start every file with something like:

      // Copyright YEAR_FIRST_PUBLISHED Signal Messenger, LLC
      // SPDX-License-Identifier: AGPL-3.0-only

---

First, I removed existing license headers with this Ruby 3.1.2 script:

    require 'set'

    EXTENSIONS_TO_CHECK = Set['.h', '.hpp', '.cpp', '.m', '.mm', '.pch', '.swift']

    same = 0
    different = 0

    all_files = `git ls-files`.lines.map { |line| line.strip }
    all_files.each do |relative_path|
      if relative_path == 'Pods'
        next
      end

      unless EXTENSIONS_TO_CHECK.include? File.extname(relative_path)
        next
      end

      path = File.expand_path(relative_path)

      contents = File.read(path)
      new_contents = contents.sub(/\/\/\n\/\/  Copyright .*\n\/\/\n\n/, '')

      if contents == new_contents
        same += 1
      else
        different += 1
      end

      File.write(path, new_contents)
    end

    puts "updated #{different} file(s), left #{same} untouched"

I'm sure this script could be improved, but it worked well enough.

Then, I created `Scripts/lint/lint-license-headers` and ran it to auto-
fix a lot of files. This changed the mode of some files, but I think
that's actually desirable. For example,
`SignalServiceKit/src/Util/AppContext.m` previously had a mode of
`0755/-rwxr-xr-x`, and it's now `0644/-rw-r--r--`.

Then I fixed some stragglers and updated the precommit script.

See [a similar change in the Desktop app][0].

[0]: 8bfaf598af
2022-10-13 08:25:37 -05:00

186 lines
7.4 KiB
Objective-C

//
// Copyright 2016 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
//
#import "UIViewController+Permissions.h"
#import "UIUtil.h"
#import <AVFoundation/AVFoundation.h>
#import <Photos/Photos.h>
#import <SignalCoreKit/Threading.h>
#import <SignalUI/SignalUI-Swift.h>
NS_ASSUME_NONNULL_BEGIN
@implementation UIViewController (Permissions)
- (void)ows_askForCameraPermissions:(void (^)(BOOL granted))callbackParam
{
OWSLogVerbose(@"[%@] ows_askForCameraPermissions", NSStringFromClass(self.class));
// Ensure callback is invoked on main thread.
void (^callback)(BOOL) = ^(BOOL granted) { DispatchMainThreadSafe(^{ callbackParam(granted); }); };
if (CurrentAppContext().reportedApplicationState == UIApplicationStateBackground) {
OWSLogError(@"Skipping camera permissions request when app is in background.");
callback(NO);
return;
}
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]
&& !Platform.isSimulator) {
OWSLogError(@"Camera ImagePicker source not available");
callback(NO);
return;
}
AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
if (status == AVAuthorizationStatusDenied) {
ActionSheetController *alert = [[ActionSheetController alloc]
initWithTitle:OWSLocalizedString(@"MISSING_CAMERA_PERMISSION_TITLE", @"Alert title")
message:OWSLocalizedString(@"MISSING_CAMERA_PERMISSION_MESSAGE", @"Alert body")];
ActionSheetAction *_Nullable openSettingsAction =
[AppContextUtils openSystemSettingsActionWithCompletion:^{ callback(NO); }];
if (openSettingsAction != nil) {
[alert addAction:openSettingsAction];
}
ActionSheetAction *dismissAction =
[[ActionSheetAction alloc] initWithTitle:CommonStrings.dismissButton
style:ActionSheetActionStyleCancel
handler:^(ActionSheetAction *action) { callback(NO); }];
[alert addAction:dismissAction];
[self presentActionSheet:alert];
} else if (status == AVAuthorizationStatusAuthorized) {
callback(YES);
} else if (status == AVAuthorizationStatusNotDetermined) {
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:callback];
} else {
OWSLogError(@"Unknown AVAuthorizationStatus: %ld", (long)status);
callback(NO);
}
}
- (void)ows_askForMediaLibraryPermissions:(void (^)(BOOL granted))callbackParam
{
OWSLogVerbose(@"[%@] ows_askForMediaLibraryPermissions", NSStringFromClass(self.class));
// Ensure callback is invoked on main thread.
void (^completionCallback)(BOOL) = ^(BOOL granted) { DispatchMainThreadSafe(^{ callbackParam(granted); }); };
void (^presentSettingsDialog)(void) = ^(void) {
DispatchMainThreadSafe(^{
ActionSheetController *alert = [[ActionSheetController alloc]
initWithTitle:OWSLocalizedString(@"MISSING_MEDIA_LIBRARY_PERMISSION_TITLE",
@"Alert title when user has previously denied media library access")
message:OWSLocalizedString(@"MISSING_MEDIA_LIBRARY_PERMISSION_MESSAGE",
@"Alert body when user has previously denied media library access")];
ActionSheetAction *_Nullable openSettingsAction =
[AppContextUtils openSystemSettingsActionWithCompletion:^() { completionCallback(NO); }];
if (openSettingsAction) {
[alert addAction:openSettingsAction];
}
ActionSheetAction *dismissAction =
[[ActionSheetAction alloc] initWithTitle:CommonStrings.dismissButton
style:ActionSheetActionStyleCancel
handler:^(ActionSheetAction *action) { completionCallback(NO); }];
[alert addAction:dismissAction];
[self presentActionSheet:alert];
});
};
if (CurrentAppContext().reportedApplicationState == UIApplicationStateBackground) {
OWSLogError(@"Skipping media library permissions request when app is in background.");
completionCallback(NO);
return;
}
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
OWSLogError(@"PhotoLibrary ImagePicker source not available");
completionCallback(NO);
}
// TODO Xcode 12: When we're compiling on in Xcode 12, adjust this to
// use the new non-deprecated API that returns the "limited" status.
PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
switch (status) {
case PHAuthorizationStatusAuthorized: {
completionCallback(YES);
return;
}
case PHAuthorizationStatusDenied: {
presentSettingsDialog();
return;
}
case PHAuthorizationStatusNotDetermined: {
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus newStatus) {
if (newStatus == PHAuthorizationStatusAuthorized) {
completionCallback(YES);
} else {
presentSettingsDialog();
}
}];
return;
}
case PHAuthorizationStatusRestricted: {
// when does this happen?
OWSFailDebug(@"PHAuthorizationStatusRestricted");
return;
}
case PHAuthorizationStatusLimited: {
completionCallback(YES);
return;
}
}
}
- (void)ows_askForMicrophonePermissions:(void (^)(BOOL granted))callbackParam
{
OWSLogVerbose(@"[%@] ows_askForMicrophonePermissions", NSStringFromClass(self.class));
// Ensure callback is invoked on main thread.
void (^callback)(BOOL) = ^(BOOL granted) { DispatchMainThreadSafe(^{ callbackParam(granted); }); };
// We want to avoid asking for audio permission while the app is in the background,
// as WebRTC can ask at some strange times. However, if we're currently in a call
// it's important we allow you to request audio permission regardless of app state.
if (CurrentAppContext().reportedApplicationState == UIApplicationStateBackground
&& !CurrentAppContext().hasActiveCall) {
OWSLogError(@"Skipping microphone permissions request when app is in background.");
callback(NO);
return;
}
[[AVAudioSession sharedInstance] requestRecordPermission:callback];
}
- (void)ows_showNoMicrophonePermissionActionSheet
{
DispatchMainThreadSafe(^{
ActionSheetController *alert = [[ActionSheetController alloc]
initWithTitle:OWSLocalizedString(@"CALL_AUDIO_PERMISSION_TITLE",
@"Alert title when calling and permissions for microphone are missing")
message:OWSLocalizedString(@"CALL_AUDIO_PERMISSION_MESSAGE",
@"Alert message when calling and permissions for microphone are missing")];
ActionSheetAction *_Nullable openSettingsAction = [AppContextUtils openSystemSettingsActionWithCompletion:nil];
if (openSettingsAction) {
[alert addAction:openSettingsAction];
}
[alert addAction:OWSActionSheets.dismissAction];
[self presentActionSheet:alert];
});
}
@end
NS_ASSUME_NONNULL_END