Commit d69d5a2b by Maurits van Beusekom

Implemented open settings and should show permission rationale API on Android

parent 5ab3ffcc
...@@ -108,6 +108,10 @@ class PermissionHandlerPlugin(private val registrar: Registrar, private var requ ...@@ -108,6 +108,10 @@ class PermissionHandlerPlugin(private val registrar: Registrar, private var requ
val permissions = Codec.decodePermissionGroups(call.arguments) val permissions = Codec.decodePermissionGroups(call.arguments)
requestPermissions(permissions) requestPermissions(permissions)
} }
call.method == "shouldShowRequestPermissionRationale" -> {
val permission = Codec.decodePermissionGroup(call.arguments)
result.success(shouldShowRequestPermissionRationale(permission))
}
call.method == "openAppSettings" -> { call.method == "openAppSettings" -> {
val isOpen = openAppSettings() val isOpen = openAppSettings()
result.success(isOpen) result.success(isOpen)
...@@ -148,6 +152,37 @@ class PermissionHandlerPlugin(private val registrar: Registrar, private var requ ...@@ -148,6 +152,37 @@ class PermissionHandlerPlugin(private val registrar: Registrar, private var requ
return PermissionStatus.GRANTED return PermissionStatus.GRANTED
} }
private fun shouldShowRequestPermissionRationale(permission: PermissionGroup) : Boolean {
val activity = registrar.activity()
if(activity == null)
{
Log.d(mLogTag, "Unable to detect current Activity.")
return false
}
val names = getManifestNames(permission)
// if isn't an android specific group then go ahead and return false;
if (names == null)
{
Log.d(mLogTag, "No android specific permissions needed for: $permission")
return false
}
if (names.isEmpty())
{
Log.d(mLogTag,"No permissions found in manifest for: $permission no need to show request rationale")
return false
}
for(name in names)
{
return ActivityCompat.shouldShowRequestPermissionRationale(activity, name)
}
return false
}
private fun requestPermissions(permissions: Array<PermissionGroup>) { private fun requestPermissions(permissions: Array<PermissionGroup>) {
if (registrar.activity() == null) { if (registrar.activity() == null) {
Log.d(mLogTag, "Unable to detect current Activity.") Log.d(mLogTag, "Unable to detect current Activity.")
...@@ -242,8 +277,8 @@ class PermissionHandlerPlugin(private val registrar: Registrar, private var requ ...@@ -242,8 +277,8 @@ class PermissionHandlerPlugin(private val registrar: Registrar, private var requ
return false return false
} }
try { return try {
var settingsIntent = Intent() val settingsIntent = Intent()
settingsIntent.action = android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS settingsIntent.action = android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS
settingsIntent.addCategory(Intent.CATEGORY_DEFAULT) settingsIntent.addCategory(Intent.CATEGORY_DEFAULT)
settingsIntent.data = android.net.Uri.parse("package:" + context.packageName) settingsIntent.data = android.net.Uri.parse("package:" + context.packageName)
...@@ -253,9 +288,9 @@ class PermissionHandlerPlugin(private val registrar: Registrar, private var requ ...@@ -253,9 +288,9 @@ class PermissionHandlerPlugin(private val registrar: Registrar, private var requ
context.startActivity(settingsIntent) context.startActivity(settingsIntent)
return true true
} catch(ex: Exception) { } catch(ex: Exception) {
return false false
} }
} }
......
...@@ -18,9 +18,15 @@ class PermissionHandler { ...@@ -18,9 +18,15 @@ class PermissionHandler {
return Codec.decodePermissionStatus(status); return Codec.decodePermissionStatus(status);
} }
/// Open the App settings page.
///
/// Returns [true] if the app settings page could be opened, otherwise [false] is returned.
static Future<bool> openAppSettings() async => static Future<bool> openAppSettings() async =>
await _channel.invokeMethod("openAppSettings"); await _channel.invokeMethod("openAppSettings");
/// Request the user for access to the supplied list of permissiongroups.
///
/// Returns a [Map] containing the status per requested permissiongroup.
static Future<Map<PermissionGroup, PermissionStatus>> requestPermissions(List<PermissionGroup> permissions) async { static Future<Map<PermissionGroup, PermissionStatus>> requestPermissions(List<PermissionGroup> permissions) async {
final jsonData = Codec.encodePermissionGroups(permissions); final jsonData = Codec.encodePermissionGroups(permissions);
final status = await _channel.invokeMethod( final status = await _channel.invokeMethod(
...@@ -29,4 +35,14 @@ class PermissionHandler { ...@@ -29,4 +35,14 @@ class PermissionHandler {
return Codec.decodePermissionRequestResult(status); return Codec.decodePermissionRequestResult(status);
} }
/// Request to see if you should show a rationale for requesting permission.
///
/// This method is only implemented on Android, calling this on iOS always
/// returns [false].
static Future<bool> shouldShowRequestPermissionRationale(PermissionGroup permission) async =>
await _channel.invokeMethod(
'shouldShowRequestPermissionRationale',
Codec.encodePermissionGroup(permission));
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment