Skip to content

feat: Add PermissionHelperAndroid to support additional permissions validation - #3025

Closed
pkar70 wants to merge 2 commits into
unoplatform:masterfrom
pkar70:PermissionHelper3
Closed

pkar70 wants to merge 2 commits into
unoplatform:masterfrom
pkar70:PermissionHelper3

Conversation

@pkar70

@pkar70 pkar70 commented Apr 20, 2020 •

Copy link
Copy Markdown
Contributor

GitHub Issue (If applicable): #

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Refactoring (no functional changes, no api changes)
    All of above, and - at same time - none of above :)

What is the current behavior?

Current implementation of Android permission helper functions doesn't work.
Currently there is no possibility to invoke system dialog asking for permission from Uno code (e.g. from RequestAccessAsync methods).
So, PR #2024 (geolocator) is labelled as 'help needed' (to split code into levels - as was said in Lobby or in some comments), also PR #2544 (contacts), PR #2757 (calllog), and soon-to-be PR (SMS access) cannot be fully implemented (some code have to be included in app code, not inside Uno).

What is the new behavior?

New helper methods works inside Uno, and can invoke system dialog asking for permission.

PR Checklist

Please check if your PR fulfills the following requirements:

Other information

I made it as separate PR, to be used in all four PRs mentioned above, as code is common (and I don't want contacts-PR be dependend on e.g. geolocator PR). Also, it can be used from other Uno PRs and from user code.

I don't edit/modify current Android methods in permissionhelper.cs, as they can be somewhere used (structure of these methods are cumbersome, too complicated for me).

Reopened #2898/#2964
Internal Issue (If applicable):

@gitpod-io

gitpod-io Bot commented Apr 20, 2020 •

Copy link
Copy Markdown

@pkar70

pkar70 commented Apr 20, 2020

Copy link
Copy Markdown
Contributor Author

I created new branch from unoplatform/master, reapply all changes from previous PR, push it.... and got (on web) conflict in src/SamplesApp/UITests.Shared/UITests.Shared.projitems. So, I edited this file on web interface.

@jeromelaban jeromelaban changed the title feat: permissionHelperAndroid (recreated) feat: Add PermissionHelperAndroid to support additional permissions validation Apr 22, 2020

@jeromelaban jeromelaban left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I've tested your PR, and this is what the sample gives:

image

Creating an activity to validate the permissions is not the right way to do this, as it pops up a new full screen activity to ask for permissions.

Also, I noticed that the existing PermissionsHelper already provides the infrastructure to validate permissions, so I wonder why you needed another one to do so. What's missing in here ?

public class PermissionsHelper

Finally, you need to apply the code formatting rules of the Uno project. The best way to do so is to use the "cleanup" tool from VS, at the bottom of the code editor:

image

@pkar70

pkar70 commented Apr 22, 2020

Copy link
Copy Markdown
Contributor Author

Seems like test environment is different than normal app environment (as this code is working when I call it from my uno-app). Android.App.Application.Context is null?

Yes, there is some permission helper implementation, but it doesn't work. I tried to use it when I tried to use geolocation. As effects of my work on geolocation, I created PR for geolocation (#1998, Oct 31, 2019) - simple bug correction in Geolocator.RequestAccessAsync, than #2024 (Nov 5, 2019) - much more complete Geolocator implementation, and full code I'm using in my app (KrakTram, public transport in Kraków) - "// this code would not compile here - but it compile in your own app.". Than you add 'help needed' tag for it, to split this into levels (I don't know what you exactly mean by this levels), than using your suggestion I open another PR with all code that not require splitting into levels to #2536 (Jan 25). Many comments/code changes from peer review was done in this process :)

Searching how to implement OpenFilePicker, I encounter example with defining Activity (not in manifest, but as [Activity flag). I use this general idea to make permission subroutines to my build of Uno, reworked geolocator, SMS messages, call log, known folders, etc. to use this subroutines instead of implementing it in all of them (as you commented in #2773, on 23rd March).

@jeromelaban

Copy link
Copy Markdown
Member

What does not

Seems like test environment is different than normal app environment (as this code is working when I call it from my uno-app). Android.App.Application.Context is null?

No the environment is not null, but the activity is not created with the proper flags. In any case, this is not a good solution to this problem.

Yes, there is some permission helper implementation, but it doesn't work. I tried to use it when I tried to use geolocation.

Can you remind me what does not work ? The wiring up should be enough for the permissions request to be raised.

@pkar70

pkar70 commented Apr 22, 2020

Copy link
Copy Markdown
Contributor Author

Ok, environment is not null, but maybe standard Uno-Android-App is wrapped in Activity, and test is not wrapped? I only guessing, because when I call AndroidPermissionAsync or MissingPermissions from my app, it works. But in test, it throws exception (screenshot you included shows it) about StartActivity outside Activity, so in test it is outside Activity, and in app - it is inside Activity.

Previous implementation doesn't work, i.e. it doesn't display any dialog.

@jeromelaban

Copy link
Copy Markdown
Member

The point of the existing implementation is to forward the calls from the Uno layer to the Uno.UI layer. If calls are missing, let's add them to make the missing permissions work.

@pkar70

pkar70 commented Apr 22, 2020

Copy link
Copy Markdown
Contributor Author

The point of existing implementation is - it doesn't work. And never did.
If I remember correctly, to show system dialog, code has to know Activity. But this is inaccessible in Uno.UWP code. All my previous implementation of permission-related code (calllog, SMS, etc.) uses RequestAsync inside app (where I have Uno.UI.BaseActivity.Current accessible), and now (#3025) this requirement is worked-around.
As I wrote in opening PR note: "I don't edit/modify current Android methods in permissionhelper.cs, as they can be somewhere used (structure of these methods are cumbersome, too complicated for me)."

If you don't want to accept this PR, you can change current permission helper methods, and mark it as 'breaking change'. And I will simply wait till this would be done, and then create PR for accessing SMS log (read/write).
But I need methods with required and optional permissions, both as list of permissions and not simply one permission (as e.g. getting call log requires both contact and callog permission, and it should be done in one system call and not two).

CalendarDatePicker PR was ready in November, now support for it 'is planned' (six months later), so also callog, contacts, SMS, knownfolders can wait months till someone else (re)implement it.

@pkar70 pkar70 mentioned this pull request Apr 26, 2020
1 of 5 tasks
@jeromelaban

Copy link
Copy Markdown
Member

Thank you for the explanation. The current implementation you're proposing is not adequate as it starts a new activity, and should not. The current implementation of the permission helper does have access to the current activity through this method:

internal static void Initialize(FuncAsync<string, bool> getter, FuncAsync<string, bool> checkPermission)

And are currently used by existing parts of Uno (camera capture being one). Are those not working for you as well ?

As for the other PRs you're mentioning, many of those PRs are missing the samples we requested.

@pkar70

pkar70 commented Apr 27, 2020

Copy link
Copy Markdown
Contributor Author

I didn't try camera, I tried to use current implementation for GPS. As far as I remember (it was in november), current implementation doesn't show system dialog, so I was forced to make working implementation by myself. At first, code works only in app, not in Uno, as it has not access to activity (or something like that, my memory is not so good to remember such details after half a year).
But after I discover way to show FileOpenPicker, with own activity for it, I re-implemented RequestAccess/PermissionHelper with own activity, and it works also when placed within Uno.

Why making Activity is wrong?

@pkar70

pkar70 commented Apr 27, 2020

Copy link
Copy Markdown
Contributor Author

I created empty Uno app. Shared mainpage.xaml has only TextBlock and Button, button click handler exactly as in test file in this PR. As Uno.UI, I used my build (with this PR, #3025, included). Build/deploy to real device, with Android 8.1 Go.

When I click button without adding permission to Manifest, it correctly shows "MissingPermissions returned NULL, check if app manifest includes AccessFineLocation". And when I click it with declaring permission in Manifest, I got system dialog asking grant/deny permission.

So, there is some difference between running code as UserControl/inside test and as App. And this is very serious problem! Maybe some other tests also fail, both false positive and false negative...

@jeromelaban

Copy link
Copy Markdown
Member

As I mentioned above, making a activity pops up an opaque activity above the application. This is not an acceptable behavior for an application.

As for the existing permissions helper, it works properly. I've adjusted the GeoLocator in #3064 so that it requests the permissions properly.

Please take a look at the change to use it in your PR. If you need more features in the permissions helper, add it to the existing structure.

@pkar70

pkar70 commented Apr 27, 2020 •

Copy link
Copy Markdown
Contributor Author

Geolocator for Android should be rewritten - see my PRs for this. Not only asking for permission.
And when I tried to use current implementation of PermissionsHelper.TryGetFineLocationPermission, it doesn't work. Maybe I make some mistake in calling it, I don't remember after half a year how it failed.
Also, at least in my apps ported to Android, I need many permissions, not only one of them. So, methods in this PR have permission list as parameter.
To emulate call history, I need call log and contacts. Same for chat messages.

@jeromelaban

Copy link
Copy Markdown
Member

For the GeoLocation, try again, this is what the PR I mentioned does.

@pkar70

pkar70 commented Apr 27, 2020 •

Copy link
Copy Markdown
Contributor Author

Correction of false implementation of geolocator RequestAccessAsync was in PR#1998, Oct 31, 2019. At this time, no one comments that current implementation of permissionhelper works...

Before I change any of my PRs, I have to check again if current implementation of permissionhelper works. Maybe extend it to methods as in this PR (with list of permission, and not single permission on input). I don't expect that your PR #3064 would have any tests... Besides, it probably should be in permissionhelper.android.cs, not permissionhelper.cs file, as it is only for Android.

@jeromelaban

jeromelaban commented Apr 27, 2020 •

Copy link
Copy Markdown
Member

The PR #3064 already has a test, and it was failing on android: https://github.com/unoplatform/uno/blob/master/src/SamplesApp/UITests.Shared/Windows_Devices/GeolocatorTests.xaml

GitHub
Build Mobile, Desktop and WebAssembly apps with C# and XAML. Today. Open source and professionally supported. - unoplatform/uno

@pkar70

pkar70 commented Apr 27, 2020 •

Copy link
Copy Markdown
Contributor Author

Ok, I tested your version. And yes, it shows system permission dialog (when called from app, I didn't try if dialog is also shown when called from Uno). This dialog is same as in my version (same look of whole screen, same ability to interact - either answer in this dialog, or open task manager and kill app).
Your version uses ContextCompat.CheckSelfPermission , without tests if permission is declared in Manifest (my version distinguish these two situations: no permission declaration, and permission denied).
Also, CheckPermission requires CancellationToken - what is function of this parameter?

@jeromelaban

Copy link
Copy Markdown
Member

That what I mean by modifying the PermissionsHelper to add the missing parts. If there is a need for multi-permissions check, they you can add support for it.

The CancellationToken is generally provided (or should be if not) by the AsyncAction builder, but in #3064 it cannot be provided because the API signatures are not the right ones (and will need to be adjusted).

@pkar70

pkar70 commented Apr 28, 2020

Copy link
Copy Markdown
Contributor Author

Can testing permission be in Uno.UWP, without call to Uno.UI? This call is unncesessary.
Your version of check permission have two possible outputs: yes/no. My version has three outputs: yes, no, error (i.e.: not defined in Manifest).

@jeromelaban

Copy link
Copy Markdown
Member

No it cannot, it has to be delegated to Uno.UI, but that's not an issue.

It's also possible to change the output to be an enum instead of a boolean. We can add a Task<PermissionsCheckResult> CheckPermissionsExtended() and adjust the delegation method to return the appropriate values.

@pkar70

pkar70 commented Apr 28, 2020

Copy link
Copy Markdown
Contributor Author

I mean permission check, not system dialog asking for permission.
CheckPermission method in your way (check if permission is granted), or MissingPermission in my way (check Manifest, and check if permission is granted), can work inside Uno.UWP.

Why "it has to be delegated to Uno.UI"?

@jeromelaban

Copy link
Copy Markdown
Member

It has to be delegated to Uno.UI because that's where the BaseActivity is, the one that avoids popping up a new activity to check permissions. See here :

Windows.Extensions.PermissionsHelper.Initialize(getPermission, CheckPermission);

@pkar70

pkar70 commented Apr 28, 2020

Copy link
Copy Markdown
Contributor Author

BaseActivity is not used in permission checking at all.
To check Manifest, and check if permission is granted, you need only Android.App.Application.Context - accessible in Uno.UWP.
See this PR (3025) - method MissingPermissions (splitted into many submethods, as you requested).

@jeromelaban

Copy link
Copy Markdown
Member

BaseActivity is used in the permissions checking, please take a look at this line:

=> ContextCompat.CheckSelfPermission(BaseActivity.Current, permissionIdentifier) == Permission.Granted;

@jeromelaban

Copy link
Copy Markdown
Member

And the permission request:

ActivityCompat.RequestPermissions(BaseActivity.Current, new[] { permissionIdentifier }, code);

@pkar70

pkar70 commented Apr 29, 2020

Copy link
Copy Markdown
Contributor Author

Yes, you use BaseActivity. But my version uses Android.Support.V4.Content.ContextCompat.CheckSelfPermission(Android.App.Application.Context, permission). See code in this PR.

@pkar70

pkar70 commented Apr 29, 2020

Copy link
Copy Markdown
Contributor Author

My question "Can testing permission be in Uno.UWP, without call to Uno.UI?" is not about technical possibility (I know that it can be done), but "Would you accept such code?".

@jeromelaban

Copy link
Copy Markdown
Member

The relevant code should be at the same place, whether it's checking or requesting, even if Application.Context is available in the Uno assembly. If you need to makes changes, change the delegates so that it fits your extended scenario.

@pkar70

pkar70 commented Apr 29, 2020

Copy link
Copy Markdown
Contributor Author

Ok. So, if required methods has to be hard to create (and not as simple as possible), I would not have enough time for this in near future... Pity.

@jeromelaban

Copy link
Copy Markdown
Member

It's not hard to create, it's about moving code from one layer to another. Otherwise, use the methods as is in your existing PRs even if it's asking one permissions at a time.

@pkar70 pkar70 mentioned this pull request May 17, 2020
4 of 5 tasks
@pkar70
pkar70 force-pushed the PermissionHelper3 branch from 244e8c0 to c214648 Compare May 17, 2020 13:25
@pkar70

pkar70 commented May 17, 2020

Copy link
Copy Markdown
Contributor Author

Would it be ok? With body of main methods in Uno.UI, and called from several methods in Uno.UWP?

@jeromelaban jeromelaban left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the update, this is much better! This will allow you to update your other PRs.

#region "submethods for MissingPermissions"
private static IList<string> GetManifestPermissions()
{
// get all permission declared in Manifest (can be null)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This goes into the xml comment

@pkar70 pkar70 mentioned this pull request May 23, 2020
1 of 12 tasks
@pkar70
pkar70 force-pushed the PermissionHelper3 branch from c214648 to 4822d53 Compare May 27, 2020 16:22
@pkar70

pkar70 commented May 27, 2020

Copy link
Copy Markdown
Contributor Author

Why there are conflicting files? I used rebase unoplatform/master?

@CLAassistant

CLAassistant commented Jun 10, 2020 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@pkar70
pkar70 requested a review from a team August 7, 2020 22:10
/// </summary>
/// <param name="requiredPermissions">Permissions that are required. Can be null.</param>
/// <param name="optionalPermissions">Permissions that are required only if declared in Manifest. Can be null.</param>
/// <returns>Array of all permission that is not granted at this moment (can be empty!), or null if some error occured</returns>

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// <returns>Array of all permission that is not granted at this moment (can be empty!), or null if some error occured</returns>
/// <returns>Array of all permission that is not granted at this moment (can be empty!), or null if some error occurred </returns>

return null;
}

// prepare list of all permissions (all required, and all optional defined in Manifest

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// prepare list of all permissions (all required, and all optional defined in Manifest
// prepare a list of all permissions (all required, and all optional defined in Manifest)

/// </summary>
/// <param name="requiredPermissions">Permissions that are required. Can be null.</param>
/// <param name="optionalPermissions">Permissions that are required only if declared in Manifest. Can be null.</param>
/// <param name="ignoreErrors">if 'true', then do not throw exception on error, and simply retur false</param>

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// <param name="ignoreErrors">if 'true', then do not throw exception on error, and simply retur false</param>
/// <param name="ignoreErrors">if 'true', then do not throw exception on error, and simply return false</param>

bool allGranted = result.GrantResults.All(r => r == Android.Content.PM.Permission.Granted);
return allGranted;
}
return false; // it shouldn't happen (if it happens, then there is some error (in Android?). Maybe we even should throw exception...

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return false; // it shouldn't happen (if it happens, then there is some error (in Android?). Maybe we even should throw exception...
return false; // it shouldn't happen (if it happens, then there is some error (in Android?). Maybe we even should throw an exception...

/// </summary>
/// <param name="requiredPermissions">Permissions that are required. Can be null.</param>
/// <param name="optionalPermissions">Permissions that are required only if declared in Manifest. Can be null.</param>
/// <returns>Array of all permission that is not granted at this moment (can be empty!), or null if some error occured</returns>

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// <returns>Array of all permission that is not granted at this moment (can be empty!), or null if some error occured</returns>
/// <returns>Array of all permission that is not granted at this moment (can be empty!), or null if some error occurred </returns>

/// Return null on error, or array of permission to be asked for (not granted at this time).
/// </summary>
/// <param name="requiredPermission">Permission that is </param>
/// <returns>Array with permission if it is not granted at this moment (can be empty!), or null if some error occured</returns>

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// <returns>Array with permission if it is not granted at this moment (can be empty!), or null if some error occured</returns>
/// <returns>Array with permission if it is not granted at this moment (can be empty!), or null if some error occurred </returns>

/// </summary>
/// <param name="requiredPermissions">Permissions that are required. Can be null.</param>
/// <param name="optionalPermissions">Permissions that are required only if declared in Manifest. Can be null.</param>
/// <param name="ignoreErrors">if 'true', then do not throw exception on error, and simply retur false</param>

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// <param name="ignoreErrors">if 'true', then do not throw exception on error, and simply retur false</param>
/// <param name="ignoreErrors">if 'true', then do not throw exception on error, and simply return false</param>

@agneszitte

Copy link
Copy Markdown
Member

@pkar70 some conflicts need to be resolved for this PR ;)

@MartinZikmund
MartinZikmund marked this pull request as draft July 1, 2024 14:33
@github-actions

Copy link
Copy Markdown
Contributor

This PR is stale because it has been open 60 days with no activity. Remove stale label or comment or it will be closed in 10 days.

@github-actions github-actions Bot added the stale This item has been marked as stale and will be closed if there is no activity. label Jan 29, 2025
@MartinZikmund MartinZikmund removed the stale This item has been marked as stale and will be closed if there is no activity. label Jul 31, 2025
@github-actions github-actions Bot locked as resolved and limited conversation to collaborators May 6, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants