Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add property to ignore permission check. #370

Merged
merged 1 commit into from
Sep 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,18 @@ class PhotoManagerPlugin(

private val photoManager = PhotoManager(applicationContext)

private var ignorePermissionCheck = false;

override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) {
val resultHandler = ResultHandler(result, call)

if (call.method == "ignorePermissionCheck") {
val ignore = call.argument<Boolean>("ignore")!!
ignorePermissionCheck = ignore
resultHandler.reply(ignore)
return
}

var needLocationPermissions = false

val handleResult = when (call.method) {
Expand Down Expand Up @@ -129,6 +138,11 @@ class PhotoManagerPlugin(
return
}

if (ignorePermissionCheck) {
onHandlePermissionResult(call, resultHandler, true)
return
}

val utils = permissionsUtils.apply {
withActivity(activity)
permissionsListener = object : PermissionsListener {
Expand All @@ -138,6 +152,7 @@ class PhotoManagerPlugin(
resultHandler.reply(0)
} else {
if (grantedPermissions.containsAll(arrayListOf(Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE))) {
LogUtils.info("onGranted call.method = ${call.method}")
onHandlePermissionResult(call, resultHandler, false)
} else {
replyPermissionError(resultHandler)
Expand All @@ -146,6 +161,7 @@ class PhotoManagerPlugin(
}

override fun onGranted() {
LogUtils.info("onGranted call.method = ${call.method}")
onHandlePermissionResult(call, resultHandler, true)
}
}
Expand All @@ -165,7 +181,6 @@ class PhotoManagerPlugin(
}

private fun onHandlePermissionResult(call: MethodCall, resultHandler: ResultHandler, haveLocationPermission: Boolean) {
LogUtils.info("onGranted call.method = ${call.method}")
when (call.method) {
"requestPermission" -> resultHandler.reply(1)
"getGalleryList" -> {
Expand Down Expand Up @@ -210,7 +225,7 @@ class PhotoManagerPlugin(
"getThumb" -> {
runOnBackground {
val id = call.argument<String>("id")!!
val optionMap = call.argument<Map<*,*>>("option")!!
val optionMap = call.argument<Map<*, *>>("option")!!
val option = ThumbLoadOption.fromMap(optionMap)
photoManager.getThumb(id, option, resultHandler)
}
Expand Down
17 changes: 14 additions & 3 deletions ios/Classes/core/PMPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#import "PMThumbLoadOption.h"

@implementation PMPlugin {
BOOL ignoreCheckPermission;
}

- (void)registerPlugin:(NSObject <FlutterPluginRegistrar> *)registrar {
Expand Down Expand Up @@ -48,20 +49,28 @@ - (void)onMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result {
}];
} else if ([call.method isEqualToString:@"openSetting"]) {
[PMManager openSetting];
[handler reply:@1];
} else if ([call.method isEqualToString:@"ignorePermissionCheck"]) {
ignoreCheckPermission = [call.arguments[@"ignore"] boolValue];
[handler reply:@(ignoreCheckPermission)];
} else if (manager.isAuth) {
[self onAuth:call result:result];
} else if ([call.method isEqualToString:@"log"]){
} else if ([call.method isEqualToString:@"log"]) {
PMLogUtils.sharedInstance.isLog = (BOOL) call.arguments;
} else {
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
if (ignoreCheckPermission) {
[self onAuth:call result:result];
} else {
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
BOOL auth = PHAuthorizationStatusAuthorized == status;
[manager setAuth:auth];
if (auth) {
[self onAuth:call result:result];
} else {
[handler replyError:@"need permission"];
}
}];
}];
}
}
}

Expand Down Expand Up @@ -313,6 +322,8 @@ - (void)onAuth:(FlutterMethodCall *)call result:(FlutterResult)result {
BOOL favoriteResult = [manager favoriteWithId:id favorite:favorite];

[handler reply:@(favoriteResult)];
} else if ([@"isAuth" isEqualToString:call.method]) {
[handler reply:@YES];
} else {
[handler notImplemented];
}
Expand Down
9 changes: 9 additions & 0 deletions lib/src/manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,15 @@ class PhotoManager {
return _plugin.setLog(isLog);
}

/// Ignore permission checks at runtime, you can use third-party permission plugins to request permission. Default is false.
///
/// For Android, a typical usage scenario may be to use it in Service, because Activity cannot be used in Service to detect runtime permissions, but it should be noted that deleting resources above android10 require activity to accept the result, so the delete system does not apply to this Attributes.
///
/// For iOS, this feature is only added, please explore the specific application scenarios by yourself
static Future<void> setIgnorePermissionCheck(bool ignore) async {
await _plugin.ignorePermissionCheck(ignore);
}

/// get video asset
/// open setting page
static void openSetting() {
Expand Down
4 changes: 4 additions & 0 deletions lib/src/plugin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,10 @@ class Plugin with BasePlugin, IosPlugin, AndroidPlugin {
Future getPropertiesFromAssetEntity(String id) async {
return _channel.invokeMethod('getPropertiesFromAssetEntity', {"id": id});
}

Future ignorePermissionCheck(bool ignore) async {
_channel.invokeMethod('ignorePermissionCheck', {'ignore': ignore});
}
}

mixin BasePlugin {
Expand Down